Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
219d1afa | 3 | Copyright (C) 1993-2018 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) ; |
276da9b3 L |
55 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type, |
56 | size_t align) ; | |
718175fa | 57 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, |
276da9b3 | 58 | file_ptr offset, size_t align); |
50b2bdb7 | 59 | |
252b5132 RH |
60 | /* Swap version information in and out. The version information is |
61 | currently size independent. If that ever changes, this code will | |
62 | need to move into elfcode.h. */ | |
63 | ||
64 | /* Swap in a Verdef structure. */ | |
65 | ||
66 | void | |
217aa764 AM |
67 | _bfd_elf_swap_verdef_in (bfd *abfd, |
68 | const Elf_External_Verdef *src, | |
69 | Elf_Internal_Verdef *dst) | |
252b5132 | 70 | { |
dc810e39 AM |
71 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
72 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
73 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
74 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
75 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
76 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
77 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
78 | } |
79 | ||
80 | /* Swap out a Verdef structure. */ | |
81 | ||
82 | void | |
217aa764 AM |
83 | _bfd_elf_swap_verdef_out (bfd *abfd, |
84 | const Elf_Internal_Verdef *src, | |
85 | Elf_External_Verdef *dst) | |
252b5132 | 86 | { |
dc810e39 AM |
87 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
88 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
89 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
90 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
91 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
92 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
93 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
94 | } |
95 | ||
96 | /* Swap in a Verdaux structure. */ | |
97 | ||
98 | void | |
217aa764 AM |
99 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
100 | const Elf_External_Verdaux *src, | |
101 | Elf_Internal_Verdaux *dst) | |
252b5132 | 102 | { |
dc810e39 AM |
103 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
104 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
105 | } |
106 | ||
107 | /* Swap out a Verdaux structure. */ | |
108 | ||
109 | void | |
217aa764 AM |
110 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
111 | const Elf_Internal_Verdaux *src, | |
112 | Elf_External_Verdaux *dst) | |
252b5132 | 113 | { |
dc810e39 AM |
114 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
115 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
116 | } |
117 | ||
118 | /* Swap in a Verneed structure. */ | |
119 | ||
120 | void | |
217aa764 AM |
121 | _bfd_elf_swap_verneed_in (bfd *abfd, |
122 | const Elf_External_Verneed *src, | |
123 | Elf_Internal_Verneed *dst) | |
252b5132 | 124 | { |
dc810e39 AM |
125 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
126 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
127 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
128 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
129 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
130 | } |
131 | ||
132 | /* Swap out a Verneed structure. */ | |
133 | ||
134 | void | |
217aa764 AM |
135 | _bfd_elf_swap_verneed_out (bfd *abfd, |
136 | const Elf_Internal_Verneed *src, | |
137 | Elf_External_Verneed *dst) | |
252b5132 | 138 | { |
dc810e39 AM |
139 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
140 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
141 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
142 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
143 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
144 | } |
145 | ||
146 | /* Swap in a Vernaux structure. */ | |
147 | ||
148 | void | |
217aa764 AM |
149 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
150 | const Elf_External_Vernaux *src, | |
151 | Elf_Internal_Vernaux *dst) | |
252b5132 | 152 | { |
dc810e39 AM |
153 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
154 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
155 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
156 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
157 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
158 | } |
159 | ||
160 | /* Swap out a Vernaux structure. */ | |
161 | ||
162 | void | |
217aa764 AM |
163 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
164 | const Elf_Internal_Vernaux *src, | |
165 | Elf_External_Vernaux *dst) | |
252b5132 | 166 | { |
dc810e39 AM |
167 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
168 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
169 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
170 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
171 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
172 | } |
173 | ||
174 | /* Swap in a Versym structure. */ | |
175 | ||
176 | void | |
217aa764 AM |
177 | _bfd_elf_swap_versym_in (bfd *abfd, |
178 | const Elf_External_Versym *src, | |
179 | Elf_Internal_Versym *dst) | |
252b5132 | 180 | { |
dc810e39 | 181 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
182 | } |
183 | ||
184 | /* Swap out a Versym structure. */ | |
185 | ||
186 | void | |
217aa764 AM |
187 | _bfd_elf_swap_versym_out (bfd *abfd, |
188 | const Elf_Internal_Versym *src, | |
189 | Elf_External_Versym *dst) | |
252b5132 | 190 | { |
dc810e39 | 191 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
192 | } |
193 | ||
194 | /* Standard ELF hash function. Do not change this function; you will | |
195 | cause invalid hash tables to be generated. */ | |
3a99b017 | 196 | |
252b5132 | 197 | unsigned long |
217aa764 | 198 | bfd_elf_hash (const char *namearg) |
252b5132 | 199 | { |
3a99b017 | 200 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
201 | unsigned long h = 0; |
202 | unsigned long g; | |
203 | int ch; | |
204 | ||
205 | while ((ch = *name++) != '\0') | |
206 | { | |
207 | h = (h << 4) + ch; | |
208 | if ((g = (h & 0xf0000000)) != 0) | |
209 | { | |
210 | h ^= g >> 24; | |
211 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
212 | this case and on some machines one insn instead of two. */ | |
213 | h ^= g; | |
214 | } | |
215 | } | |
32dfa85d | 216 | return h & 0xffffffff; |
252b5132 RH |
217 | } |
218 | ||
fdc90cb4 JJ |
219 | /* DT_GNU_HASH hash function. Do not change this function; you will |
220 | cause invalid hash tables to be generated. */ | |
221 | ||
222 | unsigned long | |
223 | bfd_elf_gnu_hash (const char *namearg) | |
224 | { | |
225 | const unsigned char *name = (const unsigned char *) namearg; | |
226 | unsigned long h = 5381; | |
227 | unsigned char ch; | |
228 | ||
229 | while ((ch = *name++) != '\0') | |
230 | h = (h << 5) + h + ch; | |
231 | return h & 0xffffffff; | |
232 | } | |
233 | ||
0c8d6e5c AM |
234 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
235 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 236 | bfd_boolean |
0c8d6e5c | 237 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 238 | size_t object_size, |
4dfe6ac6 | 239 | enum elf_target_id object_id) |
252b5132 | 240 | { |
0ffa91dd NC |
241 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
242 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
243 | if (abfd->tdata.any == NULL) | |
244 | return FALSE; | |
252b5132 | 245 | |
0ffa91dd | 246 | elf_object_id (abfd) = object_id; |
c0355132 AM |
247 | if (abfd->direction != read_direction) |
248 | { | |
249 | struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); | |
250 | if (o == NULL) | |
251 | return FALSE; | |
252 | elf_tdata (abfd)->o = o; | |
253 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
254 | } | |
b34976b6 | 255 | return TRUE; |
252b5132 RH |
256 | } |
257 | ||
0ffa91dd NC |
258 | |
259 | bfd_boolean | |
ae95ffa6 | 260 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 261 | { |
ae95ffa6 | 262 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 263 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 264 | bed->target_id); |
0ffa91dd NC |
265 | } |
266 | ||
b34976b6 | 267 | bfd_boolean |
217aa764 | 268 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 269 | { |
c044fabd | 270 | /* I think this can be done just like an object file. */ |
228e534f AM |
271 | if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) |
272 | return FALSE; | |
273 | elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); | |
274 | return elf_tdata (abfd)->core != NULL; | |
252b5132 RH |
275 | } |
276 | ||
72a80a16 | 277 | static char * |
217aa764 | 278 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
279 | { |
280 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 281 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
282 | file_ptr offset; |
283 | bfd_size_type shstrtabsize; | |
252b5132 RH |
284 | |
285 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
286 | if (i_shdrp == 0 |
287 | || shindex >= elf_numsections (abfd) | |
288 | || i_shdrp[shindex] == 0) | |
f075ee0c | 289 | return NULL; |
252b5132 | 290 | |
f075ee0c | 291 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
292 | if (shstrtab == NULL) |
293 | { | |
c044fabd | 294 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
295 | offset = i_shdrp[shindex]->sh_offset; |
296 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
297 | |
298 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
299 | in case the string table is not terminated. */ | |
3471d59d | 300 | if (shstrtabsize + 1 <= 1 |
06614111 NC |
301 | || bfd_seek (abfd, offset, SEEK_SET) != 0 |
302 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) | |
c6c60d09 JJ |
303 | shstrtab = NULL; |
304 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
305 | { | |
306 | if (bfd_get_error () != bfd_error_system_call) | |
307 | bfd_set_error (bfd_error_file_truncated); | |
06614111 | 308 | bfd_release (abfd, shstrtab); |
c6c60d09 | 309 | shstrtab = NULL; |
3471d59d CC |
310 | /* Once we've failed to read it, make sure we don't keep |
311 | trying. Otherwise, we'll keep allocating space for | |
312 | the string table over and over. */ | |
313 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
314 | } |
315 | else | |
316 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 317 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 318 | } |
f075ee0c | 319 | return (char *) shstrtab; |
252b5132 RH |
320 | } |
321 | ||
322 | char * | |
217aa764 AM |
323 | bfd_elf_string_from_elf_section (bfd *abfd, |
324 | unsigned int shindex, | |
325 | unsigned int strindex) | |
252b5132 RH |
326 | { |
327 | Elf_Internal_Shdr *hdr; | |
328 | ||
329 | if (strindex == 0) | |
330 | return ""; | |
331 | ||
74f2e02b AM |
332 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
333 | return NULL; | |
334 | ||
252b5132 RH |
335 | hdr = elf_elfsections (abfd)[shindex]; |
336 | ||
06614111 NC |
337 | if (hdr->contents == NULL) |
338 | { | |
339 | if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) | |
340 | { | |
341 | /* PR 17512: file: f057ec89. */ | |
695344c0 | 342 | /* xgettext:c-format */ |
871b3ab2 | 343 | _bfd_error_handler (_("%pB: attempt to load strings from" |
63a5468a | 344 | " a non-string section (number %d)"), |
06614111 NC |
345 | abfd, shindex); |
346 | return NULL; | |
347 | } | |
b1fa9dd6 | 348 | |
06614111 NC |
349 | if (bfd_elf_get_str_section (abfd, shindex) == NULL) |
350 | return NULL; | |
351 | } | |
252b5132 RH |
352 | |
353 | if (strindex >= hdr->sh_size) | |
354 | { | |
1b3a8575 | 355 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
4eca0228 | 356 | _bfd_error_handler |
695344c0 | 357 | /* xgettext:c-format */ |
871b3ab2 | 358 | (_("%pB: invalid string offset %u >= %Lu for section `%s'"), |
d42c267e | 359 | abfd, strindex, hdr->sh_size, |
1b3a8575 | 360 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 361 | ? ".shstrtab" |
1b3a8575 | 362 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 363 | return NULL; |
252b5132 RH |
364 | } |
365 | ||
366 | return ((char *) hdr->contents) + strindex; | |
367 | } | |
368 | ||
6cdc0ccc AM |
369 | /* Read and convert symbols to internal format. |
370 | SYMCOUNT specifies the number of symbols to read, starting from | |
371 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
372 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
373 | symbols, and symbol section index extensions, respectively. |
374 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
375 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
376 | |
377 | Elf_Internal_Sym * | |
217aa764 AM |
378 | bfd_elf_get_elf_syms (bfd *ibfd, |
379 | Elf_Internal_Shdr *symtab_hdr, | |
380 | size_t symcount, | |
381 | size_t symoffset, | |
382 | Elf_Internal_Sym *intsym_buf, | |
383 | void *extsym_buf, | |
384 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
385 | { |
386 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 387 | void *alloc_ext; |
df622259 | 388 | const bfd_byte *esym; |
6cdc0ccc AM |
389 | Elf_External_Sym_Shndx *alloc_extshndx; |
390 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 391 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
392 | Elf_Internal_Sym *isym; |
393 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 394 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
395 | size_t extsym_size; |
396 | bfd_size_type amt; | |
397 | file_ptr pos; | |
398 | ||
e44a2c9c AM |
399 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
400 | abort (); | |
401 | ||
6cdc0ccc AM |
402 | if (symcount == 0) |
403 | return intsym_buf; | |
404 | ||
405 | /* Normal syms might have section extension entries. */ | |
406 | shndx_hdr = NULL; | |
6a40cf0c NC |
407 | if (elf_symtab_shndx_list (ibfd) != NULL) |
408 | { | |
409 | elf_section_list * entry; | |
410 | Elf_Internal_Shdr **sections = elf_elfsections (ibfd); | |
411 | ||
412 | /* Find an index section that is linked to this symtab section. */ | |
413 | for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) | |
315350be NC |
414 | { |
415 | /* PR 20063. */ | |
416 | if (entry->hdr.sh_link >= elf_numsections (ibfd)) | |
417 | continue; | |
418 | ||
419 | if (sections[entry->hdr.sh_link] == symtab_hdr) | |
420 | { | |
421 | shndx_hdr = & entry->hdr; | |
422 | break; | |
423 | }; | |
424 | } | |
6a40cf0c NC |
425 | |
426 | if (shndx_hdr == NULL) | |
427 | { | |
428 | if (symtab_hdr == & elf_symtab_hdr (ibfd)) | |
429 | /* Not really accurate, but this was how the old code used to work. */ | |
430 | shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr; | |
431 | /* Otherwise we do nothing. The assumption is that | |
432 | the index table will not be needed. */ | |
433 | } | |
434 | } | |
6cdc0ccc AM |
435 | |
436 | /* Read the symbols. */ | |
437 | alloc_ext = NULL; | |
438 | alloc_extshndx = NULL; | |
4dd07732 | 439 | alloc_intsym = NULL; |
6cdc0ccc AM |
440 | bed = get_elf_backend_data (ibfd); |
441 | extsym_size = bed->s->sizeof_sym; | |
ef53be89 | 442 | amt = (bfd_size_type) symcount * extsym_size; |
6cdc0ccc AM |
443 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; |
444 | if (extsym_buf == NULL) | |
445 | { | |
d0fb9a8d | 446 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
447 | extsym_buf = alloc_ext; |
448 | } | |
449 | if (extsym_buf == NULL | |
450 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
451 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
452 | { | |
453 | intsym_buf = NULL; | |
454 | goto out; | |
455 | } | |
456 | ||
457 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
458 | extshndx_buf = NULL; | |
459 | else | |
460 | { | |
ef53be89 | 461 | amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx); |
6cdc0ccc AM |
462 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); |
463 | if (extshndx_buf == NULL) | |
464 | { | |
a50b1753 | 465 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
07d6d2b8 | 466 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); |
6cdc0ccc AM |
467 | extshndx_buf = alloc_extshndx; |
468 | } | |
469 | if (extshndx_buf == NULL | |
470 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
471 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
472 | { | |
473 | intsym_buf = NULL; | |
474 | goto out; | |
475 | } | |
476 | } | |
477 | ||
478 | if (intsym_buf == NULL) | |
479 | { | |
a50b1753 | 480 | alloc_intsym = (Elf_Internal_Sym *) |
07d6d2b8 | 481 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); |
4dd07732 | 482 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
483 | if (intsym_buf == NULL) |
484 | goto out; | |
485 | } | |
486 | ||
487 | /* Convert the symbols to internal form. */ | |
488 | isymend = intsym_buf + symcount; | |
a50b1753 | 489 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
07d6d2b8 | 490 | shndx = extshndx_buf; |
6cdc0ccc AM |
491 | isym < isymend; |
492 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
493 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
494 | { | |
495 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
695344c0 | 496 | /* xgettext:c-format */ |
871b3ab2 | 497 | _bfd_error_handler (_("%pB symbol number %lu references" |
63a5468a | 498 | " nonexistent SHT_SYMTAB_SHNDX section"), |
4eca0228 | 499 | ibfd, (unsigned long) symoffset); |
4dd07732 AM |
500 | if (alloc_intsym != NULL) |
501 | free (alloc_intsym); | |
8384fb8f AM |
502 | intsym_buf = NULL; |
503 | goto out; | |
504 | } | |
6cdc0ccc AM |
505 | |
506 | out: | |
507 | if (alloc_ext != NULL) | |
508 | free (alloc_ext); | |
509 | if (alloc_extshndx != NULL) | |
510 | free (alloc_extshndx); | |
511 | ||
512 | return intsym_buf; | |
513 | } | |
514 | ||
5cab59f6 AM |
515 | /* Look up a symbol name. */ |
516 | const char * | |
be8dd2ca AM |
517 | bfd_elf_sym_name (bfd *abfd, |
518 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
519 | Elf_Internal_Sym *isym, |
520 | asection *sym_sec) | |
5cab59f6 | 521 | { |
26c61ae5 | 522 | const char *name; |
5cab59f6 | 523 | unsigned int iname = isym->st_name; |
be8dd2ca | 524 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 525 | |
138f35cc JJ |
526 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
527 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 528 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
529 | { |
530 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
531 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
532 | } | |
533 | ||
26c61ae5 L |
534 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
535 | if (name == NULL) | |
536 | name = "(null)"; | |
537 | else if (sym_sec && *name == '\0') | |
538 | name = bfd_section_name (abfd, sym_sec); | |
539 | ||
540 | return name; | |
5cab59f6 AM |
541 | } |
542 | ||
dbb410c3 AM |
543 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
544 | sections. The first element is the flags, the rest are section | |
545 | pointers. */ | |
546 | ||
547 | typedef union elf_internal_group { | |
548 | Elf_Internal_Shdr *shdr; | |
549 | unsigned int flags; | |
550 | } Elf_Internal_Group; | |
551 | ||
b885599b AM |
552 | /* Return the name of the group signature symbol. Why isn't the |
553 | signature just a string? */ | |
554 | ||
555 | static const char * | |
217aa764 | 556 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 557 | { |
9dce4196 | 558 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
559 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
560 | Elf_External_Sym_Shndx eshndx; | |
561 | Elf_Internal_Sym isym; | |
b885599b | 562 | |
13792e9d L |
563 | /* First we need to ensure the symbol table is available. Make sure |
564 | that it is a symbol table section. */ | |
4fbb74a6 AM |
565 | if (ghdr->sh_link >= elf_numsections (abfd)) |
566 | return NULL; | |
13792e9d L |
567 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
568 | if (hdr->sh_type != SHT_SYMTAB | |
569 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
570 | return NULL; |
571 | ||
9dce4196 AM |
572 | /* Go read the symbol. */ |
573 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
574 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
575 | &isym, esym, &eshndx) == NULL) | |
b885599b | 576 | return NULL; |
9dce4196 | 577 | |
26c61ae5 | 578 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
579 | } |
580 | ||
dbb410c3 AM |
581 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
582 | ||
b34976b6 | 583 | static bfd_boolean |
217aa764 | 584 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
585 | { |
586 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
587 | ||
588 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
589 | is set to -1 if there are no SHT_GROUP sections. */ | |
590 | if (num_group == 0) | |
591 | { | |
592 | unsigned int i, shnum; | |
593 | ||
594 | /* First count the number of groups. If we have a SHT_GROUP | |
595 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 596 | shnum = elf_numsections (abfd); |
dbb410c3 | 597 | num_group = 0; |
08a40648 | 598 | |
44534af3 | 599 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 600 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 601 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
602 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
603 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 604 | |
dbb410c3 AM |
605 | for (i = 0; i < shnum; i++) |
606 | { | |
607 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 608 | |
44534af3 | 609 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
610 | num_group += 1; |
611 | } | |
612 | ||
613 | if (num_group == 0) | |
20dbb49d L |
614 | { |
615 | num_group = (unsigned) -1; | |
616 | elf_tdata (abfd)->num_group = num_group; | |
ce497010 | 617 | elf_tdata (abfd)->group_sect_ptr = NULL; |
20dbb49d L |
618 | } |
619 | else | |
dbb410c3 AM |
620 | { |
621 | /* We keep a list of elf section headers for group sections, | |
622 | so we can find them quickly. */ | |
20dbb49d | 623 | bfd_size_type amt; |
d0fb9a8d | 624 | |
20dbb49d | 625 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 | 626 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
07d6d2b8 | 627 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); |
dbb410c3 | 628 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 629 | return FALSE; |
ce497010 | 630 | memset (elf_tdata (abfd)->group_sect_ptr, 0, num_group * sizeof (Elf_Internal_Shdr *)); |
dbb410c3 | 631 | num_group = 0; |
ce497010 | 632 | |
dbb410c3 AM |
633 | for (i = 0; i < shnum; i++) |
634 | { | |
635 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 636 | |
44534af3 | 637 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 638 | { |
973ffd63 | 639 | unsigned char *src; |
dbb410c3 AM |
640 | Elf_Internal_Group *dest; |
641 | ||
07d6d2b8 AM |
642 | /* Make sure the group section has a BFD section |
643 | attached to it. */ | |
644 | if (!bfd_section_from_shdr (abfd, i)) | |
645 | return FALSE; | |
646 | ||
dbb410c3 AM |
647 | /* Add to list of sections. */ |
648 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
649 | num_group += 1; | |
650 | ||
651 | /* Read the raw contents. */ | |
652 | BFD_ASSERT (sizeof (*dest) >= 4); | |
653 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 | 654 | shdr->contents = (unsigned char *) |
07d6d2b8 | 655 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); |
1783205a NC |
656 | /* PR binutils/4110: Handle corrupt group headers. */ |
657 | if (shdr->contents == NULL) | |
658 | { | |
659 | _bfd_error_handler | |
695344c0 | 660 | /* xgettext:c-format */ |
871b3ab2 | 661 | (_("%pB: corrupt size field in group section" |
76cfced5 | 662 | " header: %#Lx"), abfd, shdr->sh_size); |
1783205a | 663 | bfd_set_error (bfd_error_bad_value); |
493a3386 NC |
664 | -- num_group; |
665 | continue; | |
1783205a NC |
666 | } |
667 | ||
668 | memset (shdr->contents, 0, amt); | |
669 | ||
670 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
671 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
672 | != shdr->sh_size)) | |
493a3386 NC |
673 | { |
674 | _bfd_error_handler | |
695344c0 | 675 | /* xgettext:c-format */ |
871b3ab2 | 676 | (_("%pB: invalid size field in group section" |
76cfced5 | 677 | " header: %#Lx"), abfd, shdr->sh_size); |
493a3386 NC |
678 | bfd_set_error (bfd_error_bad_value); |
679 | -- num_group; | |
63a5468a AM |
680 | /* PR 17510: If the group contents are even |
681 | partially corrupt, do not allow any of the | |
682 | contents to be used. */ | |
493a3386 NC |
683 | memset (shdr->contents, 0, amt); |
684 | continue; | |
685 | } | |
708d7d0d | 686 | |
dbb410c3 AM |
687 | /* Translate raw contents, a flag word followed by an |
688 | array of elf section indices all in target byte order, | |
689 | to the flag word followed by an array of elf section | |
690 | pointers. */ | |
691 | src = shdr->contents + shdr->sh_size; | |
692 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
06614111 | 693 | |
dbb410c3 AM |
694 | while (1) |
695 | { | |
696 | unsigned int idx; | |
697 | ||
698 | src -= 4; | |
699 | --dest; | |
700 | idx = H_GET_32 (abfd, src); | |
701 | if (src == shdr->contents) | |
702 | { | |
703 | dest->flags = idx; | |
b885599b AM |
704 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
705 | shdr->bfd_section->flags | |
706 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
707 | break; |
708 | } | |
709 | if (idx >= shnum) | |
710 | { | |
4eca0228 | 711 | _bfd_error_handler |
871b3ab2 | 712 | (_("%pB: invalid SHT_GROUP entry"), abfd); |
dbb410c3 AM |
713 | idx = 0; |
714 | } | |
715 | dest->shdr = elf_elfsections (abfd)[idx]; | |
716 | } | |
717 | } | |
718 | } | |
493a3386 NC |
719 | |
720 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
721 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
722 | { | |
723 | elf_tdata (abfd)->num_group = num_group; | |
724 | ||
725 | /* If all groups are invalid then fail. */ | |
726 | if (num_group == 0) | |
727 | { | |
728 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
729 | elf_tdata (abfd)->num_group = num_group = -1; | |
4eca0228 | 730 | _bfd_error_handler |
871b3ab2 | 731 | (_("%pB: no valid group sections found"), abfd); |
493a3386 NC |
732 | bfd_set_error (bfd_error_bad_value); |
733 | } | |
734 | } | |
dbb410c3 AM |
735 | } |
736 | } | |
737 | ||
738 | if (num_group != (unsigned) -1) | |
739 | { | |
564e11c9 JW |
740 | unsigned int search_offset = elf_tdata (abfd)->group_search_offset; |
741 | unsigned int j; | |
dbb410c3 | 742 | |
564e11c9 | 743 | for (j = 0; j < num_group; j++) |
dbb410c3 | 744 | { |
564e11c9 JW |
745 | /* Begin search from previous found group. */ |
746 | unsigned i = (j + search_offset) % num_group; | |
747 | ||
dbb410c3 | 748 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; |
ce497010 | 749 | Elf_Internal_Group *idx; |
0c54f692 | 750 | bfd_size_type n_elt; |
ce497010 NC |
751 | |
752 | if (shdr == NULL) | |
753 | continue; | |
754 | ||
755 | idx = (Elf_Internal_Group *) shdr->contents; | |
0c54f692 NC |
756 | if (idx == NULL || shdr->sh_size < 4) |
757 | { | |
758 | /* See PR 21957 for a reproducer. */ | |
759 | /* xgettext:c-format */ | |
871b3ab2 | 760 | _bfd_error_handler (_("%pB: group section '%pA' has no contents"), |
0c54f692 NC |
761 | abfd, shdr->bfd_section); |
762 | elf_tdata (abfd)->group_sect_ptr[i] = NULL; | |
763 | bfd_set_error (bfd_error_bad_value); | |
764 | return FALSE; | |
765 | } | |
ce497010 | 766 | n_elt = shdr->sh_size / 4; |
dbb410c3 AM |
767 | |
768 | /* Look through this group's sections to see if current | |
769 | section is a member. */ | |
770 | while (--n_elt != 0) | |
771 | if ((++idx)->shdr == hdr) | |
772 | { | |
e0e8c97f | 773 | asection *s = NULL; |
dbb410c3 AM |
774 | |
775 | /* We are a member of this group. Go looking through | |
776 | other members to see if any others are linked via | |
777 | next_in_group. */ | |
778 | idx = (Elf_Internal_Group *) shdr->contents; | |
779 | n_elt = shdr->sh_size / 4; | |
780 | while (--n_elt != 0) | |
781 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 782 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
783 | break; |
784 | if (n_elt != 0) | |
785 | { | |
dbb410c3 AM |
786 | /* Snarf the group name from other member, and |
787 | insert current section in circular list. */ | |
945906ff AM |
788 | elf_group_name (newsect) = elf_group_name (s); |
789 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
790 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
791 | } |
792 | else | |
793 | { | |
dbb410c3 AM |
794 | const char *gname; |
795 | ||
b885599b AM |
796 | gname = group_signature (abfd, shdr); |
797 | if (gname == NULL) | |
b34976b6 | 798 | return FALSE; |
945906ff | 799 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
800 | |
801 | /* Start a circular list with one element. */ | |
945906ff | 802 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 803 | } |
b885599b | 804 | |
9dce4196 AM |
805 | /* If the group section has been created, point to the |
806 | new member. */ | |
dbb410c3 | 807 | if (shdr->bfd_section != NULL) |
945906ff | 808 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 809 | |
564e11c9 JW |
810 | elf_tdata (abfd)->group_search_offset = i; |
811 | j = num_group - 1; | |
dbb410c3 AM |
812 | break; |
813 | } | |
814 | } | |
815 | } | |
816 | ||
945906ff | 817 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 818 | { |
695344c0 | 819 | /* xgettext:c-format */ |
871b3ab2 | 820 | _bfd_error_handler (_("%pB: no group info for section '%pA'"), |
4eca0228 | 821 | abfd, newsect); |
493a3386 | 822 | return FALSE; |
dbb410c3 | 823 | } |
b34976b6 | 824 | return TRUE; |
dbb410c3 AM |
825 | } |
826 | ||
3d7f7666 | 827 | bfd_boolean |
dd863624 | 828 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
829 | { |
830 | unsigned int i; | |
831 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
832 | bfd_boolean result = TRUE; | |
dd863624 L |
833 | asection *s; |
834 | ||
835 | /* Process SHF_LINK_ORDER. */ | |
836 | for (s = abfd->sections; s != NULL; s = s->next) | |
837 | { | |
838 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
839 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
840 | { | |
841 | unsigned int elfsec = this_hdr->sh_link; | |
842 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
843 | not set the sh_link or sh_info fields. Hence we could | |
844 | get the situation where elfsec is 0. */ | |
845 | if (elfsec == 0) | |
846 | { | |
4fbb74a6 | 847 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
848 | if (bed->link_order_error_handler) |
849 | bed->link_order_error_handler | |
695344c0 | 850 | /* xgettext:c-format */ |
871b3ab2 | 851 | (_("%pB: warning: sh_link not set for section `%pA'"), |
dd863624 L |
852 | abfd, s); |
853 | } | |
854 | else | |
855 | { | |
91d6fa6a | 856 | asection *linksec = NULL; |
25bbc984 | 857 | |
4fbb74a6 AM |
858 | if (elfsec < elf_numsections (abfd)) |
859 | { | |
860 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 861 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 862 | } |
25bbc984 L |
863 | |
864 | /* PR 1991, 2008: | |
865 | Some strip/objcopy may leave an incorrect value in | |
866 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 867 | if (linksec == NULL) |
25bbc984 | 868 | { |
4eca0228 | 869 | _bfd_error_handler |
695344c0 | 870 | /* xgettext:c-format */ |
871b3ab2 | 871 | (_("%pB: sh_link [%d] in section `%pA' is incorrect"), |
c08bb8dd | 872 | s->owner, elfsec, s); |
25bbc984 L |
873 | result = FALSE; |
874 | } | |
875 | ||
91d6fa6a | 876 | elf_linked_to_section (s) = linksec; |
dd863624 L |
877 | } |
878 | } | |
53720c49 AM |
879 | else if (this_hdr->sh_type == SHT_GROUP |
880 | && elf_next_in_group (s) == NULL) | |
881 | { | |
4eca0228 | 882 | _bfd_error_handler |
695344c0 | 883 | /* xgettext:c-format */ |
871b3ab2 | 884 | (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"), |
53720c49 AM |
885 | abfd, elf_section_data (s)->this_idx); |
886 | result = FALSE; | |
887 | } | |
dd863624 | 888 | } |
3d7f7666 | 889 | |
dd863624 | 890 | /* Process section groups. */ |
3d7f7666 L |
891 | if (num_group == (unsigned) -1) |
892 | return result; | |
893 | ||
894 | for (i = 0; i < num_group; i++) | |
895 | { | |
896 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
4b0e8a5f NC |
897 | Elf_Internal_Group *idx; |
898 | unsigned int n_elt; | |
3d7f7666 | 899 | |
4b0e8a5f NC |
900 | /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ |
901 | if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) | |
902 | { | |
4eca0228 | 903 | _bfd_error_handler |
695344c0 | 904 | /* xgettext:c-format */ |
871b3ab2 | 905 | (_("%pB: section group entry number %u is corrupt"), |
4b0e8a5f NC |
906 | abfd, i); |
907 | result = FALSE; | |
908 | continue; | |
909 | } | |
910 | ||
911 | idx = (Elf_Internal_Group *) shdr->contents; | |
912 | n_elt = shdr->sh_size / 4; | |
1b786873 | 913 | |
3d7f7666 | 914 | while (--n_elt != 0) |
24d3e51b NC |
915 | { |
916 | ++ idx; | |
917 | ||
918 | if (idx->shdr == NULL) | |
919 | continue; | |
920 | else if (idx->shdr->bfd_section) | |
921 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
db4677b8 AM |
922 | else if (idx->shdr->sh_type != SHT_RELA |
923 | && idx->shdr->sh_type != SHT_REL) | |
24d3e51b NC |
924 | { |
925 | /* There are some unknown sections in the group. */ | |
926 | _bfd_error_handler | |
927 | /* xgettext:c-format */ | |
871b3ab2 | 928 | (_("%pB: unknown type [%#x] section `%s' in group [%pA]"), |
24d3e51b NC |
929 | abfd, |
930 | idx->shdr->sh_type, | |
931 | bfd_elf_string_from_elf_section (abfd, | |
932 | (elf_elfheader (abfd) | |
933 | ->e_shstrndx), | |
934 | idx->shdr->sh_name), | |
935 | shdr->bfd_section); | |
936 | result = FALSE; | |
937 | } | |
938 | } | |
3d7f7666 | 939 | } |
24d3e51b | 940 | |
3d7f7666 L |
941 | return result; |
942 | } | |
943 | ||
72adc230 AM |
944 | bfd_boolean |
945 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
946 | { | |
947 | return elf_next_in_group (sec) != NULL; | |
948 | } | |
949 | ||
f6fe1ccd L |
950 | static char * |
951 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
952 | { | |
953 | unsigned int len = strlen (name); | |
954 | char *new_name = bfd_alloc (abfd, len + 2); | |
955 | if (new_name == NULL) | |
956 | return NULL; | |
957 | new_name[0] = '.'; | |
958 | new_name[1] = 'z'; | |
959 | memcpy (new_name + 2, name + 1, len); | |
960 | return new_name; | |
961 | } | |
962 | ||
963 | static char * | |
964 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
965 | { | |
966 | unsigned int len = strlen (name); | |
967 | char *new_name = bfd_alloc (abfd, len); | |
968 | if (new_name == NULL) | |
969 | return NULL; | |
970 | new_name[0] = '.'; | |
971 | memcpy (new_name + 1, name + 2, len - 1); | |
972 | return new_name; | |
973 | } | |
974 | ||
252b5132 RH |
975 | /* Make a BFD section from an ELF section. We store a pointer to the |
976 | BFD section in the bfd_section field of the header. */ | |
977 | ||
b34976b6 | 978 | bfd_boolean |
217aa764 AM |
979 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
980 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
981 | const char *name, |
982 | int shindex) | |
252b5132 RH |
983 | { |
984 | asection *newsect; | |
985 | flagword flags; | |
9c5bfbb7 | 986 | const struct elf_backend_data *bed; |
252b5132 RH |
987 | |
988 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 989 | return TRUE; |
252b5132 RH |
990 | |
991 | newsect = bfd_make_section_anyway (abfd, name); | |
992 | if (newsect == NULL) | |
b34976b6 | 993 | return FALSE; |
252b5132 | 994 | |
1829f4b2 AM |
995 | hdr->bfd_section = newsect; |
996 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 997 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 998 | |
2f89ff8d L |
999 | /* Always use the real type/flags. */ |
1000 | elf_section_type (newsect) = hdr->sh_type; | |
1001 | elf_section_flags (newsect) = hdr->sh_flags; | |
1002 | ||
252b5132 RH |
1003 | newsect->filepos = hdr->sh_offset; |
1004 | ||
1005 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
1006 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
1007 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 1008 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 1009 | return FALSE; |
252b5132 RH |
1010 | |
1011 | flags = SEC_NO_FLAGS; | |
1012 | if (hdr->sh_type != SHT_NOBITS) | |
1013 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 1014 | if (hdr->sh_type == SHT_GROUP) |
7bdf4127 | 1015 | flags |= SEC_GROUP; |
252b5132 RH |
1016 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
1017 | { | |
1018 | flags |= SEC_ALLOC; | |
1019 | if (hdr->sh_type != SHT_NOBITS) | |
1020 | flags |= SEC_LOAD; | |
1021 | } | |
1022 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
1023 | flags |= SEC_READONLY; | |
1024 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
1025 | flags |= SEC_CODE; | |
1026 | else if ((flags & SEC_LOAD) != 0) | |
1027 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
1028 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
1029 | { | |
1030 | flags |= SEC_MERGE; | |
1031 | newsect->entsize = hdr->sh_entsize; | |
f5fa8ca2 | 1032 | } |
84865015 NC |
1033 | if ((hdr->sh_flags & SHF_STRINGS) != 0) |
1034 | flags |= SEC_STRINGS; | |
dbb410c3 AM |
1035 | if (hdr->sh_flags & SHF_GROUP) |
1036 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 1037 | return FALSE; |
13ae64f3 JJ |
1038 | if ((hdr->sh_flags & SHF_TLS) != 0) |
1039 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
1040 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
1041 | flags |= SEC_EXCLUDE; | |
252b5132 | 1042 | |
3d2b39cf | 1043 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 1044 | { |
3d2b39cf L |
1045 | /* The debugging sections appear to be recognized only by name, |
1046 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
1047 | if (name [0] == '.') |
1048 | { | |
f073ced3 AM |
1049 | const char *p; |
1050 | int n; | |
1051 | if (name[1] == 'd') | |
1052 | p = ".debug", n = 6; | |
1053 | else if (name[1] == 'g' && name[2] == 'n') | |
1054 | p = ".gnu.linkonce.wi.", n = 17; | |
1055 | else if (name[1] == 'g' && name[2] == 'd') | |
1056 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
1057 | else if (name[1] == 'l') | |
1058 | p = ".line", n = 5; | |
1059 | else if (name[1] == 's') | |
1060 | p = ".stab", n = 5; | |
1061 | else if (name[1] == 'z') | |
1062 | p = ".zdebug", n = 7; | |
1063 | else | |
1064 | p = NULL, n = 0; | |
1065 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
1066 | flags |= SEC_DEBUGGING; |
1067 | } | |
1068 | } | |
252b5132 RH |
1069 | |
1070 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
1071 | only link a single copy of the section. This is used to support | |
1072 | g++. g++ will emit each template expansion in its own section. | |
1073 | The symbols will be defined as weak, so that multiple definitions | |
1074 | are permitted. The GNU linker extension is to actually discard | |
1075 | all but one of the sections. */ | |
0112cd26 | 1076 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 1077 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
1078 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1079 | ||
fa152c49 JW |
1080 | bed = get_elf_backend_data (abfd); |
1081 | if (bed->elf_backend_section_flags) | |
1082 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 1083 | return FALSE; |
fa152c49 | 1084 | |
252b5132 | 1085 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 1086 | return FALSE; |
252b5132 | 1087 | |
718175fa JK |
1088 | /* We do not parse the PT_NOTE segments as we are interested even in the |
1089 | separate debug info files which may have the segments offsets corrupted. | |
1090 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
1091 | if (hdr->sh_type == SHT_NOTE) | |
1092 | { | |
baea7ef1 | 1093 | bfd_byte *contents; |
718175fa | 1094 | |
baea7ef1 | 1095 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1096 | return FALSE; |
1097 | ||
276da9b3 L |
1098 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, |
1099 | hdr->sh_offset, hdr->sh_addralign); | |
718175fa JK |
1100 | free (contents); |
1101 | } | |
1102 | ||
252b5132 RH |
1103 | if ((flags & SEC_ALLOC) != 0) |
1104 | { | |
1105 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1106 | unsigned int i, nload; |
1107 | ||
1108 | /* Some ELF linkers produce binaries with all the program header | |
1109 | p_paddr fields zero. If we have such a binary with more than | |
1110 | one PT_LOAD header, then leave the section lma equal to vma | |
1111 | so that we don't create sections with overlapping lma. */ | |
1112 | phdr = elf_tdata (abfd)->phdr; | |
1113 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1114 | if (phdr->p_paddr != 0) | |
1115 | break; | |
1116 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1117 | ++nload; | |
1118 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1119 | return TRUE; | |
252b5132 | 1120 | |
252b5132 RH |
1121 | phdr = elf_tdata (abfd)->phdr; |
1122 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1123 | { | |
86b2281f AM |
1124 | if (((phdr->p_type == PT_LOAD |
1125 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1126 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1127 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1128 | { |
88967714 AM |
1129 | if ((flags & SEC_LOAD) == 0) |
1130 | newsect->lma = (phdr->p_paddr | |
1131 | + hdr->sh_addr - phdr->p_vaddr); | |
1132 | else | |
1133 | /* We used to use the same adjustment for SEC_LOAD | |
1134 | sections, but that doesn't work if the segment | |
1135 | is packed with code from multiple VMAs. | |
1136 | Instead we calculate the section LMA based on | |
1137 | the segment LMA. It is assumed that the | |
1138 | segment will contain sections with contiguous | |
1139 | LMAs, even if the VMAs are not. */ | |
1140 | newsect->lma = (phdr->p_paddr | |
1141 | + hdr->sh_offset - phdr->p_offset); | |
1142 | ||
1143 | /* With contiguous segments, we can't tell from file | |
1144 | offsets whether a section with zero size should | |
1145 | be placed at the end of one segment or the | |
1146 | beginning of the next. Decide based on vaddr. */ | |
1147 | if (hdr->sh_addr >= phdr->p_vaddr | |
1148 | && (hdr->sh_addr + hdr->sh_size | |
1149 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1150 | break; | |
252b5132 RH |
1151 | } |
1152 | } | |
1153 | } | |
1154 | ||
4a114e3e L |
1155 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1156 | .zdebug_*, after the section flags is set. */ | |
1157 | if ((flags & SEC_DEBUGGING) | |
1158 | && ((name[1] == 'd' && name[6] == '_') | |
1159 | || (name[1] == 'z' && name[7] == '_'))) | |
1160 | { | |
1161 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 | 1162 | int compression_header_size; |
dab394de | 1163 | bfd_size_type uncompressed_size; |
151411f8 L |
1164 | bfd_boolean compressed |
1165 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de L |
1166 | &compression_header_size, |
1167 | &uncompressed_size); | |
4a114e3e | 1168 | |
151411f8 | 1169 | if (compressed) |
4a114e3e L |
1170 | { |
1171 | /* Compressed section. Check if we should decompress. */ | |
1172 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1173 | action = decompress; | |
1174 | } | |
151411f8 L |
1175 | |
1176 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1177 | section. Check if we should compress. */ | |
1178 | if (action == nothing) | |
4a114e3e | 1179 | { |
151411f8 L |
1180 | if (newsect->size != 0 |
1181 | && (abfd->flags & BFD_COMPRESS) | |
1182 | && compression_header_size >= 0 | |
dab394de | 1183 | && uncompressed_size > 0 |
151411f8 L |
1184 | && (!compressed |
1185 | || ((compression_header_size > 0) | |
1186 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1187 | action = compress; |
151411f8 L |
1188 | else |
1189 | return TRUE; | |
4a114e3e L |
1190 | } |
1191 | ||
151411f8 | 1192 | if (action == compress) |
4a114e3e | 1193 | { |
4a114e3e L |
1194 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1195 | { | |
4eca0228 | 1196 | _bfd_error_handler |
695344c0 | 1197 | /* xgettext:c-format */ |
871b3ab2 | 1198 | (_("%pB: unable to initialize compress status for section %s"), |
4a114e3e L |
1199 | abfd, name); |
1200 | return FALSE; | |
1201 | } | |
151411f8 L |
1202 | } |
1203 | else | |
1204 | { | |
4a114e3e L |
1205 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1206 | { | |
4eca0228 | 1207 | _bfd_error_handler |
695344c0 | 1208 | /* xgettext:c-format */ |
871b3ab2 | 1209 | (_("%pB: unable to initialize decompress status for section %s"), |
4a114e3e L |
1210 | abfd, name); |
1211 | return FALSE; | |
1212 | } | |
151411f8 L |
1213 | } |
1214 | ||
f6fe1ccd | 1215 | if (abfd->is_linker_input) |
151411f8 | 1216 | { |
f6fe1ccd L |
1217 | if (name[1] == 'z' |
1218 | && (action == decompress | |
1219 | || (action == compress | |
1220 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1221 | { |
f6fe1ccd L |
1222 | /* Convert section name from .zdebug_* to .debug_* so |
1223 | that linker will consider this section as a debug | |
1224 | section. */ | |
1225 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1226 | if (new_name == NULL) |
1227 | return FALSE; | |
f6fe1ccd | 1228 | bfd_rename_section (abfd, newsect, new_name); |
151411f8 | 1229 | } |
4a114e3e | 1230 | } |
f6fe1ccd L |
1231 | else |
1232 | /* For objdump, don't rename the section. For objcopy, delay | |
1233 | section rename to elf_fake_sections. */ | |
1234 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1235 | } |
1236 | ||
b34976b6 | 1237 | return TRUE; |
252b5132 RH |
1238 | } |
1239 | ||
84865015 NC |
1240 | const char *const bfd_elf_section_type_names[] = |
1241 | { | |
252b5132 RH |
1242 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", |
1243 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1244 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1245 | }; | |
1246 | ||
1049f94e | 1247 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1248 | output, and the reloc is against an external symbol, and nothing |
1249 | has given us any additional addend, the resulting reloc will also | |
1250 | be against the same symbol. In such a case, we don't want to | |
1251 | change anything about the way the reloc is handled, since it will | |
1252 | all be done at final link time. Rather than put special case code | |
1253 | into bfd_perform_relocation, all the reloc types use this howto | |
1254 | function. It just short circuits the reloc if producing | |
1049f94e | 1255 | relocatable output against an external symbol. */ |
252b5132 | 1256 | |
252b5132 | 1257 | bfd_reloc_status_type |
217aa764 AM |
1258 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1259 | arelent *reloc_entry, | |
1260 | asymbol *symbol, | |
1261 | void *data ATTRIBUTE_UNUSED, | |
1262 | asection *input_section, | |
1263 | bfd *output_bfd, | |
1264 | char **error_message ATTRIBUTE_UNUSED) | |
1265 | { | |
1266 | if (output_bfd != NULL | |
252b5132 RH |
1267 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1268 | && (! reloc_entry->howto->partial_inplace | |
1269 | || reloc_entry->addend == 0)) | |
1270 | { | |
1271 | reloc_entry->address += input_section->output_offset; | |
1272 | return bfd_reloc_ok; | |
1273 | } | |
1274 | ||
1275 | return bfd_reloc_continue; | |
1276 | } | |
1277 | \f | |
84865015 NC |
1278 | /* Returns TRUE if section A matches section B. |
1279 | Names, addresses and links may be different, but everything else | |
1280 | should be the same. */ | |
1281 | ||
1282 | static bfd_boolean | |
5522f910 NC |
1283 | section_match (const Elf_Internal_Shdr * a, |
1284 | const Elf_Internal_Shdr * b) | |
84865015 NC |
1285 | { |
1286 | return | |
07d6d2b8 | 1287 | a->sh_type == b->sh_type |
5522f910 NC |
1288 | && (a->sh_flags & ~ SHF_INFO_LINK) |
1289 | == (b->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1290 | && a->sh_addralign == b->sh_addralign |
1291 | && a->sh_size == b->sh_size | |
1292 | && a->sh_entsize == b->sh_entsize | |
1293 | /* FIXME: Check sh_addr ? */ | |
1294 | ; | |
1295 | } | |
1296 | ||
1297 | /* Find a section in OBFD that has the same characteristics | |
1298 | as IHEADER. Return the index of this section or SHN_UNDEF if | |
1299 | none can be found. Check's section HINT first, as this is likely | |
1300 | to be the correct section. */ | |
1301 | ||
1302 | static unsigned int | |
5cc4ca83 ST |
1303 | find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, |
1304 | const unsigned int hint) | |
84865015 NC |
1305 | { |
1306 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1307 | unsigned int i; | |
1308 | ||
a55c9876 NC |
1309 | BFD_ASSERT (iheader != NULL); |
1310 | ||
1311 | /* See PR 20922 for a reproducer of the NULL test. */ | |
5cc4ca83 ST |
1312 | if (hint < elf_numsections (obfd) |
1313 | && oheaders[hint] != NULL | |
a55c9876 | 1314 | && section_match (oheaders[hint], iheader)) |
84865015 NC |
1315 | return hint; |
1316 | ||
1317 | for (i = 1; i < elf_numsections (obfd); i++) | |
1318 | { | |
1319 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1320 | ||
a55c9876 NC |
1321 | if (oheader == NULL) |
1322 | continue; | |
84865015 NC |
1323 | if (section_match (oheader, iheader)) |
1324 | /* FIXME: Do we care if there is a potential for | |
1325 | multiple matches ? */ | |
1326 | return i; | |
1327 | } | |
1328 | ||
1329 | return SHN_UNDEF; | |
1330 | } | |
1331 | ||
5522f910 NC |
1332 | /* PR 19938: Attempt to set the ELF section header fields of an OS or |
1333 | Processor specific section, based upon a matching input section. | |
1334 | Returns TRUE upon success, FALSE otherwise. */ | |
07d6d2b8 | 1335 | |
5522f910 NC |
1336 | static bfd_boolean |
1337 | copy_special_section_fields (const bfd *ibfd, | |
1338 | bfd *obfd, | |
1339 | const Elf_Internal_Shdr *iheader, | |
1340 | Elf_Internal_Shdr *oheader, | |
1341 | const unsigned int secnum) | |
1342 | { | |
1343 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
1344 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); | |
1345 | bfd_boolean changed = FALSE; | |
1346 | unsigned int sh_link; | |
1347 | ||
1348 | if (oheader->sh_type == SHT_NOBITS) | |
1349 | { | |
1350 | /* This is a feature for objcopy --only-keep-debug: | |
1351 | When a section's type is changed to NOBITS, we preserve | |
1352 | the sh_link and sh_info fields so that they can be | |
1353 | matched up with the original. | |
1354 | ||
1355 | Note: Strictly speaking these assignments are wrong. | |
1356 | The sh_link and sh_info fields should point to the | |
1357 | relevent sections in the output BFD, which may not be in | |
1358 | the same location as they were in the input BFD. But | |
1359 | the whole point of this action is to preserve the | |
1360 | original values of the sh_link and sh_info fields, so | |
1361 | that they can be matched up with the section headers in | |
1362 | the original file. So strictly speaking we may be | |
1363 | creating an invalid ELF file, but it is only for a file | |
1364 | that just contains debug info and only for sections | |
1365 | without any contents. */ | |
1366 | if (oheader->sh_link == 0) | |
1367 | oheader->sh_link = iheader->sh_link; | |
1368 | if (oheader->sh_info == 0) | |
1369 | oheader->sh_info = iheader->sh_info; | |
1370 | return TRUE; | |
1371 | } | |
1372 | ||
1373 | /* Allow the target a chance to decide how these fields should be set. */ | |
1374 | if (bed->elf_backend_copy_special_section_fields != NULL | |
1375 | && bed->elf_backend_copy_special_section_fields | |
1376 | (ibfd, obfd, iheader, oheader)) | |
1377 | return TRUE; | |
1378 | ||
1379 | /* We have an iheader which might match oheader, and which has non-zero | |
1380 | sh_info and/or sh_link fields. Attempt to follow those links and find | |
1381 | the section in the output bfd which corresponds to the linked section | |
1382 | in the input bfd. */ | |
1383 | if (iheader->sh_link != SHN_UNDEF) | |
1384 | { | |
4f3ca05b NC |
1385 | /* See PR 20931 for a reproducer. */ |
1386 | if (iheader->sh_link >= elf_numsections (ibfd)) | |
1387 | { | |
76cfced5 | 1388 | _bfd_error_handler |
4f3ca05b | 1389 | /* xgettext:c-format */ |
871b3ab2 | 1390 | (_("%pB: Invalid sh_link field (%d) in section number %d"), |
4f3ca05b NC |
1391 | ibfd, iheader->sh_link, secnum); |
1392 | return FALSE; | |
1393 | } | |
1394 | ||
5522f910 NC |
1395 | sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); |
1396 | if (sh_link != SHN_UNDEF) | |
1397 | { | |
1398 | oheader->sh_link = sh_link; | |
1399 | changed = TRUE; | |
1400 | } | |
1401 | else | |
1402 | /* FIXME: Should we install iheader->sh_link | |
1403 | if we could not find a match ? */ | |
76cfced5 | 1404 | _bfd_error_handler |
695344c0 | 1405 | /* xgettext:c-format */ |
871b3ab2 | 1406 | (_("%pB: Failed to find link section for section %d"), obfd, secnum); |
5522f910 NC |
1407 | } |
1408 | ||
1409 | if (iheader->sh_info) | |
1410 | { | |
1411 | /* The sh_info field can hold arbitrary information, but if the | |
1412 | SHF_LINK_INFO flag is set then it should be interpreted as a | |
1413 | section index. */ | |
1414 | if (iheader->sh_flags & SHF_INFO_LINK) | |
1415 | { | |
1416 | sh_link = find_link (obfd, iheaders[iheader->sh_info], | |
1417 | iheader->sh_info); | |
1418 | if (sh_link != SHN_UNDEF) | |
1419 | oheader->sh_flags |= SHF_INFO_LINK; | |
1420 | } | |
1421 | else | |
1422 | /* No idea what it means - just copy it. */ | |
1423 | sh_link = iheader->sh_info; | |
1424 | ||
1425 | if (sh_link != SHN_UNDEF) | |
1426 | { | |
1427 | oheader->sh_info = sh_link; | |
1428 | changed = TRUE; | |
1429 | } | |
1430 | else | |
76cfced5 | 1431 | _bfd_error_handler |
695344c0 | 1432 | /* xgettext:c-format */ |
871b3ab2 | 1433 | (_("%pB: Failed to find info section for section %d"), obfd, secnum); |
5522f910 NC |
1434 | } |
1435 | ||
1436 | return changed; | |
1437 | } | |
07d6d2b8 | 1438 | |
0ac4564e L |
1439 | /* Copy the program header and other data from one object module to |
1440 | another. */ | |
252b5132 | 1441 | |
b34976b6 | 1442 | bfd_boolean |
217aa764 | 1443 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1444 | { |
5522f910 NC |
1445 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); |
1446 | Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); | |
1447 | const struct elf_backend_data *bed; | |
84865015 NC |
1448 | unsigned int i; |
1449 | ||
2d502050 | 1450 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1451 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1452 | return TRUE; |
2d502050 | 1453 | |
57b828ef L |
1454 | if (!elf_flags_init (obfd)) |
1455 | { | |
1456 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1457 | elf_flags_init (obfd) = TRUE; | |
1458 | } | |
2d502050 | 1459 | |
0ac4564e | 1460 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1461 | |
1462 | /* Also copy the EI_OSABI field. */ | |
1463 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1464 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 | 1465 | |
5522f910 NC |
1466 | /* If set, copy the EI_ABIVERSION field. */ |
1467 | if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) | |
1468 | elf_elfheader (obfd)->e_ident[EI_ABIVERSION] | |
1469 | = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; | |
07d6d2b8 | 1470 | |
104d59d1 JM |
1471 | /* Copy object attributes. */ |
1472 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1473 | |
84865015 NC |
1474 | if (iheaders == NULL || oheaders == NULL) |
1475 | return TRUE; | |
63b9bbb7 | 1476 | |
5522f910 NC |
1477 | bed = get_elf_backend_data (obfd); |
1478 | ||
1479 | /* Possibly copy other fields in the section header. */ | |
84865015 | 1480 | for (i = 1; i < elf_numsections (obfd); i++) |
63b9bbb7 | 1481 | { |
84865015 NC |
1482 | unsigned int j; |
1483 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1484 | |
5522f910 NC |
1485 | /* Ignore ordinary sections. SHT_NOBITS sections are considered however |
1486 | because of a special case need for generating separate debug info | |
1487 | files. See below for more details. */ | |
84865015 NC |
1488 | if (oheader == NULL |
1489 | || (oheader->sh_type != SHT_NOBITS | |
5522f910 NC |
1490 | && oheader->sh_type < SHT_LOOS)) |
1491 | continue; | |
1492 | ||
1493 | /* Ignore empty sections, and sections whose | |
1494 | fields have already been initialised. */ | |
1495 | if (oheader->sh_size == 0 | |
84865015 NC |
1496 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) |
1497 | continue; | |
63b9bbb7 | 1498 | |
84865015 | 1499 | /* Scan for the matching section in the input bfd. |
5522f910 NC |
1500 | First we try for a direct mapping between the input and output sections. */ |
1501 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1502 | { | |
1503 | const Elf_Internal_Shdr * iheader = iheaders[j]; | |
1504 | ||
1505 | if (iheader == NULL) | |
1506 | continue; | |
1507 | ||
1508 | if (oheader->bfd_section != NULL | |
1509 | && iheader->bfd_section != NULL | |
1510 | && iheader->bfd_section->output_section != NULL | |
1511 | && iheader->bfd_section->output_section == oheader->bfd_section) | |
1512 | { | |
1513 | /* We have found a connection from the input section to the | |
1514 | output section. Attempt to copy the header fields. If | |
1515 | this fails then do not try any further sections - there | |
1516 | should only be a one-to-one mapping between input and output. */ | |
1517 | if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) | |
1518 | j = elf_numsections (ibfd); | |
1519 | break; | |
1520 | } | |
1521 | } | |
1522 | ||
1523 | if (j < elf_numsections (ibfd)) | |
1524 | continue; | |
1525 | ||
1526 | /* That failed. So try to deduce the corresponding input section. | |
84865015 NC |
1527 | Unfortunately we cannot compare names as the output string table |
1528 | is empty, so instead we check size, address and type. */ | |
1529 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1530 | { | |
5522f910 | 1531 | const Elf_Internal_Shdr * iheader = iheaders[j]; |
84865015 | 1532 | |
5522f910 NC |
1533 | if (iheader == NULL) |
1534 | continue; | |
1535 | ||
1536 | /* Try matching fields in the input section's header. | |
1537 | Since --only-keep-debug turns all non-debug sections into | |
84865015 NC |
1538 | SHT_NOBITS sections, the output SHT_NOBITS type matches any |
1539 | input type. */ | |
1540 | if ((oheader->sh_type == SHT_NOBITS | |
1541 | || iheader->sh_type == oheader->sh_type) | |
5522f910 NC |
1542 | && (iheader->sh_flags & ~ SHF_INFO_LINK) |
1543 | == (oheader->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1544 | && iheader->sh_addralign == oheader->sh_addralign |
1545 | && iheader->sh_entsize == oheader->sh_entsize | |
1546 | && iheader->sh_size == oheader->sh_size | |
1547 | && iheader->sh_addr == oheader->sh_addr | |
1548 | && (iheader->sh_info != oheader->sh_info | |
1549 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1550 | { |
5522f910 NC |
1551 | if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) |
1552 | break; | |
63b9bbb7 NC |
1553 | } |
1554 | } | |
5522f910 NC |
1555 | |
1556 | if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) | |
1557 | { | |
1558 | /* Final attempt. Call the backend copy function | |
1559 | with a NULL input section. */ | |
1560 | if (bed->elf_backend_copy_special_section_fields != NULL) | |
1561 | bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader); | |
1562 | } | |
63b9bbb7 NC |
1563 | } |
1564 | ||
b34976b6 | 1565 | return TRUE; |
2d502050 L |
1566 | } |
1567 | ||
cedc298e L |
1568 | static const char * |
1569 | get_segment_type (unsigned int p_type) | |
1570 | { | |
1571 | const char *pt; | |
1572 | switch (p_type) | |
1573 | { | |
1574 | case PT_NULL: pt = "NULL"; break; | |
1575 | case PT_LOAD: pt = "LOAD"; break; | |
1576 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1577 | case PT_INTERP: pt = "INTERP"; break; | |
1578 | case PT_NOTE: pt = "NOTE"; break; | |
1579 | case PT_SHLIB: pt = "SHLIB"; break; | |
1580 | case PT_PHDR: pt = "PHDR"; break; | |
1581 | case PT_TLS: pt = "TLS"; break; | |
1582 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1583 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1584 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1585 | default: pt = NULL; break; | |
1586 | } | |
1587 | return pt; | |
1588 | } | |
1589 | ||
f0b79d91 L |
1590 | /* Print out the program headers. */ |
1591 | ||
b34976b6 | 1592 | bfd_boolean |
217aa764 | 1593 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1594 | { |
a50b1753 | 1595 | FILE *f = (FILE *) farg; |
252b5132 RH |
1596 | Elf_Internal_Phdr *p; |
1597 | asection *s; | |
1598 | bfd_byte *dynbuf = NULL; | |
1599 | ||
1600 | p = elf_tdata (abfd)->phdr; | |
1601 | if (p != NULL) | |
1602 | { | |
1603 | unsigned int i, c; | |
1604 | ||
1605 | fprintf (f, _("\nProgram Header:\n")); | |
1606 | c = elf_elfheader (abfd)->e_phnum; | |
1607 | for (i = 0; i < c; i++, p++) | |
1608 | { | |
cedc298e | 1609 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1610 | char buf[20]; |
1611 | ||
cedc298e | 1612 | if (pt == NULL) |
252b5132 | 1613 | { |
cedc298e L |
1614 | sprintf (buf, "0x%lx", p->p_type); |
1615 | pt = buf; | |
252b5132 | 1616 | } |
dc810e39 | 1617 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1618 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1619 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1620 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1621 | fprintf (f, " paddr 0x"); |
60b89a18 | 1622 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1623 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1624 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1625 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1626 | fprintf (f, " memsz 0x"); |
60b89a18 | 1627 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1628 | fprintf (f, " flags %c%c%c", |
1629 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1630 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1631 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1632 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1633 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1634 | fprintf (f, "\n"); |
1635 | } | |
1636 | } | |
1637 | ||
1638 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1639 | if (s != NULL) | |
1640 | { | |
cb33740c | 1641 | unsigned int elfsec; |
dc810e39 | 1642 | unsigned long shlink; |
252b5132 RH |
1643 | bfd_byte *extdyn, *extdynend; |
1644 | size_t extdynsize; | |
217aa764 | 1645 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1646 | |
1647 | fprintf (f, _("\nDynamic Section:\n")); | |
1648 | ||
eea6121a | 1649 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1650 | goto error_return; |
1651 | ||
1652 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1653 | if (elfsec == SHN_BAD) |
252b5132 | 1654 | goto error_return; |
dc810e39 | 1655 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1656 | |
1657 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1658 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1659 | ||
1660 | extdyn = dynbuf; | |
06614111 NC |
1661 | /* PR 17512: file: 6f427532. */ |
1662 | if (s->size < extdynsize) | |
1663 | goto error_return; | |
eea6121a | 1664 | extdynend = extdyn + s->size; |
1036838a | 1665 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
07d6d2b8 | 1666 | Fix range check. */ |
1036838a | 1667 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) |
252b5132 RH |
1668 | { |
1669 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1670 | const char *name = ""; |
252b5132 | 1671 | char ab[20]; |
b34976b6 | 1672 | bfd_boolean stringp; |
ad9563d6 | 1673 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1674 | |
217aa764 | 1675 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1676 | |
1677 | if (dyn.d_tag == DT_NULL) | |
1678 | break; | |
1679 | ||
b34976b6 | 1680 | stringp = FALSE; |
252b5132 RH |
1681 | switch (dyn.d_tag) |
1682 | { | |
1683 | default: | |
ad9563d6 CM |
1684 | if (bed->elf_backend_get_target_dtag) |
1685 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1686 | ||
1687 | if (!strcmp (name, "")) | |
1688 | { | |
cd9af601 | 1689 | sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag); |
ad9563d6 CM |
1690 | name = ab; |
1691 | } | |
252b5132 RH |
1692 | break; |
1693 | ||
b34976b6 | 1694 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1695 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1696 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1697 | case DT_HASH: name = "HASH"; break; | |
1698 | case DT_STRTAB: name = "STRTAB"; break; | |
1699 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1700 | case DT_RELA: name = "RELA"; break; | |
1701 | case DT_RELASZ: name = "RELASZ"; break; | |
1702 | case DT_RELAENT: name = "RELAENT"; break; | |
1703 | case DT_STRSZ: name = "STRSZ"; break; | |
1704 | case DT_SYMENT: name = "SYMENT"; break; | |
1705 | case DT_INIT: name = "INIT"; break; | |
1706 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1707 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1708 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1709 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1710 | case DT_REL: name = "REL"; break; | |
1711 | case DT_RELSZ: name = "RELSZ"; break; | |
1712 | case DT_RELENT: name = "RELENT"; break; | |
1713 | case DT_PLTREL: name = "PLTREL"; break; | |
1714 | case DT_DEBUG: name = "DEBUG"; break; | |
1715 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1716 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1717 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1718 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1719 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1720 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1721 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1722 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1723 | case DT_FLAGS: name = "FLAGS"; break; |
1724 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1725 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1726 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1727 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1728 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1729 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1730 | case DT_FEATURE: name = "FEATURE"; break; | |
1731 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1732 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1733 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1734 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1735 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1736 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1737 | case DT_PLTPAD: name = "PLTPAD"; break; |
1738 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1739 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1740 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1741 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1742 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1743 | case DT_VERSYM: name = "VERSYM"; break; |
1744 | case DT_VERDEF: name = "VERDEF"; break; | |
1745 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1746 | case DT_VERNEED: name = "VERNEED"; break; | |
1747 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1748 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1749 | case DT_USED: name = "USED"; break; |
b34976b6 | 1750 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1751 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1752 | } |
1753 | ||
ad9563d6 | 1754 | fprintf (f, " %-20s ", name); |
252b5132 | 1755 | if (! stringp) |
a1f3c56e AN |
1756 | { |
1757 | fprintf (f, "0x"); | |
1758 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1759 | } | |
252b5132 RH |
1760 | else |
1761 | { | |
1762 | const char *string; | |
dc810e39 | 1763 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1764 | |
dc810e39 | 1765 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1766 | if (string == NULL) |
1767 | goto error_return; | |
1768 | fprintf (f, "%s", string); | |
1769 | } | |
1770 | fprintf (f, "\n"); | |
1771 | } | |
1772 | ||
1773 | free (dynbuf); | |
1774 | dynbuf = NULL; | |
1775 | } | |
1776 | ||
1777 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1778 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1779 | { | |
fc0e6df6 | 1780 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1781 | return FALSE; |
252b5132 RH |
1782 | } |
1783 | ||
1784 | if (elf_dynverdef (abfd) != 0) | |
1785 | { | |
1786 | Elf_Internal_Verdef *t; | |
1787 | ||
1788 | fprintf (f, _("\nVersion definitions:\n")); | |
1789 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1790 | { | |
1791 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1792 | t->vd_flags, t->vd_hash, |
1793 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1794 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1795 | { |
1796 | Elf_Internal_Verdaux *a; | |
1797 | ||
1798 | fprintf (f, "\t"); | |
1799 | for (a = t->vd_auxptr->vda_nextptr; | |
1800 | a != NULL; | |
1801 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1802 | fprintf (f, "%s ", |
1803 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1804 | fprintf (f, "\n"); |
1805 | } | |
1806 | } | |
1807 | } | |
1808 | ||
1809 | if (elf_dynverref (abfd) != 0) | |
1810 | { | |
1811 | Elf_Internal_Verneed *t; | |
1812 | ||
1813 | fprintf (f, _("\nVersion References:\n")); | |
1814 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1815 | { | |
1816 | Elf_Internal_Vernaux *a; | |
1817 | ||
d0fb9a8d JJ |
1818 | fprintf (f, _(" required from %s:\n"), |
1819 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1820 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1821 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1822 | a->vna_flags, a->vna_other, |
1823 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1824 | } |
1825 | } | |
1826 | ||
b34976b6 | 1827 | return TRUE; |
252b5132 RH |
1828 | |
1829 | error_return: | |
1830 | if (dynbuf != NULL) | |
1831 | free (dynbuf); | |
b34976b6 | 1832 | return FALSE; |
252b5132 RH |
1833 | } |
1834 | ||
bb4d2ac2 L |
1835 | /* Get version string. */ |
1836 | ||
1837 | const char * | |
60bb06bc L |
1838 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1839 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1840 | { |
1841 | const char *version_string = NULL; | |
1842 | if (elf_dynversym (abfd) != 0 | |
1843 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1844 | { | |
1845 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1846 | ||
1847 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1848 | vernum &= VERSYM_VERSION; | |
1849 | ||
1850 | if (vernum == 0) | |
1851 | version_string = ""; | |
1852 | else if (vernum == 1) | |
1853 | version_string = "Base"; | |
1854 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1855 | version_string = | |
1856 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1857 | else | |
1858 | { | |
1859 | Elf_Internal_Verneed *t; | |
1860 | ||
1861 | version_string = ""; | |
1862 | for (t = elf_tdata (abfd)->verref; | |
1863 | t != NULL; | |
1864 | t = t->vn_nextref) | |
1865 | { | |
1866 | Elf_Internal_Vernaux *a; | |
1867 | ||
1868 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1869 | { | |
1870 | if (a->vna_other == vernum) | |
1871 | { | |
1872 | version_string = a->vna_nodename; | |
1873 | break; | |
1874 | } | |
1875 | } | |
1876 | } | |
1877 | } | |
1878 | } | |
1879 | return version_string; | |
1880 | } | |
1881 | ||
252b5132 RH |
1882 | /* Display ELF-specific fields of a symbol. */ |
1883 | ||
1884 | void | |
217aa764 AM |
1885 | bfd_elf_print_symbol (bfd *abfd, |
1886 | void *filep, | |
1887 | asymbol *symbol, | |
1888 | bfd_print_symbol_type how) | |
252b5132 | 1889 | { |
a50b1753 | 1890 | FILE *file = (FILE *) filep; |
252b5132 RH |
1891 | switch (how) |
1892 | { | |
1893 | case bfd_print_symbol_name: | |
1894 | fprintf (file, "%s", symbol->name); | |
1895 | break; | |
1896 | case bfd_print_symbol_more: | |
1897 | fprintf (file, "elf "); | |
60b89a18 | 1898 | bfd_fprintf_vma (abfd, file, symbol->value); |
cd9af601 | 1899 | fprintf (file, " %x", symbol->flags); |
252b5132 RH |
1900 | break; |
1901 | case bfd_print_symbol_all: | |
1902 | { | |
4e8a9624 AM |
1903 | const char *section_name; |
1904 | const char *name = NULL; | |
9c5bfbb7 | 1905 | const struct elf_backend_data *bed; |
7a13edea | 1906 | unsigned char st_other; |
dbb410c3 | 1907 | bfd_vma val; |
bb4d2ac2 L |
1908 | const char *version_string; |
1909 | bfd_boolean hidden; | |
c044fabd | 1910 | |
252b5132 | 1911 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1912 | |
1913 | bed = get_elf_backend_data (abfd); | |
1914 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1915 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1916 | |
1917 | if (name == NULL) | |
1918 | { | |
7ee38065 | 1919 | name = symbol->name; |
217aa764 | 1920 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1921 | } |
1922 | ||
252b5132 RH |
1923 | fprintf (file, " %s\t", section_name); |
1924 | /* Print the "other" value for a symbol. For common symbols, | |
1925 | we've already printed the size; now print the alignment. | |
1926 | For other symbols, we have no specified alignment, and | |
1927 | we've printed the address; now print the size. */ | |
dcf6c779 | 1928 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1929 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1930 | else | |
1931 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1932 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1933 | |
1934 | /* If we have version information, print it. */ | |
60bb06bc L |
1935 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1936 | symbol, | |
1937 | &hidden); | |
bb4d2ac2 | 1938 | if (version_string) |
252b5132 | 1939 | { |
bb4d2ac2 | 1940 | if (!hidden) |
252b5132 RH |
1941 | fprintf (file, " %-11s", version_string); |
1942 | else | |
1943 | { | |
1944 | int i; | |
1945 | ||
1946 | fprintf (file, " (%s)", version_string); | |
1947 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1948 | putc (' ', file); | |
1949 | } | |
1950 | } | |
1951 | ||
1952 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1953 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1954 | |
7a13edea NC |
1955 | switch (st_other) |
1956 | { | |
1957 | case 0: break; | |
1958 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1959 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1960 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1961 | default: | |
1962 | /* Some other non-defined flags are also present, so print | |
1963 | everything hex. */ | |
1964 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1965 | } | |
252b5132 | 1966 | |
587ff49e | 1967 | fprintf (file, " %s", name); |
252b5132 RH |
1968 | } |
1969 | break; | |
1970 | } | |
1971 | } | |
252b5132 RH |
1972 | \f |
1973 | /* ELF .o/exec file reading */ | |
1974 | ||
c044fabd | 1975 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1976 | |
b34976b6 | 1977 | bfd_boolean |
217aa764 | 1978 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1979 | { |
4fbb74a6 AM |
1980 | Elf_Internal_Shdr *hdr; |
1981 | Elf_Internal_Ehdr *ehdr; | |
1982 | const struct elf_backend_data *bed; | |
90937f86 | 1983 | const char *name; |
bf67003b NC |
1984 | bfd_boolean ret = TRUE; |
1985 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1986 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1987 | static unsigned int nesting = 0; |
252b5132 | 1988 | |
4fbb74a6 AM |
1989 | if (shindex >= elf_numsections (abfd)) |
1990 | return FALSE; | |
1991 | ||
bf67003b NC |
1992 | if (++ nesting > 3) |
1993 | { | |
1994 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1995 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1996 | loop. Detect this here, by refusing to load a section that we are |
1997 | already in the process of loading. We only trigger this test if | |
1998 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1999 | can expect to recurse at least once. |
2000 | ||
2001 | FIXME: It would be better if this array was attached to the bfd, | |
2002 | rather than being held in a static pointer. */ | |
2003 | ||
2004 | if (sections_being_created_abfd != abfd) | |
2005 | sections_being_created = NULL; | |
bf67003b NC |
2006 | if (sections_being_created == NULL) |
2007 | { | |
2008 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
2009 | sections_being_created = (bfd_boolean *) | |
2010 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 2011 | sections_being_created_abfd = abfd; |
bf67003b NC |
2012 | } |
2013 | if (sections_being_created [shindex]) | |
2014 | { | |
4eca0228 | 2015 | _bfd_error_handler |
871b3ab2 | 2016 | (_("%pB: warning: loop in section dependencies detected"), abfd); |
bf67003b NC |
2017 | return FALSE; |
2018 | } | |
2019 | sections_being_created [shindex] = TRUE; | |
2020 | } | |
2021 | ||
4fbb74a6 AM |
2022 | hdr = elf_elfsections (abfd)[shindex]; |
2023 | ehdr = elf_elfheader (abfd); | |
2024 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 2025 | hdr->sh_name); |
933d961a | 2026 | if (name == NULL) |
bf67003b | 2027 | goto fail; |
252b5132 | 2028 | |
4fbb74a6 | 2029 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
2030 | switch (hdr->sh_type) |
2031 | { | |
2032 | case SHT_NULL: | |
2033 | /* Inactive section. Throw it away. */ | |
bf67003b | 2034 | goto success; |
252b5132 | 2035 | |
bf67003b NC |
2036 | case SHT_PROGBITS: /* Normal section with contents. */ |
2037 | case SHT_NOBITS: /* .bss section. */ | |
2038 | case SHT_HASH: /* .hash section. */ | |
2039 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
2040 | case SHT_INIT_ARRAY: /* .init_array section. */ |
2041 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
2042 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 2043 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 2044 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
2045 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2046 | goto success; | |
252b5132 | 2047 | |
797fc050 | 2048 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 2049 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2050 | goto fail; |
2051 | ||
cfcac11d NC |
2052 | if (hdr->sh_link > elf_numsections (abfd)) |
2053 | { | |
caa83f8b | 2054 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
2055 | field set to SHN_BEFORE or SHN_AFTER. */ |
2056 | switch (bfd_get_arch (abfd)) | |
2057 | { | |
caa83f8b | 2058 | case bfd_arch_i386: |
cfcac11d NC |
2059 | case bfd_arch_sparc: |
2060 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
2061 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
2062 | break; | |
2063 | /* Otherwise fall through. */ | |
2064 | default: | |
bf67003b | 2065 | goto fail; |
cfcac11d NC |
2066 | } |
2067 | } | |
2068 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 2069 | goto fail; |
cfcac11d | 2070 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
2071 | { |
2072 | Elf_Internal_Shdr *dynsymhdr; | |
2073 | ||
2074 | /* The shared libraries distributed with hpux11 have a bogus | |
2075 | sh_link field for the ".dynamic" section. Find the | |
2076 | string table for the ".dynsym" section instead. */ | |
2077 | if (elf_dynsymtab (abfd) != 0) | |
2078 | { | |
2079 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
2080 | hdr->sh_link = dynsymhdr->sh_link; | |
2081 | } | |
2082 | else | |
2083 | { | |
2084 | unsigned int i, num_sec; | |
2085 | ||
2086 | num_sec = elf_numsections (abfd); | |
2087 | for (i = 1; i < num_sec; i++) | |
2088 | { | |
2089 | dynsymhdr = elf_elfsections (abfd)[i]; | |
2090 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
2091 | { | |
2092 | hdr->sh_link = dynsymhdr->sh_link; | |
2093 | break; | |
2094 | } | |
2095 | } | |
2096 | } | |
2097 | } | |
bf67003b | 2098 | goto success; |
797fc050 | 2099 | |
bf67003b | 2100 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 2101 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 2102 | goto success; |
252b5132 | 2103 | |
a50b2160 | 2104 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2105 | goto fail; |
2106 | ||
3337c1e5 | 2107 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
2108 | { |
2109 | if (hdr->sh_size != 0) | |
bf67003b | 2110 | goto fail; |
eee3b786 AM |
2111 | /* Some assemblers erroneously set sh_info to one with a |
2112 | zero sh_size. ld sees this as a global symbol count | |
2113 | of (unsigned) -1. Fix it here. */ | |
2114 | hdr->sh_info = 0; | |
bf67003b | 2115 | goto success; |
eee3b786 | 2116 | } |
bf67003b | 2117 | |
16ad13ec NC |
2118 | /* PR 18854: A binary might contain more than one symbol table. |
2119 | Unusual, but possible. Warn, but continue. */ | |
2120 | if (elf_onesymtab (abfd) != 0) | |
2121 | { | |
4eca0228 | 2122 | _bfd_error_handler |
695344c0 | 2123 | /* xgettext:c-format */ |
871b3ab2 | 2124 | (_("%pB: warning: multiple symbol tables detected" |
63a5468a | 2125 | " - ignoring the table in section %u"), |
16ad13ec NC |
2126 | abfd, shindex); |
2127 | goto success; | |
2128 | } | |
252b5132 | 2129 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
2130 | elf_symtab_hdr (abfd) = *hdr; |
2131 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
2132 | abfd->flags |= HAS_SYMS; |
2133 | ||
2134 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
2135 | SHF_ALLOC is set, and this is a shared object, then we also |
2136 | treat this section as a BFD section. We can not base the | |
2137 | decision purely on SHF_ALLOC, because that flag is sometimes | |
2138 | set in a relocatable object file, which would confuse the | |
2139 | linker. */ | |
252b5132 RH |
2140 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
2141 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
2142 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2143 | shindex)) | |
bf67003b | 2144 | goto fail; |
252b5132 | 2145 | |
1b3a8575 AM |
2146 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2147 | can't read symbols without that section loaded as well. It | |
2148 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2149 | { |
2150 | elf_section_list * entry; | |
2151 | unsigned int i, num_sec; | |
1b3a8575 | 2152 | |
6a40cf0c NC |
2153 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2154 | if (entry->hdr.sh_link == shindex) | |
2155 | goto success; | |
2156 | ||
2157 | num_sec = elf_numsections (abfd); | |
2158 | for (i = shindex + 1; i < num_sec; i++) | |
2159 | { | |
2160 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2161 | ||
2162 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2163 | && hdr2->sh_link == shindex) | |
2164 | break; | |
2165 | } | |
2166 | ||
2167 | if (i == num_sec) | |
2168 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2169 | { |
2170 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2171 | |
1b3a8575 AM |
2172 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2173 | && hdr2->sh_link == shindex) | |
2174 | break; | |
2175 | } | |
6a40cf0c NC |
2176 | |
2177 | if (i != shindex) | |
2178 | ret = bfd_section_from_shdr (abfd, i); | |
2179 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2180 | goto success; | |
2181 | } | |
252b5132 | 2182 | |
bf67003b | 2183 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2184 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2185 | goto success; |
252b5132 | 2186 | |
a50b2160 | 2187 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2188 | goto fail; |
2189 | ||
eee3b786 AM |
2190 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2191 | { | |
2192 | if (hdr->sh_size != 0) | |
bf67003b NC |
2193 | goto fail; |
2194 | ||
eee3b786 AM |
2195 | /* Some linkers erroneously set sh_info to one with a |
2196 | zero sh_size. ld sees this as a global symbol count | |
2197 | of (unsigned) -1. Fix it here. */ | |
2198 | hdr->sh_info = 0; | |
bf67003b | 2199 | goto success; |
eee3b786 | 2200 | } |
bf67003b | 2201 | |
16ad13ec NC |
2202 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2203 | Unusual, but possible. Warn, but continue. */ | |
2204 | if (elf_dynsymtab (abfd) != 0) | |
2205 | { | |
4eca0228 | 2206 | _bfd_error_handler |
695344c0 | 2207 | /* xgettext:c-format */ |
871b3ab2 | 2208 | (_("%pB: warning: multiple dynamic symbol tables detected" |
63a5468a | 2209 | " - ignoring the table in section %u"), |
16ad13ec NC |
2210 | abfd, shindex); |
2211 | goto success; | |
2212 | } | |
252b5132 RH |
2213 | elf_dynsymtab (abfd) = shindex; |
2214 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2215 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2216 | abfd->flags |= HAS_SYMS; | |
2217 | ||
2218 | /* Besides being a symbol table, we also treat this as a regular | |
2219 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2220 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2221 | goto success; | |
252b5132 | 2222 | |
bf67003b | 2223 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2224 | { |
2225 | elf_section_list * entry; | |
9ad5cbcf | 2226 | |
6a40cf0c NC |
2227 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2228 | if (entry->ndx == shindex) | |
2229 | goto success; | |
07d6d2b8 | 2230 | |
6a40cf0c NC |
2231 | entry = bfd_alloc (abfd, sizeof * entry); |
2232 | if (entry == NULL) | |
2233 | goto fail; | |
2234 | entry->ndx = shindex; | |
2235 | entry->hdr = * hdr; | |
2236 | entry->next = elf_symtab_shndx_list (abfd); | |
2237 | elf_symtab_shndx_list (abfd) = entry; | |
2238 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2239 | goto success; | |
2240 | } | |
9ad5cbcf | 2241 | |
bf67003b | 2242 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2243 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2244 | goto success; |
2245 | ||
252b5132 RH |
2246 | if (ehdr->e_shstrndx == shindex) |
2247 | { | |
2248 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2249 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2250 | goto success; |
252b5132 | 2251 | } |
bf67003b | 2252 | |
1b3a8575 AM |
2253 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2254 | { | |
2255 | symtab_strtab: | |
2256 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2257 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2258 | goto success; |
1b3a8575 | 2259 | } |
bf67003b | 2260 | |
1b3a8575 AM |
2261 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2262 | { | |
2263 | dynsymtab_strtab: | |
2264 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2265 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2266 | elf_elfsections (abfd)[shindex] = hdr; | |
2267 | /* We also treat this as a regular section, so that objcopy | |
2268 | can handle it. */ | |
bf67003b NC |
2269 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2270 | shindex); | |
2271 | goto success; | |
1b3a8575 | 2272 | } |
252b5132 | 2273 | |
1b3a8575 AM |
2274 | /* If the string table isn't one of the above, then treat it as a |
2275 | regular section. We need to scan all the headers to be sure, | |
2276 | just in case this strtab section appeared before the above. */ | |
2277 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2278 | { | |
2279 | unsigned int i, num_sec; | |
252b5132 | 2280 | |
1b3a8575 AM |
2281 | num_sec = elf_numsections (abfd); |
2282 | for (i = 1; i < num_sec; i++) | |
2283 | { | |
2284 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2285 | if (hdr2->sh_link == shindex) | |
2286 | { | |
933d961a JJ |
2287 | /* Prevent endless recursion on broken objects. */ |
2288 | if (i == shindex) | |
bf67003b | 2289 | goto fail; |
1b3a8575 | 2290 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2291 | goto fail; |
1b3a8575 AM |
2292 | if (elf_onesymtab (abfd) == i) |
2293 | goto symtab_strtab; | |
2294 | if (elf_dynsymtab (abfd) == i) | |
2295 | goto dynsymtab_strtab; | |
2296 | } | |
2297 | } | |
2298 | } | |
bf67003b NC |
2299 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2300 | goto success; | |
252b5132 RH |
2301 | |
2302 | case SHT_REL: | |
2303 | case SHT_RELA: | |
2304 | /* *These* do a lot of work -- but build no sections! */ | |
2305 | { | |
2306 | asection *target_sect; | |
d4730f92 | 2307 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2308 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 | 2309 | struct bfd_elf_section_data *esdt; |
252b5132 | 2310 | |
aa2ca951 JJ |
2311 | if (hdr->sh_entsize |
2312 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2313 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2314 | goto fail; |
a50b2160 | 2315 | |
03ae5f59 | 2316 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2317 | if (hdr->sh_link >= num_sec) |
03ae5f59 | 2318 | { |
4eca0228 | 2319 | _bfd_error_handler |
695344c0 | 2320 | /* xgettext:c-format */ |
871b3ab2 | 2321 | (_("%pB: invalid link %u for reloc section %s (index %u)"), |
4eca0228 | 2322 | abfd, hdr->sh_link, name, shindex); |
bf67003b NC |
2323 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2324 | shindex); | |
2325 | goto success; | |
03ae5f59 ILT |
2326 | } |
2327 | ||
252b5132 RH |
2328 | /* For some incomprehensible reason Oracle distributes |
2329 | libraries for Solaris in which some of the objects have | |
2330 | bogus sh_link fields. It would be nice if we could just | |
2331 | reject them, but, unfortunately, some people need to use | |
2332 | them. We scan through the section headers; if we find only | |
2333 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2334 | to it. I hope this doesn't break anything. |
2335 | ||
2336 | Don't do it on executable nor shared library. */ | |
2337 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2338 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2339 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2340 | { | |
9ad5cbcf | 2341 | unsigned int scan; |
252b5132 RH |
2342 | int found; |
2343 | ||
2344 | found = 0; | |
9ad5cbcf | 2345 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2346 | { |
2347 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2348 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2349 | { | |
2350 | if (found != 0) | |
2351 | { | |
2352 | found = 0; | |
2353 | break; | |
2354 | } | |
2355 | found = scan; | |
2356 | } | |
2357 | } | |
2358 | if (found != 0) | |
2359 | hdr->sh_link = found; | |
2360 | } | |
2361 | ||
2362 | /* Get the symbol table. */ | |
1b3a8575 AM |
2363 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2364 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2365 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2366 | goto fail; |
252b5132 RH |
2367 | |
2368 | /* If this reloc section does not use the main symbol table we | |
2369 | don't treat it as a reloc section. BFD can't adequately | |
2370 | represent such a section, so at least for now, we don't | |
c044fabd | 2371 | try. We just present it as a normal section. We also |
60bcf0fa | 2372 | can't use it as a reloc section if it points to the null |
83b89087 L |
2373 | section, an invalid section, another reloc section, or its |
2374 | sh_link points to the null section. */ | |
185ef66d | 2375 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 2376 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 2377 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2378 | || hdr->sh_info >= num_sec |
2379 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2380 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2381 | { |
2382 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2383 | shindex); | |
2384 | goto success; | |
2385 | } | |
252b5132 RH |
2386 | |
2387 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2388 | goto fail; |
2389 | ||
252b5132 RH |
2390 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2391 | if (target_sect == NULL) | |
bf67003b | 2392 | goto fail; |
252b5132 | 2393 | |
d4730f92 BS |
2394 | esdt = elf_section_data (target_sect); |
2395 | if (hdr->sh_type == SHT_RELA) | |
2396 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2397 | else |
d4730f92 BS |
2398 | p_hdr = &esdt->rel.hdr; |
2399 | ||
06614111 NC |
2400 | /* PR 17512: file: 0b4f81b7. */ |
2401 | if (*p_hdr != NULL) | |
2402 | goto fail; | |
ef53be89 | 2403 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); |
d4730f92 | 2404 | if (hdr2 == NULL) |
bf67003b | 2405 | goto fail; |
252b5132 | 2406 | *hdr2 = *hdr; |
d4730f92 | 2407 | *p_hdr = hdr2; |
252b5132 | 2408 | elf_elfsections (abfd)[shindex] = hdr2; |
056bafd4 MR |
2409 | target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) |
2410 | * bed->s->int_rels_per_ext_rel); | |
252b5132 RH |
2411 | target_sect->flags |= SEC_RELOC; |
2412 | target_sect->relocation = NULL; | |
2413 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2414 | /* In the section to which the relocations apply, mark whether |
2415 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2416 | if (hdr->sh_size != 0) |
d4730f92 BS |
2417 | { |
2418 | if (hdr->sh_type == SHT_RELA) | |
2419 | target_sect->use_rela_p = 1; | |
2420 | } | |
252b5132 | 2421 | abfd->flags |= HAS_RELOC; |
bf67003b | 2422 | goto success; |
252b5132 | 2423 | } |
252b5132 RH |
2424 | |
2425 | case SHT_GNU_verdef: | |
2426 | elf_dynverdef (abfd) = shindex; | |
2427 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2428 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2429 | goto success; | |
252b5132 RH |
2430 | |
2431 | case SHT_GNU_versym: | |
a50b2160 | 2432 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2433 | goto fail; |
2434 | ||
252b5132 RH |
2435 | elf_dynversym (abfd) = shindex; |
2436 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2437 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2438 | goto success; | |
252b5132 RH |
2439 | |
2440 | case SHT_GNU_verneed: | |
2441 | elf_dynverref (abfd) = shindex; | |
2442 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2443 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2444 | goto success; | |
252b5132 RH |
2445 | |
2446 | case SHT_SHLIB: | |
bf67003b | 2447 | goto success; |
252b5132 | 2448 | |
dbb410c3 | 2449 | case SHT_GROUP: |
44534af3 | 2450 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2451 | goto fail; |
2452 | ||
6dc132d9 | 2453 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2454 | goto fail; |
2455 | ||
bf67003b | 2456 | goto success; |
dbb410c3 | 2457 | |
252b5132 | 2458 | default: |
104d59d1 JM |
2459 | /* Possibly an attributes section. */ |
2460 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2461 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2462 | { | |
2463 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2464 | goto fail; |
104d59d1 | 2465 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2466 | goto success; |
104d59d1 JM |
2467 | } |
2468 | ||
252b5132 | 2469 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2470 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2471 | goto success; |
3eb70a79 L |
2472 | |
2473 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2474 | { | |
2475 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2476 | /* FIXME: How to properly handle allocated section reserved | |
2477 | for applications? */ | |
4eca0228 | 2478 | _bfd_error_handler |
695344c0 | 2479 | /* xgettext:c-format */ |
871b3ab2 | 2480 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2481 | abfd, hdr->sh_type, name); |
3eb70a79 | 2482 | else |
bf67003b NC |
2483 | { |
2484 | /* Allow sections reserved for applications. */ | |
2485 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2486 | shindex); | |
2487 | goto success; | |
2488 | } | |
3eb70a79 L |
2489 | } |
2490 | else if (hdr->sh_type >= SHT_LOPROC | |
2491 | && hdr->sh_type <= SHT_HIPROC) | |
2492 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2493 | _bfd_error_handler |
695344c0 | 2494 | /* xgettext:c-format */ |
871b3ab2 | 2495 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2496 | abfd, hdr->sh_type, name); |
3eb70a79 | 2497 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) |
ff15b240 NC |
2498 | { |
2499 | /* Unrecognised OS-specific sections. */ | |
2500 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2501 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2502 | required to correctly process the section and the file should |
ff15b240 | 2503 | be rejected with an error message. */ |
4eca0228 | 2504 | _bfd_error_handler |
695344c0 | 2505 | /* xgettext:c-format */ |
871b3ab2 | 2506 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2507 | abfd, hdr->sh_type, name); |
ff15b240 | 2508 | else |
bf67003b NC |
2509 | { |
2510 | /* Otherwise it should be processed. */ | |
2511 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2512 | goto success; | |
2513 | } | |
ff15b240 | 2514 | } |
3eb70a79 L |
2515 | else |
2516 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2517 | _bfd_error_handler |
695344c0 | 2518 | /* xgettext:c-format */ |
871b3ab2 | 2519 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2520 | abfd, hdr->sh_type, name); |
3eb70a79 | 2521 | |
bf67003b | 2522 | goto fail; |
252b5132 RH |
2523 | } |
2524 | ||
bf67003b NC |
2525 | fail: |
2526 | ret = FALSE; | |
2527 | success: | |
e5b470e2 | 2528 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2529 | sections_being_created [shindex] = FALSE; |
2530 | if (-- nesting == 0) | |
5a4b0ccc NC |
2531 | { |
2532 | sections_being_created = NULL; | |
2533 | sections_being_created_abfd = abfd; | |
2534 | } | |
bf67003b | 2535 | return ret; |
252b5132 RH |
2536 | } |
2537 | ||
87d72d41 | 2538 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2539 | |
87d72d41 AM |
2540 | Elf_Internal_Sym * |
2541 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2542 | bfd *abfd, | |
2543 | unsigned long r_symndx) | |
ec338859 | 2544 | { |
ec338859 AM |
2545 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2546 | ||
a5d1b3b5 AM |
2547 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2548 | { | |
2549 | Elf_Internal_Shdr *symtab_hdr; | |
2550 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2551 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2552 | |
a5d1b3b5 AM |
2553 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2554 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2555 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2556 | return NULL; |
9ad5cbcf | 2557 | |
a5d1b3b5 AM |
2558 | if (cache->abfd != abfd) |
2559 | { | |
2560 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2561 | cache->abfd = abfd; | |
2562 | } | |
2563 | cache->indx[ent] = r_symndx; | |
ec338859 | 2564 | } |
a5d1b3b5 | 2565 | |
87d72d41 | 2566 | return &cache->sym[ent]; |
ec338859 AM |
2567 | } |
2568 | ||
252b5132 RH |
2569 | /* Given an ELF section number, retrieve the corresponding BFD |
2570 | section. */ | |
2571 | ||
2572 | asection * | |
91d6fa6a | 2573 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2574 | { |
91d6fa6a | 2575 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2576 | return NULL; |
91d6fa6a | 2577 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2578 | } |
2579 | ||
b35d266b | 2580 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2581 | { |
0112cd26 | 2582 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 | 2583 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2584 | }; |
2585 | ||
b35d266b | 2586 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2587 | { |
0112cd26 | 2588 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 | 2589 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2590 | }; |
2591 | ||
b35d266b | 2592 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2593 | { |
07d6d2b8 AM |
2594 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2595 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2596 | /* There are more DWARF sections than these, but they needn't be added here |
2597 | unless you have to cope with broken compilers that don't emit section | |
2598 | attributes or you want to help the user writing assembler. */ | |
07d6d2b8 AM |
2599 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2600 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2601 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2602 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
0112cd26 | 2603 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 AM |
2604 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, |
2605 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2606 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2607 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2608 | }; |
2609 | ||
b35d266b | 2610 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2611 | { |
07d6d2b8 | 2612 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
6f9dbcd4 | 2613 | { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 | 2614 | { NULL, 0 , 0, 0, 0 } |
7f4d3958 L |
2615 | }; |
2616 | ||
b35d266b | 2617 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2618 | { |
0112cd26 | 2619 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2620 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
2621 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2622 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
0112cd26 NC |
2623 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, |
2624 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
07d6d2b8 AM |
2625 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, |
2626 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2627 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2628 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2629 | }; |
2630 | ||
b35d266b | 2631 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2632 | { |
07d6d2b8 AM |
2633 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2634 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2635 | }; |
2636 | ||
b35d266b | 2637 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2638 | { |
07d6d2b8 | 2639 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
6f9dbcd4 | 2640 | { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2641 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, |
2642 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2643 | }; |
2644 | ||
b35d266b | 2645 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2646 | { |
0112cd26 | 2647 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 | 2648 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2649 | }; |
2650 | ||
b35d266b | 2651 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2652 | { |
0112cd26 | 2653 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 AM |
2654 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, |
2655 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2656 | }; |
2657 | ||
b35d266b | 2658 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2659 | { |
6f9dbcd4 | 2660 | { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2661 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2662 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2663 | }; |
2664 | ||
b35d266b | 2665 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2666 | { |
0112cd26 NC |
2667 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2668 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
07d6d2b8 AM |
2669 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, |
2670 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2671 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2672 | }; |
2673 | ||
b35d266b | 2674 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2675 | { |
0112cd26 NC |
2676 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2677 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2678 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2679 | /* See struct bfd_elf_special_section declaration for the semantics of |
2680 | this special case where .prefix_length != strlen (.prefix). */ | |
2681 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
07d6d2b8 | 2682 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2683 | }; |
2684 | ||
b35d266b | 2685 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2686 | { |
07d6d2b8 AM |
2687 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2688 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
0112cd26 | 2689 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, |
07d6d2b8 | 2690 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2691 | }; |
2692 | ||
1b315056 CS |
2693 | static const struct bfd_elf_special_section special_sections_z[] = |
2694 | { | |
07d6d2b8 AM |
2695 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, |
2696 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
1b315056 CS |
2697 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, |
2698 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
07d6d2b8 | 2699 | { NULL, 0, 0, 0, 0 } |
1b315056 CS |
2700 | }; |
2701 | ||
e4c93b56 | 2702 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2703 | { |
7f4d3958 | 2704 | special_sections_b, /* 'b' */ |
98ece1b3 | 2705 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2706 | special_sections_d, /* 'd' */ |
2707 | NULL, /* 'e' */ | |
2708 | special_sections_f, /* 'f' */ | |
2709 | special_sections_g, /* 'g' */ | |
2710 | special_sections_h, /* 'h' */ | |
2711 | special_sections_i, /* 'i' */ | |
2712 | NULL, /* 'j' */ | |
2713 | NULL, /* 'k' */ | |
2714 | special_sections_l, /* 'l' */ | |
2715 | NULL, /* 'm' */ | |
2716 | special_sections_n, /* 'n' */ | |
2717 | NULL, /* 'o' */ | |
2718 | special_sections_p, /* 'p' */ | |
2719 | NULL, /* 'q' */ | |
2720 | special_sections_r, /* 'r' */ | |
2721 | special_sections_s, /* 's' */ | |
2722 | special_sections_t, /* 't' */ | |
1b315056 CS |
2723 | NULL, /* 'u' */ |
2724 | NULL, /* 'v' */ | |
2725 | NULL, /* 'w' */ | |
2726 | NULL, /* 'x' */ | |
2727 | NULL, /* 'y' */ | |
2728 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2729 | }; |
2730 | ||
551b43fd AM |
2731 | const struct bfd_elf_special_section * |
2732 | _bfd_elf_get_special_section (const char *name, | |
2733 | const struct bfd_elf_special_section *spec, | |
2734 | unsigned int rela) | |
2f89ff8d L |
2735 | { |
2736 | int i; | |
7f4d3958 | 2737 | int len; |
7f4d3958 | 2738 | |
551b43fd | 2739 | len = strlen (name); |
7f4d3958 | 2740 | |
551b43fd | 2741 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2742 | { |
2743 | int suffix_len; | |
551b43fd | 2744 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2745 | |
2746 | if (len < prefix_len) | |
2747 | continue; | |
551b43fd | 2748 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2749 | continue; |
2750 | ||
551b43fd | 2751 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2752 | if (suffix_len <= 0) |
2753 | { | |
2754 | if (name[prefix_len] != 0) | |
2755 | { | |
2756 | if (suffix_len == 0) | |
2757 | continue; | |
2758 | if (name[prefix_len] != '.' | |
2759 | && (suffix_len == -2 | |
551b43fd | 2760 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2761 | continue; |
2762 | } | |
2763 | } | |
2764 | else | |
2765 | { | |
2766 | if (len < prefix_len + suffix_len) | |
2767 | continue; | |
2768 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2769 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2770 | suffix_len) != 0) |
2771 | continue; | |
2772 | } | |
551b43fd | 2773 | return &spec[i]; |
7dcb9820 | 2774 | } |
2f89ff8d L |
2775 | |
2776 | return NULL; | |
2777 | } | |
2778 | ||
7dcb9820 | 2779 | const struct bfd_elf_special_section * |
29ef7005 | 2780 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2781 | { |
551b43fd AM |
2782 | int i; |
2783 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2784 | const struct elf_backend_data *bed; |
2f89ff8d L |
2785 | |
2786 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2787 | if (sec->name == NULL) |
2788 | return NULL; | |
2f89ff8d | 2789 | |
29ef7005 L |
2790 | bed = get_elf_backend_data (abfd); |
2791 | spec = bed->special_sections; | |
2792 | if (spec) | |
2793 | { | |
2794 | spec = _bfd_elf_get_special_section (sec->name, | |
2795 | bed->special_sections, | |
2796 | sec->use_rela_p); | |
2797 | if (spec != NULL) | |
2798 | return spec; | |
2799 | } | |
2800 | ||
551b43fd AM |
2801 | if (sec->name[0] != '.') |
2802 | return NULL; | |
2f89ff8d | 2803 | |
551b43fd | 2804 | i = sec->name[1] - 'b'; |
1b315056 | 2805 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2806 | return NULL; |
2807 | ||
2808 | spec = special_sections[i]; | |
2f89ff8d | 2809 | |
551b43fd AM |
2810 | if (spec == NULL) |
2811 | return NULL; | |
2812 | ||
2813 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2814 | } |
2815 | ||
b34976b6 | 2816 | bfd_boolean |
217aa764 | 2817 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2818 | { |
2819 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2820 | const struct elf_backend_data *bed; |
7dcb9820 | 2821 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2822 | |
f0abc2a1 AM |
2823 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2824 | if (sdata == NULL) | |
2825 | { | |
a50b1753 | 2826 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
07d6d2b8 | 2827 | sizeof (*sdata)); |
f0abc2a1 AM |
2828 | if (sdata == NULL) |
2829 | return FALSE; | |
217aa764 | 2830 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2831 | } |
bf572ba0 | 2832 | |
551b43fd AM |
2833 | /* Indicate whether or not this section should use RELA relocations. */ |
2834 | bed = get_elf_backend_data (abfd); | |
2835 | sec->use_rela_p = bed->default_use_rela_p; | |
2836 | ||
e843e0f8 L |
2837 | /* When we read a file, we don't need to set ELF section type and |
2838 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2839 | anyway. We will set ELF section type and flags for all linker | |
2840 | created sections. If user specifies BFD section flags, we will | |
2841 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2842 | elf_fake_sections. Special handling for .init_array/.fini_array |
2843 | output sections since they may contain .ctors/.dtors input | |
2844 | sections. We don't want _bfd_elf_init_private_section_data to | |
2845 | copy ELF section type from .ctors/.dtors input sections. */ | |
2846 | if (abfd->direction != read_direction | |
3496cb2a | 2847 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2848 | { |
551b43fd | 2849 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2850 | if (ssect != NULL |
2851 | && (!sec->flags | |
2852 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2853 | || ssect->type == SHT_INIT_ARRAY | |
2854 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2855 | { |
2856 | elf_section_type (sec) = ssect->type; | |
2857 | elf_section_flags (sec) = ssect->attr; | |
2858 | } | |
2f89ff8d L |
2859 | } |
2860 | ||
f592407e | 2861 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2862 | } |
2863 | ||
2864 | /* Create a new bfd section from an ELF program header. | |
2865 | ||
2866 | Since program segments have no names, we generate a synthetic name | |
2867 | of the form segment<NUM>, where NUM is generally the index in the | |
2868 | program header table. For segments that are split (see below) we | |
2869 | generate the names segment<NUM>a and segment<NUM>b. | |
2870 | ||
2871 | Note that some program segments may have a file size that is different than | |
2872 | (less than) the memory size. All this means is that at execution the | |
2873 | system must allocate the amount of memory specified by the memory size, | |
2874 | but only initialize it with the first "file size" bytes read from the | |
2875 | file. This would occur for example, with program segments consisting | |
2876 | of combined data+bss. | |
2877 | ||
2878 | To handle the above situation, this routine generates TWO bfd sections | |
2879 | for the single program segment. The first has the length specified by | |
2880 | the file size of the segment, and the second has the length specified | |
2881 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2882 | into its initialized and uninitialized parts. |
252b5132 RH |
2883 | |
2884 | */ | |
2885 | ||
b34976b6 | 2886 | bfd_boolean |
217aa764 AM |
2887 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2888 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2889 | int hdr_index, |
a50b1753 | 2890 | const char *type_name) |
252b5132 RH |
2891 | { |
2892 | asection *newsect; | |
2893 | char *name; | |
2894 | char namebuf[64]; | |
d4c88bbb | 2895 | size_t len; |
252b5132 RH |
2896 | int split; |
2897 | ||
2898 | split = ((hdr->p_memsz > 0) | |
2899 | && (hdr->p_filesz > 0) | |
2900 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2901 | |
2902 | if (hdr->p_filesz > 0) | |
252b5132 | 2903 | { |
91d6fa6a | 2904 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2905 | len = strlen (namebuf) + 1; |
a50b1753 | 2906 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2907 | if (!name) |
2908 | return FALSE; | |
2909 | memcpy (name, namebuf, len); | |
2910 | newsect = bfd_make_section (abfd, name); | |
2911 | if (newsect == NULL) | |
2912 | return FALSE; | |
2913 | newsect->vma = hdr->p_vaddr; | |
2914 | newsect->lma = hdr->p_paddr; | |
2915 | newsect->size = hdr->p_filesz; | |
2916 | newsect->filepos = hdr->p_offset; | |
2917 | newsect->flags |= SEC_HAS_CONTENTS; | |
2918 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2919 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2920 | { |
d5191d0c AM |
2921 | newsect->flags |= SEC_ALLOC; |
2922 | newsect->flags |= SEC_LOAD; | |
2923 | if (hdr->p_flags & PF_X) | |
2924 | { | |
2925 | /* FIXME: all we known is that it has execute PERMISSION, | |
2926 | may be data. */ | |
2927 | newsect->flags |= SEC_CODE; | |
2928 | } | |
2929 | } | |
2930 | if (!(hdr->p_flags & PF_W)) | |
2931 | { | |
2932 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2933 | } |
252b5132 RH |
2934 | } |
2935 | ||
d5191d0c | 2936 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2937 | { |
d5191d0c AM |
2938 | bfd_vma align; |
2939 | ||
91d6fa6a | 2940 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2941 | len = strlen (namebuf) + 1; |
a50b1753 | 2942 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2943 | if (!name) |
b34976b6 | 2944 | return FALSE; |
d4c88bbb | 2945 | memcpy (name, namebuf, len); |
252b5132 RH |
2946 | newsect = bfd_make_section (abfd, name); |
2947 | if (newsect == NULL) | |
b34976b6 | 2948 | return FALSE; |
252b5132 RH |
2949 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2950 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2951 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2952 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2953 | align = newsect->vma & -newsect->vma; | |
2954 | if (align == 0 || align > hdr->p_align) | |
2955 | align = hdr->p_align; | |
2956 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2957 | if (hdr->p_type == PT_LOAD) |
2958 | { | |
d5191d0c AM |
2959 | /* Hack for gdb. Segments that have not been modified do |
2960 | not have their contents written to a core file, on the | |
2961 | assumption that a debugger can find the contents in the | |
2962 | executable. We flag this case by setting the fake | |
2963 | section size to zero. Note that "real" bss sections will | |
2964 | always have their contents dumped to the core file. */ | |
2965 | if (bfd_get_format (abfd) == bfd_core) | |
2966 | newsect->size = 0; | |
252b5132 RH |
2967 | newsect->flags |= SEC_ALLOC; |
2968 | if (hdr->p_flags & PF_X) | |
2969 | newsect->flags |= SEC_CODE; | |
2970 | } | |
2971 | if (!(hdr->p_flags & PF_W)) | |
2972 | newsect->flags |= SEC_READONLY; | |
2973 | } | |
2974 | ||
b34976b6 | 2975 | return TRUE; |
252b5132 RH |
2976 | } |
2977 | ||
b34976b6 | 2978 | bfd_boolean |
91d6fa6a | 2979 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2980 | { |
9c5bfbb7 | 2981 | const struct elf_backend_data *bed; |
20cfcaae NC |
2982 | |
2983 | switch (hdr->p_type) | |
2984 | { | |
2985 | case PT_NULL: | |
91d6fa6a | 2986 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2987 | |
2988 | case PT_LOAD: | |
91d6fa6a | 2989 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2990 | |
2991 | case PT_DYNAMIC: | |
91d6fa6a | 2992 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2993 | |
2994 | case PT_INTERP: | |
91d6fa6a | 2995 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2996 | |
2997 | case PT_NOTE: | |
91d6fa6a | 2998 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2999 | return FALSE; |
276da9b3 L |
3000 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz, |
3001 | hdr->p_align)) | |
b34976b6 AM |
3002 | return FALSE; |
3003 | return TRUE; | |
20cfcaae NC |
3004 | |
3005 | case PT_SHLIB: | |
91d6fa6a | 3006 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
3007 | |
3008 | case PT_PHDR: | |
91d6fa6a | 3009 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 3010 | |
811072d8 | 3011 | case PT_GNU_EH_FRAME: |
91d6fa6a | 3012 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
3013 | "eh_frame_hdr"); |
3014 | ||
2b05f1b7 | 3015 | case PT_GNU_STACK: |
91d6fa6a | 3016 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 3017 | |
8c37241b | 3018 | case PT_GNU_RELRO: |
91d6fa6a | 3019 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 3020 | |
20cfcaae | 3021 | default: |
8c1acd09 | 3022 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 3023 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 3024 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
3025 | } |
3026 | } | |
3027 | ||
d4730f92 BS |
3028 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
3029 | REL or RELA. */ | |
3030 | ||
3031 | Elf_Internal_Shdr * | |
3032 | _bfd_elf_single_rel_hdr (asection *sec) | |
3033 | { | |
3034 | if (elf_section_data (sec)->rel.hdr) | |
3035 | { | |
3036 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
3037 | return elf_section_data (sec)->rel.hdr; | |
3038 | } | |
3039 | else | |
3040 | return elf_section_data (sec)->rela.hdr; | |
3041 | } | |
3042 | ||
3e19fb8f L |
3043 | static bfd_boolean |
3044 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
3045 | Elf_Internal_Shdr *rel_hdr, | |
3046 | const char *sec_name, | |
3047 | bfd_boolean use_rela_p) | |
3048 | { | |
3049 | char *name = (char *) bfd_alloc (abfd, | |
3050 | sizeof ".rela" + strlen (sec_name)); | |
3051 | if (name == NULL) | |
3052 | return FALSE; | |
3053 | ||
3054 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
3055 | rel_hdr->sh_name = | |
3056 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
3057 | FALSE); | |
3058 | if (rel_hdr->sh_name == (unsigned int) -1) | |
3059 | return FALSE; | |
3060 | ||
3061 | return TRUE; | |
3062 | } | |
3063 | ||
d4730f92 BS |
3064 | /* Allocate and initialize a section-header for a new reloc section, |
3065 | containing relocations against ASECT. It is stored in RELDATA. If | |
3066 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
3067 | relocations. */ | |
23bc299b | 3068 | |
5d13b3b3 | 3069 | static bfd_boolean |
217aa764 | 3070 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 3071 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 3072 | const char *sec_name, |
3e19fb8f L |
3073 | bfd_boolean use_rela_p, |
3074 | bfd_boolean delay_st_name_p) | |
23bc299b | 3075 | { |
d4730f92 | 3076 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 3077 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3078 | |
d4730f92 | 3079 | BFD_ASSERT (reldata->hdr == NULL); |
ef53be89 | 3080 | rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); |
d4730f92 | 3081 | reldata->hdr = rel_hdr; |
23bc299b | 3082 | |
3e19fb8f L |
3083 | if (delay_st_name_p) |
3084 | rel_hdr->sh_name = (unsigned int) -1; | |
3085 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
3086 | use_rela_p)) | |
b34976b6 | 3087 | return FALSE; |
23bc299b MM |
3088 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
3089 | rel_hdr->sh_entsize = (use_rela_p | |
3090 | ? bed->s->sizeof_rela | |
3091 | : bed->s->sizeof_rel); | |
72de5009 | 3092 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
28e07a05 | 3093 | rel_hdr->sh_flags = 0; |
23bc299b MM |
3094 | rel_hdr->sh_addr = 0; |
3095 | rel_hdr->sh_size = 0; | |
3096 | rel_hdr->sh_offset = 0; | |
3097 | ||
b34976b6 | 3098 | return TRUE; |
23bc299b MM |
3099 | } |
3100 | ||
94be91de JB |
3101 | /* Return the default section type based on the passed in section flags. */ |
3102 | ||
3103 | int | |
3104 | bfd_elf_get_default_section_type (flagword flags) | |
3105 | { | |
3106 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 3107 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
3108 | return SHT_NOBITS; |
3109 | return SHT_PROGBITS; | |
3110 | } | |
3111 | ||
d4730f92 BS |
3112 | struct fake_section_arg |
3113 | { | |
3114 | struct bfd_link_info *link_info; | |
3115 | bfd_boolean failed; | |
3116 | }; | |
3117 | ||
252b5132 RH |
3118 | /* Set up an ELF internal section header for a section. */ |
3119 | ||
252b5132 | 3120 | static void |
d4730f92 | 3121 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3122 | { |
d4730f92 | 3123 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3124 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3125 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3126 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3127 | unsigned int sh_type; |
0ce398f1 | 3128 | const char *name = asect->name; |
3e19fb8f | 3129 | bfd_boolean delay_st_name_p = FALSE; |
252b5132 | 3130 | |
d4730f92 | 3131 | if (arg->failed) |
252b5132 RH |
3132 | { |
3133 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3134 | loop. */ |
252b5132 RH |
3135 | return; |
3136 | } | |
3137 | ||
d4730f92 | 3138 | this_hdr = &esd->this_hdr; |
252b5132 | 3139 | |
f6fe1ccd | 3140 | if (arg->link_info) |
0ce398f1 | 3141 | { |
f6fe1ccd L |
3142 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3143 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3144 | && (asect->flags & SEC_DEBUGGING) | |
3145 | && name[1] == 'd' | |
3146 | && name[6] == '_') | |
3147 | { | |
3148 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3149 | compressed. */ | |
3150 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 3151 | |
dd905818 | 3152 | /* If this section will be compressed, delay adding section |
3e19fb8f L |
3153 | name to section name section after it is compressed in |
3154 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3155 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3156 | } |
3157 | } | |
3158 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3159 | { | |
3160 | /* objcopy: rename output DWARF debug section. */ | |
3161 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3162 | { | |
3163 | /* When we decompress or compress with SHF_COMPRESSED, | |
3164 | convert section name from .zdebug_* to .debug_* if | |
3165 | needed. */ | |
3166 | if (name[1] == 'z') | |
3167 | { | |
3168 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3169 | if (new_name == NULL) | |
3170 | { | |
3171 | arg->failed = TRUE; | |
3172 | return; | |
3173 | } | |
3174 | name = new_name; | |
3175 | } | |
3176 | } | |
3177 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3178 | { |
f6fe1ccd L |
3179 | /* PR binutils/18087: Compression does not always make a |
3180 | section smaller. So only rename the section when | |
3181 | compression has actually taken place. If input section | |
3182 | name is .zdebug_*, we should never compress it again. */ | |
3183 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3184 | if (new_name == NULL) |
3185 | { | |
3186 | arg->failed = TRUE; | |
3187 | return; | |
3188 | } | |
f6fe1ccd L |
3189 | BFD_ASSERT (name[1] != 'z'); |
3190 | name = new_name; | |
0ce398f1 L |
3191 | } |
3192 | } | |
3193 | ||
3e19fb8f L |
3194 | if (delay_st_name_p) |
3195 | this_hdr->sh_name = (unsigned int) -1; | |
3196 | else | |
252b5132 | 3197 | { |
3e19fb8f L |
3198 | this_hdr->sh_name |
3199 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3200 | name, FALSE); | |
3201 | if (this_hdr->sh_name == (unsigned int) -1) | |
3202 | { | |
3203 | arg->failed = TRUE; | |
3204 | return; | |
3205 | } | |
252b5132 RH |
3206 | } |
3207 | ||
a4d8e49b | 3208 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3209 | |
3210 | if ((asect->flags & SEC_ALLOC) != 0 | |
3211 | || asect->user_set_vma) | |
3212 | this_hdr->sh_addr = asect->vma; | |
3213 | else | |
3214 | this_hdr->sh_addr = 0; | |
3215 | ||
3216 | this_hdr->sh_offset = 0; | |
eea6121a | 3217 | this_hdr->sh_size = asect->size; |
252b5132 | 3218 | this_hdr->sh_link = 0; |
c86934ce NC |
3219 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3220 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3221 | { | |
4eca0228 | 3222 | _bfd_error_handler |
695344c0 | 3223 | /* xgettext:c-format */ |
871b3ab2 | 3224 | (_("%pB: error: Alignment power %d of section `%pA' is too big"), |
c08bb8dd | 3225 | abfd, asect->alignment_power, asect); |
c86934ce NC |
3226 | arg->failed = TRUE; |
3227 | return; | |
3228 | } | |
72de5009 | 3229 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
3230 | /* The sh_entsize and sh_info fields may have been set already by |
3231 | copy_private_section_data. */ | |
3232 | ||
3233 | this_hdr->bfd_section = asect; | |
3234 | this_hdr->contents = NULL; | |
3235 | ||
3cddba1e L |
3236 | /* If the section type is unspecified, we set it based on |
3237 | asect->flags. */ | |
98ece1b3 AM |
3238 | if ((asect->flags & SEC_GROUP) != 0) |
3239 | sh_type = SHT_GROUP; | |
98ece1b3 | 3240 | else |
94be91de | 3241 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3242 | |
3cddba1e | 3243 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3244 | this_hdr->sh_type = sh_type; |
3245 | else if (this_hdr->sh_type == SHT_NOBITS | |
3246 | && sh_type == SHT_PROGBITS | |
3247 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3248 | { |
98ece1b3 AM |
3249 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3250 | allow the link to proceed. This can happen when users link | |
3251 | non-bss input sections to bss output sections, or emit data | |
3252 | to a bss output section via a linker script. */ | |
4eca0228 | 3253 | _bfd_error_handler |
871b3ab2 | 3254 | (_("warning: section `%pA' type changed to PROGBITS"), asect); |
98ece1b3 | 3255 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3256 | } |
3257 | ||
2f89ff8d | 3258 | switch (this_hdr->sh_type) |
252b5132 | 3259 | { |
2f89ff8d | 3260 | default: |
2f89ff8d L |
3261 | break; |
3262 | ||
3263 | case SHT_STRTAB: | |
2f89ff8d L |
3264 | case SHT_NOTE: |
3265 | case SHT_NOBITS: | |
3266 | case SHT_PROGBITS: | |
3267 | break; | |
606851fb AM |
3268 | |
3269 | case SHT_INIT_ARRAY: | |
3270 | case SHT_FINI_ARRAY: | |
3271 | case SHT_PREINIT_ARRAY: | |
3272 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
3273 | break; | |
2f89ff8d L |
3274 | |
3275 | case SHT_HASH: | |
c7ac6ff8 | 3276 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3277 | break; |
5de3bf90 | 3278 | |
2f89ff8d | 3279 | case SHT_DYNSYM: |
252b5132 | 3280 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3281 | break; |
3282 | ||
3283 | case SHT_DYNAMIC: | |
252b5132 | 3284 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3285 | break; |
3286 | ||
3287 | case SHT_RELA: | |
3288 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3289 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3290 | break; | |
3291 | ||
3292 | case SHT_REL: | |
3293 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3294 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3295 | break; | |
3296 | ||
3297 | case SHT_GNU_versym: | |
252b5132 | 3298 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3299 | break; |
3300 | ||
3301 | case SHT_GNU_verdef: | |
252b5132 RH |
3302 | this_hdr->sh_entsize = 0; |
3303 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3304 | cverdefs. The linker will set cverdefs, but sh_info will be |
3305 | zero. */ | |
252b5132 RH |
3306 | if (this_hdr->sh_info == 0) |
3307 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3308 | else | |
3309 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3310 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3311 | break; |
3312 | ||
3313 | case SHT_GNU_verneed: | |
252b5132 RH |
3314 | this_hdr->sh_entsize = 0; |
3315 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3316 | cverrefs. The linker will set cverrefs, but sh_info will be |
3317 | zero. */ | |
252b5132 RH |
3318 | if (this_hdr->sh_info == 0) |
3319 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3320 | else | |
3321 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3322 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3323 | break; |
3324 | ||
3325 | case SHT_GROUP: | |
1783205a | 3326 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3327 | break; |
fdc90cb4 JJ |
3328 | |
3329 | case SHT_GNU_HASH: | |
3330 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3331 | break; | |
dbb410c3 | 3332 | } |
252b5132 RH |
3333 | |
3334 | if ((asect->flags & SEC_ALLOC) != 0) | |
3335 | this_hdr->sh_flags |= SHF_ALLOC; | |
3336 | if ((asect->flags & SEC_READONLY) == 0) | |
3337 | this_hdr->sh_flags |= SHF_WRITE; | |
3338 | if ((asect->flags & SEC_CODE) != 0) | |
3339 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3340 | if ((asect->flags & SEC_MERGE) != 0) |
3341 | { | |
3342 | this_hdr->sh_flags |= SHF_MERGE; | |
3343 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3344 | } |
84865015 NC |
3345 | if ((asect->flags & SEC_STRINGS) != 0) |
3346 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3347 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3348 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3349 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3350 | { |
3351 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3352 | if (asect->size == 0 |
3353 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3354 | { |
3a800eb9 | 3355 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3356 | |
704afa60 | 3357 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3358 | if (o != NULL) |
3359 | { | |
704afa60 | 3360 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3361 | if (this_hdr->sh_size != 0) |
3362 | this_hdr->sh_type = SHT_NOBITS; | |
3363 | } | |
704afa60 JJ |
3364 | } |
3365 | } | |
18ae9cc1 L |
3366 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3367 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3368 | |
d4730f92 BS |
3369 | /* If the section has relocs, set up a section header for the |
3370 | SHT_REL[A] section. If two relocation sections are required for | |
3371 | this section, it is up to the processor-specific back-end to | |
3372 | create the other. */ | |
3373 | if ((asect->flags & SEC_RELOC) != 0) | |
3374 | { | |
3375 | /* When doing a relocatable link, create both REL and RELA sections if | |
3376 | needed. */ | |
3377 | if (arg->link_info | |
3378 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3379 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3380 | && (bfd_link_relocatable (arg->link_info) |
3381 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3382 | { |
3383 | if (esd->rel.count && esd->rel.hdr == NULL | |
28e07a05 | 3384 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, |
db4677b8 | 3385 | FALSE, delay_st_name_p)) |
d4730f92 BS |
3386 | { |
3387 | arg->failed = TRUE; | |
3388 | return; | |
3389 | } | |
3390 | if (esd->rela.count && esd->rela.hdr == NULL | |
28e07a05 | 3391 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, |
db4677b8 | 3392 | TRUE, delay_st_name_p)) |
d4730f92 BS |
3393 | { |
3394 | arg->failed = TRUE; | |
3395 | return; | |
3396 | } | |
3397 | } | |
3398 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3399 | (asect->use_rela_p | |
3400 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3401 | name, |
3e19fb8f L |
3402 | asect->use_rela_p, |
3403 | delay_st_name_p)) | |
db4677b8 | 3404 | { |
d4730f92 | 3405 | arg->failed = TRUE; |
db4677b8 AM |
3406 | return; |
3407 | } | |
d4730f92 BS |
3408 | } |
3409 | ||
252b5132 | 3410 | /* Check for processor-specific section types. */ |
0414f35b | 3411 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3412 | if (bed->elf_backend_fake_sections |
3413 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
db4677b8 AM |
3414 | { |
3415 | arg->failed = TRUE; | |
3416 | return; | |
3417 | } | |
252b5132 | 3418 | |
42bb2e33 | 3419 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3420 | { |
3421 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3422 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3423 | this_hdr->sh_type = sh_type; |
3424 | } | |
252b5132 RH |
3425 | } |
3426 | ||
bcacc0f5 AM |
3427 | /* Fill in the contents of a SHT_GROUP section. Called from |
3428 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3429 | when ELF targets use the generic linker, ld. Called for ld -r | |
3430 | from bfd_elf_final_link. */ | |
dbb410c3 | 3431 | |
1126897b | 3432 | void |
217aa764 | 3433 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3434 | { |
a50b1753 | 3435 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3436 | asection *elt, *first; |
dbb410c3 | 3437 | unsigned char *loc; |
b34976b6 | 3438 | bfd_boolean gas; |
dbb410c3 | 3439 | |
7e4111ad L |
3440 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3441 | elfxx-ia64.c. */ | |
3442 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
3443 | || *failedptr) |
3444 | return; | |
3445 | ||
bcacc0f5 AM |
3446 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3447 | { | |
3448 | unsigned long symindx = 0; | |
3449 | ||
3450 | /* elf_group_id will have been set up by objcopy and the | |
3451 | generic linker. */ | |
3452 | if (elf_group_id (sec) != NULL) | |
3453 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3454 | |
bcacc0f5 AM |
3455 | if (symindx == 0) |
3456 | { | |
3457 | /* If called from the assembler, swap_out_syms will have set up | |
3458 | elf_section_syms. */ | |
3459 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3460 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3461 | } | |
3462 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3463 | } | |
3464 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3465 | { |
bcacc0f5 AM |
3466 | /* The ELF backend linker sets sh_info to -2 when the group |
3467 | signature symbol is global, and thus the index can't be | |
3468 | set until all local symbols are output. */ | |
53720c49 AM |
3469 | asection *igroup; |
3470 | struct bfd_elf_section_data *sec_data; | |
3471 | unsigned long symndx; | |
3472 | unsigned long extsymoff; | |
bcacc0f5 AM |
3473 | struct elf_link_hash_entry *h; |
3474 | ||
53720c49 AM |
3475 | /* The point of this little dance to the first SHF_GROUP section |
3476 | then back to the SHT_GROUP section is that this gets us to | |
3477 | the SHT_GROUP in the input object. */ | |
3478 | igroup = elf_sec_group (elf_next_in_group (sec)); | |
3479 | sec_data = elf_section_data (igroup); | |
3480 | symndx = sec_data->this_hdr.sh_info; | |
3481 | extsymoff = 0; | |
bcacc0f5 AM |
3482 | if (!elf_bad_symtab (igroup->owner)) |
3483 | { | |
3484 | Elf_Internal_Shdr *symtab_hdr; | |
3485 | ||
3486 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3487 | extsymoff = symtab_hdr->sh_info; | |
3488 | } | |
3489 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3490 | while (h->root.type == bfd_link_hash_indirect | |
3491 | || h->root.type == bfd_link_hash_warning) | |
3492 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3493 | ||
3494 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3495 | } |
dbb410c3 | 3496 | |
1126897b | 3497 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3498 | gas = TRUE; |
dbb410c3 AM |
3499 | if (sec->contents == NULL) |
3500 | { | |
b34976b6 | 3501 | gas = FALSE; |
a50b1753 | 3502 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3503 | |
3504 | /* Arrange for the section to be written out. */ | |
3505 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3506 | if (sec->contents == NULL) |
3507 | { | |
b34976b6 | 3508 | *failedptr = TRUE; |
dbb410c3 AM |
3509 | return; |
3510 | } | |
3511 | } | |
3512 | ||
eea6121a | 3513 | loc = sec->contents + sec->size; |
dbb410c3 | 3514 | |
9dce4196 AM |
3515 | /* Get the pointer to the first section in the group that gas |
3516 | squirreled away here. objcopy arranges for this to be set to the | |
3517 | start of the input section group. */ | |
3518 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3519 | |
3520 | /* First element is a flag word. Rest of section is elf section | |
3521 | indices for all the sections of the group. Write them backwards | |
3522 | just to keep the group in the same order as given in .section | |
3523 | directives, not that it matters. */ | |
3524 | while (elt != NULL) | |
3525 | { | |
9dce4196 | 3526 | asection *s; |
9dce4196 | 3527 | |
9dce4196 | 3528 | s = elt; |
415f38a6 AM |
3529 | if (!gas) |
3530 | s = s->output_section; | |
3531 | if (s != NULL | |
3532 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3533 | { |
db4677b8 | 3534 | struct bfd_elf_section_data *elf_sec = elf_section_data (s); |
28e07a05 AM |
3535 | struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt); |
3536 | ||
3537 | if (elf_sec->rel.hdr != NULL | |
3538 | && (gas | |
3539 | || (input_elf_sec->rel.hdr != NULL | |
3540 | && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)) | |
db4677b8 | 3541 | { |
28e07a05 | 3542 | elf_sec->rel.hdr->sh_flags |= SHF_GROUP; |
db4677b8 AM |
3543 | loc -= 4; |
3544 | H_PUT_32 (abfd, elf_sec->rel.idx, loc); | |
3545 | } | |
28e07a05 AM |
3546 | if (elf_sec->rela.hdr != NULL |
3547 | && (gas | |
3548 | || (input_elf_sec->rela.hdr != NULL | |
3549 | && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)) | |
db4677b8 | 3550 | { |
28e07a05 | 3551 | elf_sec->rela.hdr->sh_flags |= SHF_GROUP; |
db4677b8 AM |
3552 | loc -= 4; |
3553 | H_PUT_32 (abfd, elf_sec->rela.idx, loc); | |
3554 | } | |
01e1a5bc | 3555 | loc -= 4; |
db4677b8 | 3556 | H_PUT_32 (abfd, elf_sec->this_idx, loc); |
01e1a5bc | 3557 | } |
945906ff | 3558 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3559 | if (elt == first) |
3560 | break; | |
dbb410c3 AM |
3561 | } |
3562 | ||
7bdf4127 AB |
3563 | loc -= 4; |
3564 | BFD_ASSERT (loc == sec->contents); | |
dbb410c3 | 3565 | |
9dce4196 | 3566 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3567 | } |
3568 | ||
bce964aa AM |
3569 | /* Given NAME, the name of a relocation section stripped of its |
3570 | .rel/.rela prefix, return the section in ABFD to which the | |
3571 | relocations apply. */ | |
bd53a53a L |
3572 | |
3573 | asection * | |
bce964aa AM |
3574 | _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) |
3575 | { | |
3576 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3577 | section likely apply to .got.plt or .got section. */ | |
3578 | if (get_elf_backend_data (abfd)->want_got_plt | |
3579 | && strcmp (name, ".plt") == 0) | |
3580 | { | |
3581 | asection *sec; | |
3582 | ||
3583 | name = ".got.plt"; | |
3584 | sec = bfd_get_section_by_name (abfd, name); | |
3585 | if (sec != NULL) | |
3586 | return sec; | |
3587 | name = ".got"; | |
3588 | } | |
3589 | ||
3590 | return bfd_get_section_by_name (abfd, name); | |
3591 | } | |
3592 | ||
3593 | /* Return the section to which RELOC_SEC applies. */ | |
3594 | ||
3595 | static asection * | |
3596 | elf_get_reloc_section (asection *reloc_sec) | |
bd53a53a L |
3597 | { |
3598 | const char *name; | |
3599 | unsigned int type; | |
3600 | bfd *abfd; | |
bce964aa | 3601 | const struct elf_backend_data *bed; |
bd53a53a L |
3602 | |
3603 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3604 | if (type != SHT_REL && type != SHT_RELA) | |
3605 | return NULL; | |
3606 | ||
3607 | /* We look up the section the relocs apply to by name. */ | |
3608 | name = reloc_sec->name; | |
bce964aa AM |
3609 | if (strncmp (name, ".rel", 4) != 0) |
3610 | return NULL; | |
3611 | name += 4; | |
3612 | if (type == SHT_RELA && *name++ != 'a') | |
3613 | return NULL; | |
bd53a53a | 3614 | |
bd53a53a | 3615 | abfd = reloc_sec->owner; |
bce964aa AM |
3616 | bed = get_elf_backend_data (abfd); |
3617 | return bed->get_reloc_section (abfd, name); | |
bd53a53a L |
3618 | } |
3619 | ||
252b5132 RH |
3620 | /* Assign all ELF section numbers. The dummy first section is handled here |
3621 | too. The link/info pointers for the standard section types are filled | |
3622 | in here too, while we're at it. */ | |
3623 | ||
b34976b6 | 3624 | static bfd_boolean |
da9f89d4 | 3625 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3626 | { |
3627 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3628 | asection *sec; | |
3e19fb8f | 3629 | unsigned int section_number; |
252b5132 | 3630 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3631 | struct bfd_elf_section_data *d; |
3516e984 | 3632 | bfd_boolean need_symtab; |
252b5132 RH |
3633 | |
3634 | section_number = 1; | |
3635 | ||
2b0f7ef9 JJ |
3636 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3637 | ||
da9f89d4 | 3638 | /* SHT_GROUP sections are in relocatable files only. */ |
7bdf4127 | 3639 | if (link_info == NULL || !link_info->resolve_section_groups) |
252b5132 | 3640 | { |
ef53be89 | 3641 | size_t reloc_count = 0; |
14f2c699 | 3642 | |
da9f89d4 | 3643 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3644 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3645 | { |
5daa8fe7 | 3646 | d = elf_section_data (sec); |
da9f89d4 L |
3647 | |
3648 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3649 | { |
5daa8fe7 | 3650 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3651 | { |
3652 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3653 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3654 | abfd->section_count--; |
da9f89d4 | 3655 | } |
08a40648 | 3656 | else |
4fbb74a6 | 3657 | d->this_idx = section_number++; |
da9f89d4 | 3658 | } |
14f2c699 L |
3659 | |
3660 | /* Count relocations. */ | |
3661 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3662 | } |
14f2c699 L |
3663 | |
3664 | /* Clear HAS_RELOC if there are no relocations. */ | |
3665 | if (reloc_count == 0) | |
3666 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3667 | } |
3668 | ||
3669 | for (sec = abfd->sections; sec; sec = sec->next) | |
3670 | { | |
3671 | d = elf_section_data (sec); | |
3672 | ||
3673 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3674 | d->this_idx = section_number++; |
3e19fb8f L |
3675 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3676 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3677 | if (d->rel.hdr) |
2b0f7ef9 | 3678 | { |
d4730f92 | 3679 | d->rel.idx = section_number++; |
3e19fb8f L |
3680 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3681 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3682 | } |
d4730f92 BS |
3683 | else |
3684 | d->rel.idx = 0; | |
23bc299b | 3685 | |
d4730f92 | 3686 | if (d->rela.hdr) |
2b0f7ef9 | 3687 | { |
d4730f92 | 3688 | d->rela.idx = section_number++; |
3e19fb8f L |
3689 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3690 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3691 | } |
23bc299b | 3692 | else |
d4730f92 | 3693 | d->rela.idx = 0; |
252b5132 RH |
3694 | } |
3695 | ||
3516e984 L |
3696 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3697 | || (link_info == NULL | |
3698 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3699 | == HAS_RELOC))); | |
3700 | if (need_symtab) | |
252b5132 | 3701 | { |
12bd6957 | 3702 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3703 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3704 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3705 | { |
6a40cf0c NC |
3706 | elf_section_list * entry; |
3707 | ||
3708 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3709 | ||
3710 | entry = bfd_zalloc (abfd, sizeof * entry); | |
3711 | entry->ndx = section_number++; | |
3712 | elf_symtab_shndx_list (abfd) = entry; | |
3713 | entry->hdr.sh_name | |
9ad5cbcf | 3714 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3715 | ".symtab_shndx", FALSE); |
6a40cf0c | 3716 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3717 | return FALSE; |
9ad5cbcf | 3718 | } |
12bd6957 | 3719 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3720 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3721 | } |
3722 | ||
dd905818 NC |
3723 | elf_shstrtab_sec (abfd) = section_number++; |
3724 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
3725 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); | |
3726 | ||
1c52a645 L |
3727 | if (section_number >= SHN_LORESERVE) |
3728 | { | |
695344c0 | 3729 | /* xgettext:c-format */ |
871b3ab2 | 3730 | _bfd_error_handler (_("%pB: too many sections: %u"), |
1c52a645 L |
3731 | abfd, section_number); |
3732 | return FALSE; | |
3733 | } | |
3734 | ||
9ad5cbcf | 3735 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3736 | elf_elfheader (abfd)->e_shnum = section_number; |
3737 | ||
3738 | /* Set up the list of section header pointers, in agreement with the | |
3739 | indices. */ | |
a50b1753 | 3740 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
07d6d2b8 | 3741 | sizeof (Elf_Internal_Shdr *)); |
252b5132 | 3742 | if (i_shdrp == NULL) |
b34976b6 | 3743 | return FALSE; |
252b5132 | 3744 | |
a50b1753 | 3745 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
07d6d2b8 | 3746 | sizeof (Elf_Internal_Shdr)); |
252b5132 RH |
3747 | if (i_shdrp[0] == NULL) |
3748 | { | |
3749 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3750 | return FALSE; |
252b5132 | 3751 | } |
252b5132 RH |
3752 | |
3753 | elf_elfsections (abfd) = i_shdrp; | |
3754 | ||
12bd6957 | 3755 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3756 | if (need_symtab) |
252b5132 | 3757 | { |
12bd6957 | 3758 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3759 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3760 | { |
6a40cf0c NC |
3761 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3762 | BFD_ASSERT (entry != NULL); | |
3763 | i_shdrp[entry->ndx] = & entry->hdr; | |
3764 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3765 | } |
12bd6957 AM |
3766 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3767 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3768 | } |
38ce5b11 | 3769 | |
252b5132 RH |
3770 | for (sec = abfd->sections; sec; sec = sec->next) |
3771 | { | |
252b5132 | 3772 | asection *s; |
252b5132 | 3773 | |
91d6fa6a NC |
3774 | d = elf_section_data (sec); |
3775 | ||
252b5132 | 3776 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3777 | if (d->rel.idx != 0) |
3778 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3779 | if (d->rela.idx != 0) | |
3780 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3781 | |
3782 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3783 | ||
3784 | /* sh_link of a reloc section is the section index of the symbol | |
3785 | table. sh_info is the section index of the section to which | |
3786 | the relocation entries apply. */ | |
d4730f92 | 3787 | if (d->rel.idx != 0) |
252b5132 | 3788 | { |
12bd6957 | 3789 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3790 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3791 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3792 | } |
d4730f92 | 3793 | if (d->rela.idx != 0) |
23bc299b | 3794 | { |
12bd6957 | 3795 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3796 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3797 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3798 | } |
252b5132 | 3799 | |
38ce5b11 L |
3800 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3801 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3802 | { | |
3803 | s = elf_linked_to_section (sec); | |
3804 | if (s) | |
38ce5b11 | 3805 | { |
f2876037 | 3806 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3807 | if (link_info != NULL) |
38ce5b11 | 3808 | { |
f2876037 | 3809 | /* Check discarded linkonce section. */ |
dbaa2011 | 3810 | if (discarded_section (s)) |
38ce5b11 | 3811 | { |
ccd2ec6a | 3812 | asection *kept; |
4eca0228 | 3813 | _bfd_error_handler |
695344c0 | 3814 | /* xgettext:c-format */ |
871b3ab2 AM |
3815 | (_("%pB: sh_link of section `%pA' points to" |
3816 | " discarded section `%pA' of `%pB'"), | |
ccd2ec6a L |
3817 | abfd, d->this_hdr.bfd_section, |
3818 | s, s->owner); | |
3819 | /* Point to the kept section if it has the same | |
3820 | size as the discarded one. */ | |
c0f00686 | 3821 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3822 | if (kept == NULL) |
185d09ad | 3823 | { |
ccd2ec6a L |
3824 | bfd_set_error (bfd_error_bad_value); |
3825 | return FALSE; | |
185d09ad | 3826 | } |
ccd2ec6a | 3827 | s = kept; |
38ce5b11 | 3828 | } |
e424ecc8 | 3829 | |
ccd2ec6a L |
3830 | s = s->output_section; |
3831 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3832 | } |
f2876037 L |
3833 | else |
3834 | { | |
3835 | /* Handle objcopy. */ | |
3836 | if (s->output_section == NULL) | |
3837 | { | |
4eca0228 | 3838 | _bfd_error_handler |
695344c0 | 3839 | /* xgettext:c-format */ |
871b3ab2 AM |
3840 | (_("%pB: sh_link of section `%pA' points to" |
3841 | " removed section `%pA' of `%pB'"), | |
f2876037 L |
3842 | abfd, d->this_hdr.bfd_section, s, s->owner); |
3843 | bfd_set_error (bfd_error_bad_value); | |
3844 | return FALSE; | |
3845 | } | |
3846 | s = s->output_section; | |
3847 | } | |
ccd2ec6a L |
3848 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3849 | } | |
3850 | else | |
3851 | { | |
3852 | /* PR 290: | |
3853 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3854 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3855 | sh_info fields. Hence we could get the situation | |
08a40648 | 3856 | where s is NULL. */ |
ccd2ec6a L |
3857 | const struct elf_backend_data *bed |
3858 | = get_elf_backend_data (abfd); | |
3859 | if (bed->link_order_error_handler) | |
3860 | bed->link_order_error_handler | |
695344c0 | 3861 | /* xgettext:c-format */ |
871b3ab2 | 3862 | (_("%pB: warning: sh_link not set for section `%pA'"), |
ccd2ec6a | 3863 | abfd, sec); |
38ce5b11 L |
3864 | } |
3865 | } | |
3866 | ||
252b5132 RH |
3867 | switch (d->this_hdr.sh_type) |
3868 | { | |
3869 | case SHT_REL: | |
3870 | case SHT_RELA: | |
3871 | /* A reloc section which we are treating as a normal BFD | |
3872 | section. sh_link is the section index of the symbol | |
3873 | table. sh_info is the section index of the section to | |
3874 | which the relocation entries apply. We assume that an | |
3875 | allocated reloc section uses the dynamic symbol table. | |
3876 | FIXME: How can we be sure? */ | |
3877 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3878 | if (s != NULL) | |
3879 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3880 | ||
bce964aa | 3881 | s = elf_get_reloc_section (sec); |
252b5132 | 3882 | if (s != NULL) |
9ef5d938 L |
3883 | { |
3884 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3885 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3886 | } | |
252b5132 RH |
3887 | break; |
3888 | ||
3889 | case SHT_STRTAB: | |
3890 | /* We assume that a section named .stab*str is a stabs | |
3891 | string section. We look for a section with the same name | |
3892 | but without the trailing ``str'', and set its sh_link | |
3893 | field to point to this section. */ | |
0112cd26 | 3894 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3895 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3896 | { | |
3897 | size_t len; | |
3898 | char *alc; | |
3899 | ||
3900 | len = strlen (sec->name); | |
a50b1753 | 3901 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3902 | if (alc == NULL) |
b34976b6 | 3903 | return FALSE; |
d4c88bbb | 3904 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3905 | alc[len - 3] = '\0'; |
3906 | s = bfd_get_section_by_name (abfd, alc); | |
3907 | free (alc); | |
3908 | if (s != NULL) | |
3909 | { | |
3910 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3911 | ||
3912 | /* This is a .stab section. */ | |
0594c12d AM |
3913 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3914 | elf_section_data (s)->this_hdr.sh_entsize | |
3915 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3916 | } |
3917 | } | |
3918 | break; | |
3919 | ||
3920 | case SHT_DYNAMIC: | |
3921 | case SHT_DYNSYM: | |
3922 | case SHT_GNU_verneed: | |
3923 | case SHT_GNU_verdef: | |
3924 | /* sh_link is the section header index of the string table | |
3925 | used for the dynamic entries, or the symbol table, or the | |
3926 | version strings. */ | |
3927 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3928 | if (s != NULL) | |
3929 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3930 | break; | |
3931 | ||
7f1204bb JJ |
3932 | case SHT_GNU_LIBLIST: |
3933 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3934 | list used for the dynamic entries, or the symbol table, or |
3935 | the version strings. */ | |
7f1204bb JJ |
3936 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3937 | ? ".dynstr" : ".gnu.libstr"); | |
3938 | if (s != NULL) | |
3939 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3940 | break; | |
3941 | ||
252b5132 | 3942 | case SHT_HASH: |
fdc90cb4 | 3943 | case SHT_GNU_HASH: |
252b5132 RH |
3944 | case SHT_GNU_versym: |
3945 | /* sh_link is the section header index of the symbol table | |
3946 | this hash table or version table is for. */ | |
3947 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3948 | if (s != NULL) | |
3949 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3950 | break; | |
dbb410c3 AM |
3951 | |
3952 | case SHT_GROUP: | |
12bd6957 | 3953 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3954 | } |
3955 | } | |
3956 | ||
3e19fb8f L |
3957 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
3958 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
3959 | debug section name from .debug_* to .zdebug_* if needed. */ | |
3960 | ||
b34976b6 | 3961 | return TRUE; |
252b5132 RH |
3962 | } |
3963 | ||
5372391b | 3964 | static bfd_boolean |
217aa764 | 3965 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3966 | { |
3967 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3968 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3969 | if (bed->elf_backend_sym_is_global) |
3970 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3971 | |
e47bf690 | 3972 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3973 | || bfd_is_und_section (bfd_get_section (sym)) |
3974 | || bfd_is_com_section (bfd_get_section (sym))); | |
3975 | } | |
3976 | ||
76359541 TP |
3977 | /* Filter global symbols of ABFD to include in the import library. All |
3978 | SYMCOUNT symbols of ABFD can be examined from their pointers in | |
3979 | SYMS. Pointers of symbols to keep should be stored contiguously at | |
3980 | the beginning of that array. | |
3981 | ||
3982 | Returns the number of symbols to keep. */ | |
3983 | ||
3984 | unsigned int | |
3985 | _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, | |
3986 | asymbol **syms, long symcount) | |
3987 | { | |
3988 | long src_count, dst_count = 0; | |
3989 | ||
3990 | for (src_count = 0; src_count < symcount; src_count++) | |
3991 | { | |
3992 | asymbol *sym = syms[src_count]; | |
3993 | char *name = (char *) bfd_asymbol_name (sym); | |
3994 | struct bfd_link_hash_entry *h; | |
3995 | ||
3996 | if (!sym_is_global (abfd, sym)) | |
3997 | continue; | |
3998 | ||
3999 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE); | |
5df1bc57 AM |
4000 | if (h == NULL) |
4001 | continue; | |
76359541 TP |
4002 | if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) |
4003 | continue; | |
76359541 TP |
4004 | if (h->linker_def || h->ldscript_def) |
4005 | continue; | |
4006 | ||
4007 | syms[dst_count++] = sym; | |
4008 | } | |
4009 | ||
4010 | syms[dst_count] = NULL; | |
4011 | ||
4012 | return dst_count; | |
4013 | } | |
4014 | ||
5372391b | 4015 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 4016 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
4017 | |
4018 | static bfd_boolean | |
4019 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
4020 | { | |
c6d8cab4 L |
4021 | elf_symbol_type *type_ptr; |
4022 | ||
4023 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
4024 | return FALSE; | |
4025 | ||
4026 | type_ptr = elf_symbol_from (abfd, sym); | |
4027 | return ((type_ptr != NULL | |
4028 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
4029 | && bfd_is_abs_section (sym->section)) | |
4030 | || !(sym->section->owner == abfd | |
0f0a5e58 | 4031 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
4032 | && sym->section->output_offset == 0) |
4033 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
4034 | } |
4035 | ||
2633a79c AM |
4036 | /* Map symbol from it's internal number to the external number, moving |
4037 | all local symbols to be at the head of the list. */ | |
4038 | ||
b34976b6 | 4039 | static bfd_boolean |
12bd6957 | 4040 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 4041 | { |
dc810e39 | 4042 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
4043 | asymbol **syms = bfd_get_outsymbols (abfd); |
4044 | asymbol **sect_syms; | |
dc810e39 AM |
4045 | unsigned int num_locals = 0; |
4046 | unsigned int num_globals = 0; | |
4047 | unsigned int num_locals2 = 0; | |
4048 | unsigned int num_globals2 = 0; | |
7292b3ac | 4049 | unsigned int max_index = 0; |
dc810e39 | 4050 | unsigned int idx; |
252b5132 RH |
4051 | asection *asect; |
4052 | asymbol **new_syms; | |
252b5132 RH |
4053 | |
4054 | #ifdef DEBUG | |
4055 | fprintf (stderr, "elf_map_symbols\n"); | |
4056 | fflush (stderr); | |
4057 | #endif | |
4058 | ||
252b5132 RH |
4059 | for (asect = abfd->sections; asect; asect = asect->next) |
4060 | { | |
4061 | if (max_index < asect->index) | |
4062 | max_index = asect->index; | |
4063 | } | |
4064 | ||
4065 | max_index++; | |
a50b1753 | 4066 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 4067 | if (sect_syms == NULL) |
b34976b6 | 4068 | return FALSE; |
252b5132 | 4069 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 4070 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 4071 | |
079e9a2f AM |
4072 | /* Init sect_syms entries for any section symbols we have already |
4073 | decided to output. */ | |
252b5132 RH |
4074 | for (idx = 0; idx < symcount; idx++) |
4075 | { | |
dc810e39 | 4076 | asymbol *sym = syms[idx]; |
c044fabd | 4077 | |
252b5132 | 4078 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 4079 | && sym->value == 0 |
2633a79c AM |
4080 | && !ignore_section_sym (abfd, sym) |
4081 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 4082 | { |
5372391b | 4083 | asection *sec = sym->section; |
252b5132 | 4084 | |
5372391b AM |
4085 | if (sec->owner != abfd) |
4086 | sec = sec->output_section; | |
252b5132 | 4087 | |
5372391b | 4088 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
4089 | } |
4090 | } | |
4091 | ||
252b5132 RH |
4092 | /* Classify all of the symbols. */ |
4093 | for (idx = 0; idx < symcount; idx++) | |
4094 | { | |
2633a79c | 4095 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 4096 | num_globals++; |
2633a79c AM |
4097 | else if (!ignore_section_sym (abfd, syms[idx])) |
4098 | num_locals++; | |
252b5132 | 4099 | } |
079e9a2f | 4100 | |
5372391b | 4101 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
4102 | sections will already have a section symbol in outsymbols, but |
4103 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
4104 | at least in that case. */ | |
252b5132 RH |
4105 | for (asect = abfd->sections; asect; asect = asect->next) |
4106 | { | |
079e9a2f | 4107 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4108 | { |
079e9a2f | 4109 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
4110 | num_locals++; |
4111 | else | |
4112 | num_globals++; | |
252b5132 RH |
4113 | } |
4114 | } | |
4115 | ||
4116 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 | 4117 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
07d6d2b8 | 4118 | sizeof (asymbol *)); |
dc810e39 | 4119 | |
252b5132 | 4120 | if (new_syms == NULL) |
b34976b6 | 4121 | return FALSE; |
252b5132 RH |
4122 | |
4123 | for (idx = 0; idx < symcount; idx++) | |
4124 | { | |
4125 | asymbol *sym = syms[idx]; | |
dc810e39 | 4126 | unsigned int i; |
252b5132 | 4127 | |
2633a79c AM |
4128 | if (sym_is_global (abfd, sym)) |
4129 | i = num_locals + num_globals2++; | |
4130 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
4131 | i = num_locals2++; |
4132 | else | |
2633a79c | 4133 | continue; |
252b5132 RH |
4134 | new_syms[i] = sym; |
4135 | sym->udata.i = i + 1; | |
4136 | } | |
4137 | for (asect = abfd->sections; asect; asect = asect->next) | |
4138 | { | |
079e9a2f | 4139 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4140 | { |
079e9a2f | 4141 | asymbol *sym = asect->symbol; |
dc810e39 | 4142 | unsigned int i; |
252b5132 | 4143 | |
079e9a2f | 4144 | sect_syms[asect->index] = sym; |
252b5132 RH |
4145 | if (!sym_is_global (abfd, sym)) |
4146 | i = num_locals2++; | |
4147 | else | |
4148 | i = num_locals + num_globals2++; | |
4149 | new_syms[i] = sym; | |
4150 | sym->udata.i = i + 1; | |
4151 | } | |
4152 | } | |
4153 | ||
4154 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
4155 | ||
12bd6957 | 4156 | *pnum_locals = num_locals; |
b34976b6 | 4157 | return TRUE; |
252b5132 RH |
4158 | } |
4159 | ||
4160 | /* Align to the maximum file alignment that could be required for any | |
4161 | ELF data structure. */ | |
4162 | ||
268b6b39 | 4163 | static inline file_ptr |
217aa764 | 4164 | align_file_position (file_ptr off, int align) |
252b5132 RH |
4165 | { |
4166 | return (off + align - 1) & ~(align - 1); | |
4167 | } | |
4168 | ||
4169 | /* Assign a file position to a section, optionally aligning to the | |
4170 | required section alignment. */ | |
4171 | ||
217aa764 AM |
4172 | file_ptr |
4173 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
4174 | file_ptr offset, | |
4175 | bfd_boolean align) | |
252b5132 | 4176 | { |
72de5009 AM |
4177 | if (align && i_shdrp->sh_addralign > 1) |
4178 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
4179 | i_shdrp->sh_offset = offset; |
4180 | if (i_shdrp->bfd_section != NULL) | |
4181 | i_shdrp->bfd_section->filepos = offset; | |
4182 | if (i_shdrp->sh_type != SHT_NOBITS) | |
4183 | offset += i_shdrp->sh_size; | |
4184 | return offset; | |
4185 | } | |
4186 | ||
4187 | /* Compute the file positions we are going to put the sections at, and | |
4188 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
4189 | is not NULL, this is being called by the ELF backend linker. */ | |
4190 | ||
b34976b6 | 4191 | bfd_boolean |
217aa764 AM |
4192 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
4193 | struct bfd_link_info *link_info) | |
252b5132 | 4194 | { |
9c5bfbb7 | 4195 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 4196 | struct fake_section_arg fsargs; |
b34976b6 | 4197 | bfd_boolean failed; |
ef10c3ac | 4198 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 4199 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 4200 | bfd_boolean need_symtab; |
252b5132 RH |
4201 | |
4202 | if (abfd->output_has_begun) | |
b34976b6 | 4203 | return TRUE; |
252b5132 RH |
4204 | |
4205 | /* Do any elf backend specific processing first. */ | |
4206 | if (bed->elf_backend_begin_write_processing) | |
4207 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
4208 | ||
4209 | if (! prep_headers (abfd)) | |
b34976b6 | 4210 | return FALSE; |
252b5132 | 4211 | |
e6c51ed4 | 4212 | /* Post process the headers if necessary. */ |
78245035 | 4213 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 4214 | |
d4730f92 BS |
4215 | fsargs.failed = FALSE; |
4216 | fsargs.link_info = link_info; | |
4217 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4218 | if (fsargs.failed) | |
b34976b6 | 4219 | return FALSE; |
252b5132 | 4220 | |
da9f89d4 | 4221 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4222 | return FALSE; |
252b5132 RH |
4223 | |
4224 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4225 | need_symtab = (link_info == NULL |
4226 | && (bfd_get_symcount (abfd) > 0 | |
4227 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4228 | == HAS_RELOC))); | |
4229 | if (need_symtab) | |
252b5132 RH |
4230 | { |
4231 | /* Non-zero if doing a relocatable link. */ | |
4232 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4233 | ||
4234 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4235 | return FALSE; |
252b5132 RH |
4236 | } |
4237 | ||
d4730f92 | 4238 | failed = FALSE; |
1126897b | 4239 | if (link_info == NULL) |
dbb410c3 | 4240 | { |
1126897b | 4241 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4242 | if (failed) |
b34976b6 | 4243 | return FALSE; |
dbb410c3 AM |
4244 | } |
4245 | ||
252b5132 RH |
4246 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
4247 | /* sh_name was set in prep_headers. */ | |
4248 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 4249 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4250 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4251 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4252 | shstrtab_hdr->sh_entsize = 0; |
4253 | shstrtab_hdr->sh_link = 0; | |
4254 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4255 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4256 | shstrtab_hdr->sh_addralign = 1; |
4257 | ||
c84fca4d | 4258 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4259 | return FALSE; |
252b5132 | 4260 | |
3516e984 | 4261 | if (need_symtab) |
252b5132 RH |
4262 | { |
4263 | file_ptr off; | |
4264 | Elf_Internal_Shdr *hdr; | |
4265 | ||
12bd6957 | 4266 | off = elf_next_file_pos (abfd); |
252b5132 | 4267 | |
6a40cf0c | 4268 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4269 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4270 | |
6a40cf0c NC |
4271 | if (elf_symtab_shndx_list (abfd) != NULL) |
4272 | { | |
4273 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4274 | if (hdr->sh_size != 0) | |
4275 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4276 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4277 | } | |
9ad5cbcf | 4278 | |
252b5132 | 4279 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4280 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4281 | |
12bd6957 | 4282 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4283 | |
4284 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4285 | out. */ |
252b5132 | 4286 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4287 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4288 | return FALSE; |
ef10c3ac | 4289 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4290 | } |
4291 | ||
b34976b6 | 4292 | abfd->output_has_begun = TRUE; |
252b5132 | 4293 | |
b34976b6 | 4294 | return TRUE; |
252b5132 RH |
4295 | } |
4296 | ||
8ded5a0f AM |
4297 | /* Make an initial estimate of the size of the program header. If we |
4298 | get the number wrong here, we'll redo section placement. */ | |
4299 | ||
4300 | static bfd_size_type | |
4301 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4302 | { | |
4303 | size_t segs; | |
4304 | asection *s; | |
2b05f1b7 | 4305 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4306 | |
4307 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4308 | and one for data. */ | |
4309 | segs = 2; | |
4310 | ||
4311 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4312 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4313 | { | |
4314 | /* If we have a loadable interpreter section, we need a | |
4315 | PT_INTERP segment. In this case, assume we also need a | |
4316 | PT_PHDR segment, although that may not be true for all | |
4317 | targets. */ | |
e9a38e0f | 4318 | segs += 2; |
8ded5a0f AM |
4319 | } |
4320 | ||
4321 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4322 | { | |
4323 | /* We need a PT_DYNAMIC segment. */ | |
4324 | ++segs; | |
f210dcff | 4325 | } |
08a40648 | 4326 | |
ceae84aa | 4327 | if (info != NULL && info->relro) |
f210dcff L |
4328 | { |
4329 | /* We need a PT_GNU_RELRO segment. */ | |
4330 | ++segs; | |
8ded5a0f AM |
4331 | } |
4332 | ||
12bd6957 | 4333 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4334 | { |
4335 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4336 | ++segs; | |
4337 | } | |
4338 | ||
12bd6957 | 4339 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4340 | { |
2b05f1b7 L |
4341 | /* We need a PT_GNU_STACK segment. */ |
4342 | ++segs; | |
4343 | } | |
94b11780 | 4344 | |
2b05f1b7 L |
4345 | for (s = abfd->sections; s != NULL; s = s->next) |
4346 | { | |
8ded5a0f | 4347 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 4348 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
4349 | { |
4350 | /* We need a PT_NOTE segment. */ | |
4351 | ++segs; | |
1c5265b5 JJ |
4352 | /* Try to create just one PT_NOTE segment |
4353 | for all adjacent loadable .note* sections. | |
4354 | gABI requires that within a PT_NOTE segment | |
4355 | (and also inside of each SHT_NOTE section) | |
4356 | each note is padded to a multiple of 4 size, | |
4357 | so we check whether the sections are correctly | |
4358 | aligned. */ | |
4359 | if (s->alignment_power == 2) | |
4360 | while (s->next != NULL | |
4361 | && s->next->alignment_power == 2 | |
4362 | && (s->next->flags & SEC_LOAD) != 0 | |
4363 | && CONST_STRNEQ (s->next->name, ".note")) | |
4364 | s = s->next; | |
8ded5a0f AM |
4365 | } |
4366 | } | |
4367 | ||
4368 | for (s = abfd->sections; s != NULL; s = s->next) | |
4369 | { | |
4370 | if (s->flags & SEC_THREAD_LOCAL) | |
4371 | { | |
4372 | /* We need a PT_TLS segment. */ | |
4373 | ++segs; | |
4374 | break; | |
4375 | } | |
4376 | } | |
4377 | ||
2b05f1b7 | 4378 | bed = get_elf_backend_data (abfd); |
a91e1603 L |
4379 | |
4380 | if ((abfd->flags & D_PAGED) != 0) | |
4381 | { | |
4382 | /* Add a PT_GNU_MBIND segment for each mbind section. */ | |
4383 | unsigned int page_align_power = bfd_log2 (bed->commonpagesize); | |
4384 | for (s = abfd->sections; s != NULL; s = s->next) | |
4385 | if (elf_section_flags (s) & SHF_GNU_MBIND) | |
4386 | { | |
4387 | if (elf_section_data (s)->this_hdr.sh_info | |
4388 | > PT_GNU_MBIND_NUM) | |
4389 | { | |
4390 | _bfd_error_handler | |
4391 | /* xgettext:c-format */ | |
871b3ab2 | 4392 | (_("%pB: GNU_MBIN section `%pA' has invalid sh_info field: %d"), |
a91e1603 L |
4393 | abfd, s, elf_section_data (s)->this_hdr.sh_info); |
4394 | continue; | |
4395 | } | |
4396 | /* Align mbind section to page size. */ | |
4397 | if (s->alignment_power < page_align_power) | |
4398 | s->alignment_power = page_align_power; | |
4399 | segs ++; | |
4400 | } | |
4401 | } | |
4402 | ||
4403 | /* Let the backend count up any program headers it might need. */ | |
4404 | if (bed->elf_backend_additional_program_headers) | |
8ded5a0f AM |
4405 | { |
4406 | int a; | |
4407 | ||
4408 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4409 | if (a == -1) | |
4410 | abort (); | |
4411 | segs += a; | |
4412 | } | |
4413 | ||
4414 | return segs * bed->s->sizeof_phdr; | |
4415 | } | |
4416 | ||
2ea37f1c NC |
4417 | /* Find the segment that contains the output_section of section. */ |
4418 | ||
4419 | Elf_Internal_Phdr * | |
4420 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4421 | { | |
4422 | struct elf_segment_map *m; | |
4423 | Elf_Internal_Phdr *p; | |
4424 | ||
12bd6957 | 4425 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4426 | m != NULL; |
4427 | m = m->next, p++) | |
4428 | { | |
4429 | int i; | |
4430 | ||
4431 | for (i = m->count - 1; i >= 0; i--) | |
4432 | if (m->sections[i] == section) | |
4433 | return p; | |
4434 | } | |
4435 | ||
4436 | return NULL; | |
4437 | } | |
4438 | ||
252b5132 RH |
4439 | /* Create a mapping from a set of sections to a program segment. */ |
4440 | ||
217aa764 AM |
4441 | static struct elf_segment_map * |
4442 | make_mapping (bfd *abfd, | |
4443 | asection **sections, | |
4444 | unsigned int from, | |
4445 | unsigned int to, | |
4446 | bfd_boolean phdr) | |
252b5132 RH |
4447 | { |
4448 | struct elf_segment_map *m; | |
4449 | unsigned int i; | |
4450 | asection **hdrpp; | |
dc810e39 | 4451 | bfd_size_type amt; |
252b5132 | 4452 | |
dc810e39 AM |
4453 | amt = sizeof (struct elf_segment_map); |
4454 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 4455 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4456 | if (m == NULL) |
4457 | return NULL; | |
4458 | m->next = NULL; | |
4459 | m->p_type = PT_LOAD; | |
4460 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4461 | m->sections[i - from] = *hdrpp; | |
4462 | m->count = to - from; | |
4463 | ||
4464 | if (from == 0 && phdr) | |
4465 | { | |
4466 | /* Include the headers in the first PT_LOAD segment. */ | |
4467 | m->includes_filehdr = 1; | |
4468 | m->includes_phdrs = 1; | |
4469 | } | |
4470 | ||
4471 | return m; | |
4472 | } | |
4473 | ||
229fcec5 MM |
4474 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4475 | on failure. */ | |
4476 | ||
4477 | struct elf_segment_map * | |
4478 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4479 | { | |
4480 | struct elf_segment_map *m; | |
4481 | ||
a50b1753 | 4482 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
07d6d2b8 | 4483 | sizeof (struct elf_segment_map)); |
229fcec5 MM |
4484 | if (m == NULL) |
4485 | return NULL; | |
4486 | m->next = NULL; | |
4487 | m->p_type = PT_DYNAMIC; | |
4488 | m->count = 1; | |
4489 | m->sections[0] = dynsec; | |
08a40648 | 4490 | |
229fcec5 MM |
4491 | return m; |
4492 | } | |
4493 | ||
8ded5a0f | 4494 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4495 | |
b34976b6 | 4496 | static bfd_boolean |
3dea8fca AM |
4497 | elf_modify_segment_map (bfd *abfd, |
4498 | struct bfd_link_info *info, | |
4499 | bfd_boolean remove_empty_load) | |
252b5132 | 4500 | { |
252e386e | 4501 | struct elf_segment_map **m; |
8ded5a0f | 4502 | const struct elf_backend_data *bed; |
252b5132 | 4503 | |
8ded5a0f AM |
4504 | /* The placement algorithm assumes that non allocated sections are |
4505 | not in PT_LOAD segments. We ensure this here by removing such | |
4506 | sections from the segment map. We also remove excluded | |
252e386e AM |
4507 | sections. Finally, any PT_LOAD segment without sections is |
4508 | removed. */ | |
12bd6957 | 4509 | m = &elf_seg_map (abfd); |
252e386e | 4510 | while (*m) |
8ded5a0f AM |
4511 | { |
4512 | unsigned int i, new_count; | |
252b5132 | 4513 | |
252e386e | 4514 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4515 | { |
252e386e AM |
4516 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4517 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4518 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4519 | { |
252e386e AM |
4520 | (*m)->sections[new_count] = (*m)->sections[i]; |
4521 | new_count++; | |
8ded5a0f AM |
4522 | } |
4523 | } | |
252e386e | 4524 | (*m)->count = new_count; |
252b5132 | 4525 | |
1a9ccd70 NC |
4526 | if (remove_empty_load |
4527 | && (*m)->p_type == PT_LOAD | |
4528 | && (*m)->count == 0 | |
4529 | && !(*m)->includes_phdrs) | |
252e386e AM |
4530 | *m = (*m)->next; |
4531 | else | |
4532 | m = &(*m)->next; | |
8ded5a0f | 4533 | } |
252b5132 | 4534 | |
8ded5a0f AM |
4535 | bed = get_elf_backend_data (abfd); |
4536 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4537 | { |
252e386e | 4538 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4539 | return FALSE; |
252b5132 | 4540 | } |
252b5132 | 4541 | |
8ded5a0f AM |
4542 | return TRUE; |
4543 | } | |
252b5132 | 4544 | |
dbc88fc1 AM |
4545 | #define IS_TBSS(s) \ |
4546 | ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL) | |
4547 | ||
8ded5a0f | 4548 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4549 | |
8ded5a0f AM |
4550 | bfd_boolean |
4551 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4552 | { | |
4553 | unsigned int count; | |
4554 | struct elf_segment_map *m; | |
4555 | asection **sections = NULL; | |
4556 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4557 | bfd_boolean no_user_phdrs; |
252b5132 | 4558 | |
12bd6957 | 4559 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4560 | |
4561 | if (info != NULL) | |
4562 | info->user_phdrs = !no_user_phdrs; | |
4563 | ||
3dea8fca | 4564 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4565 | { |
8ded5a0f AM |
4566 | asection *s; |
4567 | unsigned int i; | |
4568 | struct elf_segment_map *mfirst; | |
4569 | struct elf_segment_map **pm; | |
4570 | asection *last_hdr; | |
4571 | bfd_vma last_size; | |
4572 | unsigned int phdr_index; | |
4573 | bfd_vma maxpagesize; | |
4574 | asection **hdrpp; | |
4575 | bfd_boolean phdr_in_segment = TRUE; | |
4576 | bfd_boolean writable; | |
2888249f | 4577 | bfd_boolean executable; |
8ded5a0f AM |
4578 | int tls_count = 0; |
4579 | asection *first_tls = NULL; | |
a91e1603 | 4580 | asection *first_mbind = NULL; |
8ded5a0f AM |
4581 | asection *dynsec, *eh_frame_hdr; |
4582 | bfd_size_type amt; | |
8d06853e | 4583 | bfd_vma addr_mask, wrap_to = 0; |
1a9ccd70 | 4584 | bfd_boolean linker_created_pt_phdr_segment = FALSE; |
252b5132 | 4585 | |
8ded5a0f | 4586 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4587 | |
a50b1753 | 4588 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
07d6d2b8 | 4589 | sizeof (asection *)); |
8ded5a0f | 4590 | if (sections == NULL) |
252b5132 | 4591 | goto error_return; |
252b5132 | 4592 | |
8d06853e AM |
4593 | /* Calculate top address, avoiding undefined behaviour of shift |
4594 | left operator when shift count is equal to size of type | |
4595 | being shifted. */ | |
4596 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4597 | addr_mask = (addr_mask << 1) + 1; | |
4598 | ||
8ded5a0f AM |
4599 | i = 0; |
4600 | for (s = abfd->sections; s != NULL; s = s->next) | |
4601 | { | |
4602 | if ((s->flags & SEC_ALLOC) != 0) | |
4603 | { | |
4604 | sections[i] = s; | |
4605 | ++i; | |
8d06853e AM |
4606 | /* A wrapping section potentially clashes with header. */ |
4607 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4608 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4609 | } |
4610 | } | |
4611 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4612 | count = i; | |
252b5132 | 4613 | |
8ded5a0f | 4614 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4615 | |
8ded5a0f | 4616 | /* Build the mapping. */ |
252b5132 | 4617 | |
8ded5a0f AM |
4618 | mfirst = NULL; |
4619 | pm = &mfirst; | |
252b5132 | 4620 | |
8ded5a0f AM |
4621 | /* If we have a .interp section, then create a PT_PHDR segment for |
4622 | the program headers and a PT_INTERP segment for the .interp | |
4623 | section. */ | |
4624 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4625 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4626 | { | |
4627 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4628 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4629 | if (m == NULL) |
4630 | goto error_return; | |
4631 | m->next = NULL; | |
4632 | m->p_type = PT_PHDR; | |
f882209d | 4633 | m->p_flags = PF_R; |
8ded5a0f AM |
4634 | m->p_flags_valid = 1; |
4635 | m->includes_phdrs = 1; | |
1a9ccd70 | 4636 | linker_created_pt_phdr_segment = TRUE; |
8ded5a0f AM |
4637 | *pm = m; |
4638 | pm = &m->next; | |
252b5132 | 4639 | |
8ded5a0f | 4640 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4641 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4642 | if (m == NULL) |
4643 | goto error_return; | |
4644 | m->next = NULL; | |
4645 | m->p_type = PT_INTERP; | |
4646 | m->count = 1; | |
4647 | m->sections[0] = s; | |
4648 | ||
4649 | *pm = m; | |
4650 | pm = &m->next; | |
252b5132 | 4651 | } |
8ded5a0f AM |
4652 | |
4653 | /* Look through the sections. We put sections in the same program | |
4654 | segment when the start of the second section can be placed within | |
4655 | a few bytes of the end of the first section. */ | |
4656 | last_hdr = NULL; | |
4657 | last_size = 0; | |
4658 | phdr_index = 0; | |
4659 | maxpagesize = bed->maxpagesize; | |
063bb025 NC |
4660 | /* PR 17512: file: c8455299. |
4661 | Avoid divide-by-zero errors later on. | |
4662 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4663 | if (maxpagesize == 0) | |
4664 | maxpagesize = 1; | |
8ded5a0f | 4665 | writable = FALSE; |
2888249f | 4666 | executable = FALSE; |
8ded5a0f AM |
4667 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); |
4668 | if (dynsec != NULL | |
4669 | && (dynsec->flags & SEC_LOAD) == 0) | |
4670 | dynsec = NULL; | |
4671 | ||
4672 | /* Deal with -Ttext or something similar such that the first section | |
4673 | is not adjacent to the program headers. This is an | |
4674 | approximation, since at this point we don't know exactly how many | |
4675 | program headers we will need. */ | |
4676 | if (count > 0) | |
252b5132 | 4677 | { |
12bd6957 | 4678 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4679 | |
62d7a5f6 | 4680 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4681 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4682 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4683 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4684 | || (sections[0]->lma & addr_mask) < phdr_size |
4685 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4686 | < phdr_size % maxpagesize) | |
4687 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
1a9ccd70 NC |
4688 | { |
4689 | /* PR 20815: The ELF standard says that a PT_PHDR segment, if | |
4690 | present, must be included as part of the memory image of the | |
4691 | program. Ie it must be part of a PT_LOAD segment as well. | |
4692 | If we have had to create our own PT_PHDR segment, but it is | |
4693 | not going to be covered by the first PT_LOAD segment, then | |
4694 | force the inclusion if we can... */ | |
4695 | if ((abfd->flags & D_PAGED) != 0 | |
4696 | && linker_created_pt_phdr_segment) | |
4697 | phdr_in_segment = TRUE; | |
4698 | else | |
4699 | phdr_in_segment = FALSE; | |
4700 | } | |
252b5132 RH |
4701 | } |
4702 | ||
8ded5a0f | 4703 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4704 | { |
8ded5a0f AM |
4705 | asection *hdr; |
4706 | bfd_boolean new_segment; | |
4707 | ||
4708 | hdr = *hdrpp; | |
4709 | ||
4710 | /* See if this section and the last one will fit in the same | |
4711 | segment. */ | |
4712 | ||
4713 | if (last_hdr == NULL) | |
4714 | { | |
4715 | /* If we don't have a segment yet, then we don't need a new | |
4716 | one (we build the last one after this loop). */ | |
4717 | new_segment = FALSE; | |
4718 | } | |
4719 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4720 | { | |
4721 | /* If this section has a different relation between the | |
4722 | virtual address and the load address, then we need a new | |
4723 | segment. */ | |
4724 | new_segment = TRUE; | |
4725 | } | |
b5599592 AM |
4726 | else if (hdr->lma < last_hdr->lma + last_size |
4727 | || last_hdr->lma + last_size < last_hdr->lma) | |
4728 | { | |
4729 | /* If this section has a load address that makes it overlap | |
4730 | the previous section, then we need a new segment. */ | |
4731 | new_segment = TRUE; | |
4732 | } | |
76cb3a89 AM |
4733 | else if ((abfd->flags & D_PAGED) != 0 |
4734 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4735 | == (hdr->lma & -maxpagesize))) | |
4736 | { | |
4737 | /* If we are demand paged then we can't map two disk | |
4738 | pages onto the same memory page. */ | |
4739 | new_segment = FALSE; | |
4740 | } | |
39948a60 NC |
4741 | /* In the next test we have to be careful when last_hdr->lma is close |
4742 | to the end of the address space. If the aligned address wraps | |
4743 | around to the start of the address space, then there are no more | |
4744 | pages left in memory and it is OK to assume that the current | |
4745 | section can be included in the current segment. */ | |
76cb3a89 AM |
4746 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) |
4747 | + maxpagesize > last_hdr->lma) | |
4748 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) | |
4749 | + maxpagesize <= hdr->lma)) | |
8ded5a0f AM |
4750 | { |
4751 | /* If putting this section in this segment would force us to | |
4752 | skip a page in the segment, then we need a new segment. */ | |
4753 | new_segment = TRUE; | |
4754 | } | |
4755 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
76cb3a89 | 4756 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) |
8ded5a0f | 4757 | { |
e5654c0f AM |
4758 | /* We don't want to put a loaded section after a |
4759 | nonloaded (ie. bss style) section in the same segment | |
4760 | as that will force the non-loaded section to be loaded. | |
76cb3a89 | 4761 | Consider .tbss sections as loaded for this purpose. */ |
8ded5a0f AM |
4762 | new_segment = TRUE; |
4763 | } | |
4764 | else if ((abfd->flags & D_PAGED) == 0) | |
4765 | { | |
4766 | /* If the file is not demand paged, which means that we | |
4767 | don't require the sections to be correctly aligned in the | |
4768 | file, then there is no other reason for a new segment. */ | |
4769 | new_segment = FALSE; | |
4770 | } | |
2888249f L |
4771 | else if (info != NULL |
4772 | && info->separate_code | |
4773 | && executable != ((hdr->flags & SEC_CODE) != 0)) | |
4774 | { | |
4775 | new_segment = TRUE; | |
4776 | } | |
8ded5a0f | 4777 | else if (! writable |
76cb3a89 | 4778 | && (hdr->flags & SEC_READONLY) == 0) |
8ded5a0f AM |
4779 | { |
4780 | /* We don't want to put a writable section in a read only | |
76cb3a89 | 4781 | segment. */ |
8ded5a0f AM |
4782 | new_segment = TRUE; |
4783 | } | |
4784 | else | |
4785 | { | |
4786 | /* Otherwise, we can use the same segment. */ | |
4787 | new_segment = FALSE; | |
4788 | } | |
4789 | ||
2889e75b | 4790 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4791 | if (last_hdr != NULL |
4792 | && info != NULL | |
4793 | && info->callbacks->override_segment_assignment != NULL) | |
4794 | new_segment | |
4795 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4796 | last_hdr, | |
4797 | new_segment); | |
2889e75b | 4798 | |
8ded5a0f AM |
4799 | if (! new_segment) |
4800 | { | |
4801 | if ((hdr->flags & SEC_READONLY) == 0) | |
4802 | writable = TRUE; | |
2888249f L |
4803 | if ((hdr->flags & SEC_CODE) != 0) |
4804 | executable = TRUE; | |
8ded5a0f AM |
4805 | last_hdr = hdr; |
4806 | /* .tbss sections effectively have zero size. */ | |
dbc88fc1 | 4807 | last_size = !IS_TBSS (hdr) ? hdr->size : 0; |
8ded5a0f AM |
4808 | continue; |
4809 | } | |
4810 | ||
4811 | /* We need a new program segment. We must create a new program | |
4812 | header holding all the sections from phdr_index until hdr. */ | |
4813 | ||
4814 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4815 | if (m == NULL) | |
4816 | goto error_return; | |
4817 | ||
4818 | *pm = m; | |
4819 | pm = &m->next; | |
4820 | ||
252b5132 | 4821 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4822 | writable = TRUE; |
8ded5a0f AM |
4823 | else |
4824 | writable = FALSE; | |
4825 | ||
2888249f L |
4826 | if ((hdr->flags & SEC_CODE) == 0) |
4827 | executable = FALSE; | |
4828 | else | |
4829 | executable = TRUE; | |
4830 | ||
baaff79e JJ |
4831 | last_hdr = hdr; |
4832 | /* .tbss sections effectively have zero size. */ | |
dbc88fc1 | 4833 | last_size = !IS_TBSS (hdr) ? hdr->size : 0; |
8ded5a0f AM |
4834 | phdr_index = i; |
4835 | phdr_in_segment = FALSE; | |
252b5132 RH |
4836 | } |
4837 | ||
86b2281f AM |
4838 | /* Create a final PT_LOAD program segment, but not if it's just |
4839 | for .tbss. */ | |
4840 | if (last_hdr != NULL | |
4841 | && (i - phdr_index != 1 | |
dbc88fc1 | 4842 | || !IS_TBSS (last_hdr))) |
8ded5a0f AM |
4843 | { |
4844 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4845 | if (m == NULL) | |
4846 | goto error_return; | |
252b5132 | 4847 | |
8ded5a0f AM |
4848 | *pm = m; |
4849 | pm = &m->next; | |
4850 | } | |
252b5132 | 4851 | |
8ded5a0f AM |
4852 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4853 | if (dynsec != NULL) | |
4854 | { | |
4855 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4856 | if (m == NULL) | |
4857 | goto error_return; | |
4858 | *pm = m; | |
4859 | pm = &m->next; | |
4860 | } | |
252b5132 | 4861 | |
1c5265b5 JJ |
4862 | /* For each batch of consecutive loadable .note sections, |
4863 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4864 | because if we link together nonloadable .note sections and | |
4865 | loadable .note sections, we will generate two .note sections | |
4866 | in the output file. FIXME: Using names for section types is | |
4867 | bogus anyhow. */ | |
8ded5a0f AM |
4868 | for (s = abfd->sections; s != NULL; s = s->next) |
4869 | { | |
4870 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4871 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4872 | { |
1c5265b5 | 4873 | asection *s2; |
91d6fa6a NC |
4874 | |
4875 | count = 1; | |
8ded5a0f | 4876 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4877 | if (s->alignment_power == 2) |
4878 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4879 | { |
4880 | if (s2->next->alignment_power == 2 | |
4881 | && (s2->next->flags & SEC_LOAD) != 0 | |
4882 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4883 | && align_power (s2->lma + s2->size, 2) |
4884 | == s2->next->lma) | |
55b581a6 JJ |
4885 | count++; |
4886 | else | |
4887 | break; | |
4888 | } | |
1c5265b5 | 4889 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4890 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4891 | if (m == NULL) |
4892 | goto error_return; | |
4893 | m->next = NULL; | |
4894 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4895 | m->count = count; |
4896 | while (count > 1) | |
4897 | { | |
4898 | m->sections[m->count - count--] = s; | |
4899 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4900 | s = s->next; | |
4901 | } | |
4902 | m->sections[m->count - 1] = s; | |
4903 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4904 | *pm = m; |
4905 | pm = &m->next; | |
4906 | } | |
4907 | if (s->flags & SEC_THREAD_LOCAL) | |
4908 | { | |
4909 | if (! tls_count) | |
4910 | first_tls = s; | |
4911 | tls_count++; | |
4912 | } | |
a91e1603 L |
4913 | if (first_mbind == NULL |
4914 | && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) | |
4915 | first_mbind = s; | |
8ded5a0f | 4916 | } |
252b5132 | 4917 | |
8ded5a0f AM |
4918 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4919 | if (tls_count > 0) | |
4920 | { | |
8ded5a0f AM |
4921 | amt = sizeof (struct elf_segment_map); |
4922 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4923 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4924 | if (m == NULL) |
4925 | goto error_return; | |
4926 | m->next = NULL; | |
4927 | m->p_type = PT_TLS; | |
4928 | m->count = tls_count; | |
4929 | /* Mandated PF_R. */ | |
4930 | m->p_flags = PF_R; | |
4931 | m->p_flags_valid = 1; | |
d923cae0 | 4932 | s = first_tls; |
91d6fa6a | 4933 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4934 | { |
d923cae0 L |
4935 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4936 | { | |
4937 | _bfd_error_handler | |
871b3ab2 | 4938 | (_("%pB: TLS sections are not adjacent:"), abfd); |
d923cae0 L |
4939 | s = first_tls; |
4940 | i = 0; | |
4941 | while (i < (unsigned int) tls_count) | |
4942 | { | |
4943 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4944 | { | |
871b3ab2 | 4945 | _bfd_error_handler (_(" TLS: %pA"), s); |
d923cae0 L |
4946 | i++; |
4947 | } | |
4948 | else | |
871b3ab2 | 4949 | _bfd_error_handler (_(" non-TLS: %pA"), s); |
d923cae0 L |
4950 | s = s->next; |
4951 | } | |
4952 | bfd_set_error (bfd_error_bad_value); | |
4953 | goto error_return; | |
4954 | } | |
4955 | m->sections[i] = s; | |
4956 | s = s->next; | |
8ded5a0f | 4957 | } |
252b5132 | 4958 | |
8ded5a0f AM |
4959 | *pm = m; |
4960 | pm = &m->next; | |
4961 | } | |
252b5132 | 4962 | |
a91e1603 L |
4963 | if (first_mbind && (abfd->flags & D_PAGED) != 0) |
4964 | for (s = first_mbind; s != NULL; s = s->next) | |
4965 | if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 | |
4966 | && (elf_section_data (s)->this_hdr.sh_info | |
4967 | <= PT_GNU_MBIND_NUM)) | |
4968 | { | |
4969 | /* Mandated PF_R. */ | |
4970 | unsigned long p_flags = PF_R; | |
4971 | if ((s->flags & SEC_READONLY) == 0) | |
4972 | p_flags |= PF_W; | |
4973 | if ((s->flags & SEC_CODE) != 0) | |
4974 | p_flags |= PF_X; | |
4975 | ||
4976 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
4977 | m = bfd_zalloc (abfd, amt); | |
4978 | if (m == NULL) | |
4979 | goto error_return; | |
4980 | m->next = NULL; | |
4981 | m->p_type = (PT_GNU_MBIND_LO | |
4982 | + elf_section_data (s)->this_hdr.sh_info); | |
4983 | m->count = 1; | |
4984 | m->p_flags_valid = 1; | |
4985 | m->sections[0] = s; | |
4986 | m->p_flags = p_flags; | |
4987 | ||
4988 | *pm = m; | |
4989 | pm = &m->next; | |
4990 | } | |
4991 | ||
8ded5a0f AM |
4992 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4993 | segment. */ | |
12bd6957 | 4994 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4995 | if (eh_frame_hdr != NULL |
4996 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4997 | { |
dc810e39 | 4998 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4999 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
5000 | if (m == NULL) |
5001 | goto error_return; | |
5002 | m->next = NULL; | |
8ded5a0f | 5003 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 5004 | m->count = 1; |
8ded5a0f | 5005 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
5006 | |
5007 | *pm = m; | |
5008 | pm = &m->next; | |
5009 | } | |
13ae64f3 | 5010 | |
12bd6957 | 5011 | if (elf_stack_flags (abfd)) |
13ae64f3 | 5012 | { |
8ded5a0f | 5013 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 5014 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5015 | if (m == NULL) |
5016 | goto error_return; | |
5017 | m->next = NULL; | |
2b05f1b7 | 5018 | m->p_type = PT_GNU_STACK; |
12bd6957 | 5019 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 5020 | m->p_align = bed->stack_align; |
8ded5a0f | 5021 | m->p_flags_valid = 1; |
04c3a755 NS |
5022 | m->p_align_valid = m->p_align != 0; |
5023 | if (info->stacksize > 0) | |
5024 | { | |
5025 | m->p_size = info->stacksize; | |
5026 | m->p_size_valid = 1; | |
5027 | } | |
252b5132 | 5028 | |
8ded5a0f AM |
5029 | *pm = m; |
5030 | pm = &m->next; | |
5031 | } | |
65765700 | 5032 | |
ceae84aa | 5033 | if (info != NULL && info->relro) |
8ded5a0f | 5034 | { |
f210dcff L |
5035 | for (m = mfirst; m != NULL; m = m->next) |
5036 | { | |
3832a4d8 AM |
5037 | if (m->p_type == PT_LOAD |
5038 | && m->count != 0 | |
5039 | && m->sections[0]->vma >= info->relro_start | |
5040 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 5041 | { |
3832a4d8 AM |
5042 | i = m->count; |
5043 | while (--i != (unsigned) -1) | |
5044 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5045 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5046 | break; | |
5047 | ||
43a8475c | 5048 | if (i != (unsigned) -1) |
f210dcff L |
5049 | break; |
5050 | } | |
be01b344 | 5051 | } |
f210dcff L |
5052 | |
5053 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
5054 | if (m != NULL) | |
5055 | { | |
5056 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 5057 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
5058 | if (m == NULL) |
5059 | goto error_return; | |
5060 | m->next = NULL; | |
5061 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
5062 | *pm = m; |
5063 | pm = &m->next; | |
5064 | } | |
8ded5a0f | 5065 | } |
9ee5e499 | 5066 | |
8ded5a0f | 5067 | free (sections); |
12bd6957 | 5068 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
5069 | } |
5070 | ||
3dea8fca | 5071 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 5072 | return FALSE; |
8c37241b | 5073 | |
12bd6957 | 5074 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 5075 | ++count; |
12bd6957 | 5076 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 5077 | |
b34976b6 | 5078 | return TRUE; |
252b5132 RH |
5079 | |
5080 | error_return: | |
5081 | if (sections != NULL) | |
5082 | free (sections); | |
b34976b6 | 5083 | return FALSE; |
252b5132 RH |
5084 | } |
5085 | ||
5086 | /* Sort sections by address. */ | |
5087 | ||
5088 | static int | |
217aa764 | 5089 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
5090 | { |
5091 | const asection *sec1 = *(const asection **) arg1; | |
5092 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 5093 | bfd_size_type size1, size2; |
252b5132 RH |
5094 | |
5095 | /* Sort by LMA first, since this is the address used to | |
5096 | place the section into a segment. */ | |
5097 | if (sec1->lma < sec2->lma) | |
5098 | return -1; | |
5099 | else if (sec1->lma > sec2->lma) | |
5100 | return 1; | |
5101 | ||
5102 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
5103 | the same, and this will do nothing. */ | |
5104 | if (sec1->vma < sec2->vma) | |
5105 | return -1; | |
5106 | else if (sec1->vma > sec2->vma) | |
5107 | return 1; | |
5108 | ||
5109 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
5110 | ||
07c6e936 | 5111 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
5112 | |
5113 | if (TOEND (sec1)) | |
5114 | { | |
5115 | if (TOEND (sec2)) | |
00a7cdc5 NC |
5116 | { |
5117 | /* If the indicies are the same, do not return 0 | |
5118 | here, but continue to try the next comparison. */ | |
5119 | if (sec1->target_index - sec2->target_index != 0) | |
5120 | return sec1->target_index - sec2->target_index; | |
5121 | } | |
252b5132 RH |
5122 | else |
5123 | return 1; | |
5124 | } | |
00a7cdc5 | 5125 | else if (TOEND (sec2)) |
252b5132 RH |
5126 | return -1; |
5127 | ||
5128 | #undef TOEND | |
5129 | ||
00a7cdc5 NC |
5130 | /* Sort by size, to put zero sized sections |
5131 | before others at the same address. */ | |
252b5132 | 5132 | |
eea6121a AM |
5133 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
5134 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
5135 | |
5136 | if (size1 < size2) | |
252b5132 | 5137 | return -1; |
eecdbe52 | 5138 | if (size1 > size2) |
252b5132 RH |
5139 | return 1; |
5140 | ||
5141 | return sec1->target_index - sec2->target_index; | |
5142 | } | |
5143 | ||
340b6d91 AC |
5144 | /* Ian Lance Taylor writes: |
5145 | ||
5146 | We shouldn't be using % with a negative signed number. That's just | |
5147 | not good. We have to make sure either that the number is not | |
5148 | negative, or that the number has an unsigned type. When the types | |
5149 | are all the same size they wind up as unsigned. When file_ptr is a | |
5150 | larger signed type, the arithmetic winds up as signed long long, | |
5151 | which is wrong. | |
5152 | ||
5153 | What we're trying to say here is something like ``increase OFF by | |
5154 | the least amount that will cause it to be equal to the VMA modulo | |
5155 | the page size.'' */ | |
5156 | /* In other words, something like: | |
5157 | ||
5158 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
5159 | off_offset = off % bed->maxpagesize; | |
5160 | if (vma_offset < off_offset) | |
5161 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
5162 | else | |
5163 | adjustment = vma_offset - off_offset; | |
08a40648 | 5164 | |
de194d85 | 5165 | which can be collapsed into the expression below. */ |
340b6d91 AC |
5166 | |
5167 | static file_ptr | |
5168 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
5169 | { | |
dc9155b2 NC |
5170 | /* PR binutils/16199: Handle an alignment of zero. */ |
5171 | if (maxpagesize == 0) | |
5172 | maxpagesize = 1; | |
340b6d91 AC |
5173 | return ((vma - off) % maxpagesize); |
5174 | } | |
5175 | ||
6d33f217 L |
5176 | static void |
5177 | print_segment_map (const struct elf_segment_map *m) | |
5178 | { | |
5179 | unsigned int j; | |
5180 | const char *pt = get_segment_type (m->p_type); | |
5181 | char buf[32]; | |
5182 | ||
5183 | if (pt == NULL) | |
5184 | { | |
5185 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
5186 | sprintf (buf, "LOPROC+%7.7x", | |
5187 | (unsigned int) (m->p_type - PT_LOPROC)); | |
5188 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
5189 | sprintf (buf, "LOOS+%7.7x", | |
5190 | (unsigned int) (m->p_type - PT_LOOS)); | |
5191 | else | |
5192 | snprintf (buf, sizeof (buf), "%8.8x", | |
5193 | (unsigned int) m->p_type); | |
5194 | pt = buf; | |
5195 | } | |
4a97a0e5 | 5196 | fflush (stdout); |
6d33f217 L |
5197 | fprintf (stderr, "%s:", pt); |
5198 | for (j = 0; j < m->count; j++) | |
5199 | fprintf (stderr, " %s", m->sections [j]->name); | |
5200 | putc ('\n',stderr); | |
4a97a0e5 | 5201 | fflush (stderr); |
6d33f217 L |
5202 | } |
5203 | ||
32812159 AM |
5204 | static bfd_boolean |
5205 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
5206 | { | |
5207 | void *buf; | |
5208 | bfd_boolean ret; | |
5209 | ||
5210 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
5211 | return FALSE; | |
5212 | buf = bfd_zmalloc (len); | |
5213 | if (buf == NULL) | |
5214 | return FALSE; | |
5215 | ret = bfd_bwrite (buf, len, abfd) == len; | |
5216 | free (buf); | |
5217 | return ret; | |
5218 | } | |
5219 | ||
252b5132 RH |
5220 | /* Assign file positions to the sections based on the mapping from |
5221 | sections to segments. This function also sets up some fields in | |
f3520d2f | 5222 | the file header. */ |
252b5132 | 5223 | |
b34976b6 | 5224 | static bfd_boolean |
f3520d2f AM |
5225 | assign_file_positions_for_load_sections (bfd *abfd, |
5226 | struct bfd_link_info *link_info) | |
252b5132 RH |
5227 | { |
5228 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 5229 | struct elf_segment_map *m; |
252b5132 | 5230 | Elf_Internal_Phdr *phdrs; |
252b5132 | 5231 | Elf_Internal_Phdr *p; |
02bf8d82 | 5232 | file_ptr off; |
3f570048 | 5233 | bfd_size_type maxpagesize; |
a8c75b76 | 5234 | unsigned int pt_load_count = 0; |
f3520d2f | 5235 | unsigned int alloc; |
0920dee7 | 5236 | unsigned int i, j; |
2b0bc088 | 5237 | bfd_vma header_pad = 0; |
252b5132 | 5238 | |
e36284ab | 5239 | if (link_info == NULL |
ceae84aa | 5240 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 5241 | return FALSE; |
252b5132 | 5242 | |
8ded5a0f | 5243 | alloc = 0; |
12bd6957 | 5244 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
5245 | { |
5246 | ++alloc; | |
5247 | if (m->header_size) | |
5248 | header_pad = m->header_size; | |
5249 | } | |
252b5132 | 5250 | |
82f2dbf7 NC |
5251 | if (alloc) |
5252 | { | |
5253 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
5254 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
5255 | } | |
5256 | else | |
5257 | { | |
5258 | /* PR binutils/12467. */ | |
5259 | elf_elfheader (abfd)->e_phoff = 0; | |
5260 | elf_elfheader (abfd)->e_phentsize = 0; | |
5261 | } | |
d324f6d6 | 5262 | |
8ded5a0f | 5263 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 5264 | |
12bd6957 AM |
5265 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
5266 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 5267 | else |
12bd6957 | 5268 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 5269 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
5270 | |
5271 | if (alloc == 0) | |
f3520d2f | 5272 | { |
12bd6957 | 5273 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 5274 | return TRUE; |
f3520d2f | 5275 | } |
252b5132 | 5276 | |
12bd6957 | 5277 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
5278 | see assign_file_positions_except_relocs, so make sure we have |
5279 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
5280 | The variable alloc contains the computed need, while |
5281 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
5282 | layout. |
5283 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
5284 | where the layout is forced to according to a larger size in the | |
5285 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 5286 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 5287 | == 0); |
a50b1753 NC |
5288 | phdrs = (Elf_Internal_Phdr *) |
5289 | bfd_zalloc2 (abfd, | |
07d6d2b8 AM |
5290 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
5291 | sizeof (Elf_Internal_Phdr)); | |
f3520d2f | 5292 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5293 | if (phdrs == NULL) |
b34976b6 | 5294 | return FALSE; |
252b5132 | 5295 | |
3f570048 AM |
5296 | maxpagesize = 1; |
5297 | if ((abfd->flags & D_PAGED) != 0) | |
5298 | maxpagesize = bed->maxpagesize; | |
5299 | ||
252b5132 RH |
5300 | off = bed->s->sizeof_ehdr; |
5301 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5302 | if (header_pad < (bfd_vma) off) |
5303 | header_pad = 0; | |
5304 | else | |
5305 | header_pad -= off; | |
5306 | off += header_pad; | |
252b5132 | 5307 | |
12bd6957 | 5308 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 5309 | m != NULL; |
0920dee7 | 5310 | m = m->next, p++, j++) |
252b5132 | 5311 | { |
252b5132 | 5312 | asection **secpp; |
bf988460 AM |
5313 | bfd_vma off_adjust; |
5314 | bfd_boolean no_contents; | |
252b5132 RH |
5315 | |
5316 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 5317 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5318 | not be done to the PT_NOTE section of a corefile, which may |
5319 | contain several pseudo-sections artificially created by bfd. | |
5320 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5321 | if (m->count > 1 |
5322 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5323 | && m->p_type == PT_NOTE)) |
252b5132 RH |
5324 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
5325 | elf_sort_sections); | |
5326 | ||
b301b248 AM |
5327 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5328 | number of sections with contents contributing to both p_filesz | |
5329 | and p_memsz, followed by a number of sections with no contents | |
5330 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5331 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 5332 | p->p_type = m->p_type; |
28a7f3e7 | 5333 | p->p_flags = m->p_flags; |
252b5132 | 5334 | |
3f570048 AM |
5335 | if (m->count == 0) |
5336 | p->p_vaddr = 0; | |
5337 | else | |
3271a814 | 5338 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
5339 | |
5340 | if (m->p_paddr_valid) | |
5341 | p->p_paddr = m->p_paddr; | |
5342 | else if (m->count == 0) | |
5343 | p->p_paddr = 0; | |
5344 | else | |
08a40648 | 5345 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
5346 | |
5347 | if (p->p_type == PT_LOAD | |
5348 | && (abfd->flags & D_PAGED) != 0) | |
5349 | { | |
5350 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5351 | the maximum page size. When copying an executable with | |
5352 | objcopy, we set m->p_align from the input file. Use this | |
5353 | value for maxpagesize rather than bed->maxpagesize, which | |
5354 | may be different. Note that we use maxpagesize for PT_TLS | |
5355 | segment alignment later in this function, so we are relying | |
5356 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5357 | segment. */ | |
5358 | if (m->p_align_valid) | |
5359 | maxpagesize = m->p_align; | |
5360 | ||
5361 | p->p_align = maxpagesize; | |
a8c75b76 | 5362 | pt_load_count += 1; |
3f570048 | 5363 | } |
3271a814 NS |
5364 | else if (m->p_align_valid) |
5365 | p->p_align = m->p_align; | |
e970b90a DJ |
5366 | else if (m->count == 0) |
5367 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
5368 | else |
5369 | p->p_align = 0; | |
5370 | ||
bf988460 AM |
5371 | no_contents = FALSE; |
5372 | off_adjust = 0; | |
252b5132 | 5373 | if (p->p_type == PT_LOAD |
b301b248 | 5374 | && m->count > 0) |
252b5132 | 5375 | { |
b301b248 | 5376 | bfd_size_type align; |
a49e53ed | 5377 | unsigned int align_power = 0; |
b301b248 | 5378 | |
3271a814 NS |
5379 | if (m->p_align_valid) |
5380 | align = p->p_align; | |
5381 | else | |
252b5132 | 5382 | { |
3271a814 NS |
5383 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5384 | { | |
5385 | unsigned int secalign; | |
08a40648 | 5386 | |
3271a814 NS |
5387 | secalign = bfd_get_section_alignment (abfd, *secpp); |
5388 | if (secalign > align_power) | |
5389 | align_power = secalign; | |
5390 | } | |
5391 | align = (bfd_size_type) 1 << align_power; | |
5392 | if (align < maxpagesize) | |
5393 | align = maxpagesize; | |
b301b248 | 5394 | } |
252b5132 | 5395 | |
02bf8d82 AM |
5396 | for (i = 0; i < m->count; i++) |
5397 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5398 | /* If we aren't making room for this section, then | |
5399 | it must be SHT_NOBITS regardless of what we've | |
5400 | set via struct bfd_elf_special_section. */ | |
5401 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5402 | ||
bf988460 | 5403 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5404 | sections. */ |
5405 | no_contents = TRUE; | |
5406 | for (i = 0; i < m->count; i++) | |
5407 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5408 | { | |
5409 | no_contents = FALSE; | |
5410 | break; | |
5411 | } | |
bf988460 | 5412 | |
85cfcbfb | 5413 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
a8c75b76 AM |
5414 | |
5415 | /* Broken hardware and/or kernel require that files do not | |
5416 | map the same page with different permissions on some hppa | |
5417 | processors. */ | |
5418 | if (pt_load_count > 1 | |
5419 | && bed->no_page_alias | |
5420 | && (off & (maxpagesize - 1)) != 0 | |
5421 | && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize)) | |
5422 | off_adjust += maxpagesize; | |
bf988460 AM |
5423 | off += off_adjust; |
5424 | if (no_contents) | |
5425 | { | |
5426 | /* We shouldn't need to align the segment on disk since | |
5427 | the segment doesn't need file space, but the gABI | |
5428 | arguably requires the alignment and glibc ld.so | |
5429 | checks it. So to comply with the alignment | |
5430 | requirement but not waste file space, we adjust | |
5431 | p_offset for just this segment. (OFF_ADJUST is | |
5432 | subtracted from OFF later.) This may put p_offset | |
5433 | past the end of file, but that shouldn't matter. */ | |
5434 | } | |
5435 | else | |
5436 | off_adjust = 0; | |
252b5132 | 5437 | } |
b1a6d0b1 NC |
5438 | /* Make sure the .dynamic section is the first section in the |
5439 | PT_DYNAMIC segment. */ | |
5440 | else if (p->p_type == PT_DYNAMIC | |
5441 | && m->count > 1 | |
5442 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5443 | { | |
5444 | _bfd_error_handler | |
871b3ab2 | 5445 | (_("%pB: The first section in the PT_DYNAMIC segment" |
63a5468a | 5446 | " is not the .dynamic section"), |
b301b248 | 5447 | abfd); |
b1a6d0b1 NC |
5448 | bfd_set_error (bfd_error_bad_value); |
5449 | return FALSE; | |
5450 | } | |
3f001e84 JK |
5451 | /* Set the note section type to SHT_NOTE. */ |
5452 | else if (p->p_type == PT_NOTE) | |
5453 | for (i = 0; i < m->count; i++) | |
5454 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5455 | |
252b5132 RH |
5456 | p->p_offset = 0; |
5457 | p->p_filesz = 0; | |
5458 | p->p_memsz = 0; | |
5459 | ||
5460 | if (m->includes_filehdr) | |
5461 | { | |
bf988460 | 5462 | if (!m->p_flags_valid) |
252b5132 | 5463 | p->p_flags |= PF_R; |
252b5132 RH |
5464 | p->p_filesz = bed->s->sizeof_ehdr; |
5465 | p->p_memsz = bed->s->sizeof_ehdr; | |
5466 | if (m->count > 0) | |
5467 | { | |
9ab82472 L |
5468 | if (p->p_vaddr < (bfd_vma) off |
5469 | || (!m->p_paddr_valid | |
5470 | && p->p_paddr < (bfd_vma) off)) | |
252b5132 | 5471 | { |
4eca0228 | 5472 | _bfd_error_handler |
871b3ab2 | 5473 | (_("%pB: Not enough room for program headers," |
63a5468a | 5474 | " try linking with -N"), |
b301b248 | 5475 | abfd); |
252b5132 | 5476 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 5477 | return FALSE; |
252b5132 RH |
5478 | } |
5479 | ||
5480 | p->p_vaddr -= off; | |
bf988460 | 5481 | if (!m->p_paddr_valid) |
252b5132 RH |
5482 | p->p_paddr -= off; |
5483 | } | |
252b5132 RH |
5484 | } |
5485 | ||
5486 | if (m->includes_phdrs) | |
5487 | { | |
bf988460 | 5488 | if (!m->p_flags_valid) |
252b5132 RH |
5489 | p->p_flags |= PF_R; |
5490 | ||
f3520d2f | 5491 | if (!m->includes_filehdr) |
252b5132 RH |
5492 | { |
5493 | p->p_offset = bed->s->sizeof_ehdr; | |
5494 | ||
5495 | if (m->count > 0) | |
5496 | { | |
252b5132 | 5497 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 5498 | if (!m->p_paddr_valid) |
252b5132 RH |
5499 | p->p_paddr -= off - p->p_offset; |
5500 | } | |
252b5132 RH |
5501 | } |
5502 | ||
5503 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
5504 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5505 | if (m->count) |
5506 | { | |
5507 | p->p_filesz += header_pad; | |
5508 | p->p_memsz += header_pad; | |
5509 | } | |
252b5132 RH |
5510 | } |
5511 | ||
5512 | if (p->p_type == PT_LOAD | |
5513 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5514 | { | |
bf988460 | 5515 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 5516 | p->p_offset = off; |
252b5132 RH |
5517 | else |
5518 | { | |
5519 | file_ptr adjust; | |
5520 | ||
5521 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5522 | if (!no_contents) |
5523 | p->p_filesz += adjust; | |
252b5132 RH |
5524 | p->p_memsz += adjust; |
5525 | } | |
5526 | } | |
5527 | ||
1ea63fd2 AM |
5528 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5529 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5530 | core files, for sections in PT_NOTE segments. | |
5531 | assign_file_positions_for_non_load_sections will set filepos | |
5532 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5533 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5534 | { | |
5535 | asection *sec; | |
252b5132 | 5536 | bfd_size_type align; |
627b32bc | 5537 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5538 | |
5539 | sec = *secpp; | |
02bf8d82 | 5540 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 5541 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 5542 | |
88967714 AM |
5543 | if ((p->p_type == PT_LOAD |
5544 | || p->p_type == PT_TLS) | |
5545 | && (this_hdr->sh_type != SHT_NOBITS | |
5546 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5547 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5548 | || p->p_type == PT_TLS)))) | |
252b5132 | 5549 | { |
b5599592 AM |
5550 | bfd_vma p_start = p->p_paddr; |
5551 | bfd_vma p_end = p_start + p->p_memsz; | |
5552 | bfd_vma s_start = sec->lma; | |
5553 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 5554 | |
a2d1e028 L |
5555 | if (adjust != 0 |
5556 | && (s_start < p_end | |
5557 | || p_end < p_start)) | |
252b5132 | 5558 | { |
4eca0228 | 5559 | _bfd_error_handler |
695344c0 | 5560 | /* xgettext:c-format */ |
871b3ab2 | 5561 | (_("%pB: section %pA lma %#Lx adjusted to %#Lx"), |
76cfced5 | 5562 | abfd, sec, s_start, p_end); |
88967714 | 5563 | adjust = 0; |
b5599592 | 5564 | sec->lma = p_end; |
1cfb7d1e | 5565 | } |
3ac9b6c9 | 5566 | p->p_memsz += adjust; |
1cfb7d1e | 5567 | |
88967714 AM |
5568 | if (this_hdr->sh_type != SHT_NOBITS) |
5569 | { | |
32812159 AM |
5570 | if (p->p_filesz + adjust < p->p_memsz) |
5571 | { | |
5572 | /* We have a PROGBITS section following NOBITS ones. | |
07d6d2b8 | 5573 | Allocate file space for the NOBITS section(s) and |
32812159 AM |
5574 | zero it. */ |
5575 | adjust = p->p_memsz - p->p_filesz; | |
5576 | if (!write_zeros (abfd, off, adjust)) | |
5577 | return FALSE; | |
5578 | } | |
88967714 AM |
5579 | off += adjust; |
5580 | p->p_filesz += adjust; | |
252b5132 | 5581 | } |
252b5132 RH |
5582 | } |
5583 | ||
5584 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5585 | { | |
b301b248 AM |
5586 | /* The section at i == 0 is the one that actually contains |
5587 | everything. */ | |
4a938328 MS |
5588 | if (i == 0) |
5589 | { | |
627b32bc | 5590 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5591 | off += this_hdr->sh_size; |
5592 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5593 | p->p_memsz = 0; |
5594 | p->p_align = 1; | |
252b5132 | 5595 | } |
4a938328 | 5596 | else |
252b5132 | 5597 | { |
b301b248 | 5598 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5599 | sec->filepos = 0; |
eea6121a | 5600 | sec->size = 0; |
b301b248 AM |
5601 | sec->flags = 0; |
5602 | continue; | |
252b5132 | 5603 | } |
252b5132 RH |
5604 | } |
5605 | else | |
5606 | { | |
1e951488 | 5607 | if (p->p_type == PT_LOAD) |
b301b248 | 5608 | { |
1e951488 AM |
5609 | this_hdr->sh_offset = sec->filepos = off; |
5610 | if (this_hdr->sh_type != SHT_NOBITS) | |
5611 | off += this_hdr->sh_size; | |
5612 | } | |
5613 | else if (this_hdr->sh_type == SHT_NOBITS | |
5614 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5615 | && this_hdr->sh_offset == 0) | |
5616 | { | |
5617 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5618 | (See _bfd_elf_map_sections_to_segments "Create a | |
5619 | final PT_LOAD".) Set sh_offset to the value it | |
5620 | would have if we had created a zero p_filesz and | |
5621 | p_memsz PT_LOAD header for the section. This | |
5622 | also makes the PT_TLS header have the same | |
5623 | p_offset value. */ | |
5624 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5625 | off, align); | |
5626 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5627 | } |
252b5132 | 5628 | |
02bf8d82 | 5629 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5630 | { |
6a3cd2b4 | 5631 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5632 | /* A load section without SHF_ALLOC is something like |
5633 | a note section in a PT_NOTE segment. These take | |
5634 | file space but are not loaded into memory. */ | |
5635 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5636 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5637 | } |
6a3cd2b4 | 5638 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5639 | { |
6a3cd2b4 AM |
5640 | if (p->p_type == PT_TLS) |
5641 | p->p_memsz += this_hdr->sh_size; | |
5642 | ||
5643 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5644 | normal segments. */ | |
5645 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5646 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5647 | } |
5648 | ||
b10a8ae0 L |
5649 | if (align > p->p_align |
5650 | && !m->p_align_valid | |
5651 | && (p->p_type != PT_LOAD | |
5652 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5653 | p->p_align = align; |
5654 | } | |
5655 | ||
bf988460 | 5656 | if (!m->p_flags_valid) |
252b5132 RH |
5657 | { |
5658 | p->p_flags |= PF_R; | |
02bf8d82 | 5659 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5660 | p->p_flags |= PF_X; |
02bf8d82 | 5661 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5662 | p->p_flags |= PF_W; |
5663 | } | |
5664 | } | |
43a8475c | 5665 | |
bf988460 | 5666 | off -= off_adjust; |
0920dee7 | 5667 | |
7c928300 AM |
5668 | /* Check that all sections are in a PT_LOAD segment. |
5669 | Don't check funky gdb generated core files. */ | |
5670 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5671 | { |
5672 | bfd_boolean check_vma = TRUE; | |
5673 | ||
5674 | for (i = 1; i < m->count; i++) | |
5675 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5676 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5677 | ->this_hdr), p) != 0 | |
5678 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5679 | ->this_hdr), p) != 0) | |
0920dee7 | 5680 | { |
9a83a553 AM |
5681 | /* Looks like we have overlays packed into the segment. */ |
5682 | check_vma = FALSE; | |
5683 | break; | |
0920dee7 | 5684 | } |
9a83a553 AM |
5685 | |
5686 | for (i = 0; i < m->count; i++) | |
5687 | { | |
5688 | Elf_Internal_Shdr *this_hdr; | |
5689 | asection *sec; | |
5690 | ||
5691 | sec = m->sections[i]; | |
5692 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5693 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5694 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 | 5695 | { |
4eca0228 | 5696 | _bfd_error_handler |
695344c0 | 5697 | /* xgettext:c-format */ |
871b3ab2 | 5698 | (_("%pB: section `%pA' can't be allocated in segment %d"), |
9a83a553 AM |
5699 | abfd, sec, j); |
5700 | print_segment_map (m); | |
5701 | } | |
5702 | } | |
5703 | } | |
252b5132 RH |
5704 | } |
5705 | ||
12bd6957 | 5706 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5707 | return TRUE; |
5708 | } | |
5709 | ||
5710 | /* Assign file positions for the other sections. */ | |
5711 | ||
5712 | static bfd_boolean | |
5713 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5714 | struct bfd_link_info *link_info) | |
5715 | { | |
5716 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5717 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5718 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5719 | Elf_Internal_Phdr *phdrs; |
5720 | Elf_Internal_Phdr *p; | |
5721 | struct elf_segment_map *m; | |
62655c7b | 5722 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5723 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5724 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5725 | file_ptr off; | |
f3520d2f AM |
5726 | unsigned int count; |
5727 | ||
5c182d5f | 5728 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5729 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5730 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5731 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5732 | { |
5c182d5f AM |
5733 | Elf_Internal_Shdr *hdr; |
5734 | ||
5735 | hdr = *hdrpp; | |
5736 | if (hdr->bfd_section != NULL | |
252e386e AM |
5737 | && (hdr->bfd_section->filepos != 0 |
5738 | || (hdr->sh_type == SHT_NOBITS | |
5739 | && hdr->contents == NULL))) | |
627b32bc | 5740 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5741 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5742 | { | |
e8d2ba53 | 5743 | if (hdr->sh_size != 0) |
4eca0228 | 5744 | _bfd_error_handler |
695344c0 | 5745 | /* xgettext:c-format */ |
871b3ab2 | 5746 | (_("%pB: warning: allocated section `%s' not in segment"), |
e8d2ba53 AM |
5747 | abfd, |
5748 | (hdr->bfd_section == NULL | |
5749 | ? "*unknown*" | |
5750 | : hdr->bfd_section->name)); | |
3ba71138 L |
5751 | /* We don't need to page align empty sections. */ |
5752 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5753 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5754 | bed->maxpagesize); | |
5755 | else | |
5756 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5757 | hdr->sh_addralign); | |
5758 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5759 | FALSE); | |
5760 | } | |
5761 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5762 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5763 | || (hdr->bfd_section != NULL |
5764 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5765 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5766 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
5767 | || (elf_symtab_shndx_list (abfd) != NULL |
5768 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5769 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
5770 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
5771 | hdr->sh_offset = -1; |
5772 | else | |
5773 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5774 | } |
5775 | ||
252b5132 RH |
5776 | /* Now that we have set the section file positions, we can set up |
5777 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5778 | count = 0; |
5779 | filehdr_vaddr = 0; | |
5780 | filehdr_paddr = 0; | |
5781 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5782 | phdrs_paddr = 0; | |
62655c7b | 5783 | hdrs_segment = NULL; |
f3520d2f | 5784 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5785 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5786 | { |
5787 | ++count; | |
5788 | if (p->p_type != PT_LOAD) | |
5789 | continue; | |
5790 | ||
5791 | if (m->includes_filehdr) | |
5792 | { | |
5793 | filehdr_vaddr = p->p_vaddr; | |
5794 | filehdr_paddr = p->p_paddr; | |
5795 | } | |
5796 | if (m->includes_phdrs) | |
5797 | { | |
5798 | phdrs_vaddr = p->p_vaddr; | |
5799 | phdrs_paddr = p->p_paddr; | |
5800 | if (m->includes_filehdr) | |
5801 | { | |
62655c7b | 5802 | hdrs_segment = m; |
f3520d2f AM |
5803 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5804 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5805 | } | |
5806 | } | |
5807 | } | |
5808 | ||
62655c7b RM |
5809 | if (hdrs_segment != NULL && link_info != NULL) |
5810 | { | |
5811 | /* There is a segment that contains both the file headers and the | |
5812 | program headers, so provide a symbol __ehdr_start pointing there. | |
5813 | A program can use this to examine itself robustly. */ | |
5814 | ||
5815 | struct elf_link_hash_entry *hash | |
5816 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5817 | FALSE, FALSE, TRUE); | |
5818 | /* If the symbol was referenced and not defined, define it. */ | |
5819 | if (hash != NULL | |
5820 | && (hash->root.type == bfd_link_hash_new | |
5821 | || hash->root.type == bfd_link_hash_undefined | |
5822 | || hash->root.type == bfd_link_hash_undefweak | |
5823 | || hash->root.type == bfd_link_hash_common)) | |
5824 | { | |
5825 | asection *s = NULL; | |
5826 | if (hdrs_segment->count != 0) | |
5827 | /* The segment contains sections, so use the first one. */ | |
5828 | s = hdrs_segment->sections[0]; | |
5829 | else | |
5830 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5831 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5832 | if (m->count != 0) |
5833 | { | |
5834 | s = m->sections[0]; | |
5835 | break; | |
5836 | } | |
5837 | ||
5838 | if (s != NULL) | |
5839 | { | |
5840 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5841 | hash->root.u.def.section = s; | |
5842 | } | |
5843 | else | |
5844 | { | |
5845 | hash->root.u.def.value = filehdr_vaddr; | |
5846 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5847 | } | |
5848 | ||
5849 | hash->root.type = bfd_link_hash_defined; | |
5850 | hash->def_regular = 1; | |
5851 | hash->non_elf = 0; | |
5852 | } | |
5853 | } | |
5854 | ||
12bd6957 | 5855 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5856 | { |
129af99f | 5857 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5858 | { |
f2731e0c | 5859 | bfd_vma start, end; |
1ea63fd2 | 5860 | |
129af99f | 5861 | if (link_info != NULL) |
8c37241b | 5862 | { |
129af99f | 5863 | /* During linking the range of the RELRO segment is passed |
f2731e0c AM |
5864 | in link_info. Note that there may be padding between |
5865 | relro_start and the first RELRO section. */ | |
5866 | start = link_info->relro_start; | |
5867 | end = link_info->relro_end; | |
5868 | } | |
5869 | else if (m->count != 0) | |
5870 | { | |
5871 | if (!m->p_size_valid) | |
5872 | abort (); | |
5873 | start = m->sections[0]->vma; | |
5874 | end = start + m->p_size; | |
5875 | } | |
5876 | else | |
5877 | { | |
5878 | start = 0; | |
5879 | end = 0; | |
5880 | } | |
5881 | ||
5882 | if (start < end) | |
5883 | { | |
5884 | struct elf_segment_map *lm; | |
5885 | const Elf_Internal_Phdr *lp; | |
5886 | unsigned int i; | |
5887 | ||
5888 | /* Find a LOAD segment containing a section in the RELRO | |
5889 | segment. */ | |
12bd6957 | 5890 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5891 | lm != NULL; |
5892 | lm = lm->next, lp++) | |
8c37241b JJ |
5893 | { |
5894 | if (lp->p_type == PT_LOAD | |
3146fac4 | 5895 | && lm->count != 0 |
dbc88fc1 AM |
5896 | && (lm->sections[lm->count - 1]->vma |
5897 | + (!IS_TBSS (lm->sections[lm->count - 1]) | |
5898 | ? lm->sections[lm->count - 1]->size | |
5899 | : 0)) > start | |
f2731e0c | 5900 | && lm->sections[0]->vma < end) |
8c37241b JJ |
5901 | break; |
5902 | } | |
3146fac4 | 5903 | BFD_ASSERT (lm != NULL); |
f2731e0c AM |
5904 | |
5905 | /* Find the section starting the RELRO segment. */ | |
5906 | for (i = 0; i < lm->count; i++) | |
129af99f | 5907 | { |
f2731e0c AM |
5908 | asection *s = lm->sections[i]; |
5909 | if (s->vma >= start | |
5910 | && s->vma < end | |
5911 | && s->size != 0) | |
129af99f AS |
5912 | break; |
5913 | } | |
f2731e0c AM |
5914 | BFD_ASSERT (i < lm->count); |
5915 | ||
5916 | p->p_vaddr = lm->sections[i]->vma; | |
5917 | p->p_paddr = lm->sections[i]->lma; | |
5918 | p->p_offset = lm->sections[i]->filepos; | |
5919 | p->p_memsz = end - p->p_vaddr; | |
5920 | p->p_filesz = p->p_memsz; | |
5921 | ||
5922 | /* The RELRO segment typically ends a few bytes into | |
5923 | .got.plt but other layouts are possible. In cases | |
5924 | where the end does not match any loaded section (for | |
5925 | instance is in file padding), trim p_filesz back to | |
5926 | correspond to the end of loaded section contents. */ | |
5927 | if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr) | |
5928 | p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr; | |
b84a33b5 | 5929 | |
f3944f72 | 5930 | /* Preserve the alignment and flags if they are valid. The |
07d6d2b8 | 5931 | gold linker generates RW/4 for the PT_GNU_RELRO section. |
f3944f72 L |
5932 | It is better for objcopy/strip to honor these attributes |
5933 | otherwise gdb will choke when using separate debug files. | |
5934 | */ | |
5935 | if (!m->p_align_valid) | |
5936 | p->p_align = 1; | |
5937 | if (!m->p_flags_valid) | |
341b8ee7 | 5938 | p->p_flags = PF_R; |
129af99f | 5939 | } |
9433b9b1 | 5940 | else |
b84a33b5 AM |
5941 | { |
5942 | memset (p, 0, sizeof *p); | |
5943 | p->p_type = PT_NULL; | |
5944 | } | |
129af99f | 5945 | } |
04c3a755 NS |
5946 | else if (p->p_type == PT_GNU_STACK) |
5947 | { | |
5948 | if (m->p_size_valid) | |
5949 | p->p_memsz = m->p_size; | |
5950 | } | |
129af99f AS |
5951 | else if (m->count != 0) |
5952 | { | |
e06efbf1 | 5953 | unsigned int i; |
1a9ccd70 | 5954 | |
129af99f AS |
5955 | if (p->p_type != PT_LOAD |
5956 | && (p->p_type != PT_NOTE | |
5957 | || bfd_get_format (abfd) != bfd_core)) | |
5958 | { | |
1a9ccd70 NC |
5959 | /* A user specified segment layout may include a PHDR |
5960 | segment that overlaps with a LOAD segment... */ | |
5961 | if (p->p_type == PT_PHDR) | |
5962 | { | |
5963 | m->count = 0; | |
5964 | continue; | |
5965 | } | |
5966 | ||
c86934ce NC |
5967 | if (m->includes_filehdr || m->includes_phdrs) |
5968 | { | |
b1fa9dd6 | 5969 | /* PR 17512: file: 2195325e. */ |
4eca0228 | 5970 | _bfd_error_handler |
871b3ab2 | 5971 | (_("%pB: error: non-load segment %d includes file header " |
76cfced5 AM |
5972 | "and/or program header"), |
5973 | abfd, (int) (p - phdrs)); | |
c86934ce NC |
5974 | return FALSE; |
5975 | } | |
129af99f | 5976 | |
86b2281f | 5977 | p->p_filesz = 0; |
129af99f | 5978 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5979 | for (i = m->count; i-- != 0;) |
5980 | { | |
5981 | asection *sect = m->sections[i]; | |
5982 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5983 | if (hdr->sh_type != SHT_NOBITS) | |
5984 | { | |
5985 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5986 | + hdr->sh_size); | |
5987 | break; | |
5988 | } | |
5989 | } | |
129af99f AS |
5990 | } |
5991 | } | |
5992 | else if (m->includes_filehdr) | |
5993 | { | |
5994 | p->p_vaddr = filehdr_vaddr; | |
5995 | if (! m->p_paddr_valid) | |
5996 | p->p_paddr = filehdr_paddr; | |
5997 | } | |
5998 | else if (m->includes_phdrs) | |
5999 | { | |
6000 | p->p_vaddr = phdrs_vaddr; | |
6001 | if (! m->p_paddr_valid) | |
6002 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
6003 | } |
6004 | } | |
6005 | ||
12bd6957 | 6006 | elf_next_file_pos (abfd) = off; |
252b5132 | 6007 | |
b34976b6 | 6008 | return TRUE; |
252b5132 RH |
6009 | } |
6010 | ||
6a40cf0c NC |
6011 | static elf_section_list * |
6012 | find_section_in_list (unsigned int i, elf_section_list * list) | |
6013 | { | |
6014 | for (;list != NULL; list = list->next) | |
6015 | if (list->ndx == i) | |
6016 | break; | |
6017 | return list; | |
6018 | } | |
6019 | ||
252b5132 RH |
6020 | /* Work out the file positions of all the sections. This is called by |
6021 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
6022 | VMAs must be known before this is called. | |
6023 | ||
e0638f70 AM |
6024 | Reloc sections come in two flavours: Those processed specially as |
6025 | "side-channel" data attached to a section to which they apply, and | |
6026 | those that bfd doesn't process as relocations. The latter sort are | |
6027 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
6028 | consider the former sort here, unless they form part of the loadable | |
6029 | image. Reloc sections not assigned here will be handled later by | |
6030 | assign_file_positions_for_relocs. | |
252b5132 RH |
6031 | |
6032 | We also don't set the positions of the .symtab and .strtab here. */ | |
6033 | ||
b34976b6 | 6034 | static bfd_boolean |
c84fca4d AO |
6035 | assign_file_positions_except_relocs (bfd *abfd, |
6036 | struct bfd_link_info *link_info) | |
252b5132 | 6037 | { |
5c182d5f AM |
6038 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
6039 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 6040 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6041 | |
6042 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
6043 | && bfd_get_format (abfd) != bfd_core) | |
6044 | { | |
5c182d5f AM |
6045 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
6046 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
6047 | Elf_Internal_Shdr **hdrpp; |
6048 | unsigned int i; | |
a485e98e | 6049 | file_ptr off; |
252b5132 RH |
6050 | |
6051 | /* Start after the ELF header. */ | |
6052 | off = i_ehdrp->e_ehsize; | |
6053 | ||
6054 | /* We are not creating an executable, which means that we are | |
6055 | not creating a program header, and that the actual order of | |
6056 | the sections in the file is unimportant. */ | |
9ad5cbcf | 6057 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
6058 | { |
6059 | Elf_Internal_Shdr *hdr; | |
6060 | ||
6061 | hdr = *hdrpp; | |
e0638f70 AM |
6062 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
6063 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
6064 | || (hdr->bfd_section != NULL |
6065 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
6066 | /* Compress DWARF debug sections. */ | |
12bd6957 | 6067 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
6068 | || (elf_symtab_shndx_list (abfd) != NULL |
6069 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
6070 | || i == elf_strtab_sec (abfd) |
6071 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
6072 | { |
6073 | hdr->sh_offset = -1; | |
252b5132 | 6074 | } |
9ad5cbcf | 6075 | else |
b34976b6 | 6076 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 6077 | } |
a485e98e AM |
6078 | |
6079 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
6080 | } |
6081 | else | |
6082 | { | |
f3520d2f AM |
6083 | unsigned int alloc; |
6084 | ||
252b5132 | 6085 | /* Assign file positions for the loaded sections based on the |
08a40648 | 6086 | assignment of sections to segments. */ |
f3520d2f AM |
6087 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
6088 | return FALSE; | |
6089 | ||
6090 | /* And for non-load sections. */ | |
6091 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
6092 | return FALSE; | |
6093 | ||
e36284ab AM |
6094 | if (bed->elf_backend_modify_program_headers != NULL) |
6095 | { | |
6096 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
6097 | return FALSE; | |
6098 | } | |
6099 | ||
58e7ebac | 6100 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
0e1862bb | 6101 | if (link_info != NULL && bfd_link_pie (link_info)) |
58e7ebac L |
6102 | { |
6103 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
6104 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
6105 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
6106 | ||
6107 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
6108 | bfd_vma p_vaddr = (bfd_vma) -1; | |
6109 | for (; segment < end_segment; segment++) | |
6110 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
6111 | p_vaddr = segment->p_vaddr; | |
6112 | ||
6113 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
6114 | segments is non-zero. */ | |
6115 | if (p_vaddr) | |
6116 | i_ehdrp->e_type = ET_EXEC; | |
6117 | } | |
6118 | ||
f3520d2f | 6119 | /* Write out the program headers. */ |
12bd6957 | 6120 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
1a9ccd70 NC |
6121 | |
6122 | /* Sort the program headers into the ordering required by the ELF standard. */ | |
6123 | if (alloc == 0) | |
6124 | return TRUE; | |
6125 | ||
1a9ccd70 NC |
6126 | /* PR ld/20815 - Check that the program header segment, if present, will |
6127 | be loaded into memory. FIXME: The check below is not sufficient as | |
6128 | really all PT_LOAD segments should be checked before issuing an error | |
6129 | message. Plus the PHDR segment does not have to be the first segment | |
6130 | in the program header table. But this version of the check should | |
cd584857 NC |
6131 | catch all real world use cases. |
6132 | ||
6133 | FIXME: We used to have code here to sort the PT_LOAD segments into | |
6134 | ascending order, as per the ELF spec. But this breaks some programs, | |
6135 | including the Linux kernel. But really either the spec should be | |
07d6d2b8 | 6136 | changed or the programs updated. */ |
1a9ccd70 | 6137 | if (alloc > 1 |
cd584857 | 6138 | && tdata->phdr[0].p_type == PT_PHDR |
d00dd7dc AM |
6139 | && (bed->elf_backend_allow_non_load_phdr == NULL |
6140 | || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr, | |
6141 | alloc)) | |
cd584857 NC |
6142 | && tdata->phdr[1].p_type == PT_LOAD |
6143 | && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr | |
6144 | || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz) | |
6145 | < (tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz))) | |
1a9ccd70 NC |
6146 | { |
6147 | /* The fix for this error is usually to edit the linker script being | |
6148 | used and set up the program headers manually. Either that or | |
6149 | leave room for the headers at the start of the SECTIONS. */ | |
6150 | _bfd_error_handler (_("\ | |
871b3ab2 | 6151 | %pB: error: PHDR segment not covered by LOAD segment"), |
1a9ccd70 | 6152 | abfd); |
1a9ccd70 NC |
6153 | return FALSE; |
6154 | } | |
6155 | ||
f3520d2f | 6156 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
cd584857 NC |
6157 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) |
6158 | return FALSE; | |
252b5132 RH |
6159 | } |
6160 | ||
b34976b6 | 6161 | return TRUE; |
252b5132 RH |
6162 | } |
6163 | ||
b34976b6 | 6164 | static bfd_boolean |
217aa764 | 6165 | prep_headers (bfd *abfd) |
252b5132 | 6166 | { |
3d540e93 | 6167 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 6168 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 6169 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6170 | |
6171 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 6172 | |
2b0f7ef9 | 6173 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 6174 | if (shstrtab == NULL) |
b34976b6 | 6175 | return FALSE; |
252b5132 RH |
6176 | |
6177 | elf_shstrtab (abfd) = shstrtab; | |
6178 | ||
6179 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
6180 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
6181 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
6182 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
6183 | ||
6184 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
6185 | i_ehdrp->e_ident[EI_DATA] = | |
6186 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
6187 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
6188 | ||
252b5132 RH |
6189 | if ((abfd->flags & DYNAMIC) != 0) |
6190 | i_ehdrp->e_type = ET_DYN; | |
6191 | else if ((abfd->flags & EXEC_P) != 0) | |
6192 | i_ehdrp->e_type = ET_EXEC; | |
6193 | else if (bfd_get_format (abfd) == bfd_core) | |
6194 | i_ehdrp->e_type = ET_CORE; | |
6195 | else | |
6196 | i_ehdrp->e_type = ET_REL; | |
6197 | ||
6198 | switch (bfd_get_arch (abfd)) | |
6199 | { | |
6200 | case bfd_arch_unknown: | |
6201 | i_ehdrp->e_machine = EM_NONE; | |
6202 | break; | |
aa4f99bb AO |
6203 | |
6204 | /* There used to be a long list of cases here, each one setting | |
6205 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
6206 | in the corresponding bfd definition. To avoid duplication, | |
6207 | the switch was removed. Machines that need special handling | |
6208 | can generally do it in elf_backend_final_write_processing(), | |
6209 | unless they need the information earlier than the final write. | |
6210 | Such need can generally be supplied by replacing the tests for | |
6211 | e_machine with the conditions used to determine it. */ | |
252b5132 | 6212 | default: |
9c5bfbb7 AM |
6213 | i_ehdrp->e_machine = bed->elf_machine_code; |
6214 | } | |
aa4f99bb | 6215 | |
252b5132 RH |
6216 | i_ehdrp->e_version = bed->s->ev_current; |
6217 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
6218 | ||
c044fabd | 6219 | /* No program header, for now. */ |
252b5132 RH |
6220 | i_ehdrp->e_phoff = 0; |
6221 | i_ehdrp->e_phentsize = 0; | |
6222 | i_ehdrp->e_phnum = 0; | |
6223 | ||
c044fabd | 6224 | /* Each bfd section is section header entry. */ |
252b5132 RH |
6225 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
6226 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
6227 | ||
c044fabd | 6228 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 6229 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
6230 | /* It all happens later. */ |
6231 | ; | |
252b5132 RH |
6232 | else |
6233 | { | |
6234 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
6235 | i_ehdrp->e_phoff = 0; |
6236 | } | |
6237 | ||
6238 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 6239 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 6240 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 6241 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 6242 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 6243 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 6244 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 6245 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 6246 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 6247 | return FALSE; |
252b5132 | 6248 | |
b34976b6 | 6249 | return TRUE; |
252b5132 RH |
6250 | } |
6251 | ||
6252 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 6253 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 6254 | |
0ce398f1 L |
6255 | static bfd_boolean |
6256 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
6257 | { |
6258 | file_ptr off; | |
e06efbf1 | 6259 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 6260 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
6261 | Elf_Internal_Ehdr *i_ehdrp; |
6262 | const struct elf_backend_data *bed; | |
252b5132 | 6263 | |
12bd6957 | 6264 | off = elf_next_file_pos (abfd); |
252b5132 | 6265 | |
e06efbf1 L |
6266 | shdrpp = elf_elfsections (abfd); |
6267 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
6268 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 6269 | { |
252b5132 | 6270 | shdrp = *shdrpp; |
0ce398f1 L |
6271 | if (shdrp->sh_offset == -1) |
6272 | { | |
3e19fb8f | 6273 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
6274 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
6275 | || shdrp->sh_type == SHT_RELA); | |
6276 | if (is_rel | |
3e19fb8f | 6277 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 L |
6278 | { |
6279 | if (!is_rel) | |
6280 | { | |
3e19fb8f L |
6281 | const char *name = sec->name; |
6282 | struct bfd_elf_section_data *d; | |
6283 | ||
0ce398f1 | 6284 | /* Compress DWARF debug sections. */ |
3e19fb8f | 6285 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
6286 | shdrp->contents)) |
6287 | return FALSE; | |
3e19fb8f L |
6288 | |
6289 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
6290 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
6291 | { | |
6292 | /* If section is compressed with zlib-gnu, convert | |
6293 | section name from .debug_* to .zdebug_*. */ | |
6294 | char *new_name | |
6295 | = convert_debug_to_zdebug (abfd, name); | |
6296 | if (new_name == NULL) | |
6297 | return FALSE; | |
6298 | name = new_name; | |
6299 | } | |
dd905818 | 6300 | /* Add section name to section name section. */ |
3e19fb8f L |
6301 | if (shdrp->sh_name != (unsigned int) -1) |
6302 | abort (); | |
6303 | shdrp->sh_name | |
6304 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
6305 | name, FALSE); | |
6306 | d = elf_section_data (sec); | |
6307 | ||
dd905818 | 6308 | /* Add reloc section name to section name section. */ |
3e19fb8f L |
6309 | if (d->rel.hdr |
6310 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6311 | d->rel.hdr, | |
6312 | name, FALSE)) | |
6313 | return FALSE; | |
6314 | if (d->rela.hdr | |
6315 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6316 | d->rela.hdr, | |
91cb26da | 6317 | name, TRUE)) |
3e19fb8f L |
6318 | return FALSE; |
6319 | ||
0ce398f1 | 6320 | /* Update section size and contents. */ |
3e19fb8f L |
6321 | shdrp->sh_size = sec->size; |
6322 | shdrp->contents = sec->contents; | |
0ce398f1 L |
6323 | shdrp->bfd_section->contents = NULL; |
6324 | } | |
6325 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
6326 | off, | |
6327 | TRUE); | |
6328 | } | |
6329 | } | |
252b5132 RH |
6330 | } |
6331 | ||
3e19fb8f L |
6332 | /* Place section name section after DWARF debug sections have been |
6333 | compressed. */ | |
6334 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
6335 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
6336 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
6337 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
6338 | ||
6339 | /* Place the section headers. */ | |
a485e98e AM |
6340 | i_ehdrp = elf_elfheader (abfd); |
6341 | bed = get_elf_backend_data (abfd); | |
6342 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
6343 | i_ehdrp->e_shoff = off; | |
6344 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 6345 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
6346 | |
6347 | return TRUE; | |
252b5132 RH |
6348 | } |
6349 | ||
b34976b6 | 6350 | bfd_boolean |
217aa764 | 6351 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 6352 | { |
9c5bfbb7 | 6353 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6354 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 6355 | bfd_boolean failed; |
9ad5cbcf | 6356 | unsigned int count, num_sec; |
30e8ee25 | 6357 | struct elf_obj_tdata *t; |
252b5132 RH |
6358 | |
6359 | if (! abfd->output_has_begun | |
217aa764 | 6360 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 6361 | return FALSE; |
252b5132 RH |
6362 | |
6363 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 6364 | |
b34976b6 | 6365 | failed = FALSE; |
252b5132 RH |
6366 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
6367 | if (failed) | |
b34976b6 | 6368 | return FALSE; |
252b5132 | 6369 | |
0ce398f1 L |
6370 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
6371 | return FALSE; | |
252b5132 | 6372 | |
c044fabd | 6373 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
6374 | num_sec = elf_numsections (abfd); |
6375 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6376 | { |
3e19fb8f L |
6377 | i_shdrp[count]->sh_name |
6378 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6379 | i_shdrp[count]->sh_name); | |
252b5132 | 6380 | if (bed->elf_backend_section_processing) |
75506100 MR |
6381 | if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count])) |
6382 | return FALSE; | |
252b5132 RH |
6383 | if (i_shdrp[count]->contents) |
6384 | { | |
dc810e39 AM |
6385 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6386 | ||
252b5132 | 6387 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6388 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6389 | return FALSE; |
252b5132 RH |
6390 | } |
6391 | } | |
6392 | ||
6393 | /* Write out the section header names. */ | |
30e8ee25 | 6394 | t = elf_tdata (abfd); |
26ae6d5e | 6395 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6396 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6397 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6398 | return FALSE; |
252b5132 RH |
6399 | |
6400 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 6401 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 6402 | |
ff59fc36 RM |
6403 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6404 | return FALSE; | |
6405 | ||
6406 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6407 | if (t->o->build_id.after_write_object_contents != NULL) |
6408 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6409 | |
6410 | return TRUE; | |
252b5132 RH |
6411 | } |
6412 | ||
b34976b6 | 6413 | bfd_boolean |
217aa764 | 6414 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6415 | { |
c044fabd | 6416 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6417 | return _bfd_elf_write_object_contents (abfd); |
6418 | } | |
c044fabd KH |
6419 | |
6420 | /* Given a section, search the header to find them. */ | |
6421 | ||
cb33740c | 6422 | unsigned int |
198beae2 | 6423 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6424 | { |
9c5bfbb7 | 6425 | const struct elf_backend_data *bed; |
91d6fa6a | 6426 | unsigned int sec_index; |
252b5132 | 6427 | |
9ad5cbcf AM |
6428 | if (elf_section_data (asect) != NULL |
6429 | && elf_section_data (asect)->this_idx != 0) | |
6430 | return elf_section_data (asect)->this_idx; | |
6431 | ||
6432 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6433 | sec_index = SHN_ABS; |
af746e92 | 6434 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6435 | sec_index = SHN_COMMON; |
af746e92 | 6436 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6437 | sec_index = SHN_UNDEF; |
af746e92 | 6438 | else |
91d6fa6a | 6439 | sec_index = SHN_BAD; |
252b5132 | 6440 | |
af746e92 | 6441 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6442 | if (bed->elf_backend_section_from_bfd_section) |
6443 | { | |
91d6fa6a | 6444 | int retval = sec_index; |
9ad5cbcf | 6445 | |
af746e92 AM |
6446 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6447 | return retval; | |
252b5132 RH |
6448 | } |
6449 | ||
91d6fa6a | 6450 | if (sec_index == SHN_BAD) |
af746e92 | 6451 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6452 | |
91d6fa6a | 6453 | return sec_index; |
252b5132 RH |
6454 | } |
6455 | ||
6456 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6457 | on error. */ | |
6458 | ||
6459 | int | |
217aa764 | 6460 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6461 | { |
6462 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6463 | int idx; | |
6464 | flagword flags = asym_ptr->flags; | |
6465 | ||
6466 | /* When gas creates relocations against local labels, it creates its | |
6467 | own symbol for the section, but does put the symbol into the | |
6468 | symbol chain, so udata is 0. When the linker is generating | |
6469 | relocatable output, this section symbol may be for one of the | |
6470 | input sections rather than the output section. */ | |
6471 | if (asym_ptr->udata.i == 0 | |
6472 | && (flags & BSF_SECTION_SYM) | |
6473 | && asym_ptr->section) | |
6474 | { | |
5372391b | 6475 | asection *sec; |
252b5132 RH |
6476 | int indx; |
6477 | ||
5372391b AM |
6478 | sec = asym_ptr->section; |
6479 | if (sec->owner != abfd && sec->output_section != NULL) | |
6480 | sec = sec->output_section; | |
6481 | if (sec->owner == abfd | |
6482 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6483 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6484 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6485 | } | |
6486 | ||
6487 | idx = asym_ptr->udata.i; | |
6488 | ||
6489 | if (idx == 0) | |
6490 | { | |
6491 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6492 | which is used in a relocation entry. */ |
4eca0228 | 6493 | _bfd_error_handler |
695344c0 | 6494 | /* xgettext:c-format */ |
871b3ab2 | 6495 | (_("%pB: symbol `%s' required but not present"), |
d003868e | 6496 | abfd, bfd_asymbol_name (asym_ptr)); |
252b5132 RH |
6497 | bfd_set_error (bfd_error_no_symbols); |
6498 | return -1; | |
6499 | } | |
6500 | ||
6501 | #if DEBUG & 4 | |
6502 | { | |
6503 | fprintf (stderr, | |
cd9af601 AM |
6504 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n", |
6505 | (long) asym_ptr, asym_ptr->name, idx, flags); | |
252b5132 RH |
6506 | fflush (stderr); |
6507 | } | |
6508 | #endif | |
6509 | ||
6510 | return idx; | |
6511 | } | |
6512 | ||
84d1d650 | 6513 | /* Rewrite program header information. */ |
252b5132 | 6514 | |
b34976b6 | 6515 | static bfd_boolean |
84d1d650 | 6516 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6517 | { |
b34976b6 AM |
6518 | Elf_Internal_Ehdr *iehdr; |
6519 | struct elf_segment_map *map; | |
6520 | struct elf_segment_map *map_first; | |
6521 | struct elf_segment_map **pointer_to_map; | |
6522 | Elf_Internal_Phdr *segment; | |
6523 | asection *section; | |
6524 | unsigned int i; | |
6525 | unsigned int num_segments; | |
6526 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6527 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6528 | bfd_vma maxpagesize; |
6529 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6530 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6531 | const struct elf_backend_data *bed; |
bc67d8a6 | 6532 | |
caf47ea6 | 6533 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6534 | iehdr = elf_elfheader (ibfd); |
6535 | ||
bc67d8a6 | 6536 | map_first = NULL; |
c044fabd | 6537 | pointer_to_map = &map_first; |
252b5132 RH |
6538 | |
6539 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6540 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6541 | ||
6542 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6543 | #define SEGMENT_END(segment, start) \ |
6544 | (start + (segment->p_memsz > segment->p_filesz \ | |
6545 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6546 | |
eecdbe52 JJ |
6547 | #define SECTION_SIZE(section, segment) \ |
6548 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6549 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6550 | ? section->size : 0) |
eecdbe52 | 6551 | |
b34976b6 | 6552 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6553 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
6554 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
6555 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 6556 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6557 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6558 | |
b34976b6 | 6559 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6560 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
6561 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
6562 | (section->lma >= base \ | |
eecdbe52 | 6563 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6564 | <= SEGMENT_END (segment, base))) |
252b5132 | 6565 | |
0efc80c8 L |
6566 | /* Handle PT_NOTE segment. */ |
6567 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6568 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6569 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6570 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6571 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6572 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6573 | |
0efc80c8 L |
6574 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6575 | etc. */ | |
6576 | #define IS_COREFILE_NOTE(p, s) \ | |
6577 | (IS_NOTE (p, s) \ | |
6578 | && bfd_get_format (ibfd) == bfd_core \ | |
6579 | && s->vma == 0 \ | |
6580 | && s->lma == 0) | |
6581 | ||
252b5132 RH |
6582 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6583 | linker, which generates a PT_INTERP section with p_vaddr and | |
6584 | p_memsz set to 0. */ | |
aecc8f8a AM |
6585 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6586 | (p->p_vaddr == 0 \ | |
6587 | && p->p_paddr == 0 \ | |
6588 | && p->p_memsz == 0 \ | |
6589 | && p->p_filesz > 0 \ | |
6590 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6591 | && s->size > 0 \ |
aecc8f8a | 6592 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6593 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6594 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6595 | |
bc67d8a6 NC |
6596 | /* Decide if the given section should be included in the given segment. |
6597 | A section will be included if: | |
f5ffc919 | 6598 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6599 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6600 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6601 | segment. |
bc67d8a6 | 6602 | 3. There is an output section associated with it, |
eecdbe52 | 6603 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6604 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6605 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6606 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6607 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6608 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 6609 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
6610 | ((((segment->p_paddr \ |
6611 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
6612 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
6613 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 6614 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6615 | && segment->p_type != PT_GNU_STACK \ |
6616 | && (segment->p_type != PT_TLS \ | |
6617 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6618 | && (segment->p_type == PT_LOAD \ | |
6619 | || segment->p_type == PT_TLS \ | |
6620 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6621 | && (segment->p_type != PT_DYNAMIC \ | |
6622 | || SECTION_SIZE (section, segment) > 0 \ | |
6623 | || (segment->p_paddr \ | |
6624 | ? segment->p_paddr != section->lma \ | |
6625 | : segment->p_vaddr != section->vma) \ | |
6626 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
6627 | == 0)) \ | |
0067a569 | 6628 | && !section->segment_mark) |
bc67d8a6 | 6629 | |
9f17e2a6 L |
6630 | /* If the output section of a section in the input segment is NULL, |
6631 | it is removed from the corresponding output segment. */ | |
6632 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
6633 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
6634 | && section->output_section != NULL) | |
6635 | ||
b34976b6 | 6636 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6637 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6638 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6639 | ||
6640 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6641 | their VMA address ranges and their LMA address ranges overlap. | |
6642 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6643 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6644 | to the same VMA range, but with the .data section mapped to a different | |
6645 | LMA. */ | |
aecc8f8a | 6646 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6647 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6648 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6649 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6650 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6651 | |
6652 | /* Initialise the segment mark field. */ | |
6653 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6654 | section->segment_mark = FALSE; |
bc67d8a6 | 6655 | |
5c44b38e AM |
6656 | /* The Solaris linker creates program headers in which all the |
6657 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6658 | file, we get confused. Check for this case, and if we find it | |
6659 | don't set the p_paddr_valid fields. */ | |
6660 | p_paddr_valid = FALSE; | |
6661 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6662 | i < num_segments; | |
6663 | i++, segment++) | |
6664 | if (segment->p_paddr != 0) | |
6665 | { | |
6666 | p_paddr_valid = TRUE; | |
6667 | break; | |
6668 | } | |
6669 | ||
252b5132 | 6670 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6671 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6672 | in the loadable segments. These can be created by weird |
aecc8f8a | 6673 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6674 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6675 | i < num_segments; | |
c044fabd | 6676 | i++, segment++) |
252b5132 | 6677 | { |
252b5132 | 6678 | unsigned int j; |
c044fabd | 6679 | Elf_Internal_Phdr *segment2; |
252b5132 | 6680 | |
aecc8f8a AM |
6681 | if (segment->p_type == PT_INTERP) |
6682 | for (section = ibfd->sections; section; section = section->next) | |
6683 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6684 | { | |
6685 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6686 | assignment code will work. */ |
aecc8f8a AM |
6687 | segment->p_vaddr = section->vma; |
6688 | break; | |
6689 | } | |
6690 | ||
bc67d8a6 | 6691 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6692 | { |
6693 | /* Remove PT_GNU_RELRO segment. */ | |
6694 | if (segment->p_type == PT_GNU_RELRO) | |
6695 | segment->p_type = PT_NULL; | |
6696 | continue; | |
6697 | } | |
c044fabd | 6698 | |
bc67d8a6 | 6699 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6700 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6701 | { |
6702 | bfd_signed_vma extra_length; | |
c044fabd | 6703 | |
bc67d8a6 | 6704 | if (segment2->p_type != PT_LOAD |
0067a569 | 6705 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6706 | continue; |
c044fabd | 6707 | |
bc67d8a6 NC |
6708 | /* Merge the two segments together. */ |
6709 | if (segment2->p_vaddr < segment->p_vaddr) | |
6710 | { | |
c044fabd | 6711 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6712 | SEGMENT. */ |
0067a569 AM |
6713 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6714 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6715 | |
bc67d8a6 NC |
6716 | if (extra_length > 0) |
6717 | { | |
0067a569 | 6718 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6719 | segment2->p_filesz += extra_length; |
6720 | } | |
c044fabd | 6721 | |
bc67d8a6 | 6722 | segment->p_type = PT_NULL; |
c044fabd | 6723 | |
bc67d8a6 NC |
6724 | /* Since we have deleted P we must restart the outer loop. */ |
6725 | i = 0; | |
6726 | segment = elf_tdata (ibfd)->phdr; | |
6727 | break; | |
6728 | } | |
6729 | else | |
6730 | { | |
c044fabd | 6731 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6732 | SEGMENT2. */ |
0067a569 AM |
6733 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6734 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6735 | |
bc67d8a6 NC |
6736 | if (extra_length > 0) |
6737 | { | |
0067a569 | 6738 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6739 | segment->p_filesz += extra_length; |
6740 | } | |
c044fabd | 6741 | |
bc67d8a6 NC |
6742 | segment2->p_type = PT_NULL; |
6743 | } | |
6744 | } | |
6745 | } | |
c044fabd | 6746 | |
bc67d8a6 NC |
6747 | /* The second scan attempts to assign sections to segments. */ |
6748 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6749 | i < num_segments; | |
0067a569 | 6750 | i++, segment++) |
bc67d8a6 | 6751 | { |
0067a569 AM |
6752 | unsigned int section_count; |
6753 | asection **sections; | |
6754 | asection *output_section; | |
6755 | unsigned int isec; | |
6756 | bfd_vma matching_lma; | |
6757 | bfd_vma suggested_lma; | |
6758 | unsigned int j; | |
dc810e39 | 6759 | bfd_size_type amt; |
0067a569 AM |
6760 | asection *first_section; |
6761 | bfd_boolean first_matching_lma; | |
6762 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
6763 | |
6764 | if (segment->p_type == PT_NULL) | |
6765 | continue; | |
c044fabd | 6766 | |
9f17e2a6 | 6767 | first_section = NULL; |
bc67d8a6 | 6768 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6769 | for (section = ibfd->sections, section_count = 0; |
6770 | section != NULL; | |
6771 | section = section->next) | |
9f17e2a6 L |
6772 | { |
6773 | /* Find the first section in the input segment, which may be | |
6774 | removed from the corresponding output segment. */ | |
6775 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6776 | { | |
6777 | if (first_section == NULL) | |
6778 | first_section = section; | |
6779 | if (section->output_section != NULL) | |
6780 | ++section_count; | |
6781 | } | |
6782 | } | |
811072d8 | 6783 | |
b5f852ea NC |
6784 | /* Allocate a segment map big enough to contain |
6785 | all of the sections we have selected. */ | |
dc810e39 AM |
6786 | amt = sizeof (struct elf_segment_map); |
6787 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6788 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6789 | if (map == NULL) |
b34976b6 | 6790 | return FALSE; |
252b5132 RH |
6791 | |
6792 | /* Initialise the fields of the segment map. Default to | |
6793 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
6794 | map->next = NULL; |
6795 | map->p_type = segment->p_type; | |
6796 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 6797 | map->p_flags_valid = 1; |
55d55ac7 | 6798 | |
9f17e2a6 L |
6799 | /* If the first section in the input segment is removed, there is |
6800 | no need to preserve segment physical address in the corresponding | |
6801 | output segment. */ | |
945c025a | 6802 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
6803 | { |
6804 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 6805 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 6806 | } |
252b5132 RH |
6807 | |
6808 | /* Determine if this segment contains the ELF file header | |
6809 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
6810 | map->includes_filehdr = (segment->p_offset == 0 |
6811 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 6812 | map->includes_phdrs = 0; |
252b5132 | 6813 | |
0067a569 | 6814 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 6815 | { |
bc67d8a6 NC |
6816 | map->includes_phdrs = |
6817 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6818 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
6819 | >= ((bfd_vma) iehdr->e_phoff |
6820 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 6821 | |
bc67d8a6 | 6822 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 6823 | phdr_included = TRUE; |
252b5132 RH |
6824 | } |
6825 | ||
bc67d8a6 | 6826 | if (section_count == 0) |
252b5132 RH |
6827 | { |
6828 | /* Special segments, such as the PT_PHDR segment, may contain | |
6829 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 | 6830 | something. They are allowed by the ELF spec however, so only |
07d6d2b8 | 6831 | a warning is produced. |
f98450c6 NC |
6832 | There is however the valid use case of embedded systems which |
6833 | have segments with p_filesz of 0 and a p_memsz > 0 to initialize | |
6834 | flash memory with zeros. No warning is shown for that case. */ | |
6835 | if (segment->p_type == PT_LOAD | |
6836 | && (segment->p_filesz > 0 || segment->p_memsz == 0)) | |
6837 | /* xgettext:c-format */ | |
871b3ab2 | 6838 | _bfd_error_handler (_("%pB: warning: Empty loadable segment detected" |
76cfced5 | 6839 | " at vaddr=%#Lx, is this intentional?"), |
f98450c6 | 6840 | ibfd, segment->p_vaddr); |
252b5132 | 6841 | |
bc67d8a6 | 6842 | map->count = 0; |
c044fabd KH |
6843 | *pointer_to_map = map; |
6844 | pointer_to_map = &map->next; | |
252b5132 RH |
6845 | |
6846 | continue; | |
6847 | } | |
6848 | ||
6849 | /* Now scan the sections in the input BFD again and attempt | |
6850 | to add their corresponding output sections to the segment map. | |
6851 | The problem here is how to handle an output section which has | |
6852 | been moved (ie had its LMA changed). There are four possibilities: | |
6853 | ||
6854 | 1. None of the sections have been moved. | |
6855 | In this case we can continue to use the segment LMA from the | |
6856 | input BFD. | |
6857 | ||
6858 | 2. All of the sections have been moved by the same amount. | |
6859 | In this case we can change the segment's LMA to match the LMA | |
6860 | of the first section. | |
6861 | ||
6862 | 3. Some of the sections have been moved, others have not. | |
6863 | In this case those sections which have not been moved can be | |
6864 | placed in the current segment which will have to have its size, | |
6865 | and possibly its LMA changed, and a new segment or segments will | |
6866 | have to be created to contain the other sections. | |
6867 | ||
b5f852ea | 6868 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6869 | In this case we can change the segment's LMA to match the LMA |
6870 | of the first section and we will have to create a new segment | |
6871 | or segments to contain the other sections. | |
6872 | ||
6873 | In order to save time, we allocate an array to hold the section | |
6874 | pointers that we are interested in. As these sections get assigned | |
6875 | to a segment, they are removed from this array. */ | |
6876 | ||
a50b1753 | 6877 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6878 | if (sections == NULL) |
b34976b6 | 6879 | return FALSE; |
252b5132 RH |
6880 | |
6881 | /* Step One: Scan for segment vs section LMA conflicts. | |
6882 | Also add the sections to the section array allocated above. | |
6883 | Also add the sections to the current segment. In the common | |
6884 | case, where the sections have not been moved, this means that | |
6885 | we have completely filled the segment, and there is nothing | |
6886 | more to do. */ | |
252b5132 | 6887 | isec = 0; |
72730e0c | 6888 | matching_lma = 0; |
252b5132 | 6889 | suggested_lma = 0; |
0067a569 AM |
6890 | first_matching_lma = TRUE; |
6891 | first_suggested_lma = TRUE; | |
252b5132 | 6892 | |
461c4b2e | 6893 | for (section = first_section, j = 0; |
bc67d8a6 NC |
6894 | section != NULL; |
6895 | section = section->next) | |
252b5132 | 6896 | { |
caf47ea6 | 6897 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6898 | { |
bc67d8a6 NC |
6899 | output_section = section->output_section; |
6900 | ||
0067a569 | 6901 | sections[j++] = section; |
252b5132 RH |
6902 | |
6903 | /* The Solaris native linker always sets p_paddr to 0. | |
6904 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6905 | correct value. Note - some backends require that |
6906 | p_paddr be left as zero. */ | |
5c44b38e | 6907 | if (!p_paddr_valid |
4455705d | 6908 | && segment->p_vaddr != 0 |
0067a569 | 6909 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6910 | && isec == 0 |
bc67d8a6 | 6911 | && output_section->lma != 0 |
0067a569 AM |
6912 | && output_section->vma == (segment->p_vaddr |
6913 | + (map->includes_filehdr | |
6914 | ? iehdr->e_ehsize | |
6915 | : 0) | |
6916 | + (map->includes_phdrs | |
6917 | ? (iehdr->e_phnum | |
6918 | * iehdr->e_phentsize) | |
6919 | : 0))) | |
bc67d8a6 | 6920 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6921 | |
6922 | /* Match up the physical address of the segment with the | |
6923 | LMA address of the output section. */ | |
bc67d8a6 | 6924 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6925 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6926 | || (bed->want_p_paddr_set_to_zero |
6927 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6928 | { |
0067a569 AM |
6929 | if (first_matching_lma || output_section->lma < matching_lma) |
6930 | { | |
6931 | matching_lma = output_section->lma; | |
6932 | first_matching_lma = FALSE; | |
6933 | } | |
252b5132 RH |
6934 | |
6935 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6936 | then it does not overlap any other section within that |
252b5132 | 6937 | segment. */ |
0067a569 AM |
6938 | map->sections[isec++] = output_section; |
6939 | } | |
6940 | else if (first_suggested_lma) | |
6941 | { | |
6942 | suggested_lma = output_section->lma; | |
6943 | first_suggested_lma = FALSE; | |
252b5132 | 6944 | } |
147d51c2 L |
6945 | |
6946 | if (j == section_count) | |
6947 | break; | |
252b5132 RH |
6948 | } |
6949 | } | |
6950 | ||
bc67d8a6 | 6951 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6952 | |
6953 | /* Step Two: Adjust the physical address of the current segment, | |
6954 | if necessary. */ | |
bc67d8a6 | 6955 | if (isec == section_count) |
252b5132 RH |
6956 | { |
6957 | /* All of the sections fitted within the segment as currently | |
6958 | specified. This is the default case. Add the segment to | |
6959 | the list of built segments and carry on to process the next | |
6960 | program header in the input BFD. */ | |
bc67d8a6 | 6961 | map->count = section_count; |
c044fabd KH |
6962 | *pointer_to_map = map; |
6963 | pointer_to_map = &map->next; | |
08a40648 | 6964 | |
5c44b38e AM |
6965 | if (p_paddr_valid |
6966 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6967 | && matching_lma != map->p_paddr |
5c44b38e AM |
6968 | && !map->includes_filehdr |
6969 | && !map->includes_phdrs) | |
3271a814 NS |
6970 | /* There is some padding before the first section in the |
6971 | segment. So, we must account for that in the output | |
6972 | segment's vma. */ | |
6973 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6974 | |
252b5132 RH |
6975 | free (sections); |
6976 | continue; | |
6977 | } | |
252b5132 RH |
6978 | else |
6979 | { | |
0067a569 | 6980 | if (!first_matching_lma) |
72730e0c AM |
6981 | { |
6982 | /* At least one section fits inside the current segment. | |
6983 | Keep it, but modify its physical address to match the | |
6984 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6985 | map->p_paddr = matching_lma; |
72730e0c AM |
6986 | } |
6987 | else | |
6988 | { | |
6989 | /* None of the sections fitted inside the current segment. | |
6990 | Change the current segment's physical address to match | |
6991 | the LMA of the first section. */ | |
bc67d8a6 | 6992 | map->p_paddr = suggested_lma; |
72730e0c AM |
6993 | } |
6994 | ||
bc67d8a6 NC |
6995 | /* Offset the segment physical address from the lma |
6996 | to allow for space taken up by elf headers. */ | |
6997 | if (map->includes_filehdr) | |
010c8431 AM |
6998 | { |
6999 | if (map->p_paddr >= iehdr->e_ehsize) | |
7000 | map->p_paddr -= iehdr->e_ehsize; | |
7001 | else | |
7002 | { | |
7003 | map->includes_filehdr = FALSE; | |
7004 | map->includes_phdrs = FALSE; | |
7005 | } | |
7006 | } | |
252b5132 | 7007 | |
bc67d8a6 NC |
7008 | if (map->includes_phdrs) |
7009 | { | |
010c8431 AM |
7010 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
7011 | { | |
7012 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
7013 | ||
7014 | /* iehdr->e_phnum is just an estimate of the number | |
7015 | of program headers that we will need. Make a note | |
7016 | here of the number we used and the segment we chose | |
7017 | to hold these headers, so that we can adjust the | |
7018 | offset when we know the correct value. */ | |
7019 | phdr_adjust_num = iehdr->e_phnum; | |
7020 | phdr_adjust_seg = map; | |
7021 | } | |
7022 | else | |
7023 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 7024 | } |
252b5132 RH |
7025 | } |
7026 | ||
7027 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 7028 | those that fit to the current segment and removing them from the |
252b5132 RH |
7029 | sections array; but making sure not to leave large gaps. Once all |
7030 | possible sections have been assigned to the current segment it is | |
7031 | added to the list of built segments and if sections still remain | |
7032 | to be assigned, a new segment is constructed before repeating | |
7033 | the loop. */ | |
7034 | isec = 0; | |
7035 | do | |
7036 | { | |
bc67d8a6 | 7037 | map->count = 0; |
252b5132 | 7038 | suggested_lma = 0; |
0067a569 | 7039 | first_suggested_lma = TRUE; |
252b5132 RH |
7040 | |
7041 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 7042 | for (j = 0; j < section_count; j++) |
252b5132 | 7043 | { |
bc67d8a6 | 7044 | section = sections[j]; |
252b5132 | 7045 | |
bc67d8a6 | 7046 | if (section == NULL) |
252b5132 RH |
7047 | continue; |
7048 | ||
bc67d8a6 | 7049 | output_section = section->output_section; |
252b5132 | 7050 | |
bc67d8a6 | 7051 | BFD_ASSERT (output_section != NULL); |
c044fabd | 7052 | |
bc67d8a6 NC |
7053 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
7054 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 7055 | { |
bc67d8a6 | 7056 | if (map->count == 0) |
252b5132 RH |
7057 | { |
7058 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
7059 | the beginning of the segment, then something is |
7060 | wrong. */ | |
0067a569 AM |
7061 | if (output_section->lma |
7062 | != (map->p_paddr | |
7063 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
7064 | + (map->includes_phdrs | |
7065 | ? iehdr->e_phnum * iehdr->e_phentsize | |
7066 | : 0))) | |
252b5132 RH |
7067 | abort (); |
7068 | } | |
7069 | else | |
7070 | { | |
0067a569 | 7071 | asection *prev_sec; |
252b5132 | 7072 | |
bc67d8a6 | 7073 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
7074 | |
7075 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
7076 | and the start of this section is more than |
7077 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 7078 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 7079 | maxpagesize) |
caf47ea6 | 7080 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 7081 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 7082 | > output_section->lma)) |
252b5132 | 7083 | { |
0067a569 AM |
7084 | if (first_suggested_lma) |
7085 | { | |
7086 | suggested_lma = output_section->lma; | |
7087 | first_suggested_lma = FALSE; | |
7088 | } | |
252b5132 RH |
7089 | |
7090 | continue; | |
7091 | } | |
7092 | } | |
7093 | ||
bc67d8a6 | 7094 | map->sections[map->count++] = output_section; |
252b5132 RH |
7095 | ++isec; |
7096 | sections[j] = NULL; | |
b34976b6 | 7097 | section->segment_mark = TRUE; |
252b5132 | 7098 | } |
0067a569 AM |
7099 | else if (first_suggested_lma) |
7100 | { | |
7101 | suggested_lma = output_section->lma; | |
7102 | first_suggested_lma = FALSE; | |
7103 | } | |
252b5132 RH |
7104 | } |
7105 | ||
bc67d8a6 | 7106 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
7107 | |
7108 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
7109 | *pointer_to_map = map; |
7110 | pointer_to_map = &map->next; | |
252b5132 | 7111 | |
bc67d8a6 | 7112 | if (isec < section_count) |
252b5132 RH |
7113 | { |
7114 | /* We still have not allocated all of the sections to | |
7115 | segments. Create a new segment here, initialise it | |
7116 | and carry on looping. */ | |
dc810e39 AM |
7117 | amt = sizeof (struct elf_segment_map); |
7118 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 7119 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7120 | if (map == NULL) |
5ed6aba4 NC |
7121 | { |
7122 | free (sections); | |
7123 | return FALSE; | |
7124 | } | |
252b5132 RH |
7125 | |
7126 | /* Initialise the fields of the segment map. Set the physical | |
7127 | physical address to the LMA of the first section that has | |
7128 | not yet been assigned. */ | |
0067a569 AM |
7129 | map->next = NULL; |
7130 | map->p_type = segment->p_type; | |
7131 | map->p_flags = segment->p_flags; | |
7132 | map->p_flags_valid = 1; | |
7133 | map->p_paddr = suggested_lma; | |
5c44b38e | 7134 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 7135 | map->includes_filehdr = 0; |
0067a569 | 7136 | map->includes_phdrs = 0; |
252b5132 RH |
7137 | } |
7138 | } | |
bc67d8a6 | 7139 | while (isec < section_count); |
252b5132 RH |
7140 | |
7141 | free (sections); | |
7142 | } | |
7143 | ||
12bd6957 | 7144 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
7145 | |
7146 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 7147 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
7148 | the offset if necessary. */ |
7149 | if (phdr_adjust_seg != NULL) | |
7150 | { | |
7151 | unsigned int count; | |
c044fabd | 7152 | |
bc67d8a6 | 7153 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 7154 | count++; |
252b5132 | 7155 | |
bc67d8a6 NC |
7156 | if (count > phdr_adjust_num) |
7157 | phdr_adjust_seg->p_paddr | |
7158 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
7159 | } | |
c044fabd | 7160 | |
bc67d8a6 | 7161 | #undef SEGMENT_END |
eecdbe52 | 7162 | #undef SECTION_SIZE |
bc67d8a6 NC |
7163 | #undef IS_CONTAINED_BY_VMA |
7164 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 7165 | #undef IS_NOTE |
252b5132 | 7166 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 7167 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 7168 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
7169 | #undef INCLUDE_SECTION_IN_SEGMENT |
7170 | #undef SEGMENT_AFTER_SEGMENT | |
7171 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 7172 | return TRUE; |
252b5132 RH |
7173 | } |
7174 | ||
84d1d650 L |
7175 | /* Copy ELF program header information. */ |
7176 | ||
7177 | static bfd_boolean | |
7178 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
7179 | { | |
7180 | Elf_Internal_Ehdr *iehdr; | |
7181 | struct elf_segment_map *map; | |
7182 | struct elf_segment_map *map_first; | |
7183 | struct elf_segment_map **pointer_to_map; | |
7184 | Elf_Internal_Phdr *segment; | |
7185 | unsigned int i; | |
7186 | unsigned int num_segments; | |
7187 | bfd_boolean phdr_included = FALSE; | |
88967714 | 7188 | bfd_boolean p_paddr_valid; |
84d1d650 L |
7189 | |
7190 | iehdr = elf_elfheader (ibfd); | |
7191 | ||
7192 | map_first = NULL; | |
7193 | pointer_to_map = &map_first; | |
7194 | ||
88967714 AM |
7195 | /* If all the segment p_paddr fields are zero, don't set |
7196 | map->p_paddr_valid. */ | |
7197 | p_paddr_valid = FALSE; | |
84d1d650 | 7198 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
7199 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7200 | i < num_segments; | |
7201 | i++, segment++) | |
7202 | if (segment->p_paddr != 0) | |
7203 | { | |
7204 | p_paddr_valid = TRUE; | |
7205 | break; | |
7206 | } | |
7207 | ||
84d1d650 L |
7208 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7209 | i < num_segments; | |
7210 | i++, segment++) | |
7211 | { | |
7212 | asection *section; | |
7213 | unsigned int section_count; | |
7214 | bfd_size_type amt; | |
7215 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 7216 | asection *first_section = NULL; |
a76e6f2f | 7217 | asection *lowest_section; |
84d1d650 | 7218 | |
84d1d650 L |
7219 | /* Compute how many sections are in this segment. */ |
7220 | for (section = ibfd->sections, section_count = 0; | |
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)) |
3271a814 | 7226 | { |
a76e6f2f AM |
7227 | if (first_section == NULL) |
7228 | first_section = section; | |
3271a814 NS |
7229 | section_count++; |
7230 | } | |
84d1d650 L |
7231 | } |
7232 | ||
7233 | /* Allocate a segment map big enough to contain | |
7234 | all of the sections we have selected. */ | |
7235 | amt = sizeof (struct elf_segment_map); | |
7236 | if (section_count != 0) | |
7237 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 7238 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
7239 | if (map == NULL) |
7240 | return FALSE; | |
7241 | ||
7242 | /* Initialize the fields of the output segment map with the | |
7243 | input segment. */ | |
7244 | map->next = NULL; | |
7245 | map->p_type = segment->p_type; | |
7246 | map->p_flags = segment->p_flags; | |
7247 | map->p_flags_valid = 1; | |
7248 | map->p_paddr = segment->p_paddr; | |
88967714 | 7249 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
7250 | map->p_align = segment->p_align; |
7251 | map->p_align_valid = 1; | |
3271a814 | 7252 | map->p_vaddr_offset = 0; |
84d1d650 | 7253 | |
04c3a755 NS |
7254 | if (map->p_type == PT_GNU_RELRO |
7255 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
7256 | { |
7257 | /* The PT_GNU_RELRO segment may contain the first a few | |
7258 | bytes in the .got.plt section even if the whole .got.plt | |
7259 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
7260 | change the size of the PT_GNU_RELRO segment. |
7261 | Similarly, PT_GNU_STACK size is significant on uclinux | |
7262 | systems. */ | |
9433b9b1 | 7263 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
7264 | map->p_size_valid = 1; |
7265 | } | |
7266 | ||
84d1d650 L |
7267 | /* Determine if this segment contains the ELF file header |
7268 | and if it contains the program headers themselves. */ | |
7269 | map->includes_filehdr = (segment->p_offset == 0 | |
7270 | && segment->p_filesz >= iehdr->e_ehsize); | |
7271 | ||
7272 | map->includes_phdrs = 0; | |
7273 | if (! phdr_included || segment->p_type != PT_LOAD) | |
7274 | { | |
7275 | map->includes_phdrs = | |
7276 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7277 | && (segment->p_offset + segment->p_filesz | |
7278 | >= ((bfd_vma) iehdr->e_phoff | |
7279 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
7280 | ||
7281 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
7282 | phdr_included = TRUE; | |
7283 | } | |
7284 | ||
bbefd0a9 | 7285 | lowest_section = NULL; |
84d1d650 L |
7286 | if (section_count != 0) |
7287 | { | |
7288 | unsigned int isec = 0; | |
7289 | ||
53020534 | 7290 | for (section = first_section; |
84d1d650 L |
7291 | section != NULL; |
7292 | section = section->next) | |
7293 | { | |
7294 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7295 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
7296 | { |
7297 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
7298 | if ((section->flags & SEC_ALLOC) != 0) |
7299 | { | |
7300 | bfd_vma seg_off; | |
7301 | ||
bbefd0a9 AM |
7302 | if (lowest_section == NULL |
7303 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
7304 | lowest_section = section; |
7305 | ||
a76e6f2f AM |
7306 | /* Section lmas are set up from PT_LOAD header |
7307 | p_paddr in _bfd_elf_make_section_from_shdr. | |
7308 | If this header has a p_paddr that disagrees | |
7309 | with the section lma, flag the p_paddr as | |
7310 | invalid. */ | |
7311 | if ((section->flags & SEC_LOAD) != 0) | |
7312 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
7313 | else | |
7314 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
7315 | if (section->lma - segment->p_paddr != seg_off) | |
7316 | map->p_paddr_valid = FALSE; | |
7317 | } | |
53020534 L |
7318 | if (isec == section_count) |
7319 | break; | |
7320 | } | |
84d1d650 L |
7321 | } |
7322 | } | |
7323 | ||
a76e6f2f AM |
7324 | if (map->includes_filehdr && lowest_section != NULL) |
7325 | /* We need to keep the space used by the headers fixed. */ | |
7326 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 7327 | |
a76e6f2f AM |
7328 | if (!map->includes_phdrs |
7329 | && !map->includes_filehdr | |
7330 | && map->p_paddr_valid) | |
7331 | /* There is some other padding before the first section. */ | |
7332 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
7333 | - segment->p_paddr); | |
7334 | ||
84d1d650 L |
7335 | map->count = section_count; |
7336 | *pointer_to_map = map; | |
7337 | pointer_to_map = &map->next; | |
7338 | } | |
7339 | ||
12bd6957 | 7340 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
7341 | return TRUE; |
7342 | } | |
7343 | ||
7344 | /* Copy private BFD data. This copies or rewrites ELF program header | |
7345 | information. */ | |
7346 | ||
7347 | static bfd_boolean | |
7348 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
7349 | { | |
84d1d650 L |
7350 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
7351 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7352 | return TRUE; | |
7353 | ||
7354 | if (elf_tdata (ibfd)->phdr == NULL) | |
7355 | return TRUE; | |
7356 | ||
7357 | if (ibfd->xvec == obfd->xvec) | |
7358 | { | |
cb3ff1e5 NC |
7359 | /* Check to see if any sections in the input BFD |
7360 | covered by ELF program header have changed. */ | |
d55ce4e2 | 7361 | Elf_Internal_Phdr *segment; |
84d1d650 L |
7362 | asection *section, *osec; |
7363 | unsigned int i, num_segments; | |
7364 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
7365 | const struct elf_backend_data *bed; |
7366 | ||
7367 | bed = get_elf_backend_data (ibfd); | |
7368 | ||
7369 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
7370 | if (bed->want_p_paddr_set_to_zero) | |
7371 | goto rewrite; | |
84d1d650 L |
7372 | |
7373 | /* Initialize the segment mark field. */ | |
7374 | for (section = obfd->sections; section != NULL; | |
7375 | section = section->next) | |
7376 | section->segment_mark = FALSE; | |
7377 | ||
7378 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
7379 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7380 | i < num_segments; | |
7381 | i++, segment++) | |
7382 | { | |
5f6999aa NC |
7383 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7384 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7385 | which severly confuses things, so always regenerate the segment | |
7386 | map in this case. */ | |
7387 | if (segment->p_paddr == 0 | |
7388 | && segment->p_memsz == 0 | |
7389 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7390 | goto rewrite; |
5f6999aa | 7391 | |
84d1d650 L |
7392 | for (section = ibfd->sections; |
7393 | section != NULL; section = section->next) | |
7394 | { | |
7395 | /* We mark the output section so that we know it comes | |
7396 | from the input BFD. */ | |
7397 | osec = section->output_section; | |
7398 | if (osec) | |
7399 | osec->segment_mark = TRUE; | |
7400 | ||
7401 | /* Check if this section is covered by the segment. */ | |
7402 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7403 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7404 | { |
7405 | /* FIXME: Check if its output section is changed or | |
7406 | removed. What else do we need to check? */ | |
7407 | if (osec == NULL | |
7408 | || section->flags != osec->flags | |
7409 | || section->lma != osec->lma | |
7410 | || section->vma != osec->vma | |
7411 | || section->size != osec->size | |
7412 | || section->rawsize != osec->rawsize | |
7413 | || section->alignment_power != osec->alignment_power) | |
7414 | goto rewrite; | |
7415 | } | |
7416 | } | |
7417 | } | |
7418 | ||
cb3ff1e5 | 7419 | /* Check to see if any output section do not come from the |
84d1d650 L |
7420 | input BFD. */ |
7421 | for (section = obfd->sections; section != NULL; | |
7422 | section = section->next) | |
7423 | { | |
535b785f | 7424 | if (!section->segment_mark) |
84d1d650 L |
7425 | goto rewrite; |
7426 | else | |
7427 | section->segment_mark = FALSE; | |
7428 | } | |
7429 | ||
7430 | return copy_elf_program_header (ibfd, obfd); | |
7431 | } | |
7432 | ||
7433 | rewrite: | |
f1d85785 L |
7434 | if (ibfd->xvec == obfd->xvec) |
7435 | { | |
7436 | /* When rewriting program header, set the output maxpagesize to | |
7437 | the maximum alignment of input PT_LOAD segments. */ | |
7438 | Elf_Internal_Phdr *segment; | |
7439 | unsigned int i; | |
7440 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7441 | bfd_vma maxpagesize = 0; | |
7442 | ||
7443 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7444 | i < num_segments; | |
7445 | i++, segment++) | |
7446 | if (segment->p_type == PT_LOAD | |
7447 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7448 | { |
7449 | /* PR 17512: file: f17299af. */ | |
7450 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
695344c0 | 7451 | /* xgettext:c-format */ |
871b3ab2 | 7452 | _bfd_error_handler (_("%pB: warning: segment alignment of %#Lx" |
76cfced5 AM |
7453 | " is too large"), |
7454 | ibfd, segment->p_align); | |
c86934ce NC |
7455 | else |
7456 | maxpagesize = segment->p_align; | |
7457 | } | |
f1d85785 L |
7458 | |
7459 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7460 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7461 | } | |
7462 | ||
84d1d650 L |
7463 | return rewrite_elf_program_header (ibfd, obfd); |
7464 | } | |
7465 | ||
ccd2ec6a L |
7466 | /* Initialize private output section information from input section. */ |
7467 | ||
7468 | bfd_boolean | |
7469 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7470 | asection *isec, | |
7471 | bfd *obfd, | |
7472 | asection *osec, | |
7473 | struct bfd_link_info *link_info) | |
7474 | ||
7475 | { | |
7476 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7477 | bfd_boolean final_link = (link_info != NULL |
7478 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7479 | |
7480 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7481 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7482 | return TRUE; | |
7483 | ||
ba85c43e NC |
7484 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7485 | ||
dfa7b0b8 AM |
7486 | /* For objcopy and relocatable link, don't copy the output ELF |
7487 | section type from input if the output BFD section flags have been | |
7488 | set to something different. For a final link allow some flags | |
7489 | that the linker clears to differ. */ | |
42bb2e33 | 7490 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7491 | && (osec->flags == isec->flags |
7492 | || (final_link | |
7493 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7494 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7495 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7496 | |
7497 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
7498 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7499 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a | 7500 | |
a91e1603 L |
7501 | /* Copy sh_info from input for mbind section. */ |
7502 | if (elf_section_flags (isec) & SHF_GNU_MBIND) | |
7503 | elf_section_data (osec)->this_hdr.sh_info | |
7504 | = elf_section_data (isec)->this_hdr.sh_info; | |
7505 | ||
ccd2ec6a L |
7506 | /* Set things up for objcopy and relocatable link. The output |
7507 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7508 | to the input group members. Ignore linker created group section. | |
7509 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
7bdf4127 AB |
7510 | if ((link_info == NULL |
7511 | || !link_info->resolve_section_groups) | |
7512 | && (elf_sec_group (isec) == NULL | |
7513 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) | |
ccd2ec6a | 7514 | { |
7bdf4127 AB |
7515 | if (elf_section_flags (isec) & SHF_GROUP) |
7516 | elf_section_flags (osec) |= SHF_GROUP; | |
7517 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
7518 | elf_section_data (osec)->group = elf_section_data (isec)->group; | |
ccd2ec6a L |
7519 | } |
7520 | ||
7bdf4127 AB |
7521 | /* If not decompress, preserve SHF_COMPRESSED. */ |
7522 | if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) | |
7523 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7524 | & SHF_COMPRESSED); | |
7525 | ||
ccd2ec6a L |
7526 | ihdr = &elf_section_data (isec)->this_hdr; |
7527 | ||
7528 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7529 | don't use the output section of the linked-to section since it | |
7530 | may be NULL at this point. */ | |
7531 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7532 | { | |
7533 | ohdr = &elf_section_data (osec)->this_hdr; | |
7534 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7535 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7536 | } | |
7537 | ||
7538 | osec->use_rela_p = isec->use_rela_p; | |
7539 | ||
7540 | return TRUE; | |
7541 | } | |
7542 | ||
252b5132 RH |
7543 | /* Copy private section information. This copies over the entsize |
7544 | field, and sometimes the info field. */ | |
7545 | ||
b34976b6 | 7546 | bfd_boolean |
217aa764 AM |
7547 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7548 | asection *isec, | |
7549 | bfd *obfd, | |
7550 | asection *osec) | |
252b5132 RH |
7551 | { |
7552 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7553 | ||
7554 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7555 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7556 | return TRUE; |
252b5132 | 7557 | |
252b5132 RH |
7558 | ihdr = &elf_section_data (isec)->this_hdr; |
7559 | ohdr = &elf_section_data (osec)->this_hdr; | |
7560 | ||
7561 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7562 | ||
7563 | if (ihdr->sh_type == SHT_SYMTAB | |
7564 | || ihdr->sh_type == SHT_DYNSYM | |
7565 | || ihdr->sh_type == SHT_GNU_verneed | |
7566 | || ihdr->sh_type == SHT_GNU_verdef) | |
7567 | ohdr->sh_info = ihdr->sh_info; | |
7568 | ||
ccd2ec6a L |
7569 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7570 | NULL); | |
252b5132 RH |
7571 | } |
7572 | ||
d0bf826b AM |
7573 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7574 | necessary if we are removing either the SHT_GROUP section or any of | |
7575 | the group member sections. DISCARDED is the value that a section's | |
7576 | output_section has if the section will be discarded, NULL when this | |
7577 | function is called from objcopy, bfd_abs_section_ptr when called | |
7578 | from the linker. */ | |
80fccad2 BW |
7579 | |
7580 | bfd_boolean | |
d0bf826b | 7581 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7582 | { |
30288845 AM |
7583 | asection *isec; |
7584 | ||
30288845 | 7585 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7586 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7587 | { |
7588 | asection *first = elf_next_in_group (isec); | |
7589 | asection *s = first; | |
d0bf826b AM |
7590 | bfd_size_type removed = 0; |
7591 | ||
30288845 AM |
7592 | while (s != NULL) |
7593 | { | |
415f38a6 AM |
7594 | /* If this member section is being output but the |
7595 | SHT_GROUP section is not, then clear the group info | |
7596 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7597 | if (s->output_section != discarded |
7598 | && isec->output_section == discarded) | |
30288845 AM |
7599 | { |
7600 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7601 | elf_group_name (s->output_section) = NULL; | |
7602 | } | |
415f38a6 AM |
7603 | /* Conversely, if the member section is not being output |
7604 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7605 | else if (s->output_section == discarded |
7606 | && isec->output_section != discarded) | |
6e5e9d58 AM |
7607 | { |
7608 | struct bfd_elf_section_data *elf_sec = elf_section_data (s); | |
7609 | removed += 4; | |
7610 | if (elf_sec->rel.hdr != NULL | |
7611 | && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0) | |
7612 | removed += 4; | |
7613 | if (elf_sec->rela.hdr != NULL | |
7614 | && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0) | |
7615 | removed += 4; | |
7616 | } | |
30288845 AM |
7617 | s = elf_next_in_group (s); |
7618 | if (s == first) | |
7619 | break; | |
7620 | } | |
d0bf826b AM |
7621 | if (removed != 0) |
7622 | { | |
7623 | if (discarded != NULL) | |
7624 | { | |
7625 | /* If we've been called for ld -r, then we need to | |
6e5e9d58 | 7626 | adjust the input section size. */ |
d0bf826b AM |
7627 | if (isec->rawsize == 0) |
7628 | isec->rawsize = isec->size; | |
7629 | isec->size = isec->rawsize - removed; | |
6e5e9d58 AM |
7630 | if (isec->size <= 4) |
7631 | { | |
7632 | isec->size = 0; | |
7633 | isec->flags |= SEC_EXCLUDE; | |
7634 | } | |
d0bf826b AM |
7635 | } |
7636 | else | |
7637 | { | |
7638 | /* Adjust the output section size when called from | |
7639 | objcopy. */ | |
7640 | isec->output_section->size -= removed; | |
6e5e9d58 AM |
7641 | if (isec->output_section->size <= 4) |
7642 | { | |
7643 | isec->output_section->size = 0; | |
7644 | isec->output_section->flags |= SEC_EXCLUDE; | |
7645 | } | |
d0bf826b AM |
7646 | } |
7647 | } | |
30288845 AM |
7648 | } |
7649 | ||
80fccad2 BW |
7650 | return TRUE; |
7651 | } | |
7652 | ||
d0bf826b AM |
7653 | /* Copy private header information. */ |
7654 | ||
7655 | bfd_boolean | |
7656 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7657 | { | |
7658 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7659 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7660 | return TRUE; | |
7661 | ||
7662 | /* Copy over private BFD data if it has not already been copied. | |
7663 | This must be done here, rather than in the copy_private_bfd_data | |
7664 | entry point, because the latter is called after the section | |
7665 | contents have been set, which means that the program headers have | |
7666 | already been worked out. */ | |
12bd6957 | 7667 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7668 | { |
7669 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7670 | return FALSE; | |
7671 | } | |
7672 | ||
7673 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7674 | } | |
7675 | ||
252b5132 RH |
7676 | /* Copy private symbol information. If this symbol is in a section |
7677 | which we did not map into a BFD section, try to map the section | |
7678 | index correctly. We use special macro definitions for the mapped | |
7679 | section indices; these definitions are interpreted by the | |
7680 | swap_out_syms function. */ | |
7681 | ||
9ad5cbcf AM |
7682 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7683 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7684 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7685 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7686 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7687 | |
b34976b6 | 7688 | bfd_boolean |
217aa764 AM |
7689 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7690 | asymbol *isymarg, | |
7691 | bfd *obfd, | |
7692 | asymbol *osymarg) | |
252b5132 RH |
7693 | { |
7694 | elf_symbol_type *isym, *osym; | |
7695 | ||
7696 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7697 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7698 | return TRUE; |
252b5132 RH |
7699 | |
7700 | isym = elf_symbol_from (ibfd, isymarg); | |
7701 | osym = elf_symbol_from (obfd, osymarg); | |
7702 | ||
7703 | if (isym != NULL | |
8424d8f5 | 7704 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
7705 | && osym != NULL |
7706 | && bfd_is_abs_section (isym->symbol.section)) | |
7707 | { | |
7708 | unsigned int shndx; | |
7709 | ||
7710 | shndx = isym->internal_elf_sym.st_shndx; | |
7711 | if (shndx == elf_onesymtab (ibfd)) | |
7712 | shndx = MAP_ONESYMTAB; | |
7713 | else if (shndx == elf_dynsymtab (ibfd)) | |
7714 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 7715 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 7716 | shndx = MAP_STRTAB; |
12bd6957 | 7717 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 7718 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 7719 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 7720 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
7721 | osym->internal_elf_sym.st_shndx = shndx; |
7722 | } | |
7723 | ||
b34976b6 | 7724 | return TRUE; |
252b5132 RH |
7725 | } |
7726 | ||
7727 | /* Swap out the symbols. */ | |
7728 | ||
b34976b6 | 7729 | static bfd_boolean |
217aa764 | 7730 | swap_out_syms (bfd *abfd, |
ef10c3ac | 7731 | struct elf_strtab_hash **sttp, |
217aa764 | 7732 | int relocatable_p) |
252b5132 | 7733 | { |
9c5bfbb7 | 7734 | const struct elf_backend_data *bed; |
079e9a2f AM |
7735 | int symcount; |
7736 | asymbol **syms; | |
ef10c3ac | 7737 | struct elf_strtab_hash *stt; |
079e9a2f | 7738 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 7739 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7740 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 7741 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
7742 | bfd_byte *outbound_syms; |
7743 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
7744 | unsigned long outbound_syms_index; |
7745 | unsigned long outbound_shndx_index; | |
079e9a2f | 7746 | int idx; |
12bd6957 | 7747 | unsigned int num_locals; |
079e9a2f | 7748 | bfd_size_type amt; |
174fd7f9 | 7749 | bfd_boolean name_local_sections; |
252b5132 | 7750 | |
12bd6957 | 7751 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7752 | return FALSE; |
252b5132 | 7753 | |
c044fabd | 7754 | /* Dump out the symtabs. */ |
ef10c3ac | 7755 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 7756 | if (stt == NULL) |
b34976b6 | 7757 | return FALSE; |
252b5132 | 7758 | |
079e9a2f AM |
7759 | bed = get_elf_backend_data (abfd); |
7760 | symcount = bfd_get_symcount (abfd); | |
7761 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7762 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7763 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7764 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7765 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7766 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7767 | |
7768 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7769 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7770 | ||
ef10c3ac L |
7771 | /* Allocate buffer to swap out the .strtab section. */ |
7772 | symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1) | |
7773 | * sizeof (*symstrtab)); | |
7774 | if (symstrtab == NULL) | |
7775 | { | |
7776 | _bfd_elf_strtab_free (stt); | |
7777 | return FALSE; | |
7778 | } | |
7779 | ||
a50b1753 | 7780 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
07d6d2b8 | 7781 | bed->s->sizeof_sym); |
079e9a2f | 7782 | if (outbound_syms == NULL) |
5ed6aba4 | 7783 | { |
ef10c3ac L |
7784 | error_return: |
7785 | _bfd_elf_strtab_free (stt); | |
7786 | free (symstrtab); | |
5ed6aba4 NC |
7787 | return FALSE; |
7788 | } | |
217aa764 | 7789 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 7790 | outbound_syms_index = 0; |
252b5132 | 7791 | |
9ad5cbcf | 7792 | outbound_shndx = NULL; |
ef10c3ac | 7793 | outbound_shndx_index = 0; |
6a40cf0c NC |
7794 | |
7795 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 7796 | { |
6a40cf0c NC |
7797 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
7798 | if (symtab_shndx_hdr->sh_name != 0) | |
7799 | { | |
7800 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
7801 | outbound_shndx = (bfd_byte *) | |
7802 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
7803 | if (outbound_shndx == NULL) | |
7804 | goto error_return; | |
5ed6aba4 | 7805 | |
6a40cf0c NC |
7806 | symtab_shndx_hdr->contents = outbound_shndx; |
7807 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7808 | symtab_shndx_hdr->sh_size = amt; | |
7809 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7810 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7811 | } | |
7812 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
7813 | } |
7814 | ||
589e6347 | 7815 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
7816 | { |
7817 | /* Fill in zeroth symbol and swap it out. */ | |
7818 | Elf_Internal_Sym sym; | |
7819 | sym.st_name = 0; | |
7820 | sym.st_value = 0; | |
7821 | sym.st_size = 0; | |
7822 | sym.st_info = 0; | |
7823 | sym.st_other = 0; | |
7824 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 7825 | sym.st_target_internal = 0; |
ef10c3ac L |
7826 | symstrtab[0].sym = sym; |
7827 | symstrtab[0].dest_index = outbound_syms_index; | |
7828 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
7829 | outbound_syms_index++; | |
9ad5cbcf | 7830 | if (outbound_shndx != NULL) |
ef10c3ac | 7831 | outbound_shndx_index++; |
079e9a2f | 7832 | } |
252b5132 | 7833 | |
174fd7f9 RS |
7834 | name_local_sections |
7835 | = (bed->elf_backend_name_local_section_symbols | |
7836 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
7837 | ||
079e9a2f | 7838 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 7839 | for (idx = 0; idx < symcount;) |
252b5132 | 7840 | { |
252b5132 | 7841 | Elf_Internal_Sym sym; |
079e9a2f AM |
7842 | bfd_vma value = syms[idx]->value; |
7843 | elf_symbol_type *type_ptr; | |
7844 | flagword flags = syms[idx]->flags; | |
7845 | int type; | |
252b5132 | 7846 | |
174fd7f9 RS |
7847 | if (!name_local_sections |
7848 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
7849 | { |
7850 | /* Local section symbols have no name. */ | |
ef10c3ac | 7851 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
7852 | } |
7853 | else | |
7854 | { | |
ef10c3ac L |
7855 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
7856 | to get the final offset for st_name. */ | |
7857 | sym.st_name | |
7858 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
7859 | FALSE); | |
079e9a2f | 7860 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 7861 | goto error_return; |
079e9a2f | 7862 | } |
252b5132 | 7863 | |
079e9a2f | 7864 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 7865 | |
079e9a2f AM |
7866 | if ((flags & BSF_SECTION_SYM) == 0 |
7867 | && bfd_is_com_section (syms[idx]->section)) | |
7868 | { | |
7869 | /* ELF common symbols put the alignment into the `value' field, | |
7870 | and the size into the `size' field. This is backwards from | |
7871 | how BFD handles it, so reverse it here. */ | |
7872 | sym.st_size = value; | |
7873 | if (type_ptr == NULL | |
7874 | || type_ptr->internal_elf_sym.st_value == 0) | |
7875 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7876 | else | |
7877 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7878 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7879 | (abfd, syms[idx]->section); | |
7880 | } | |
7881 | else | |
7882 | { | |
7883 | asection *sec = syms[idx]->section; | |
cb33740c | 7884 | unsigned int shndx; |
252b5132 | 7885 | |
079e9a2f AM |
7886 | if (sec->output_section) |
7887 | { | |
7888 | value += sec->output_offset; | |
7889 | sec = sec->output_section; | |
7890 | } | |
589e6347 | 7891 | |
079e9a2f AM |
7892 | /* Don't add in the section vma for relocatable output. */ |
7893 | if (! relocatable_p) | |
7894 | value += sec->vma; | |
7895 | sym.st_value = value; | |
7896 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7897 | ||
7898 | if (bfd_is_abs_section (sec) | |
7899 | && type_ptr != NULL | |
7900 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7901 | { | |
7902 | /* This symbol is in a real ELF section which we did | |
7903 | not create as a BFD section. Undo the mapping done | |
7904 | by copy_private_symbol_data. */ | |
7905 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7906 | switch (shndx) | |
7907 | { | |
7908 | case MAP_ONESYMTAB: | |
7909 | shndx = elf_onesymtab (abfd); | |
7910 | break; | |
7911 | case MAP_DYNSYMTAB: | |
7912 | shndx = elf_dynsymtab (abfd); | |
7913 | break; | |
7914 | case MAP_STRTAB: | |
12bd6957 | 7915 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
7916 | break; |
7917 | case MAP_SHSTRTAB: | |
12bd6957 | 7918 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 7919 | break; |
9ad5cbcf | 7920 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
7921 | if (elf_symtab_shndx_list (abfd)) |
7922 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 7923 | break; |
079e9a2f | 7924 | default: |
15bc576a | 7925 | shndx = SHN_ABS; |
079e9a2f AM |
7926 | break; |
7927 | } | |
7928 | } | |
7929 | else | |
7930 | { | |
7931 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 7932 | |
cb33740c | 7933 | if (shndx == SHN_BAD) |
079e9a2f AM |
7934 | { |
7935 | asection *sec2; | |
7936 | ||
7937 | /* Writing this would be a hell of a lot easier if | |
7938 | we had some decent documentation on bfd, and | |
7939 | knew what to expect of the library, and what to | |
7940 | demand of applications. For example, it | |
7941 | appears that `objcopy' might not set the | |
7942 | section of a symbol to be a section that is | |
7943 | actually in the output file. */ | |
7944 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
5df1bc57 AM |
7945 | if (sec2 != NULL) |
7946 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
7947 | if (shndx == SHN_BAD) | |
589e6347 | 7948 | { |
695344c0 | 7949 | /* xgettext:c-format */ |
589e6347 NC |
7950 | _bfd_error_handler (_("\ |
7951 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7952 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7953 | sec->name); | |
811072d8 | 7954 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 7955 | goto error_return; |
589e6347 | 7956 | } |
079e9a2f AM |
7957 | } |
7958 | } | |
252b5132 | 7959 | |
079e9a2f AM |
7960 | sym.st_shndx = shndx; |
7961 | } | |
252b5132 | 7962 | |
13ae64f3 JJ |
7963 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7964 | type = STT_TLS; | |
d8045f23 NC |
7965 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7966 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7967 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7968 | type = STT_FUNC; |
7969 | else if ((flags & BSF_OBJECT) != 0) | |
7970 | type = STT_OBJECT; | |
d9352518 DB |
7971 | else if ((flags & BSF_RELC) != 0) |
7972 | type = STT_RELC; | |
7973 | else if ((flags & BSF_SRELC) != 0) | |
7974 | type = STT_SRELC; | |
079e9a2f AM |
7975 | else |
7976 | type = STT_NOTYPE; | |
252b5132 | 7977 | |
13ae64f3 JJ |
7978 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7979 | type = STT_TLS; | |
7980 | ||
589e6347 | 7981 | /* Processor-specific types. */ |
079e9a2f AM |
7982 | if (type_ptr != NULL |
7983 | && bed->elf_backend_get_symbol_type) | |
7984 | type = ((*bed->elf_backend_get_symbol_type) | |
7985 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7986 | |
079e9a2f AM |
7987 | if (flags & BSF_SECTION_SYM) |
7988 | { | |
7989 | if (flags & BSF_GLOBAL) | |
7990 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7991 | else | |
7992 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7993 | } | |
7994 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7995 | { |
b8871f35 L |
7996 | if (type != STT_TLS) |
7997 | { | |
7998 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
7999 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
8000 | ? STT_COMMON : STT_OBJECT); | |
8001 | else | |
8002 | type = ((flags & BSF_ELF_COMMON) != 0 | |
8003 | ? STT_COMMON : STT_OBJECT); | |
8004 | } | |
8005 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 8006 | } |
079e9a2f AM |
8007 | else if (bfd_is_und_section (syms[idx]->section)) |
8008 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
8009 | ? STB_WEAK | |
8010 | : STB_GLOBAL), | |
8011 | type); | |
8012 | else if (flags & BSF_FILE) | |
8013 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
8014 | else | |
8015 | { | |
8016 | int bind = STB_LOCAL; | |
252b5132 | 8017 | |
079e9a2f AM |
8018 | if (flags & BSF_LOCAL) |
8019 | bind = STB_LOCAL; | |
3e7a7d11 NC |
8020 | else if (flags & BSF_GNU_UNIQUE) |
8021 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
8022 | else if (flags & BSF_WEAK) |
8023 | bind = STB_WEAK; | |
8024 | else if (flags & BSF_GLOBAL) | |
8025 | bind = STB_GLOBAL; | |
252b5132 | 8026 | |
079e9a2f AM |
8027 | sym.st_info = ELF_ST_INFO (bind, type); |
8028 | } | |
252b5132 | 8029 | |
079e9a2f | 8030 | if (type_ptr != NULL) |
35fc36a8 RS |
8031 | { |
8032 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
8033 | sym.st_target_internal | |
8034 | = type_ptr->internal_elf_sym.st_target_internal; | |
8035 | } | |
079e9a2f | 8036 | else |
35fc36a8 RS |
8037 | { |
8038 | sym.st_other = 0; | |
8039 | sym.st_target_internal = 0; | |
8040 | } | |
252b5132 | 8041 | |
ef10c3ac L |
8042 | idx++; |
8043 | symstrtab[idx].sym = sym; | |
8044 | symstrtab[idx].dest_index = outbound_syms_index; | |
8045 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
8046 | ||
8047 | outbound_syms_index++; | |
9ad5cbcf | 8048 | if (outbound_shndx != NULL) |
ef10c3ac L |
8049 | outbound_shndx_index++; |
8050 | } | |
8051 | ||
8052 | /* Finalize the .strtab section. */ | |
8053 | _bfd_elf_strtab_finalize (stt); | |
8054 | ||
8055 | /* Swap out the .strtab section. */ | |
8056 | for (idx = 0; idx <= symcount; idx++) | |
8057 | { | |
8058 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
8059 | if (elfsym->sym.st_name == (unsigned long) -1) | |
8060 | elfsym->sym.st_name = 0; | |
8061 | else | |
8062 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
8063 | elfsym->sym.st_name); | |
8064 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
8065 | (outbound_syms | |
8066 | + (elfsym->dest_index | |
8067 | * bed->s->sizeof_sym)), | |
8068 | (outbound_shndx | |
8069 | + (elfsym->destshndx_index | |
8070 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 8071 | } |
ef10c3ac | 8072 | free (symstrtab); |
252b5132 | 8073 | |
079e9a2f | 8074 | *sttp = stt; |
ef10c3ac | 8075 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 8076 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 8077 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
8078 | symstrtab_hdr->sh_addr = 0; |
8079 | symstrtab_hdr->sh_entsize = 0; | |
8080 | symstrtab_hdr->sh_link = 0; | |
8081 | symstrtab_hdr->sh_info = 0; | |
8082 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 8083 | |
b34976b6 | 8084 | return TRUE; |
252b5132 RH |
8085 | } |
8086 | ||
8087 | /* Return the number of bytes required to hold the symtab vector. | |
8088 | ||
8089 | Note that we base it on the count plus 1, since we will null terminate | |
8090 | the vector allocated based on this size. However, the ELF symbol table | |
8091 | always has a dummy entry as symbol #0, so it ends up even. */ | |
8092 | ||
8093 | long | |
217aa764 | 8094 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
8095 | { |
8096 | long symcount; | |
8097 | long symtab_size; | |
8098 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
8099 | ||
8100 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
8101 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8102 | if (symcount > 0) | |
8103 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8104 | |
8105 | return symtab_size; | |
8106 | } | |
8107 | ||
8108 | long | |
217aa764 | 8109 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
8110 | { |
8111 | long symcount; | |
8112 | long symtab_size; | |
8113 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
8114 | ||
8115 | if (elf_dynsymtab (abfd) == 0) | |
8116 | { | |
8117 | bfd_set_error (bfd_error_invalid_operation); | |
8118 | return -1; | |
8119 | } | |
8120 | ||
8121 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
8122 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8123 | if (symcount > 0) | |
8124 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8125 | |
8126 | return symtab_size; | |
8127 | } | |
8128 | ||
8129 | long | |
217aa764 AM |
8130 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
8131 | sec_ptr asect) | |
252b5132 RH |
8132 | { |
8133 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
8134 | } | |
8135 | ||
8136 | /* Canonicalize the relocs. */ | |
8137 | ||
8138 | long | |
217aa764 AM |
8139 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
8140 | sec_ptr section, | |
8141 | arelent **relptr, | |
8142 | asymbol **symbols) | |
252b5132 RH |
8143 | { |
8144 | arelent *tblptr; | |
8145 | unsigned int i; | |
9c5bfbb7 | 8146 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 8147 | |
b34976b6 | 8148 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
8149 | return -1; |
8150 | ||
8151 | tblptr = section->relocation; | |
8152 | for (i = 0; i < section->reloc_count; i++) | |
8153 | *relptr++ = tblptr++; | |
8154 | ||
8155 | *relptr = NULL; | |
8156 | ||
8157 | return section->reloc_count; | |
8158 | } | |
8159 | ||
8160 | long | |
6cee3f79 | 8161 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 8162 | { |
9c5bfbb7 | 8163 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8164 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
8165 | |
8166 | if (symcount >= 0) | |
8167 | bfd_get_symcount (abfd) = symcount; | |
8168 | return symcount; | |
8169 | } | |
8170 | ||
8171 | long | |
217aa764 AM |
8172 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
8173 | asymbol **allocation) | |
252b5132 | 8174 | { |
9c5bfbb7 | 8175 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8176 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
8177 | |
8178 | if (symcount >= 0) | |
8179 | bfd_get_dynamic_symcount (abfd) = symcount; | |
8180 | return symcount; | |
252b5132 RH |
8181 | } |
8182 | ||
8615f3f2 AM |
8183 | /* Return the size required for the dynamic reloc entries. Any loadable |
8184 | section that was actually installed in the BFD, and has type SHT_REL | |
8185 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
8186 | dynamic reloc section. */ | |
252b5132 RH |
8187 | |
8188 | long | |
217aa764 | 8189 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
8190 | { |
8191 | long ret; | |
8192 | asection *s; | |
8193 | ||
8194 | if (elf_dynsymtab (abfd) == 0) | |
8195 | { | |
8196 | bfd_set_error (bfd_error_invalid_operation); | |
8197 | return -1; | |
8198 | } | |
8199 | ||
8200 | ret = sizeof (arelent *); | |
8201 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 8202 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8203 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8204 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 8205 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
8206 | * sizeof (arelent *)); |
8207 | ||
8208 | return ret; | |
8209 | } | |
8210 | ||
8615f3f2 AM |
8211 | /* Canonicalize the dynamic relocation entries. Note that we return the |
8212 | dynamic relocations as a single block, although they are actually | |
8213 | associated with particular sections; the interface, which was | |
8214 | designed for SunOS style shared libraries, expects that there is only | |
8215 | one set of dynamic relocs. Any loadable section that was actually | |
8216 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
8217 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
8218 | |
8219 | long | |
217aa764 AM |
8220 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
8221 | arelent **storage, | |
8222 | asymbol **syms) | |
252b5132 | 8223 | { |
217aa764 | 8224 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
8225 | asection *s; |
8226 | long ret; | |
8227 | ||
8228 | if (elf_dynsymtab (abfd) == 0) | |
8229 | { | |
8230 | bfd_set_error (bfd_error_invalid_operation); | |
8231 | return -1; | |
8232 | } | |
8233 | ||
8234 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
8235 | ret = 0; | |
8236 | for (s = abfd->sections; s != NULL; s = s->next) | |
8237 | { | |
266b05cf | 8238 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8239 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8240 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
8241 | { | |
8242 | arelent *p; | |
8243 | long count, i; | |
8244 | ||
b34976b6 | 8245 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 8246 | return -1; |
eea6121a | 8247 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
8248 | p = s->relocation; |
8249 | for (i = 0; i < count; i++) | |
8250 | *storage++ = p++; | |
8251 | ret += count; | |
8252 | } | |
8253 | } | |
8254 | ||
8255 | *storage = NULL; | |
8256 | ||
8257 | return ret; | |
8258 | } | |
8259 | \f | |
8260 | /* Read in the version information. */ | |
8261 | ||
b34976b6 | 8262 | bfd_boolean |
fc0e6df6 | 8263 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
8264 | { |
8265 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
8266 | unsigned int freeidx = 0; |
8267 | ||
8268 | if (elf_dynverref (abfd) != 0) | |
8269 | { | |
8270 | Elf_Internal_Shdr *hdr; | |
8271 | Elf_External_Verneed *everneed; | |
8272 | Elf_Internal_Verneed *iverneed; | |
8273 | unsigned int i; | |
d0fb9a8d | 8274 | bfd_byte *contents_end; |
fc0e6df6 PB |
8275 | |
8276 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
8277 | ||
bd61e135 AM |
8278 | if (hdr->sh_info == 0 |
8279 | || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed)) | |
d0fb9a8d | 8280 | { |
601a03ba | 8281 | error_return_bad_verref: |
4eca0228 | 8282 | _bfd_error_handler |
871b3ab2 | 8283 | (_("%pB: .gnu.version_r invalid entry"), abfd); |
601a03ba | 8284 | bfd_set_error (bfd_error_bad_value); |
d0fb9a8d JJ |
8285 | error_return_verref: |
8286 | elf_tdata (abfd)->verref = NULL; | |
8287 | elf_tdata (abfd)->cverrefs = 0; | |
8288 | goto error_return; | |
8289 | } | |
601a03ba AM |
8290 | |
8291 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
8292 | if (contents == NULL) | |
8293 | goto error_return_verref; | |
8294 | ||
fc0e6df6 PB |
8295 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
8296 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 8297 | goto error_return_verref; |
fc0e6df6 | 8298 | |
601a03ba | 8299 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
bd61e135 | 8300 | bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); |
601a03ba AM |
8301 | |
8302 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
8303 | goto error_return_verref; |
8304 | ||
8305 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
8306 | == sizeof (Elf_External_Vernaux)); | |
8307 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
8308 | everneed = (Elf_External_Verneed *) contents; |
8309 | iverneed = elf_tdata (abfd)->verref; | |
8310 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
8311 | { | |
8312 | Elf_External_Vernaux *evernaux; | |
8313 | Elf_Internal_Vernaux *ivernaux; | |
8314 | unsigned int j; | |
8315 | ||
8316 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
8317 | ||
8318 | iverneed->vn_bfd = abfd; | |
8319 | ||
8320 | iverneed->vn_filename = | |
8321 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8322 | iverneed->vn_file); | |
8323 | if (iverneed->vn_filename == NULL) | |
601a03ba | 8324 | goto error_return_bad_verref; |
fc0e6df6 | 8325 | |
d0fb9a8d JJ |
8326 | if (iverneed->vn_cnt == 0) |
8327 | iverneed->vn_auxptr = NULL; | |
8328 | else | |
8329 | { | |
a50b1753 | 8330 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
07d6d2b8 AM |
8331 | bfd_alloc2 (abfd, iverneed->vn_cnt, |
8332 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
8333 | if (iverneed->vn_auxptr == NULL) |
8334 | goto error_return_verref; | |
8335 | } | |
8336 | ||
8337 | if (iverneed->vn_aux | |
8338 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8339 | goto error_return_bad_verref; |
fc0e6df6 PB |
8340 | |
8341 | evernaux = ((Elf_External_Vernaux *) | |
8342 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
8343 | ivernaux = iverneed->vn_auxptr; | |
8344 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
8345 | { | |
8346 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
8347 | ||
8348 | ivernaux->vna_nodename = | |
8349 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8350 | ivernaux->vna_name); | |
8351 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 8352 | goto error_return_bad_verref; |
fc0e6df6 | 8353 | |
25ff461f AM |
8354 | if (ivernaux->vna_other > freeidx) |
8355 | freeidx = ivernaux->vna_other; | |
8356 | ||
8357 | ivernaux->vna_nextptr = NULL; | |
8358 | if (ivernaux->vna_next == 0) | |
8359 | { | |
8360 | iverneed->vn_cnt = j + 1; | |
8361 | break; | |
8362 | } | |
fc0e6df6 PB |
8363 | if (j + 1 < iverneed->vn_cnt) |
8364 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 8365 | |
d0fb9a8d JJ |
8366 | if (ivernaux->vna_next |
8367 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 8368 | goto error_return_bad_verref; |
d0fb9a8d | 8369 | |
fc0e6df6 PB |
8370 | evernaux = ((Elf_External_Vernaux *) |
8371 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
8372 | } |
8373 | ||
25ff461f AM |
8374 | iverneed->vn_nextref = NULL; |
8375 | if (iverneed->vn_next == 0) | |
8376 | break; | |
fc0e6df6 PB |
8377 | if (i + 1 < hdr->sh_info) |
8378 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 8379 | |
d0fb9a8d JJ |
8380 | if (iverneed->vn_next |
8381 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8382 | goto error_return_bad_verref; |
d0fb9a8d | 8383 | |
fc0e6df6 PB |
8384 | everneed = ((Elf_External_Verneed *) |
8385 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
8386 | } | |
25ff461f | 8387 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
8388 | |
8389 | free (contents); | |
8390 | contents = NULL; | |
8391 | } | |
252b5132 RH |
8392 | |
8393 | if (elf_dynverdef (abfd) != 0) | |
8394 | { | |
8395 | Elf_Internal_Shdr *hdr; | |
8396 | Elf_External_Verdef *everdef; | |
8397 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
8398 | Elf_Internal_Verdef *iverdefarr; |
8399 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 8400 | unsigned int i; |
062e2358 | 8401 | unsigned int maxidx; |
d0fb9a8d | 8402 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
8403 | |
8404 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
8405 | ||
601a03ba AM |
8406 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8407 | { | |
8408 | error_return_bad_verdef: | |
4eca0228 | 8409 | _bfd_error_handler |
871b3ab2 | 8410 | (_("%pB: .gnu.version_d invalid entry"), abfd); |
601a03ba AM |
8411 | bfd_set_error (bfd_error_bad_value); |
8412 | error_return_verdef: | |
8413 | elf_tdata (abfd)->verdef = NULL; | |
8414 | elf_tdata (abfd)->cverdefs = 0; | |
8415 | goto error_return; | |
8416 | } | |
8417 | ||
a50b1753 | 8418 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 8419 | if (contents == NULL) |
601a03ba | 8420 | goto error_return_verdef; |
252b5132 | 8421 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 8422 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 8423 | goto error_return_verdef; |
d0fb9a8d JJ |
8424 | |
8425 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8426 | >= sizeof (Elf_External_Verdaux)); | |
8427 | contents_end_def = contents + hdr->sh_size | |
8428 | - sizeof (Elf_External_Verdef); | |
8429 | contents_end_aux = contents + hdr->sh_size | |
8430 | - sizeof (Elf_External_Verdaux); | |
8431 | ||
f631889e UD |
8432 | /* We know the number of entries in the section but not the maximum |
8433 | index. Therefore we have to run through all entries and find | |
8434 | the maximum. */ | |
252b5132 | 8435 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8436 | maxidx = 0; |
8437 | for (i = 0; i < hdr->sh_info; ++i) | |
8438 | { | |
8439 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8440 | ||
601a03ba AM |
8441 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8442 | goto error_return_bad_verdef; | |
062e2358 AM |
8443 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8444 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8445 | |
25ff461f AM |
8446 | if (iverdefmem.vd_next == 0) |
8447 | break; | |
8448 | ||
d0fb9a8d JJ |
8449 | if (iverdefmem.vd_next |
8450 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8451 | goto error_return_bad_verdef; |
d0fb9a8d | 8452 | |
f631889e UD |
8453 | everdef = ((Elf_External_Verdef *) |
8454 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8455 | } | |
8456 | ||
fc0e6df6 PB |
8457 | if (default_imported_symver) |
8458 | { | |
8459 | if (freeidx > maxidx) | |
8460 | maxidx = ++freeidx; | |
8461 | else | |
8462 | freeidx = ++maxidx; | |
8463 | } | |
201159ec | 8464 | |
601a03ba AM |
8465 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8466 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 8467 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8468 | goto error_return_verdef; |
f631889e UD |
8469 | |
8470 | elf_tdata (abfd)->cverdefs = maxidx; | |
8471 | ||
8472 | everdef = (Elf_External_Verdef *) contents; | |
8473 | iverdefarr = elf_tdata (abfd)->verdef; | |
8474 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8475 | { |
8476 | Elf_External_Verdaux *everdaux; | |
8477 | Elf_Internal_Verdaux *iverdaux; | |
8478 | unsigned int j; | |
8479 | ||
f631889e UD |
8480 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8481 | ||
d0fb9a8d | 8482 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8483 | goto error_return_bad_verdef; |
d0fb9a8d | 8484 | |
f631889e | 8485 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8486 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8487 | |
8488 | iverdef->vd_bfd = abfd; | |
8489 | ||
d0fb9a8d JJ |
8490 | if (iverdef->vd_cnt == 0) |
8491 | iverdef->vd_auxptr = NULL; | |
8492 | else | |
8493 | { | |
a50b1753 | 8494 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
07d6d2b8 AM |
8495 | bfd_alloc2 (abfd, iverdef->vd_cnt, |
8496 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
8497 | if (iverdef->vd_auxptr == NULL) |
8498 | goto error_return_verdef; | |
8499 | } | |
8500 | ||
8501 | if (iverdef->vd_aux | |
8502 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8503 | goto error_return_bad_verdef; |
252b5132 RH |
8504 | |
8505 | everdaux = ((Elf_External_Verdaux *) | |
8506 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8507 | iverdaux = iverdef->vd_auxptr; | |
8508 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8509 | { | |
8510 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8511 | ||
8512 | iverdaux->vda_nodename = | |
8513 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8514 | iverdaux->vda_name); | |
8515 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8516 | goto error_return_bad_verdef; |
252b5132 | 8517 | |
25ff461f AM |
8518 | iverdaux->vda_nextptr = NULL; |
8519 | if (iverdaux->vda_next == 0) | |
8520 | { | |
8521 | iverdef->vd_cnt = j + 1; | |
8522 | break; | |
8523 | } | |
252b5132 RH |
8524 | if (j + 1 < iverdef->vd_cnt) |
8525 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8526 | |
d0fb9a8d JJ |
8527 | if (iverdaux->vda_next |
8528 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8529 | goto error_return_bad_verdef; |
d0fb9a8d | 8530 | |
252b5132 RH |
8531 | everdaux = ((Elf_External_Verdaux *) |
8532 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8533 | } | |
8534 | ||
595bce75 | 8535 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8536 | if (iverdef->vd_cnt) |
8537 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8538 | |
25ff461f AM |
8539 | iverdef->vd_nextdef = NULL; |
8540 | if (iverdef->vd_next == 0) | |
8541 | break; | |
d0fb9a8d | 8542 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8543 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8544 | |
8545 | everdef = ((Elf_External_Verdef *) | |
8546 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8547 | } | |
8548 | ||
8549 | free (contents); | |
8550 | contents = NULL; | |
8551 | } | |
fc0e6df6 | 8552 | else if (default_imported_symver) |
252b5132 | 8553 | { |
fc0e6df6 PB |
8554 | if (freeidx < 3) |
8555 | freeidx = 3; | |
8556 | else | |
8557 | freeidx++; | |
252b5132 | 8558 | |
a50b1753 | 8559 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
07d6d2b8 | 8560 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); |
fc0e6df6 | 8561 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8562 | goto error_return; |
8563 | ||
fc0e6df6 PB |
8564 | elf_tdata (abfd)->cverdefs = freeidx; |
8565 | } | |
252b5132 | 8566 | |
fc0e6df6 PB |
8567 | /* Create a default version based on the soname. */ |
8568 | if (default_imported_symver) | |
8569 | { | |
8570 | Elf_Internal_Verdef *iverdef; | |
8571 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8572 | |
5bb3703f | 8573 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8574 | |
fc0e6df6 PB |
8575 | iverdef->vd_version = VER_DEF_CURRENT; |
8576 | iverdef->vd_flags = 0; | |
8577 | iverdef->vd_ndx = freeidx; | |
8578 | iverdef->vd_cnt = 1; | |
252b5132 | 8579 | |
fc0e6df6 | 8580 | iverdef->vd_bfd = abfd; |
252b5132 | 8581 | |
fc0e6df6 PB |
8582 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8583 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8584 | goto error_return_verdef; |
fc0e6df6 | 8585 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8586 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8587 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8588 | if (iverdef->vd_auxptr == NULL) |
8589 | goto error_return_verdef; | |
252b5132 | 8590 | |
fc0e6df6 PB |
8591 | iverdaux = iverdef->vd_auxptr; |
8592 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8593 | } |
8594 | ||
b34976b6 | 8595 | return TRUE; |
252b5132 RH |
8596 | |
8597 | error_return: | |
5ed6aba4 | 8598 | if (contents != NULL) |
252b5132 | 8599 | free (contents); |
b34976b6 | 8600 | return FALSE; |
252b5132 RH |
8601 | } |
8602 | \f | |
8603 | asymbol * | |
217aa764 | 8604 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8605 | { |
8606 | elf_symbol_type *newsym; | |
8607 | ||
201159ec | 8608 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
8609 | if (!newsym) |
8610 | return NULL; | |
201159ec NC |
8611 | newsym->symbol.the_bfd = abfd; |
8612 | return &newsym->symbol; | |
252b5132 RH |
8613 | } |
8614 | ||
8615 | void | |
217aa764 AM |
8616 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8617 | asymbol *symbol, | |
8618 | symbol_info *ret) | |
252b5132 RH |
8619 | { |
8620 | bfd_symbol_info (symbol, ret); | |
8621 | } | |
8622 | ||
8623 | /* Return whether a symbol name implies a local symbol. Most targets | |
8624 | use this function for the is_local_label_name entry point, but some | |
8625 | override it. */ | |
8626 | ||
b34976b6 | 8627 | bfd_boolean |
217aa764 AM |
8628 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8629 | const char *name) | |
252b5132 RH |
8630 | { |
8631 | /* Normal local symbols start with ``.L''. */ | |
8632 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8633 | return TRUE; |
252b5132 RH |
8634 | |
8635 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8636 | DWARF debugging symbols starting with ``..''. */ | |
8637 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 8638 | return TRUE; |
252b5132 RH |
8639 | |
8640 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
8641 | emitting DWARF debugging output. I suspect this is actually a | |
8642 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
8643 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
8644 | underscore to be emitted on some ELF targets). For ease of use, | |
8645 | we treat such symbols as local. */ | |
8646 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 8647 | return TRUE; |
252b5132 | 8648 | |
b1fa9dd6 NC |
8649 | /* Treat assembler generated fake symbols, dollar local labels and |
8650 | forward-backward labels (aka local labels) as locals. | |
8651 | These labels have the form: | |
8652 | ||
07d6d2b8 | 8653 | L0^A.* (fake symbols) |
b1fa9dd6 NC |
8654 | |
8655 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
8656 | ||
8657 | Versions which start with .L will have already been matched above, | |
8658 | so we only need to match the rest. */ | |
8659 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
8660 | { | |
8661 | bfd_boolean ret = FALSE; | |
8662 | const char * p; | |
8663 | char c; | |
8664 | ||
8665 | for (p = name + 2; (c = *p); p++) | |
8666 | { | |
8667 | if (c == 1 || c == 2) | |
8668 | { | |
8669 | if (c == 1 && p == name + 2) | |
8670 | /* A fake symbol. */ | |
8671 | return TRUE; | |
8672 | ||
8673 | /* FIXME: We are being paranoid here and treating symbols like | |
8674 | L0^Bfoo as if there were non-local, on the grounds that the | |
8675 | assembler will never generate them. But can any symbol | |
8676 | containing an ASCII value in the range 1-31 ever be anything | |
8677 | other than some kind of local ? */ | |
8678 | ret = TRUE; | |
8679 | } | |
8680 | ||
8681 | if (! ISDIGIT (c)) | |
8682 | { | |
8683 | ret = FALSE; | |
8684 | break; | |
8685 | } | |
8686 | } | |
8687 | return ret; | |
8688 | } | |
ffa54770 | 8689 | |
b34976b6 | 8690 | return FALSE; |
252b5132 RH |
8691 | } |
8692 | ||
8693 | alent * | |
217aa764 AM |
8694 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
8695 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
8696 | { |
8697 | abort (); | |
8698 | return NULL; | |
8699 | } | |
8700 | ||
b34976b6 | 8701 | bfd_boolean |
217aa764 AM |
8702 | _bfd_elf_set_arch_mach (bfd *abfd, |
8703 | enum bfd_architecture arch, | |
8704 | unsigned long machine) | |
252b5132 RH |
8705 | { |
8706 | /* If this isn't the right architecture for this backend, and this | |
8707 | isn't the generic backend, fail. */ | |
8708 | if (arch != get_elf_backend_data (abfd)->arch | |
8709 | && arch != bfd_arch_unknown | |
8710 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 8711 | return FALSE; |
252b5132 RH |
8712 | |
8713 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
8714 | } | |
8715 | ||
d1fad7c6 NC |
8716 | /* Find the nearest line to a particular section and offset, |
8717 | for error reporting. */ | |
8718 | ||
b34976b6 | 8719 | bfd_boolean |
217aa764 | 8720 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 8721 | asymbol **symbols, |
fb167eb2 | 8722 | asection *section, |
217aa764 AM |
8723 | bfd_vma offset, |
8724 | const char **filename_ptr, | |
8725 | const char **functionname_ptr, | |
fb167eb2 AM |
8726 | unsigned int *line_ptr, |
8727 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 8728 | { |
b34976b6 | 8729 | bfd_boolean found; |
d1fad7c6 | 8730 | |
fb167eb2 | 8731 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 8732 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
8733 | line_ptr, discriminator_ptr, |
8734 | dwarf_debug_sections, 0, | |
e00e8198 AM |
8735 | &elf_tdata (abfd)->dwarf2_find_line_info) |
8736 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
8737 | filename_ptr, functionname_ptr, | |
8738 | line_ptr)) | |
d1fad7c6 NC |
8739 | { |
8740 | if (!*functionname_ptr) | |
e00e8198 AM |
8741 | _bfd_elf_find_function (abfd, symbols, section, offset, |
8742 | *filename_ptr ? NULL : filename_ptr, | |
8743 | functionname_ptr); | |
b34976b6 | 8744 | return TRUE; |
d1fad7c6 NC |
8745 | } |
8746 | ||
8747 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
8748 | &found, filename_ptr, |
8749 | functionname_ptr, line_ptr, | |
8750 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 8751 | return FALSE; |
dc43ada5 | 8752 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 8753 | return TRUE; |
d1fad7c6 NC |
8754 | |
8755 | if (symbols == NULL) | |
b34976b6 | 8756 | return FALSE; |
d1fad7c6 | 8757 | |
e00e8198 AM |
8758 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
8759 | filename_ptr, functionname_ptr)) | |
b34976b6 | 8760 | return FALSE; |
d1fad7c6 | 8761 | |
252b5132 | 8762 | *line_ptr = 0; |
b34976b6 | 8763 | return TRUE; |
252b5132 RH |
8764 | } |
8765 | ||
5420f73d L |
8766 | /* Find the line for a symbol. */ |
8767 | ||
8768 | bfd_boolean | |
8769 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
8770 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 8771 | { |
fb167eb2 AM |
8772 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
8773 | filename_ptr, NULL, line_ptr, NULL, | |
8774 | dwarf_debug_sections, 0, | |
8775 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
8776 | } |
8777 | ||
4ab527b0 FF |
8778 | /* After a call to bfd_find_nearest_line, successive calls to |
8779 | bfd_find_inliner_info can be used to get source information about | |
8780 | each level of function inlining that terminated at the address | |
8781 | passed to bfd_find_nearest_line. Currently this is only supported | |
8782 | for DWARF2 with appropriate DWARF3 extensions. */ | |
8783 | ||
8784 | bfd_boolean | |
8785 | _bfd_elf_find_inliner_info (bfd *abfd, | |
8786 | const char **filename_ptr, | |
8787 | const char **functionname_ptr, | |
8788 | unsigned int *line_ptr) | |
8789 | { | |
8790 | bfd_boolean found; | |
8791 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
8792 | functionname_ptr, line_ptr, | |
8793 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
8794 | return found; | |
8795 | } | |
8796 | ||
252b5132 | 8797 | int |
a6b96beb | 8798 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 8799 | { |
8ded5a0f AM |
8800 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8801 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 8802 | |
0e1862bb | 8803 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 8804 | { |
12bd6957 | 8805 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 8806 | |
62d7a5f6 AM |
8807 | if (phdr_size == (bfd_size_type) -1) |
8808 | { | |
8809 | struct elf_segment_map *m; | |
8810 | ||
8811 | phdr_size = 0; | |
12bd6957 | 8812 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 8813 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 8814 | |
62d7a5f6 AM |
8815 | if (phdr_size == 0) |
8816 | phdr_size = get_program_header_size (abfd, info); | |
8817 | } | |
8ded5a0f | 8818 | |
12bd6957 | 8819 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
8820 | ret += phdr_size; |
8821 | } | |
8822 | ||
252b5132 RH |
8823 | return ret; |
8824 | } | |
8825 | ||
b34976b6 | 8826 | bfd_boolean |
217aa764 AM |
8827 | _bfd_elf_set_section_contents (bfd *abfd, |
8828 | sec_ptr section, | |
0f867abe | 8829 | const void *location, |
217aa764 AM |
8830 | file_ptr offset, |
8831 | bfd_size_type count) | |
252b5132 RH |
8832 | { |
8833 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 8834 | file_ptr pos; |
252b5132 RH |
8835 | |
8836 | if (! abfd->output_has_begun | |
217aa764 | 8837 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 8838 | return FALSE; |
252b5132 | 8839 | |
0ce398f1 L |
8840 | if (!count) |
8841 | return TRUE; | |
8842 | ||
252b5132 | 8843 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
8844 | if (hdr->sh_offset == (file_ptr) -1) |
8845 | { | |
8846 | /* We must compress this section. Write output to the buffer. */ | |
8847 | unsigned char *contents = hdr->contents; | |
8848 | if ((offset + count) > hdr->sh_size | |
8849 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
8850 | || contents == NULL) | |
8851 | abort (); | |
8852 | memcpy (contents + offset, location, count); | |
8853 | return TRUE; | |
8854 | } | |
dc810e39 AM |
8855 | pos = hdr->sh_offset + offset; |
8856 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
8857 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 8858 | return FALSE; |
252b5132 | 8859 | |
b34976b6 | 8860 | return TRUE; |
252b5132 RH |
8861 | } |
8862 | ||
8863 | void | |
217aa764 AM |
8864 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
8865 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
8866 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
8867 | { |
8868 | abort (); | |
8869 | } | |
8870 | ||
252b5132 RH |
8871 | /* Try to convert a non-ELF reloc into an ELF one. */ |
8872 | ||
b34976b6 | 8873 | bfd_boolean |
217aa764 | 8874 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 8875 | { |
c044fabd | 8876 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
8877 | |
8878 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
8879 | { | |
8880 | bfd_reloc_code_real_type code; | |
8881 | reloc_howto_type *howto; | |
8882 | ||
8883 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 8884 | equivalent ELF reloc. */ |
252b5132 RH |
8885 | |
8886 | if (areloc->howto->pc_relative) | |
8887 | { | |
8888 | switch (areloc->howto->bitsize) | |
8889 | { | |
8890 | case 8: | |
8891 | code = BFD_RELOC_8_PCREL; | |
8892 | break; | |
8893 | case 12: | |
8894 | code = BFD_RELOC_12_PCREL; | |
8895 | break; | |
8896 | case 16: | |
8897 | code = BFD_RELOC_16_PCREL; | |
8898 | break; | |
8899 | case 24: | |
8900 | code = BFD_RELOC_24_PCREL; | |
8901 | break; | |
8902 | case 32: | |
8903 | code = BFD_RELOC_32_PCREL; | |
8904 | break; | |
8905 | case 64: | |
8906 | code = BFD_RELOC_64_PCREL; | |
8907 | break; | |
8908 | default: | |
8909 | goto fail; | |
8910 | } | |
8911 | ||
8912 | howto = bfd_reloc_type_lookup (abfd, code); | |
8913 | ||
8914 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
8915 | { | |
8916 | if (howto->pcrel_offset) | |
8917 | areloc->addend += areloc->address; | |
8918 | else | |
8919 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
8920 | } | |
8921 | } | |
8922 | else | |
8923 | { | |
8924 | switch (areloc->howto->bitsize) | |
8925 | { | |
8926 | case 8: | |
8927 | code = BFD_RELOC_8; | |
8928 | break; | |
8929 | case 14: | |
8930 | code = BFD_RELOC_14; | |
8931 | break; | |
8932 | case 16: | |
8933 | code = BFD_RELOC_16; | |
8934 | break; | |
8935 | case 26: | |
8936 | code = BFD_RELOC_26; | |
8937 | break; | |
8938 | case 32: | |
8939 | code = BFD_RELOC_32; | |
8940 | break; | |
8941 | case 64: | |
8942 | code = BFD_RELOC_64; | |
8943 | break; | |
8944 | default: | |
8945 | goto fail; | |
8946 | } | |
8947 | ||
8948 | howto = bfd_reloc_type_lookup (abfd, code); | |
8949 | } | |
8950 | ||
8951 | if (howto) | |
8952 | areloc->howto = howto; | |
8953 | else | |
8954 | goto fail; | |
8955 | } | |
8956 | ||
b34976b6 | 8957 | return TRUE; |
252b5132 RH |
8958 | |
8959 | fail: | |
4eca0228 | 8960 | _bfd_error_handler |
695344c0 | 8961 | /* xgettext:c-format */ |
871b3ab2 | 8962 | (_("%pB: unsupported relocation type %s"), |
d003868e | 8963 | abfd, areloc->howto->name); |
252b5132 | 8964 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8965 | return FALSE; |
252b5132 RH |
8966 | } |
8967 | ||
b34976b6 | 8968 | bfd_boolean |
217aa764 | 8969 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8970 | { |
d9071b0c TG |
8971 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8972 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8973 | { |
c0355132 | 8974 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8975 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8976 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8977 | } |
8978 | ||
8979 | return _bfd_generic_close_and_cleanup (abfd); | |
8980 | } | |
8981 | ||
8982 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8983 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8984 | range-checking to interfere. There is nothing else to do in processing | |
8985 | this reloc. */ | |
8986 | ||
8987 | bfd_reloc_status_type | |
217aa764 AM |
8988 | _bfd_elf_rel_vtable_reloc_fn |
8989 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8990 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8991 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8992 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8993 | { |
8994 | return bfd_reloc_ok; | |
8995 | } | |
252b5132 RH |
8996 | \f |
8997 | /* Elf core file support. Much of this only works on native | |
8998 | toolchains, since we rely on knowing the | |
8999 | machine-dependent procfs structure in order to pick | |
c044fabd | 9000 | out details about the corefile. */ |
252b5132 RH |
9001 | |
9002 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
9003 | /* Needed for new procfs interface on sparc-solaris. */ |
9004 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
9005 | # include <sys/procfs.h> |
9006 | #endif | |
9007 | ||
261b8d08 PA |
9008 | /* Return a PID that identifies a "thread" for threaded cores, or the |
9009 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
9010 | |
9011 | static int | |
217aa764 | 9012 | elfcore_make_pid (bfd *abfd) |
252b5132 | 9013 | { |
261b8d08 PA |
9014 | int pid; |
9015 | ||
228e534f | 9016 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 9017 | if (pid == 0) |
228e534f | 9018 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
9019 | |
9020 | return pid; | |
252b5132 RH |
9021 | } |
9022 | ||
252b5132 RH |
9023 | /* If there isn't a section called NAME, make one, using |
9024 | data from SECT. Note, this function will generate a | |
9025 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 9026 | overwrite it. */ |
252b5132 | 9027 | |
b34976b6 | 9028 | static bfd_boolean |
217aa764 | 9029 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 9030 | { |
c044fabd | 9031 | asection *sect2; |
252b5132 RH |
9032 | |
9033 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 9034 | return TRUE; |
252b5132 | 9035 | |
117ed4f8 | 9036 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 9037 | if (sect2 == NULL) |
b34976b6 | 9038 | return FALSE; |
252b5132 | 9039 | |
eea6121a | 9040 | sect2->size = sect->size; |
252b5132 | 9041 | sect2->filepos = sect->filepos; |
252b5132 | 9042 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 9043 | return TRUE; |
252b5132 RH |
9044 | } |
9045 | ||
bb0082d6 AM |
9046 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
9047 | actually creates up to two pseudosections: | |
9048 | - For the single-threaded case, a section named NAME, unless | |
9049 | such a section already exists. | |
9050 | - For the multi-threaded case, a section named "NAME/PID", where | |
9051 | PID is elfcore_make_pid (abfd). | |
24d3e51b | 9052 | Both pseudosections have identical contents. */ |
b34976b6 | 9053 | bfd_boolean |
217aa764 AM |
9054 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
9055 | char *name, | |
9056 | size_t size, | |
9057 | ufile_ptr filepos) | |
bb0082d6 AM |
9058 | { |
9059 | char buf[100]; | |
9060 | char *threaded_name; | |
d4c88bbb | 9061 | size_t len; |
bb0082d6 AM |
9062 | asection *sect; |
9063 | ||
9064 | /* Build the section name. */ | |
9065 | ||
9066 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 9067 | len = strlen (buf) + 1; |
a50b1753 | 9068 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 9069 | if (threaded_name == NULL) |
b34976b6 | 9070 | return FALSE; |
d4c88bbb | 9071 | memcpy (threaded_name, buf, len); |
bb0082d6 | 9072 | |
117ed4f8 AM |
9073 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
9074 | SEC_HAS_CONTENTS); | |
bb0082d6 | 9075 | if (sect == NULL) |
b34976b6 | 9076 | return FALSE; |
eea6121a | 9077 | sect->size = size; |
bb0082d6 | 9078 | sect->filepos = filepos; |
bb0082d6 AM |
9079 | sect->alignment_power = 2; |
9080 | ||
936e320b | 9081 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
9082 | } |
9083 | ||
252b5132 | 9084 | /* prstatus_t exists on: |
4a938328 | 9085 | solaris 2.5+ |
252b5132 RH |
9086 | linux 2.[01] + glibc |
9087 | unixware 4.2 | |
9088 | */ | |
9089 | ||
9090 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 9091 | |
b34976b6 | 9092 | static bfd_boolean |
217aa764 | 9093 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9094 | { |
eea6121a | 9095 | size_t size; |
7ee38065 | 9096 | int offset; |
252b5132 | 9097 | |
4a938328 MS |
9098 | if (note->descsz == sizeof (prstatus_t)) |
9099 | { | |
9100 | prstatus_t prstat; | |
252b5132 | 9101 | |
eea6121a | 9102 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9103 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 9104 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 9105 | |
fa49d224 NC |
9106 | /* Do not overwrite the core signal if it |
9107 | has already been set by another thread. */ | |
228e534f AM |
9108 | if (elf_tdata (abfd)->core->signal == 0) |
9109 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9110 | if (elf_tdata (abfd)->core->pid == 0) | |
9111 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 9112 | |
4a938328 MS |
9113 | /* pr_who exists on: |
9114 | solaris 2.5+ | |
9115 | unixware 4.2 | |
9116 | pr_who doesn't exist on: | |
9117 | linux 2.[01] | |
9118 | */ | |
252b5132 | 9119 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 9120 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9121 | #else |
228e534f | 9122 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 9123 | #endif |
4a938328 | 9124 | } |
7ee38065 | 9125 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
9126 | else if (note->descsz == sizeof (prstatus32_t)) |
9127 | { | |
9128 | /* 64-bit host, 32-bit corefile */ | |
9129 | prstatus32_t prstat; | |
9130 | ||
eea6121a | 9131 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9132 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
9133 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
9134 | ||
fa49d224 NC |
9135 | /* Do not overwrite the core signal if it |
9136 | has already been set by another thread. */ | |
228e534f AM |
9137 | if (elf_tdata (abfd)->core->signal == 0) |
9138 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9139 | if (elf_tdata (abfd)->core->pid == 0) | |
9140 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
9141 | |
9142 | /* pr_who exists on: | |
9143 | solaris 2.5+ | |
9144 | unixware 4.2 | |
9145 | pr_who doesn't exist on: | |
9146 | linux 2.[01] | |
9147 | */ | |
7ee38065 | 9148 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 9149 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9150 | #else |
228e534f | 9151 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
9152 | #endif |
9153 | } | |
7ee38065 | 9154 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
9155 | else |
9156 | { | |
9157 | /* Fail - we don't know how to handle any other | |
9158 | note size (ie. data object type). */ | |
b34976b6 | 9159 | return TRUE; |
4a938328 | 9160 | } |
252b5132 | 9161 | |
bb0082d6 | 9162 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 9163 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 9164 | size, note->descpos + offset); |
252b5132 RH |
9165 | } |
9166 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
9167 | ||
bb0082d6 | 9168 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 9169 | static bfd_boolean |
217aa764 AM |
9170 | elfcore_make_note_pseudosection (bfd *abfd, |
9171 | char *name, | |
9172 | Elf_Internal_Note *note) | |
252b5132 | 9173 | { |
936e320b AM |
9174 | return _bfd_elfcore_make_pseudosection (abfd, name, |
9175 | note->descsz, note->descpos); | |
252b5132 RH |
9176 | } |
9177 | ||
ff08c6bb JB |
9178 | /* There isn't a consistent prfpregset_t across platforms, |
9179 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
9180 | data structure apart. */ |
9181 | ||
b34976b6 | 9182 | static bfd_boolean |
217aa764 | 9183 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9184 | { |
9185 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9186 | } | |
9187 | ||
ff08c6bb | 9188 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 9189 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 9190 | literally. */ |
c044fabd | 9191 | |
b34976b6 | 9192 | static bfd_boolean |
217aa764 | 9193 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9194 | { |
9195 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9196 | } | |
9197 | ||
4339cae0 L |
9198 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
9199 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
9200 | contents literally. */ | |
9201 | ||
9202 | static bfd_boolean | |
9203 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
9204 | { | |
9205 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
9206 | } | |
9207 | ||
97753bd5 AM |
9208 | static bfd_boolean |
9209 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
9210 | { | |
9211 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
9212 | } | |
9213 | ||
89eeb0bc LM |
9214 | static bfd_boolean |
9215 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
9216 | { | |
9217 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
9218 | } | |
97753bd5 | 9219 | |
0675e188 UW |
9220 | static bfd_boolean |
9221 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
9222 | { | |
9223 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
9224 | } | |
9225 | ||
d7eeb400 MS |
9226 | static bfd_boolean |
9227 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
9228 | { | |
9229 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
9230 | } | |
9231 | ||
9232 | static bfd_boolean | |
9233 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
9234 | { | |
9235 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
9236 | } | |
9237 | ||
9238 | static bfd_boolean | |
9239 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
9240 | { | |
9241 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
9242 | } | |
9243 | ||
9244 | static bfd_boolean | |
9245 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
9246 | { | |
9247 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
9248 | } | |
9249 | ||
9250 | static bfd_boolean | |
9251 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
9252 | { | |
9253 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
9254 | } | |
9255 | ||
355b81d9 UW |
9256 | static bfd_boolean |
9257 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
9258 | { | |
9259 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
9260 | } | |
9261 | ||
9262 | static bfd_boolean | |
9263 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
9264 | { | |
9265 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
9266 | } | |
9267 | ||
abb3f6cc NC |
9268 | static bfd_boolean |
9269 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
9270 | { | |
9271 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
9272 | } | |
9273 | ||
4ef9f41a AA |
9274 | static bfd_boolean |
9275 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
9276 | { | |
9277 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
9278 | } | |
9279 | ||
9280 | static bfd_boolean | |
9281 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
9282 | { | |
9283 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
9284 | } | |
9285 | ||
88ab90e8 AA |
9286 | static bfd_boolean |
9287 | elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) | |
9288 | { | |
9289 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note); | |
9290 | } | |
9291 | ||
9292 | static bfd_boolean | |
9293 | elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) | |
9294 | { | |
9295 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note); | |
9296 | } | |
9297 | ||
faa9a424 UW |
9298 | static bfd_boolean |
9299 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
9300 | { | |
9301 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
9302 | } | |
9303 | ||
652451f8 YZ |
9304 | static bfd_boolean |
9305 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
9306 | { | |
9307 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
9308 | } | |
9309 | ||
9310 | static bfd_boolean | |
9311 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
9312 | { | |
9313 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
9314 | } | |
9315 | ||
9316 | static bfd_boolean | |
9317 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
9318 | { | |
9319 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
9320 | } | |
9321 | ||
252b5132 | 9322 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 9323 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 9324 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9325 | typedef prpsinfo32_t elfcore_psinfo32_t; |
9326 | #endif | |
252b5132 RH |
9327 | #endif |
9328 | ||
9329 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 9330 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 9331 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9332 | typedef psinfo32_t elfcore_psinfo32_t; |
9333 | #endif | |
252b5132 RH |
9334 | #endif |
9335 | ||
252b5132 RH |
9336 | /* return a malloc'ed copy of a string at START which is at |
9337 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 9338 | the copy will always have a terminating '\0'. */ |
252b5132 | 9339 | |
936e320b | 9340 | char * |
217aa764 | 9341 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 9342 | { |
dc810e39 | 9343 | char *dups; |
a50b1753 | 9344 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 9345 | size_t len; |
252b5132 RH |
9346 | |
9347 | if (end == NULL) | |
9348 | len = max; | |
9349 | else | |
9350 | len = end - start; | |
9351 | ||
a50b1753 | 9352 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 9353 | if (dups == NULL) |
252b5132 RH |
9354 | return NULL; |
9355 | ||
dc810e39 AM |
9356 | memcpy (dups, start, len); |
9357 | dups[len] = '\0'; | |
252b5132 | 9358 | |
dc810e39 | 9359 | return dups; |
252b5132 RH |
9360 | } |
9361 | ||
bb0082d6 | 9362 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 9363 | static bfd_boolean |
217aa764 | 9364 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9365 | { |
4a938328 MS |
9366 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
9367 | { | |
9368 | elfcore_psinfo_t psinfo; | |
252b5132 | 9369 | |
7ee38065 | 9370 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9371 | |
335e41d4 | 9372 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 9373 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9374 | #endif |
228e534f | 9375 | elf_tdata (abfd)->core->program |
936e320b AM |
9376 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9377 | sizeof (psinfo.pr_fname)); | |
252b5132 | 9378 | |
228e534f | 9379 | elf_tdata (abfd)->core->command |
936e320b AM |
9380 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9381 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 9382 | } |
7ee38065 | 9383 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
9384 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
9385 | { | |
9386 | /* 64-bit host, 32-bit corefile */ | |
9387 | elfcore_psinfo32_t psinfo; | |
9388 | ||
7ee38065 | 9389 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9390 | |
335e41d4 | 9391 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 9392 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9393 | #endif |
228e534f | 9394 | elf_tdata (abfd)->core->program |
936e320b AM |
9395 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9396 | sizeof (psinfo.pr_fname)); | |
4a938328 | 9397 | |
228e534f | 9398 | elf_tdata (abfd)->core->command |
936e320b AM |
9399 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9400 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
9401 | } |
9402 | #endif | |
9403 | ||
9404 | else | |
9405 | { | |
9406 | /* Fail - we don't know how to handle any other | |
9407 | note size (ie. data object type). */ | |
b34976b6 | 9408 | return TRUE; |
4a938328 | 9409 | } |
252b5132 RH |
9410 | |
9411 | /* Note that for some reason, a spurious space is tacked | |
9412 | onto the end of the args in some (at least one anyway) | |
c044fabd | 9413 | implementations, so strip it off if it exists. */ |
252b5132 RH |
9414 | |
9415 | { | |
228e534f | 9416 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
9417 | int n = strlen (command); |
9418 | ||
9419 | if (0 < n && command[n - 1] == ' ') | |
9420 | command[n - 1] = '\0'; | |
9421 | } | |
9422 | ||
b34976b6 | 9423 | return TRUE; |
252b5132 RH |
9424 | } |
9425 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9426 | ||
252b5132 | 9427 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9428 | static bfd_boolean |
217aa764 | 9429 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9430 | { |
f572a39d AM |
9431 | if (note->descsz == sizeof (pstatus_t) |
9432 | #if defined (HAVE_PXSTATUS_T) | |
9433 | || note->descsz == sizeof (pxstatus_t) | |
9434 | #endif | |
9435 | ) | |
4a938328 MS |
9436 | { |
9437 | pstatus_t pstat; | |
252b5132 | 9438 | |
4a938328 | 9439 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9440 | |
228e534f | 9441 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9442 | } |
7ee38065 | 9443 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9444 | else if (note->descsz == sizeof (pstatus32_t)) |
9445 | { | |
9446 | /* 64-bit host, 32-bit corefile */ | |
9447 | pstatus32_t pstat; | |
252b5132 | 9448 | |
4a938328 | 9449 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9450 | |
228e534f | 9451 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
9452 | } |
9453 | #endif | |
252b5132 RH |
9454 | /* Could grab some more details from the "representative" |
9455 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 9456 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 9457 | |
b34976b6 | 9458 | return TRUE; |
252b5132 RH |
9459 | } |
9460 | #endif /* defined (HAVE_PSTATUS_T) */ | |
9461 | ||
252b5132 | 9462 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 9463 | static bfd_boolean |
217aa764 | 9464 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
9465 | { |
9466 | lwpstatus_t lwpstat; | |
9467 | char buf[100]; | |
c044fabd | 9468 | char *name; |
d4c88bbb | 9469 | size_t len; |
c044fabd | 9470 | asection *sect; |
252b5132 | 9471 | |
f572a39d AM |
9472 | if (note->descsz != sizeof (lwpstat) |
9473 | #if defined (HAVE_LWPXSTATUS_T) | |
9474 | && note->descsz != sizeof (lwpxstatus_t) | |
9475 | #endif | |
9476 | ) | |
b34976b6 | 9477 | return TRUE; |
252b5132 RH |
9478 | |
9479 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
9480 | ||
228e534f | 9481 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
9482 | /* Do not overwrite the core signal if it has already been set by |
9483 | another thread. */ | |
228e534f AM |
9484 | if (elf_tdata (abfd)->core->signal == 0) |
9485 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 9486 | |
c044fabd | 9487 | /* Make a ".reg/999" section. */ |
252b5132 RH |
9488 | |
9489 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9490 | len = strlen (buf) + 1; |
217aa764 | 9491 | name = bfd_alloc (abfd, len); |
252b5132 | 9492 | if (name == NULL) |
b34976b6 | 9493 | return FALSE; |
d4c88bbb | 9494 | memcpy (name, buf, len); |
252b5132 | 9495 | |
117ed4f8 | 9496 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9497 | if (sect == NULL) |
b34976b6 | 9498 | return FALSE; |
252b5132 RH |
9499 | |
9500 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9501 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
9502 | sect->filepos = note->descpos |
9503 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
9504 | #endif | |
9505 | ||
9506 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 9507 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
9508 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
9509 | #endif | |
9510 | ||
252b5132 RH |
9511 | sect->alignment_power = 2; |
9512 | ||
9513 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 9514 | return FALSE; |
252b5132 RH |
9515 | |
9516 | /* Make a ".reg2/999" section */ | |
9517 | ||
9518 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9519 | len = strlen (buf) + 1; |
217aa764 | 9520 | name = bfd_alloc (abfd, len); |
252b5132 | 9521 | if (name == NULL) |
b34976b6 | 9522 | return FALSE; |
d4c88bbb | 9523 | memcpy (name, buf, len); |
252b5132 | 9524 | |
117ed4f8 | 9525 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9526 | if (sect == NULL) |
b34976b6 | 9527 | return FALSE; |
252b5132 RH |
9528 | |
9529 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9530 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
9531 | sect->filepos = note->descpos |
9532 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
9533 | #endif | |
9534 | ||
9535 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 9536 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
9537 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
9538 | #endif | |
9539 | ||
252b5132 RH |
9540 | sect->alignment_power = 2; |
9541 | ||
936e320b | 9542 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
9543 | } |
9544 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
9545 | ||
b34976b6 | 9546 | static bfd_boolean |
217aa764 | 9547 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
9548 | { |
9549 | char buf[30]; | |
c044fabd | 9550 | char *name; |
d4c88bbb | 9551 | size_t len; |
c044fabd | 9552 | asection *sect; |
4a6636fb PA |
9553 | int type; |
9554 | int is_active_thread; | |
9555 | bfd_vma base_addr; | |
16e9c715 | 9556 | |
4a6636fb | 9557 | if (note->descsz < 728) |
b34976b6 | 9558 | return TRUE; |
16e9c715 | 9559 | |
4a6636fb PA |
9560 | if (! CONST_STRNEQ (note->namedata, "win32")) |
9561 | return TRUE; | |
9562 | ||
9563 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 9564 | |
4a6636fb | 9565 | switch (type) |
16e9c715 | 9566 | { |
4a6636fb | 9567 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 9568 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 9569 | /* process_info.pid */ |
228e534f | 9570 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 9571 | /* process_info.signal */ |
228e534f | 9572 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 9573 | break; |
16e9c715 | 9574 | |
4a6636fb | 9575 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 9576 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
9577 | /* thread_info.tid */ |
9578 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 9579 | |
d4c88bbb | 9580 | len = strlen (buf) + 1; |
a50b1753 | 9581 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9582 | if (name == NULL) |
b34976b6 | 9583 | return FALSE; |
c044fabd | 9584 | |
d4c88bbb | 9585 | memcpy (name, buf, len); |
16e9c715 | 9586 | |
117ed4f8 | 9587 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 9588 | if (sect == NULL) |
b34976b6 | 9589 | return FALSE; |
c044fabd | 9590 | |
4a6636fb PA |
9591 | /* sizeof (thread_info.thread_context) */ |
9592 | sect->size = 716; | |
9593 | /* offsetof (thread_info.thread_context) */ | |
9594 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
9595 | sect->alignment_power = 2; |
9596 | ||
4a6636fb PA |
9597 | /* thread_info.is_active_thread */ |
9598 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
9599 | ||
9600 | if (is_active_thread) | |
16e9c715 | 9601 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 9602 | return FALSE; |
16e9c715 NC |
9603 | break; |
9604 | ||
4a6636fb | 9605 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 9606 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
9607 | /* module_info.base_address */ |
9608 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 9609 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 9610 | |
d4c88bbb | 9611 | len = strlen (buf) + 1; |
a50b1753 | 9612 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9613 | if (name == NULL) |
b34976b6 | 9614 | return FALSE; |
c044fabd | 9615 | |
d4c88bbb | 9616 | memcpy (name, buf, len); |
252b5132 | 9617 | |
117ed4f8 | 9618 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 9619 | |
16e9c715 | 9620 | if (sect == NULL) |
b34976b6 | 9621 | return FALSE; |
c044fabd | 9622 | |
eea6121a | 9623 | sect->size = note->descsz; |
16e9c715 | 9624 | sect->filepos = note->descpos; |
16e9c715 NC |
9625 | sect->alignment_power = 2; |
9626 | break; | |
9627 | ||
9628 | default: | |
b34976b6 | 9629 | return TRUE; |
16e9c715 NC |
9630 | } |
9631 | ||
b34976b6 | 9632 | return TRUE; |
16e9c715 | 9633 | } |
252b5132 | 9634 | |
b34976b6 | 9635 | static bfd_boolean |
217aa764 | 9636 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9637 | { |
9c5bfbb7 | 9638 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 9639 | |
252b5132 RH |
9640 | switch (note->type) |
9641 | { | |
9642 | default: | |
b34976b6 | 9643 | return TRUE; |
252b5132 | 9644 | |
252b5132 | 9645 | case NT_PRSTATUS: |
bb0082d6 AM |
9646 | if (bed->elf_backend_grok_prstatus) |
9647 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 9648 | return TRUE; |
bb0082d6 | 9649 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 9650 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 9651 | #else |
b34976b6 | 9652 | return TRUE; |
252b5132 RH |
9653 | #endif |
9654 | ||
9655 | #if defined (HAVE_PSTATUS_T) | |
9656 | case NT_PSTATUS: | |
9657 | return elfcore_grok_pstatus (abfd, note); | |
9658 | #endif | |
9659 | ||
9660 | #if defined (HAVE_LWPSTATUS_T) | |
9661 | case NT_LWPSTATUS: | |
9662 | return elfcore_grok_lwpstatus (abfd, note); | |
9663 | #endif | |
9664 | ||
9665 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
9666 | return elfcore_grok_prfpreg (abfd, note); | |
9667 | ||
c044fabd | 9668 | case NT_WIN32PSTATUS: |
16e9c715 | 9669 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 9670 | |
c044fabd | 9671 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
9672 | if (note->namesz == 6 |
9673 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
9674 | return elfcore_grok_prxfpreg (abfd, note); |
9675 | else | |
b34976b6 | 9676 | return TRUE; |
ff08c6bb | 9677 | |
4339cae0 L |
9678 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
9679 | if (note->namesz == 6 | |
9680 | && strcmp (note->namedata, "LINUX") == 0) | |
9681 | return elfcore_grok_xstatereg (abfd, note); | |
9682 | else | |
9683 | return TRUE; | |
9684 | ||
97753bd5 AM |
9685 | case NT_PPC_VMX: |
9686 | if (note->namesz == 6 | |
9687 | && strcmp (note->namedata, "LINUX") == 0) | |
9688 | return elfcore_grok_ppc_vmx (abfd, note); | |
9689 | else | |
9690 | return TRUE; | |
9691 | ||
89eeb0bc LM |
9692 | case NT_PPC_VSX: |
9693 | if (note->namesz == 6 | |
07d6d2b8 AM |
9694 | && strcmp (note->namedata, "LINUX") == 0) |
9695 | return elfcore_grok_ppc_vsx (abfd, note); | |
89eeb0bc | 9696 | else |
07d6d2b8 | 9697 | return TRUE; |
89eeb0bc | 9698 | |
0675e188 UW |
9699 | case NT_S390_HIGH_GPRS: |
9700 | if (note->namesz == 6 | |
07d6d2b8 AM |
9701 | && strcmp (note->namedata, "LINUX") == 0) |
9702 | return elfcore_grok_s390_high_gprs (abfd, note); | |
0675e188 | 9703 | else |
07d6d2b8 | 9704 | return TRUE; |
0675e188 | 9705 | |
d7eeb400 MS |
9706 | case NT_S390_TIMER: |
9707 | if (note->namesz == 6 | |
07d6d2b8 AM |
9708 | && strcmp (note->namedata, "LINUX") == 0) |
9709 | return elfcore_grok_s390_timer (abfd, note); | |
d7eeb400 | 9710 | else |
07d6d2b8 | 9711 | return TRUE; |
d7eeb400 MS |
9712 | |
9713 | case NT_S390_TODCMP: | |
9714 | if (note->namesz == 6 | |
07d6d2b8 AM |
9715 | && strcmp (note->namedata, "LINUX") == 0) |
9716 | return elfcore_grok_s390_todcmp (abfd, note); | |
d7eeb400 | 9717 | else |
07d6d2b8 | 9718 | return TRUE; |
d7eeb400 MS |
9719 | |
9720 | case NT_S390_TODPREG: | |
9721 | if (note->namesz == 6 | |
07d6d2b8 AM |
9722 | && strcmp (note->namedata, "LINUX") == 0) |
9723 | return elfcore_grok_s390_todpreg (abfd, note); | |
d7eeb400 | 9724 | else |
07d6d2b8 | 9725 | return TRUE; |
d7eeb400 MS |
9726 | |
9727 | case NT_S390_CTRS: | |
9728 | if (note->namesz == 6 | |
07d6d2b8 AM |
9729 | && strcmp (note->namedata, "LINUX") == 0) |
9730 | return elfcore_grok_s390_ctrs (abfd, note); | |
d7eeb400 | 9731 | else |
07d6d2b8 | 9732 | return TRUE; |
d7eeb400 MS |
9733 | |
9734 | case NT_S390_PREFIX: | |
9735 | if (note->namesz == 6 | |
07d6d2b8 AM |
9736 | && strcmp (note->namedata, "LINUX") == 0) |
9737 | return elfcore_grok_s390_prefix (abfd, note); | |
d7eeb400 | 9738 | else |
07d6d2b8 | 9739 | return TRUE; |
d7eeb400 | 9740 | |
355b81d9 UW |
9741 | case NT_S390_LAST_BREAK: |
9742 | if (note->namesz == 6 | |
07d6d2b8 AM |
9743 | && strcmp (note->namedata, "LINUX") == 0) |
9744 | return elfcore_grok_s390_last_break (abfd, note); | |
355b81d9 | 9745 | else |
07d6d2b8 | 9746 | return TRUE; |
355b81d9 UW |
9747 | |
9748 | case NT_S390_SYSTEM_CALL: | |
9749 | if (note->namesz == 6 | |
07d6d2b8 AM |
9750 | && strcmp (note->namedata, "LINUX") == 0) |
9751 | return elfcore_grok_s390_system_call (abfd, note); | |
355b81d9 | 9752 | else |
07d6d2b8 | 9753 | return TRUE; |
355b81d9 | 9754 | |
abb3f6cc NC |
9755 | case NT_S390_TDB: |
9756 | if (note->namesz == 6 | |
07d6d2b8 AM |
9757 | && strcmp (note->namedata, "LINUX") == 0) |
9758 | return elfcore_grok_s390_tdb (abfd, note); | |
abb3f6cc | 9759 | else |
07d6d2b8 | 9760 | return TRUE; |
abb3f6cc | 9761 | |
4ef9f41a AA |
9762 | case NT_S390_VXRS_LOW: |
9763 | if (note->namesz == 6 | |
9764 | && strcmp (note->namedata, "LINUX") == 0) | |
9765 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
9766 | else | |
9767 | return TRUE; | |
9768 | ||
9769 | case NT_S390_VXRS_HIGH: | |
9770 | if (note->namesz == 6 | |
9771 | && strcmp (note->namedata, "LINUX") == 0) | |
9772 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
9773 | else | |
9774 | return TRUE; | |
9775 | ||
88ab90e8 AA |
9776 | case NT_S390_GS_CB: |
9777 | if (note->namesz == 6 | |
9778 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 9779 | return elfcore_grok_s390_gs_cb (abfd, note); |
88ab90e8 AA |
9780 | else |
9781 | return TRUE; | |
9782 | ||
9783 | case NT_S390_GS_BC: | |
9784 | if (note->namesz == 6 | |
9785 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 9786 | return elfcore_grok_s390_gs_bc (abfd, note); |
88ab90e8 AA |
9787 | else |
9788 | return TRUE; | |
9789 | ||
faa9a424 UW |
9790 | case NT_ARM_VFP: |
9791 | if (note->namesz == 6 | |
9792 | && strcmp (note->namedata, "LINUX") == 0) | |
9793 | return elfcore_grok_arm_vfp (abfd, note); | |
9794 | else | |
9795 | return TRUE; | |
9796 | ||
652451f8 YZ |
9797 | case NT_ARM_TLS: |
9798 | if (note->namesz == 6 | |
9799 | && strcmp (note->namedata, "LINUX") == 0) | |
9800 | return elfcore_grok_aarch_tls (abfd, note); | |
9801 | else | |
9802 | return TRUE; | |
9803 | ||
9804 | case NT_ARM_HW_BREAK: | |
9805 | if (note->namesz == 6 | |
9806 | && strcmp (note->namedata, "LINUX") == 0) | |
9807 | return elfcore_grok_aarch_hw_break (abfd, note); | |
9808 | else | |
9809 | return TRUE; | |
9810 | ||
9811 | case NT_ARM_HW_WATCH: | |
9812 | if (note->namesz == 6 | |
9813 | && strcmp (note->namedata, "LINUX") == 0) | |
9814 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
9815 | else | |
9816 | return TRUE; | |
9817 | ||
252b5132 RH |
9818 | case NT_PRPSINFO: |
9819 | case NT_PSINFO: | |
bb0082d6 AM |
9820 | if (bed->elf_backend_grok_psinfo) |
9821 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 9822 | return TRUE; |
bb0082d6 | 9823 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 9824 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 9825 | #else |
b34976b6 | 9826 | return TRUE; |
252b5132 | 9827 | #endif |
3333a7c3 RM |
9828 | |
9829 | case NT_AUXV: | |
9830 | { | |
117ed4f8 AM |
9831 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
9832 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
9833 | |
9834 | if (sect == NULL) | |
9835 | return FALSE; | |
eea6121a | 9836 | sect->size = note->descsz; |
3333a7c3 | 9837 | sect->filepos = note->descpos; |
3333a7c3 RM |
9838 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
9839 | ||
9840 | return TRUE; | |
9841 | } | |
9015683b | 9842 | |
451b7c33 TT |
9843 | case NT_FILE: |
9844 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
9845 | note); | |
9846 | ||
9015683b TT |
9847 | case NT_SIGINFO: |
9848 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
9849 | note); | |
5b2c414d | 9850 | |
252b5132 RH |
9851 | } |
9852 | } | |
9853 | ||
718175fa JK |
9854 | static bfd_boolean |
9855 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
9856 | { | |
c74f7d1c | 9857 | struct bfd_build_id* build_id; |
30e8ee25 AM |
9858 | |
9859 | if (note->descsz == 0) | |
9860 | return FALSE; | |
9861 | ||
c74f7d1c JT |
9862 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
9863 | if (build_id == NULL) | |
718175fa JK |
9864 | return FALSE; |
9865 | ||
c74f7d1c JT |
9866 | build_id->size = note->descsz; |
9867 | memcpy (build_id->data, note->descdata, note->descsz); | |
9868 | abfd->build_id = build_id; | |
718175fa JK |
9869 | |
9870 | return TRUE; | |
9871 | } | |
9872 | ||
9873 | static bfd_boolean | |
9874 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
9875 | { | |
9876 | switch (note->type) | |
9877 | { | |
9878 | default: | |
9879 | return TRUE; | |
9880 | ||
46bed679 L |
9881 | case NT_GNU_PROPERTY_TYPE_0: |
9882 | return _bfd_elf_parse_gnu_properties (abfd, note); | |
9883 | ||
718175fa JK |
9884 | case NT_GNU_BUILD_ID: |
9885 | return elfobj_grok_gnu_build_id (abfd, note); | |
9886 | } | |
9887 | } | |
9888 | ||
e21e5835 NC |
9889 | static bfd_boolean |
9890 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
9891 | { | |
9892 | struct sdt_note *cur = | |
9893 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
9894 | + note->descsz); | |
9895 | ||
9896 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
9897 | cur->size = (bfd_size_type) note->descsz; | |
9898 | memcpy (cur->data, note->descdata, note->descsz); | |
9899 | ||
9900 | elf_tdata (abfd)->sdt_note_head = cur; | |
9901 | ||
9902 | return TRUE; | |
9903 | } | |
9904 | ||
9905 | static bfd_boolean | |
9906 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
9907 | { | |
9908 | switch (note->type) | |
9909 | { | |
9910 | case NT_STAPSDT: | |
9911 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
9912 | ||
9913 | default: | |
9914 | return TRUE; | |
9915 | } | |
9916 | } | |
9917 | ||
aa1ed4a9 JB |
9918 | static bfd_boolean |
9919 | elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
9920 | { | |
9921 | size_t offset; | |
9922 | ||
b5430a3c | 9923 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 9924 | { |
b5430a3c | 9925 | case ELFCLASS32: |
0064d223 JB |
9926 | if (note->descsz < 108) |
9927 | return FALSE; | |
aa1ed4a9 JB |
9928 | break; |
9929 | ||
b5430a3c | 9930 | case ELFCLASS64: |
0064d223 JB |
9931 | if (note->descsz < 120) |
9932 | return FALSE; | |
aa1ed4a9 JB |
9933 | break; |
9934 | ||
9935 | default: | |
9936 | return FALSE; | |
9937 | } | |
9938 | ||
0064d223 JB |
9939 | /* Check for version 1 in pr_version. */ |
9940 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
9941 | return FALSE; | |
80a04378 | 9942 | |
0064d223 JB |
9943 | offset = 4; |
9944 | ||
9945 | /* Skip over pr_psinfosz. */ | |
b5430a3c | 9946 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
0064d223 JB |
9947 | offset += 4; |
9948 | else | |
9949 | { | |
9950 | offset += 4; /* Padding before pr_psinfosz. */ | |
9951 | offset += 8; | |
9952 | } | |
9953 | ||
aa1ed4a9 JB |
9954 | /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ |
9955 | elf_tdata (abfd)->core->program | |
9956 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); | |
9957 | offset += 17; | |
9958 | ||
9959 | /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ | |
9960 | elf_tdata (abfd)->core->command | |
9961 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); | |
0064d223 JB |
9962 | offset += 81; |
9963 | ||
9964 | /* Padding before pr_pid. */ | |
9965 | offset += 2; | |
9966 | ||
9967 | /* The pr_pid field was added in version "1a". */ | |
9968 | if (note->descsz < offset + 4) | |
9969 | return TRUE; | |
9970 | ||
9971 | elf_tdata (abfd)->core->pid | |
9972 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
aa1ed4a9 JB |
9973 | |
9974 | return TRUE; | |
9975 | } | |
9976 | ||
9977 | static bfd_boolean | |
9978 | elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
9979 | { | |
9980 | size_t offset; | |
9981 | size_t size; | |
24d3e51b | 9982 | size_t min_size; |
aa1ed4a9 | 9983 | |
24d3e51b NC |
9984 | /* Compute offset of pr_getregsz, skipping over pr_statussz. |
9985 | Also compute minimum size of this note. */ | |
b5430a3c | 9986 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 9987 | { |
b5430a3c | 9988 | case ELFCLASS32: |
24d3e51b NC |
9989 | offset = 4 + 4; |
9990 | min_size = offset + (4 * 2) + 4 + 4 + 4; | |
aa1ed4a9 JB |
9991 | break; |
9992 | ||
b5430a3c | 9993 | case ELFCLASS64: |
24d3e51b NC |
9994 | offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */ |
9995 | min_size = offset + (8 * 2) + 4 + 4 + 4 + 4; | |
aa1ed4a9 JB |
9996 | break; |
9997 | ||
9998 | default: | |
9999 | return FALSE; | |
10000 | } | |
10001 | ||
24d3e51b NC |
10002 | if (note->descsz < min_size) |
10003 | return FALSE; | |
10004 | ||
10005 | /* Check for version 1 in pr_version. */ | |
10006 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
10007 | return FALSE; | |
aa1ed4a9 | 10008 | |
24d3e51b NC |
10009 | /* Extract size of pr_reg from pr_gregsetsz. */ |
10010 | /* Skip over pr_gregsetsz and pr_fpregsetsz. */ | |
b5430a3c | 10011 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
24d3e51b NC |
10012 | { |
10013 | size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10014 | offset += 4 * 2; | |
10015 | } | |
b5430a3c | 10016 | else |
24d3e51b NC |
10017 | { |
10018 | size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); | |
10019 | offset += 8 * 2; | |
10020 | } | |
aa1ed4a9 | 10021 | |
24d3e51b | 10022 | /* Skip over pr_osreldate. */ |
aa1ed4a9 JB |
10023 | offset += 4; |
10024 | ||
24d3e51b | 10025 | /* Read signal from pr_cursig. */ |
aa1ed4a9 JB |
10026 | if (elf_tdata (abfd)->core->signal == 0) |
10027 | elf_tdata (abfd)->core->signal | |
10028 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10029 | offset += 4; | |
10030 | ||
24d3e51b | 10031 | /* Read TID from pr_pid. */ |
aa1ed4a9 JB |
10032 | elf_tdata (abfd)->core->lwpid |
10033 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10034 | offset += 4; | |
10035 | ||
24d3e51b | 10036 | /* Padding before pr_reg. */ |
b5430a3c | 10037 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) |
aa1ed4a9 JB |
10038 | offset += 4; |
10039 | ||
24d3e51b NC |
10040 | /* Make sure that there is enough data remaining in the note. */ |
10041 | if ((note->descsz - offset) < size) | |
10042 | return FALSE; | |
10043 | ||
aa1ed4a9 JB |
10044 | /* Make a ".reg/999" section and a ".reg" section. */ |
10045 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
10046 | size, note->descpos + offset); | |
10047 | } | |
10048 | ||
10049 | static bfd_boolean | |
10050 | elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10051 | { | |
544c67cd JB |
10052 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10053 | ||
aa1ed4a9 JB |
10054 | switch (note->type) |
10055 | { | |
10056 | case NT_PRSTATUS: | |
544c67cd JB |
10057 | if (bed->elf_backend_grok_freebsd_prstatus) |
10058 | if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note)) | |
10059 | return TRUE; | |
aa1ed4a9 JB |
10060 | return elfcore_grok_freebsd_prstatus (abfd, note); |
10061 | ||
10062 | case NT_FPREGSET: | |
10063 | return elfcore_grok_prfpreg (abfd, note); | |
10064 | ||
10065 | case NT_PRPSINFO: | |
10066 | return elfcore_grok_freebsd_psinfo (abfd, note); | |
10067 | ||
10068 | case NT_FREEBSD_THRMISC: | |
10069 | if (note->namesz == 8) | |
10070 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
10071 | else | |
10072 | return TRUE; | |
10073 | ||
ddb2bbcf JB |
10074 | case NT_FREEBSD_PROCSTAT_PROC: |
10075 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc", | |
10076 | note); | |
10077 | ||
10078 | case NT_FREEBSD_PROCSTAT_FILES: | |
10079 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files", | |
10080 | note); | |
10081 | ||
10082 | case NT_FREEBSD_PROCSTAT_VMMAP: | |
10083 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap", | |
10084 | note); | |
10085 | ||
3350c5f5 JB |
10086 | case NT_FREEBSD_PROCSTAT_AUXV: |
10087 | { | |
10088 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
10089 | SEC_HAS_CONTENTS); | |
10090 | ||
10091 | if (sect == NULL) | |
10092 | return FALSE; | |
10093 | sect->size = note->descsz - 4; | |
10094 | sect->filepos = note->descpos + 4; | |
10095 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10096 | ||
10097 | return TRUE; | |
10098 | } | |
10099 | ||
aa1ed4a9 JB |
10100 | case NT_X86_XSTATE: |
10101 | if (note->namesz == 8) | |
10102 | return elfcore_grok_xstatereg (abfd, note); | |
10103 | else | |
10104 | return TRUE; | |
10105 | ||
e6f3b9c3 JB |
10106 | case NT_FREEBSD_PTLWPINFO: |
10107 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", | |
10108 | note); | |
10109 | ||
6d5be5d6 JB |
10110 | case NT_ARM_VFP: |
10111 | return elfcore_grok_arm_vfp (abfd, note); | |
10112 | ||
aa1ed4a9 JB |
10113 | default: |
10114 | return TRUE; | |
10115 | } | |
10116 | } | |
10117 | ||
b34976b6 | 10118 | static bfd_boolean |
217aa764 | 10119 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
10120 | { |
10121 | char *cp; | |
10122 | ||
10123 | cp = strchr (note->namedata, '@'); | |
10124 | if (cp != NULL) | |
10125 | { | |
d2b64500 | 10126 | *lwpidp = atoi(cp + 1); |
b34976b6 | 10127 | return TRUE; |
50b2bdb7 | 10128 | } |
b34976b6 | 10129 | return FALSE; |
50b2bdb7 AM |
10130 | } |
10131 | ||
b34976b6 | 10132 | static bfd_boolean |
217aa764 | 10133 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 10134 | { |
80a04378 NC |
10135 | if (note->descsz <= 0x7c + 31) |
10136 | return FALSE; | |
10137 | ||
50b2bdb7 | 10138 | /* Signal number at offset 0x08. */ |
228e534f | 10139 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
10140 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10141 | ||
10142 | /* Process ID at offset 0x50. */ | |
228e534f | 10143 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
10144 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
10145 | ||
10146 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 10147 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
10148 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
10149 | ||
7720ba9f MK |
10150 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
10151 | note); | |
50b2bdb7 AM |
10152 | } |
10153 | ||
b34976b6 | 10154 | static bfd_boolean |
217aa764 | 10155 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
10156 | { |
10157 | int lwp; | |
10158 | ||
10159 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 10160 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 10161 | |
b4db1224 | 10162 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
10163 | { |
10164 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
10165 | find this note before any of the others, which is fine, |
10166 | since the kernel writes this note out first when it | |
10167 | creates a core file. */ | |
47d9a591 | 10168 | |
50b2bdb7 AM |
10169 | return elfcore_grok_netbsd_procinfo (abfd, note); |
10170 | } | |
10171 | ||
b4db1224 JT |
10172 | /* As of Jan 2002 there are no other machine-independent notes |
10173 | defined for NetBSD core files. If the note type is less | |
10174 | than the start of the machine-dependent note types, we don't | |
10175 | understand it. */ | |
47d9a591 | 10176 | |
b4db1224 | 10177 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 10178 | return TRUE; |
50b2bdb7 AM |
10179 | |
10180 | ||
10181 | switch (bfd_get_arch (abfd)) | |
10182 | { | |
08a40648 AM |
10183 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
10184 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
10185 | |
10186 | case bfd_arch_alpha: | |
10187 | case bfd_arch_sparc: | |
10188 | switch (note->type) | |
08a40648 AM |
10189 | { |
10190 | case NT_NETBSDCORE_FIRSTMACH+0: | |
10191 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10192 | |
08a40648 AM |
10193 | case NT_NETBSDCORE_FIRSTMACH+2: |
10194 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10195 | |
08a40648 AM |
10196 | default: |
10197 | return TRUE; | |
10198 | } | |
50b2bdb7 | 10199 | |
08a40648 AM |
10200 | /* On all other arch's, PT_GETREGS == mach+1 and |
10201 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
10202 | |
10203 | default: | |
10204 | switch (note->type) | |
08a40648 AM |
10205 | { |
10206 | case NT_NETBSDCORE_FIRSTMACH+1: | |
10207 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10208 | |
08a40648 AM |
10209 | case NT_NETBSDCORE_FIRSTMACH+3: |
10210 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10211 | |
08a40648 AM |
10212 | default: |
10213 | return TRUE; | |
10214 | } | |
50b2bdb7 AM |
10215 | } |
10216 | /* NOTREACHED */ | |
10217 | } | |
10218 | ||
67cc5033 MK |
10219 | static bfd_boolean |
10220 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
10221 | { | |
80a04378 NC |
10222 | if (note->descsz <= 0x48 + 31) |
10223 | return FALSE; | |
10224 | ||
67cc5033 | 10225 | /* Signal number at offset 0x08. */ |
228e534f | 10226 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
10227 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10228 | ||
10229 | /* Process ID at offset 0x20. */ | |
228e534f | 10230 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
10231 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
10232 | ||
10233 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 10234 | elf_tdata (abfd)->core->command |
67cc5033 MK |
10235 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
10236 | ||
10237 | return TRUE; | |
10238 | } | |
10239 | ||
10240 | static bfd_boolean | |
10241 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10242 | { | |
10243 | if (note->type == NT_OPENBSD_PROCINFO) | |
10244 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
10245 | ||
10246 | if (note->type == NT_OPENBSD_REGS) | |
10247 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10248 | ||
10249 | if (note->type == NT_OPENBSD_FPREGS) | |
10250 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10251 | ||
10252 | if (note->type == NT_OPENBSD_XFPREGS) | |
10253 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
10254 | ||
10255 | if (note->type == NT_OPENBSD_AUXV) | |
10256 | { | |
10257 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
10258 | SEC_HAS_CONTENTS); | |
10259 | ||
10260 | if (sect == NULL) | |
10261 | return FALSE; | |
10262 | sect->size = note->descsz; | |
10263 | sect->filepos = note->descpos; | |
10264 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10265 | ||
10266 | return TRUE; | |
10267 | } | |
10268 | ||
10269 | if (note->type == NT_OPENBSD_WCOOKIE) | |
10270 | { | |
10271 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
10272 | SEC_HAS_CONTENTS); | |
10273 | ||
10274 | if (sect == NULL) | |
10275 | return FALSE; | |
10276 | sect->size = note->descsz; | |
10277 | sect->filepos = note->descpos; | |
10278 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10279 | ||
10280 | return TRUE; | |
10281 | } | |
10282 | ||
10283 | return TRUE; | |
10284 | } | |
10285 | ||
07c6e936 | 10286 | static bfd_boolean |
d3fd4074 | 10287 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
10288 | { |
10289 | void *ddata = note->descdata; | |
10290 | char buf[100]; | |
10291 | char *name; | |
10292 | asection *sect; | |
f8843e87 AM |
10293 | short sig; |
10294 | unsigned flags; | |
07c6e936 | 10295 | |
80a04378 NC |
10296 | if (note->descsz < 16) |
10297 | return FALSE; | |
10298 | ||
07c6e936 | 10299 | /* nto_procfs_status 'pid' field is at offset 0. */ |
228e534f | 10300 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 10301 | |
f8843e87 AM |
10302 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
10303 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
10304 | ||
10305 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
10306 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
10307 | |
10308 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
10309 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
10310 | { | |
228e534f AM |
10311 | elf_tdata (abfd)->core->signal = sig; |
10312 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 10313 | } |
07c6e936 | 10314 | |
f8843e87 AM |
10315 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
10316 | do not come from signals so we make sure we set the current | |
10317 | thread just in case. */ | |
10318 | if (flags & 0x00000080) | |
228e534f | 10319 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
10320 | |
10321 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 10322 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 10323 | |
a50b1753 | 10324 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10325 | if (name == NULL) |
10326 | return FALSE; | |
10327 | strcpy (name, buf); | |
10328 | ||
117ed4f8 | 10329 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10330 | if (sect == NULL) |
10331 | return FALSE; | |
10332 | ||
07d6d2b8 AM |
10333 | sect->size = note->descsz; |
10334 | sect->filepos = note->descpos; | |
07c6e936 NC |
10335 | sect->alignment_power = 2; |
10336 | ||
10337 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
10338 | } | |
10339 | ||
10340 | static bfd_boolean | |
d69f560c KW |
10341 | elfcore_grok_nto_regs (bfd *abfd, |
10342 | Elf_Internal_Note *note, | |
d3fd4074 | 10343 | long tid, |
d69f560c | 10344 | char *base) |
07c6e936 NC |
10345 | { |
10346 | char buf[100]; | |
10347 | char *name; | |
10348 | asection *sect; | |
10349 | ||
d69f560c | 10350 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 10351 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 10352 | |
a50b1753 | 10353 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10354 | if (name == NULL) |
10355 | return FALSE; | |
10356 | strcpy (name, buf); | |
10357 | ||
117ed4f8 | 10358 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10359 | if (sect == NULL) |
10360 | return FALSE; | |
10361 | ||
07d6d2b8 AM |
10362 | sect->size = note->descsz; |
10363 | sect->filepos = note->descpos; | |
07c6e936 NC |
10364 | sect->alignment_power = 2; |
10365 | ||
f8843e87 | 10366 | /* This is the current thread. */ |
228e534f | 10367 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 10368 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
10369 | |
10370 | return TRUE; | |
07c6e936 NC |
10371 | } |
10372 | ||
10373 | #define BFD_QNT_CORE_INFO 7 | |
10374 | #define BFD_QNT_CORE_STATUS 8 | |
10375 | #define BFD_QNT_CORE_GREG 9 | |
10376 | #define BFD_QNT_CORE_FPREG 10 | |
10377 | ||
10378 | static bfd_boolean | |
217aa764 | 10379 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
10380 | { |
10381 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 10382 | tid from the previous call to pass down to the next gregs |
07c6e936 | 10383 | function. */ |
d3fd4074 | 10384 | static long tid = 1; |
07c6e936 NC |
10385 | |
10386 | switch (note->type) | |
10387 | { | |
d69f560c KW |
10388 | case BFD_QNT_CORE_INFO: |
10389 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
10390 | case BFD_QNT_CORE_STATUS: | |
10391 | return elfcore_grok_nto_status (abfd, note, &tid); | |
10392 | case BFD_QNT_CORE_GREG: | |
10393 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
10394 | case BFD_QNT_CORE_FPREG: | |
10395 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
10396 | default: | |
10397 | return TRUE; | |
07c6e936 NC |
10398 | } |
10399 | } | |
10400 | ||
b15fa79e AM |
10401 | static bfd_boolean |
10402 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
10403 | { | |
10404 | char *name; | |
10405 | asection *sect; | |
10406 | size_t len; | |
10407 | ||
10408 | /* Use note name as section name. */ | |
10409 | len = note->namesz; | |
a50b1753 | 10410 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
10411 | if (name == NULL) |
10412 | return FALSE; | |
10413 | memcpy (name, note->namedata, len); | |
10414 | name[len - 1] = '\0'; | |
10415 | ||
10416 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
10417 | if (sect == NULL) | |
10418 | return FALSE; | |
10419 | ||
07d6d2b8 AM |
10420 | sect->size = note->descsz; |
10421 | sect->filepos = note->descpos; | |
b15fa79e AM |
10422 | sect->alignment_power = 1; |
10423 | ||
10424 | return TRUE; | |
10425 | } | |
10426 | ||
7c76fa91 MS |
10427 | /* Function: elfcore_write_note |
10428 | ||
47d9a591 | 10429 | Inputs: |
a39f3346 | 10430 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
10431 | name of note |
10432 | type of note | |
10433 | data for note | |
10434 | size of data for note | |
10435 | ||
a39f3346 AM |
10436 | Writes note to end of buffer. ELF64 notes are written exactly as |
10437 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
10438 | that they ought to have 8-byte namesz and descsz field, and have | |
10439 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
10440 | ||
7c76fa91 | 10441 | Return: |
a39f3346 | 10442 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
10443 | |
10444 | char * | |
a39f3346 | 10445 | elfcore_write_note (bfd *abfd, |
217aa764 | 10446 | char *buf, |
a39f3346 | 10447 | int *bufsiz, |
217aa764 | 10448 | const char *name, |
a39f3346 | 10449 | int type, |
217aa764 | 10450 | const void *input, |
a39f3346 | 10451 | int size) |
7c76fa91 MS |
10452 | { |
10453 | Elf_External_Note *xnp; | |
d4c88bbb | 10454 | size_t namesz; |
d4c88bbb | 10455 | size_t newspace; |
a39f3346 | 10456 | char *dest; |
7c76fa91 | 10457 | |
d4c88bbb | 10458 | namesz = 0; |
d4c88bbb | 10459 | if (name != NULL) |
a39f3346 | 10460 | namesz = strlen (name) + 1; |
d4c88bbb | 10461 | |
a39f3346 | 10462 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 10463 | |
a50b1753 | 10464 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
10465 | if (buf == NULL) |
10466 | return buf; | |
a39f3346 | 10467 | dest = buf + *bufsiz; |
7c76fa91 MS |
10468 | *bufsiz += newspace; |
10469 | xnp = (Elf_External_Note *) dest; | |
10470 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
10471 | H_PUT_32 (abfd, size, xnp->descsz); | |
10472 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
10473 | dest = xnp->name; |
10474 | if (name != NULL) | |
10475 | { | |
10476 | memcpy (dest, name, namesz); | |
10477 | dest += namesz; | |
a39f3346 | 10478 | while (namesz & 3) |
d4c88bbb AM |
10479 | { |
10480 | *dest++ = '\0'; | |
a39f3346 | 10481 | ++namesz; |
d4c88bbb AM |
10482 | } |
10483 | } | |
10484 | memcpy (dest, input, size); | |
a39f3346 AM |
10485 | dest += size; |
10486 | while (size & 3) | |
10487 | { | |
10488 | *dest++ = '\0'; | |
10489 | ++size; | |
10490 | } | |
10491 | return buf; | |
7c76fa91 MS |
10492 | } |
10493 | ||
7c76fa91 | 10494 | char * |
217aa764 AM |
10495 | elfcore_write_prpsinfo (bfd *abfd, |
10496 | char *buf, | |
10497 | int *bufsiz, | |
10498 | const char *fname, | |
10499 | const char *psargs) | |
7c76fa91 | 10500 | { |
183e98be AM |
10501 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10502 | ||
10503 | if (bed->elf_backend_write_core_note != NULL) | |
10504 | { | |
10505 | char *ret; | |
10506 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10507 | NT_PRPSINFO, fname, psargs); | |
10508 | if (ret != NULL) | |
10509 | return ret; | |
10510 | } | |
7c76fa91 | 10511 | |
1f20dca5 | 10512 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
10513 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
10514 | if (bed->s->elfclass == ELFCLASS32) | |
10515 | { | |
10516 | #if defined (HAVE_PSINFO32_T) | |
10517 | psinfo32_t data; | |
10518 | int note_type = NT_PSINFO; | |
10519 | #else | |
10520 | prpsinfo32_t data; | |
10521 | int note_type = NT_PRPSINFO; | |
10522 | #endif | |
10523 | ||
10524 | memset (&data, 0, sizeof (data)); | |
10525 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10526 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10527 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10528 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
10529 | } |
10530 | else | |
10531 | #endif | |
10532 | { | |
7c76fa91 | 10533 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
10534 | psinfo_t data; |
10535 | int note_type = NT_PSINFO; | |
7c76fa91 | 10536 | #else |
183e98be AM |
10537 | prpsinfo_t data; |
10538 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
10539 | #endif |
10540 | ||
183e98be AM |
10541 | memset (&data, 0, sizeof (data)); |
10542 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10543 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10544 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10545 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 10546 | } |
7c76fa91 MS |
10547 | #endif /* PSINFO_T or PRPSINFO_T */ |
10548 | ||
1f20dca5 UW |
10549 | free (buf); |
10550 | return NULL; | |
10551 | } | |
10552 | ||
70a38d42 SDJ |
10553 | char * |
10554 | elfcore_write_linux_prpsinfo32 | |
10555 | (bfd *abfd, char *buf, int *bufsiz, | |
10556 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10557 | { | |
a2f63b2e MR |
10558 | if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16) |
10559 | { | |
10560 | struct elf_external_linux_prpsinfo32_ugid16 data; | |
10561 | ||
10562 | swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data); | |
10563 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
10564 | &data, sizeof (data)); | |
10565 | } | |
10566 | else | |
10567 | { | |
10568 | struct elf_external_linux_prpsinfo32_ugid32 data; | |
70a38d42 | 10569 | |
a2f63b2e MR |
10570 | swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data); |
10571 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
10572 | &data, sizeof (data)); | |
10573 | } | |
70a38d42 SDJ |
10574 | } |
10575 | ||
10576 | char * | |
10577 | elfcore_write_linux_prpsinfo64 | |
10578 | (bfd *abfd, char *buf, int *bufsiz, | |
10579 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10580 | { | |
3c9a7b0d MR |
10581 | if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16) |
10582 | { | |
10583 | struct elf_external_linux_prpsinfo64_ugid16 data; | |
10584 | ||
10585 | swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data); | |
10586 | return elfcore_write_note (abfd, buf, bufsiz, | |
10587 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
10588 | } | |
10589 | else | |
10590 | { | |
10591 | struct elf_external_linux_prpsinfo64_ugid32 data; | |
70a38d42 | 10592 | |
3c9a7b0d MR |
10593 | swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data); |
10594 | return elfcore_write_note (abfd, buf, bufsiz, | |
10595 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
10596 | } | |
70a38d42 SDJ |
10597 | } |
10598 | ||
7c76fa91 | 10599 | char * |
217aa764 AM |
10600 | elfcore_write_prstatus (bfd *abfd, |
10601 | char *buf, | |
10602 | int *bufsiz, | |
10603 | long pid, | |
10604 | int cursig, | |
10605 | const void *gregs) | |
7c76fa91 | 10606 | { |
183e98be | 10607 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 10608 | |
183e98be AM |
10609 | if (bed->elf_backend_write_core_note != NULL) |
10610 | { | |
10611 | char *ret; | |
10612 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10613 | NT_PRSTATUS, | |
10614 | pid, cursig, gregs); | |
10615 | if (ret != NULL) | |
10616 | return ret; | |
10617 | } | |
10618 | ||
1f20dca5 | 10619 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
10620 | #if defined (HAVE_PRSTATUS32_T) |
10621 | if (bed->s->elfclass == ELFCLASS32) | |
10622 | { | |
10623 | prstatus32_t prstat; | |
10624 | ||
10625 | memset (&prstat, 0, sizeof (prstat)); | |
10626 | prstat.pr_pid = pid; | |
10627 | prstat.pr_cursig = cursig; | |
10628 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10629 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10630 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10631 | } | |
10632 | else | |
10633 | #endif | |
10634 | { | |
10635 | prstatus_t prstat; | |
10636 | ||
10637 | memset (&prstat, 0, sizeof (prstat)); | |
10638 | prstat.pr_pid = pid; | |
10639 | prstat.pr_cursig = cursig; | |
10640 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10641 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10642 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10643 | } | |
7c76fa91 MS |
10644 | #endif /* HAVE_PRSTATUS_T */ |
10645 | ||
1f20dca5 UW |
10646 | free (buf); |
10647 | return NULL; | |
10648 | } | |
10649 | ||
51316059 MS |
10650 | #if defined (HAVE_LWPSTATUS_T) |
10651 | char * | |
217aa764 AM |
10652 | elfcore_write_lwpstatus (bfd *abfd, |
10653 | char *buf, | |
10654 | int *bufsiz, | |
10655 | long pid, | |
10656 | int cursig, | |
10657 | const void *gregs) | |
51316059 MS |
10658 | { |
10659 | lwpstatus_t lwpstat; | |
183e98be | 10660 | const char *note_name = "CORE"; |
51316059 MS |
10661 | |
10662 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
10663 | lwpstat.pr_lwpid = pid >> 16; | |
10664 | lwpstat.pr_cursig = cursig; | |
10665 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 10666 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
10667 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
10668 | #if !defined(gregs) | |
10669 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
10670 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
10671 | #else | |
10672 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
10673 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
10674 | #endif | |
10675 | #endif | |
47d9a591 | 10676 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
10677 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
10678 | } | |
10679 | #endif /* HAVE_LWPSTATUS_T */ | |
10680 | ||
7c76fa91 MS |
10681 | #if defined (HAVE_PSTATUS_T) |
10682 | char * | |
217aa764 AM |
10683 | elfcore_write_pstatus (bfd *abfd, |
10684 | char *buf, | |
10685 | int *bufsiz, | |
10686 | long pid, | |
6c10990d NC |
10687 | int cursig ATTRIBUTE_UNUSED, |
10688 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 10689 | { |
183e98be AM |
10690 | const char *note_name = "CORE"; |
10691 | #if defined (HAVE_PSTATUS32_T) | |
10692 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 10693 | |
183e98be AM |
10694 | if (bed->s->elfclass == ELFCLASS32) |
10695 | { | |
10696 | pstatus32_t pstat; | |
10697 | ||
10698 | memset (&pstat, 0, sizeof (pstat)); | |
10699 | pstat.pr_pid = pid & 0xffff; | |
10700 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10701 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10702 | return buf; | |
10703 | } | |
10704 | else | |
10705 | #endif | |
10706 | { | |
10707 | pstatus_t pstat; | |
10708 | ||
10709 | memset (&pstat, 0, sizeof (pstat)); | |
10710 | pstat.pr_pid = pid & 0xffff; | |
10711 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10712 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10713 | return buf; | |
10714 | } | |
7c76fa91 MS |
10715 | } |
10716 | #endif /* HAVE_PSTATUS_T */ | |
10717 | ||
10718 | char * | |
217aa764 AM |
10719 | elfcore_write_prfpreg (bfd *abfd, |
10720 | char *buf, | |
10721 | int *bufsiz, | |
10722 | const void *fpregs, | |
10723 | int size) | |
7c76fa91 | 10724 | { |
183e98be | 10725 | const char *note_name = "CORE"; |
47d9a591 | 10726 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10727 | note_name, NT_FPREGSET, fpregs, size); |
10728 | } | |
10729 | ||
10730 | char * | |
217aa764 AM |
10731 | elfcore_write_prxfpreg (bfd *abfd, |
10732 | char *buf, | |
10733 | int *bufsiz, | |
10734 | const void *xfpregs, | |
10735 | int size) | |
7c76fa91 MS |
10736 | { |
10737 | char *note_name = "LINUX"; | |
47d9a591 | 10738 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10739 | note_name, NT_PRXFPREG, xfpregs, size); |
10740 | } | |
10741 | ||
4339cae0 L |
10742 | char * |
10743 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
10744 | const void *xfpregs, int size) | |
10745 | { | |
97de3545 JB |
10746 | char *note_name; |
10747 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
10748 | note_name = "FreeBSD"; | |
10749 | else | |
10750 | note_name = "LINUX"; | |
4339cae0 L |
10751 | return elfcore_write_note (abfd, buf, bufsiz, |
10752 | note_name, NT_X86_XSTATE, xfpregs, size); | |
10753 | } | |
10754 | ||
97753bd5 AM |
10755 | char * |
10756 | elfcore_write_ppc_vmx (bfd *abfd, | |
10757 | char *buf, | |
10758 | int *bufsiz, | |
10759 | const void *ppc_vmx, | |
10760 | int size) | |
10761 | { | |
10762 | char *note_name = "LINUX"; | |
10763 | return elfcore_write_note (abfd, buf, bufsiz, | |
10764 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
10765 | } | |
10766 | ||
89eeb0bc LM |
10767 | char * |
10768 | elfcore_write_ppc_vsx (bfd *abfd, | |
07d6d2b8 AM |
10769 | char *buf, |
10770 | int *bufsiz, | |
10771 | const void *ppc_vsx, | |
10772 | int size) | |
89eeb0bc LM |
10773 | { |
10774 | char *note_name = "LINUX"; | |
10775 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 10776 | note_name, NT_PPC_VSX, ppc_vsx, size); |
89eeb0bc LM |
10777 | } |
10778 | ||
0675e188 UW |
10779 | static char * |
10780 | elfcore_write_s390_high_gprs (bfd *abfd, | |
10781 | char *buf, | |
10782 | int *bufsiz, | |
10783 | const void *s390_high_gprs, | |
10784 | int size) | |
10785 | { | |
10786 | char *note_name = "LINUX"; | |
10787 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 10788 | note_name, NT_S390_HIGH_GPRS, |
0675e188 UW |
10789 | s390_high_gprs, size); |
10790 | } | |
10791 | ||
d7eeb400 MS |
10792 | char * |
10793 | elfcore_write_s390_timer (bfd *abfd, | |
07d6d2b8 AM |
10794 | char *buf, |
10795 | int *bufsiz, | |
10796 | const void *s390_timer, | |
10797 | int size) | |
d7eeb400 MS |
10798 | { |
10799 | char *note_name = "LINUX"; | |
10800 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 10801 | note_name, NT_S390_TIMER, s390_timer, size); |
d7eeb400 MS |
10802 | } |
10803 | ||
10804 | char * | |
10805 | elfcore_write_s390_todcmp (bfd *abfd, | |
07d6d2b8 AM |
10806 | char *buf, |
10807 | int *bufsiz, | |
10808 | const void *s390_todcmp, | |
10809 | int size) | |
d7eeb400 MS |
10810 | { |
10811 | char *note_name = "LINUX"; | |
10812 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 10813 | note_name, NT_S390_TODCMP, s390_todcmp, size); |
d7eeb400 MS |
10814 | } |
10815 | ||
10816 | char * | |
10817 | elfcore_write_s390_todpreg (bfd *abfd, | |
07d6d2b8 AM |
10818 | char *buf, |
10819 | int *bufsiz, | |
10820 | const void *s390_todpreg, | |
10821 | int size) | |
d7eeb400 MS |
10822 | { |
10823 | char *note_name = "LINUX"; | |
10824 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 10825 | note_name, NT_S390_TODPREG, s390_todpreg, size); |
d7eeb400 MS |
10826 | } |
10827 | ||
10828 | char * | |
10829 | elfcore_write_s390_ctrs (bfd *abfd, | |
07d6d2b8 AM |
10830 | char *buf, |
10831 | int *bufsiz, | |
10832 | const void *s390_ctrs, | |
10833 | int size) | |
d7eeb400 MS |
10834 | { |
10835 | char *note_name = "LINUX"; | |
10836 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 10837 | note_name, NT_S390_CTRS, s390_ctrs, size); |
d7eeb400 MS |
10838 | } |
10839 | ||
10840 | char * | |
10841 | elfcore_write_s390_prefix (bfd *abfd, | |
07d6d2b8 AM |
10842 | char *buf, |
10843 | int *bufsiz, | |
10844 | const void *s390_prefix, | |
10845 | int size) | |
d7eeb400 MS |
10846 | { |
10847 | char *note_name = "LINUX"; | |
10848 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 10849 | note_name, NT_S390_PREFIX, s390_prefix, size); |
d7eeb400 MS |
10850 | } |
10851 | ||
355b81d9 UW |
10852 | char * |
10853 | elfcore_write_s390_last_break (bfd *abfd, | |
10854 | char *buf, | |
10855 | int *bufsiz, | |
10856 | const void *s390_last_break, | |
10857 | int size) | |
10858 | { | |
10859 | char *note_name = "LINUX"; | |
10860 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 10861 | note_name, NT_S390_LAST_BREAK, |
355b81d9 UW |
10862 | s390_last_break, size); |
10863 | } | |
10864 | ||
10865 | char * | |
10866 | elfcore_write_s390_system_call (bfd *abfd, | |
10867 | char *buf, | |
10868 | int *bufsiz, | |
10869 | const void *s390_system_call, | |
10870 | int size) | |
10871 | { | |
10872 | char *note_name = "LINUX"; | |
10873 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 10874 | note_name, NT_S390_SYSTEM_CALL, |
355b81d9 UW |
10875 | s390_system_call, size); |
10876 | } | |
10877 | ||
abb3f6cc NC |
10878 | char * |
10879 | elfcore_write_s390_tdb (bfd *abfd, | |
10880 | char *buf, | |
10881 | int *bufsiz, | |
10882 | const void *s390_tdb, | |
10883 | int size) | |
10884 | { | |
10885 | char *note_name = "LINUX"; | |
10886 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 10887 | note_name, NT_S390_TDB, s390_tdb, size); |
abb3f6cc NC |
10888 | } |
10889 | ||
4ef9f41a AA |
10890 | char * |
10891 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
10892 | char *buf, | |
10893 | int *bufsiz, | |
10894 | const void *s390_vxrs_low, | |
10895 | int size) | |
10896 | { | |
10897 | char *note_name = "LINUX"; | |
10898 | return elfcore_write_note (abfd, buf, bufsiz, | |
10899 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
10900 | } | |
10901 | ||
10902 | char * | |
10903 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
10904 | char *buf, | |
10905 | int *bufsiz, | |
10906 | const void *s390_vxrs_high, | |
10907 | int size) | |
10908 | { | |
10909 | char *note_name = "LINUX"; | |
10910 | return elfcore_write_note (abfd, buf, bufsiz, | |
10911 | note_name, NT_S390_VXRS_HIGH, | |
10912 | s390_vxrs_high, size); | |
10913 | } | |
10914 | ||
88ab90e8 AA |
10915 | char * |
10916 | elfcore_write_s390_gs_cb (bfd *abfd, | |
10917 | char *buf, | |
10918 | int *bufsiz, | |
10919 | const void *s390_gs_cb, | |
10920 | int size) | |
10921 | { | |
10922 | char *note_name = "LINUX"; | |
10923 | return elfcore_write_note (abfd, buf, bufsiz, | |
10924 | note_name, NT_S390_GS_CB, | |
10925 | s390_gs_cb, size); | |
10926 | } | |
10927 | ||
10928 | char * | |
10929 | elfcore_write_s390_gs_bc (bfd *abfd, | |
10930 | char *buf, | |
10931 | int *bufsiz, | |
10932 | const void *s390_gs_bc, | |
10933 | int size) | |
10934 | { | |
10935 | char *note_name = "LINUX"; | |
10936 | return elfcore_write_note (abfd, buf, bufsiz, | |
10937 | note_name, NT_S390_GS_BC, | |
10938 | s390_gs_bc, size); | |
10939 | } | |
10940 | ||
faa9a424 UW |
10941 | char * |
10942 | elfcore_write_arm_vfp (bfd *abfd, | |
10943 | char *buf, | |
10944 | int *bufsiz, | |
10945 | const void *arm_vfp, | |
10946 | int size) | |
10947 | { | |
10948 | char *note_name = "LINUX"; | |
10949 | return elfcore_write_note (abfd, buf, bufsiz, | |
10950 | note_name, NT_ARM_VFP, arm_vfp, size); | |
10951 | } | |
10952 | ||
652451f8 YZ |
10953 | char * |
10954 | elfcore_write_aarch_tls (bfd *abfd, | |
10955 | char *buf, | |
10956 | int *bufsiz, | |
10957 | const void *aarch_tls, | |
10958 | int size) | |
10959 | { | |
10960 | char *note_name = "LINUX"; | |
10961 | return elfcore_write_note (abfd, buf, bufsiz, | |
10962 | note_name, NT_ARM_TLS, aarch_tls, size); | |
10963 | } | |
10964 | ||
10965 | char * | |
10966 | elfcore_write_aarch_hw_break (bfd *abfd, | |
10967 | char *buf, | |
10968 | int *bufsiz, | |
10969 | const void *aarch_hw_break, | |
10970 | int size) | |
10971 | { | |
10972 | char *note_name = "LINUX"; | |
10973 | return elfcore_write_note (abfd, buf, bufsiz, | |
10974 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
10975 | } | |
10976 | ||
10977 | char * | |
10978 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
10979 | char *buf, | |
10980 | int *bufsiz, | |
10981 | const void *aarch_hw_watch, | |
10982 | int size) | |
10983 | { | |
10984 | char *note_name = "LINUX"; | |
10985 | return elfcore_write_note (abfd, buf, bufsiz, | |
10986 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
10987 | } | |
10988 | ||
bb864ac1 CES |
10989 | char * |
10990 | elfcore_write_register_note (bfd *abfd, | |
10991 | char *buf, | |
10992 | int *bufsiz, | |
10993 | const char *section, | |
10994 | const void *data, | |
10995 | int size) | |
10996 | { | |
10997 | if (strcmp (section, ".reg2") == 0) | |
10998 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
10999 | if (strcmp (section, ".reg-xfp") == 0) | |
11000 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
11001 | if (strcmp (section, ".reg-xstate") == 0) |
11002 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
11003 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
11004 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
11005 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
11006 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
11007 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
11008 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
11009 | if (strcmp (section, ".reg-s390-timer") == 0) |
11010 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
11011 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
11012 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
11013 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
11014 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
11015 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
11016 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
11017 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
11018 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
11019 | if (strcmp (section, ".reg-s390-last-break") == 0) |
11020 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
11021 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
11022 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
11023 | if (strcmp (section, ".reg-s390-tdb") == 0) |
11024 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
11025 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
11026 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
11027 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
11028 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
88ab90e8 AA |
11029 | if (strcmp (section, ".reg-s390-gs-cb") == 0) |
11030 | return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size); | |
11031 | if (strcmp (section, ".reg-s390-gs-bc") == 0) | |
11032 | return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
11033 | if (strcmp (section, ".reg-arm-vfp") == 0) |
11034 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
11035 | if (strcmp (section, ".reg-aarch-tls") == 0) |
11036 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
11037 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
11038 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
11039 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
11040 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
11041 | return NULL; |
11042 | } | |
11043 | ||
b34976b6 | 11044 | static bfd_boolean |
276da9b3 L |
11045 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset, |
11046 | size_t align) | |
252b5132 | 11047 | { |
c044fabd | 11048 | char *p; |
252b5132 | 11049 | |
276da9b3 L |
11050 | /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1. |
11051 | gABI specifies that PT_NOTE alignment should be aligned to 4 | |
11052 | bytes for 32-bit objects and to 8 bytes for 64-bit objects. If | |
11053 | align is less than 4, we use 4 byte alignment. */ | |
11054 | if (align < 4) | |
11055 | align = 4; | |
ef135d43 NC |
11056 | if (align != 4 && align != 8) |
11057 | return FALSE; | |
276da9b3 | 11058 | |
252b5132 RH |
11059 | p = buf; |
11060 | while (p < buf + size) | |
11061 | { | |
c044fabd | 11062 | Elf_External_Note *xnp = (Elf_External_Note *) p; |
252b5132 RH |
11063 | Elf_Internal_Note in; |
11064 | ||
baea7ef1 AM |
11065 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
11066 | return FALSE; | |
11067 | ||
dc810e39 | 11068 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 11069 | |
dc810e39 | 11070 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 11071 | in.namedata = xnp->name; |
baea7ef1 AM |
11072 | if (in.namesz > buf - in.namedata + size) |
11073 | return FALSE; | |
252b5132 | 11074 | |
dc810e39 | 11075 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
276da9b3 | 11076 | in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align); |
252b5132 | 11077 | in.descpos = offset + (in.descdata - buf); |
baea7ef1 AM |
11078 | if (in.descsz != 0 |
11079 | && (in.descdata >= buf + size | |
11080 | || in.descsz > buf - in.descdata + size)) | |
11081 | return FALSE; | |
252b5132 | 11082 | |
718175fa | 11083 | switch (bfd_get_format (abfd)) |
07d6d2b8 | 11084 | { |
718175fa JK |
11085 | default: |
11086 | return TRUE; | |
11087 | ||
11088 | case bfd_core: | |
f64e188b | 11089 | { |
8acbedd6 | 11090 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 11091 | struct |
718175fa | 11092 | { |
f64e188b | 11093 | const char * string; |
8acbedd6 | 11094 | size_t len; |
f64e188b | 11095 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 11096 | } |
f64e188b | 11097 | grokers[] = |
b15fa79e | 11098 | { |
8acbedd6 | 11099 | GROKER_ELEMENT ("", elfcore_grok_note), |
aa1ed4a9 | 11100 | GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), |
8acbedd6 KS |
11101 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), |
11102 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
11103 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
11104 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 11105 | }; |
8acbedd6 | 11106 | #undef GROKER_ELEMENT |
f64e188b NC |
11107 | int i; |
11108 | ||
11109 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
11110 | { |
11111 | if (in.namesz >= grokers[i].len | |
11112 | && strncmp (in.namedata, grokers[i].string, | |
11113 | grokers[i].len) == 0) | |
11114 | { | |
11115 | if (! grokers[i].func (abfd, & in)) | |
11116 | return FALSE; | |
11117 | break; | |
11118 | } | |
11119 | } | |
f64e188b NC |
11120 | break; |
11121 | } | |
718175fa JK |
11122 | |
11123 | case bfd_object: | |
11124 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
11125 | { | |
11126 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
11127 | return FALSE; | |
11128 | } | |
e21e5835 NC |
11129 | else if (in.namesz == sizeof "stapsdt" |
11130 | && strcmp (in.namedata, "stapsdt") == 0) | |
11131 | { | |
11132 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
11133 | return FALSE; | |
11134 | } | |
718175fa | 11135 | break; |
08a40648 | 11136 | } |
252b5132 | 11137 | |
276da9b3 | 11138 | p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align); |
252b5132 RH |
11139 | } |
11140 | ||
718175fa JK |
11141 | return TRUE; |
11142 | } | |
11143 | ||
11144 | static bfd_boolean | |
276da9b3 L |
11145 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size, |
11146 | size_t align) | |
718175fa JK |
11147 | { |
11148 | char *buf; | |
11149 | ||
957e1fc1 | 11150 | if (size == 0 || (size + 1) == 0) |
718175fa JK |
11151 | return TRUE; |
11152 | ||
11153 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
11154 | return FALSE; | |
11155 | ||
f64e188b | 11156 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
11157 | if (buf == NULL) |
11158 | return FALSE; | |
11159 | ||
f64e188b NC |
11160 | /* PR 17512: file: ec08f814 |
11161 | 0-termintate the buffer so that string searches will not overflow. */ | |
11162 | buf[size] = 0; | |
11163 | ||
718175fa | 11164 | if (bfd_bread (buf, size, abfd) != size |
276da9b3 | 11165 | || !elf_parse_notes (abfd, buf, size, offset, align)) |
718175fa JK |
11166 | { |
11167 | free (buf); | |
11168 | return FALSE; | |
11169 | } | |
11170 | ||
252b5132 | 11171 | free (buf); |
b34976b6 | 11172 | return TRUE; |
252b5132 | 11173 | } |
98d8431c JB |
11174 | \f |
11175 | /* Providing external access to the ELF program header table. */ | |
11176 | ||
11177 | /* Return an upper bound on the number of bytes required to store a | |
11178 | copy of ABFD's program header table entries. Return -1 if an error | |
11179 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 11180 | |
98d8431c | 11181 | long |
217aa764 | 11182 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
11183 | { |
11184 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
11185 | { | |
11186 | bfd_set_error (bfd_error_wrong_format); | |
11187 | return -1; | |
11188 | } | |
11189 | ||
936e320b | 11190 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
11191 | } |
11192 | ||
98d8431c JB |
11193 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
11194 | will be stored as an array of Elf_Internal_Phdr structures, as | |
11195 | defined in include/elf/internal.h. To find out how large the | |
11196 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
11197 | ||
11198 | Return the number of program header table entries read, or -1 if an | |
11199 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 11200 | |
98d8431c | 11201 | int |
217aa764 | 11202 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
11203 | { |
11204 | int num_phdrs; | |
11205 | ||
11206 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
11207 | { | |
11208 | bfd_set_error (bfd_error_wrong_format); | |
11209 | return -1; | |
11210 | } | |
11211 | ||
11212 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 11213 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
11214 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
11215 | ||
11216 | return num_phdrs; | |
11217 | } | |
ae4221d7 | 11218 | |
db6751f2 | 11219 | enum elf_reloc_type_class |
7e612e98 AM |
11220 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
11221 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
11222 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
11223 | { |
11224 | return reloc_class_normal; | |
11225 | } | |
f8df10f4 | 11226 | |
47d9a591 | 11227 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
11228 | relocation against a local symbol. */ |
11229 | ||
11230 | bfd_vma | |
217aa764 AM |
11231 | _bfd_elf_rela_local_sym (bfd *abfd, |
11232 | Elf_Internal_Sym *sym, | |
8517fae7 | 11233 | asection **psec, |
217aa764 | 11234 | Elf_Internal_Rela *rel) |
f8df10f4 | 11235 | { |
8517fae7 | 11236 | asection *sec = *psec; |
f8df10f4 JJ |
11237 | bfd_vma relocation; |
11238 | ||
11239 | relocation = (sec->output_section->vma | |
11240 | + sec->output_offset | |
11241 | + sym->st_value); | |
11242 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 11243 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 11244 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 11245 | { |
f8df10f4 | 11246 | rel->r_addend = |
8517fae7 | 11247 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 11248 | elf_section_data (sec)->sec_info, |
753731ee AM |
11249 | sym->st_value + rel->r_addend); |
11250 | if (sec != *psec) | |
11251 | { | |
11252 | /* If we have changed the section, and our original section is | |
11253 | marked with SEC_EXCLUDE, it means that the original | |
11254 | SEC_MERGE section has been completely subsumed in some | |
11255 | other SEC_MERGE section. In this case, we need to leave | |
11256 | some info around for --emit-relocs. */ | |
11257 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
11258 | sec->kept_section = *psec; | |
11259 | sec = *psec; | |
11260 | } | |
8517fae7 AM |
11261 | rel->r_addend -= relocation; |
11262 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
11263 | } |
11264 | return relocation; | |
11265 | } | |
c629eae0 JJ |
11266 | |
11267 | bfd_vma | |
217aa764 AM |
11268 | _bfd_elf_rel_local_sym (bfd *abfd, |
11269 | Elf_Internal_Sym *sym, | |
11270 | asection **psec, | |
11271 | bfd_vma addend) | |
47d9a591 | 11272 | { |
c629eae0 JJ |
11273 | asection *sec = *psec; |
11274 | ||
dbaa2011 | 11275 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
11276 | return sym->st_value + addend; |
11277 | ||
11278 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 11279 | elf_section_data (sec)->sec_info, |
753731ee | 11280 | sym->st_value + addend); |
c629eae0 JJ |
11281 | } |
11282 | ||
37b01f6a DG |
11283 | /* Adjust an address within a section. Given OFFSET within SEC, return |
11284 | the new offset within the section, based upon changes made to the | |
11285 | section. Returns -1 if the offset is now invalid. | |
11286 | The offset (in abnd out) is in target sized bytes, however big a | |
11287 | byte may be. */ | |
11288 | ||
c629eae0 | 11289 | bfd_vma |
217aa764 | 11290 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 11291 | struct bfd_link_info *info, |
217aa764 AM |
11292 | asection *sec, |
11293 | bfd_vma offset) | |
c629eae0 | 11294 | { |
68bfbfcc | 11295 | switch (sec->sec_info_type) |
65765700 | 11296 | { |
dbaa2011 | 11297 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
11298 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
11299 | offset); | |
dbaa2011 | 11300 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 11301 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 11302 | |
65765700 | 11303 | default: |
310fd250 L |
11304 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
11305 | { | |
37b01f6a | 11306 | /* Reverse the offset. */ |
310fd250 L |
11307 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11308 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
11309 | |
11310 | /* address_size and sec->size are in octets. Convert | |
11311 | to bytes before subtracting the original offset. */ | |
11312 | offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset; | |
310fd250 | 11313 | } |
65765700 JJ |
11314 | return offset; |
11315 | } | |
c629eae0 | 11316 | } |
3333a7c3 RM |
11317 | \f |
11318 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
11319 | reconstruct an ELF file by reading the segments out of remote memory | |
11320 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
11321 | points to. If not null, *LOADBASEP is filled in with the difference | |
11322 | between the VMAs from which the segments were read, and the VMAs the | |
11323 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
11324 | ||
11325 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
11326 | remote memory at target address VMA into the local buffer at MYADDR; it | |
11327 | should return zero on success or an `errno' code on failure. TEMPL must | |
11328 | be a BFD for an ELF target with the word size and byte order found in | |
11329 | the remote memory. */ | |
11330 | ||
11331 | bfd * | |
217aa764 AM |
11332 | bfd_elf_bfd_from_remote_memory |
11333 | (bfd *templ, | |
11334 | bfd_vma ehdr_vma, | |
f0a5d95a | 11335 | bfd_size_type size, |
217aa764 | 11336 | bfd_vma *loadbasep, |
fe78531d | 11337 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
11338 | { |
11339 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 11340 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 11341 | } |
4c45e5c9 JJ |
11342 | \f |
11343 | long | |
c9727e01 AM |
11344 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
11345 | long symcount ATTRIBUTE_UNUSED, | |
11346 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 11347 | long dynsymcount, |
c9727e01 AM |
11348 | asymbol **dynsyms, |
11349 | asymbol **ret) | |
4c45e5c9 JJ |
11350 | { |
11351 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11352 | asection *relplt; | |
11353 | asymbol *s; | |
11354 | const char *relplt_name; | |
11355 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
11356 | arelent *p; | |
11357 | long count, i, n; | |
11358 | size_t size; | |
11359 | Elf_Internal_Shdr *hdr; | |
11360 | char *names; | |
11361 | asection *plt; | |
11362 | ||
8615f3f2 AM |
11363 | *ret = NULL; |
11364 | ||
90e3cdf2 JJ |
11365 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
11366 | return 0; | |
11367 | ||
8615f3f2 AM |
11368 | if (dynsymcount <= 0) |
11369 | return 0; | |
11370 | ||
4c45e5c9 JJ |
11371 | if (!bed->plt_sym_val) |
11372 | return 0; | |
11373 | ||
11374 | relplt_name = bed->relplt_name; | |
11375 | if (relplt_name == NULL) | |
d35fd659 | 11376 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
11377 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
11378 | if (relplt == NULL) | |
11379 | return 0; | |
11380 | ||
11381 | hdr = &elf_section_data (relplt)->this_hdr; | |
11382 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
11383 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
11384 | return 0; | |
11385 | ||
11386 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
11387 | if (plt == NULL) | |
11388 | return 0; | |
11389 | ||
11390 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 11391 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
11392 | return -1; |
11393 | ||
eea6121a | 11394 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
11395 | size = count * sizeof (asymbol); |
11396 | p = relplt->relocation; | |
cb53bf42 | 11397 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
11398 | { |
11399 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
11400 | if (p->addend != 0) | |
11401 | { | |
11402 | #ifdef BFD64 | |
11403 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
11404 | #else | |
11405 | size += sizeof ("+0x") - 1 + 8; | |
11406 | #endif | |
11407 | } | |
11408 | } | |
4c45e5c9 | 11409 | |
a50b1753 | 11410 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
11411 | if (s == NULL) |
11412 | return -1; | |
11413 | ||
11414 | names = (char *) (s + count); | |
11415 | p = relplt->relocation; | |
11416 | n = 0; | |
cb53bf42 | 11417 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
11418 | { |
11419 | size_t len; | |
11420 | bfd_vma addr; | |
11421 | ||
11422 | addr = bed->plt_sym_val (i, plt, p); | |
11423 | if (addr == (bfd_vma) -1) | |
11424 | continue; | |
11425 | ||
11426 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
11427 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
11428 | we are defining a symbol, ensure one of them is set. */ | |
11429 | if ((s->flags & BSF_LOCAL) == 0) | |
11430 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 11431 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
11432 | s->section = plt; |
11433 | s->value = addr - plt->vma; | |
11434 | s->name = names; | |
8f39ba8e | 11435 | s->udata.p = NULL; |
4c45e5c9 JJ |
11436 | len = strlen ((*p->sym_ptr_ptr)->name); |
11437 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
11438 | names += len; | |
041de40d AM |
11439 | if (p->addend != 0) |
11440 | { | |
1d770845 | 11441 | char buf[30], *a; |
d324f6d6 | 11442 | |
041de40d AM |
11443 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
11444 | names += sizeof ("+0x") - 1; | |
1d770845 L |
11445 | bfd_sprintf_vma (abfd, buf, p->addend); |
11446 | for (a = buf; *a == '0'; ++a) | |
11447 | ; | |
11448 | len = strlen (a); | |
11449 | memcpy (names, a, len); | |
11450 | names += len; | |
041de40d | 11451 | } |
4c45e5c9 JJ |
11452 | memcpy (names, "@plt", sizeof ("@plt")); |
11453 | names += sizeof ("@plt"); | |
8f39ba8e | 11454 | ++s, ++n; |
4c45e5c9 JJ |
11455 | } |
11456 | ||
11457 | return n; | |
11458 | } | |
3d7f7666 | 11459 | |
821e6ff6 AM |
11460 | /* It is only used by x86-64 so far. |
11461 | ??? This repeats *COM* id of zero. sec->id is supposed to be unique, | |
7eacd66b AM |
11462 | but current usage would allow all of _bfd_std_section to be zero. */ |
11463 | static const asymbol lcomm_sym | |
11464 | = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); | |
3b22753a | 11465 | asection _bfd_elf_large_com_section |
7eacd66b | 11466 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, |
821e6ff6 | 11467 | "LARGE_COMMON", 0, SEC_IS_COMMON); |
ecca9871 | 11468 | |
d1036acb | 11469 | void |
78245035 L |
11470 | _bfd_elf_post_process_headers (bfd * abfd, |
11471 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
11472 | { |
11473 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
11474 | ||
11475 | i_ehdrp = elf_elfheader (abfd); | |
11476 | ||
11477 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
11478 | |
11479 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 11480 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 11481 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 11482 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 11483 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 11484 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 11485 | } |
fcb93ecf PB |
11486 | |
11487 | ||
11488 | /* Return TRUE for ELF symbol types that represent functions. | |
11489 | This is the default version of this function, which is sufficient for | |
d8045f23 | 11490 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
11491 | |
11492 | bfd_boolean | |
11493 | _bfd_elf_is_function_type (unsigned int type) | |
11494 | { | |
d8045f23 NC |
11495 | return (type == STT_FUNC |
11496 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 11497 | } |
9f296da3 | 11498 | |
aef36ac1 AM |
11499 | /* If the ELF symbol SYM might be a function in SEC, return the |
11500 | function size and set *CODE_OFF to the function's entry point, | |
11501 | otherwise return zero. */ | |
9f296da3 | 11502 | |
aef36ac1 AM |
11503 | bfd_size_type |
11504 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
11505 | bfd_vma *code_off) | |
9f296da3 | 11506 | { |
aef36ac1 AM |
11507 | bfd_size_type size; |
11508 | ||
ff9e0f5b | 11509 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
11510 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
11511 | || sym->section != sec) | |
11512 | return 0; | |
ff9e0f5b | 11513 | |
ff9e0f5b | 11514 | *code_off = sym->value; |
aef36ac1 AM |
11515 | size = 0; |
11516 | if (!(sym->flags & BSF_SYNTHETIC)) | |
11517 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
11518 | if (size == 0) | |
11519 | size = 1; | |
11520 | return size; | |
9f296da3 | 11521 | } |