Commit | Line | Data |
---|---|---|
32090b8e | 1 | /* ELF executable support for BFD. |
6014cea7 | 2 | Copyright 1993, 1994, 1995, 1996 Free Software Foundation, Inc. |
32090b8e KR |
3 | |
4 | This file is part of BFD, the Binary File Descriptor library. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
6f904fce | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
32090b8e | 19 | |
d1b44e83 ILT |
20 | /* |
21 | ||
22 | SECTION | |
23 | ELF backends | |
24 | ||
25 | BFD support for ELF formats is being worked on. | |
26 | Currently, the best supported back ends are for sparc and i386 | |
27 | (running svr4 or Solaris 2). | |
28 | ||
29 | Documentation of the internals of the support code still needs | |
30 | to be written. The code is changing quickly enough that we | |
31 | haven't bothered yet. | |
32 | */ | |
33 | ||
32090b8e KR |
34 | #include "bfd.h" |
35 | #include "sysdep.h" | |
013dec1a | 36 | #include "bfdlink.h" |
32090b8e KR |
37 | #include "libbfd.h" |
38 | #define ARCH_SIZE 0 | |
6ab826bd | 39 | #include "elf-bfd.h" |
32090b8e | 40 | |
fd0198f0 ILT |
41 | static INLINE struct elf_segment_map *make_mapping |
42 | PARAMS ((bfd *, asection **, unsigned int, unsigned int)); | |
43 | static int elf_sort_sections PARAMS ((const PTR, const PTR)); | |
44 | static boolean assign_file_positions_for_segments PARAMS ((bfd *)); | |
45 | static boolean assign_file_positions_except_relocs PARAMS ((bfd *)); | |
ede4eed4 KR |
46 | static boolean prep_headers PARAMS ((bfd *)); |
47 | static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **)); | |
3dbf33ee | 48 | static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *)); |
ede4eed4 | 49 | |
32090b8e KR |
50 | /* Standard ELF hash function. Do not change this function; you will |
51 | cause invalid hash tables to be generated. (Well, you would if this | |
52 | were being used yet.) */ | |
53 | unsigned long | |
013dec1a ILT |
54 | bfd_elf_hash (name) |
55 | CONST unsigned char *name; | |
32090b8e KR |
56 | { |
57 | unsigned long h = 0; | |
58 | unsigned long g; | |
59 | int ch; | |
60 | ||
61 | while ((ch = *name++) != '\0') | |
62 | { | |
63 | h = (h << 4) + ch; | |
64 | if ((g = (h & 0xf0000000)) != 0) | |
65 | { | |
66 | h ^= g >> 24; | |
67 | h &= ~g; | |
68 | } | |
69 | } | |
70 | return h; | |
71 | } | |
72 | ||
73 | /* Read a specified number of bytes at a specified offset in an ELF | |
74 | file, into a newly allocated buffer, and return a pointer to the | |
75 | buffer. */ | |
76 | ||
77 | static char * | |
013dec1a ILT |
78 | elf_read (abfd, offset, size) |
79 | bfd * abfd; | |
80 | long offset; | |
ae115e51 | 81 | unsigned int size; |
32090b8e KR |
82 | { |
83 | char *buf; | |
84 | ||
85 | if ((buf = bfd_alloc (abfd, size)) == NULL) | |
a9713b91 | 86 | return NULL; |
32090b8e | 87 | if (bfd_seek (abfd, offset, SEEK_SET) == -1) |
013dec1a | 88 | return NULL; |
32090b8e KR |
89 | if (bfd_read ((PTR) buf, size, 1, abfd) != size) |
90 | { | |
013dec1a ILT |
91 | if (bfd_get_error () != bfd_error_system_call) |
92 | bfd_set_error (bfd_error_file_truncated); | |
32090b8e KR |
93 | return NULL; |
94 | } | |
95 | return buf; | |
96 | } | |
97 | ||
98 | boolean | |
013dec1a ILT |
99 | elf_mkobject (abfd) |
100 | bfd * abfd; | |
32090b8e KR |
101 | { |
102 | /* this just does initialization */ | |
103 | /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */ | |
104 | elf_tdata (abfd) = (struct elf_obj_tdata *) | |
105 | bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)); | |
106 | if (elf_tdata (abfd) == 0) | |
a9713b91 | 107 | return false; |
32090b8e KR |
108 | /* since everything is done at close time, do we need any |
109 | initialization? */ | |
110 | ||
111 | return true; | |
112 | } | |
113 | ||
114 | char * | |
ede4eed4 | 115 | bfd_elf_get_str_section (abfd, shindex) |
013dec1a ILT |
116 | bfd * abfd; |
117 | unsigned int shindex; | |
32090b8e KR |
118 | { |
119 | Elf_Internal_Shdr **i_shdrp; | |
120 | char *shstrtab = NULL; | |
121 | unsigned int offset; | |
122 | unsigned int shstrtabsize; | |
123 | ||
124 | i_shdrp = elf_elfsections (abfd); | |
125 | if (i_shdrp == 0 || i_shdrp[shindex] == 0) | |
126 | return 0; | |
127 | ||
b176e1e9 | 128 | shstrtab = (char *) i_shdrp[shindex]->contents; |
32090b8e KR |
129 | if (shstrtab == NULL) |
130 | { | |
131 | /* No cached one, attempt to read, and cache what we read. */ | |
132 | offset = i_shdrp[shindex]->sh_offset; | |
133 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
134 | shstrtab = elf_read (abfd, offset, shstrtabsize); | |
b176e1e9 | 135 | i_shdrp[shindex]->contents = (PTR) shstrtab; |
32090b8e KR |
136 | } |
137 | return shstrtab; | |
138 | } | |
139 | ||
140 | char * | |
ede4eed4 | 141 | bfd_elf_string_from_elf_section (abfd, shindex, strindex) |
013dec1a ILT |
142 | bfd * abfd; |
143 | unsigned int shindex; | |
144 | unsigned int strindex; | |
32090b8e KR |
145 | { |
146 | Elf_Internal_Shdr *hdr; | |
147 | ||
148 | if (strindex == 0) | |
149 | return ""; | |
150 | ||
151 | hdr = elf_elfsections (abfd)[shindex]; | |
152 | ||
b176e1e9 | 153 | if (hdr->contents == NULL |
ede4eed4 | 154 | && bfd_elf_get_str_section (abfd, shindex) == NULL) |
32090b8e KR |
155 | return NULL; |
156 | ||
b176e1e9 | 157 | return ((char *) hdr->contents) + strindex; |
32090b8e KR |
158 | } |
159 | ||
497c5434 | 160 | /* Make a BFD section from an ELF section. We store a pointer to the |
b176e1e9 | 161 | BFD section in the bfd_section field of the header. */ |
497c5434 ILT |
162 | |
163 | boolean | |
164 | _bfd_elf_make_section_from_shdr (abfd, hdr, name) | |
165 | bfd *abfd; | |
166 | Elf_Internal_Shdr *hdr; | |
167 | const char *name; | |
168 | { | |
169 | asection *newsect; | |
170 | flagword flags; | |
171 | ||
b176e1e9 | 172 | if (hdr->bfd_section != NULL) |
497c5434 | 173 | { |
b176e1e9 ILT |
174 | BFD_ASSERT (strcmp (name, |
175 | bfd_get_section_name (abfd, hdr->bfd_section)) == 0); | |
497c5434 ILT |
176 | return true; |
177 | } | |
178 | ||
179 | newsect = bfd_make_section_anyway (abfd, name); | |
180 | if (newsect == NULL) | |
181 | return false; | |
182 | ||
183 | newsect->filepos = hdr->sh_offset; | |
184 | ||
185 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
186 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
187 | || ! bfd_set_section_alignment (abfd, newsect, | |
188 | bfd_log2 (hdr->sh_addralign))) | |
189 | return false; | |
190 | ||
191 | flags = SEC_NO_FLAGS; | |
192 | if (hdr->sh_type != SHT_NOBITS) | |
193 | flags |= SEC_HAS_CONTENTS; | |
194 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
195 | { | |
196 | flags |= SEC_ALLOC; | |
197 | if (hdr->sh_type != SHT_NOBITS) | |
198 | flags |= SEC_LOAD; | |
199 | } | |
200 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
201 | flags |= SEC_READONLY; | |
202 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
203 | flags |= SEC_CODE; | |
7c6da9ca | 204 | else if ((flags & SEC_LOAD) != 0) |
497c5434 ILT |
205 | flags |= SEC_DATA; |
206 | ||
207 | /* The debugging sections appear to be recognized only by name, not | |
208 | any sort of flag. */ | |
209 | if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0 | |
210 | || strncmp (name, ".line", sizeof ".line" - 1) == 0 | |
211 | || strncmp (name, ".stab", sizeof ".stab" - 1) == 0) | |
212 | flags |= SEC_DEBUGGING; | |
213 | ||
214 | if (! bfd_set_section_flags (abfd, newsect, flags)) | |
215 | return false; | |
216 | ||
fd0198f0 ILT |
217 | if ((flags & SEC_ALLOC) != 0) |
218 | { | |
219 | Elf_Internal_Phdr *phdr; | |
220 | unsigned int i; | |
221 | ||
222 | /* Look through the phdrs to see if we need to adjust the lma. */ | |
223 | phdr = elf_tdata (abfd)->phdr; | |
224 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
225 | { | |
226 | if (phdr->p_type == PT_LOAD | |
6933148a | 227 | && phdr->p_paddr != 0 |
fd0198f0 ILT |
228 | && phdr->p_vaddr != phdr->p_paddr |
229 | && phdr->p_vaddr <= hdr->sh_addr | |
230 | && phdr->p_vaddr + phdr->p_memsz >= hdr->sh_addr + hdr->sh_size) | |
231 | { | |
232 | newsect->lma += phdr->p_paddr - phdr->p_vaddr; | |
233 | break; | |
234 | } | |
235 | } | |
236 | } | |
237 | ||
b176e1e9 | 238 | hdr->bfd_section = newsect; |
497c5434 ILT |
239 | elf_section_data (newsect)->this_hdr = *hdr; |
240 | ||
241 | return true; | |
242 | } | |
243 | ||
32090b8e KR |
244 | /* |
245 | INTERNAL_FUNCTION | |
246 | bfd_elf_find_section | |
247 | ||
248 | SYNOPSIS | |
249 | struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name); | |
250 | ||
251 | DESCRIPTION | |
252 | Helper functions for GDB to locate the string tables. | |
253 | Since BFD hides string tables from callers, GDB needs to use an | |
254 | internal hook to find them. Sun's .stabstr, in particular, | |
255 | isn't even pointed to by the .stab section, so ordinary | |
256 | mechanisms wouldn't work to find it, even if we had some. | |
257 | */ | |
258 | ||
259 | struct elf_internal_shdr * | |
013dec1a ILT |
260 | bfd_elf_find_section (abfd, name) |
261 | bfd * abfd; | |
262 | char *name; | |
32090b8e KR |
263 | { |
264 | Elf_Internal_Shdr **i_shdrp; | |
265 | char *shstrtab; | |
266 | unsigned int max; | |
267 | unsigned int i; | |
268 | ||
269 | i_shdrp = elf_elfsections (abfd); | |
270 | if (i_shdrp != NULL) | |
271 | { | |
ede4eed4 | 272 | shstrtab = bfd_elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx); |
32090b8e KR |
273 | if (shstrtab != NULL) |
274 | { | |
275 | max = elf_elfheader (abfd)->e_shnum; | |
276 | for (i = 1; i < max; i++) | |
277 | if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name)) | |
278 | return i_shdrp[i]; | |
279 | } | |
280 | } | |
281 | return 0; | |
282 | } | |
283 | ||
32090b8e KR |
284 | const char *const bfd_elf_section_type_names[] = { |
285 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
286 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
287 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
288 | }; | |
289 | ||
290 | /* ELF relocs are against symbols. If we are producing relocateable | |
291 | output, and the reloc is against an external symbol, and nothing | |
292 | has given us any additional addend, the resulting reloc will also | |
293 | be against the same symbol. In such a case, we don't want to | |
294 | change anything about the way the reloc is handled, since it will | |
295 | all be done at final link time. Rather than put special case code | |
296 | into bfd_perform_relocation, all the reloc types use this howto | |
297 | function. It just short circuits the reloc if producing | |
298 | relocateable output against an external symbol. */ | |
299 | ||
013dec1a | 300 | /*ARGSUSED*/ |
32090b8e KR |
301 | bfd_reloc_status_type |
302 | bfd_elf_generic_reloc (abfd, | |
303 | reloc_entry, | |
304 | symbol, | |
305 | data, | |
306 | input_section, | |
4c3721d5 ILT |
307 | output_bfd, |
308 | error_message) | |
32090b8e KR |
309 | bfd *abfd; |
310 | arelent *reloc_entry; | |
311 | asymbol *symbol; | |
312 | PTR data; | |
313 | asection *input_section; | |
314 | bfd *output_bfd; | |
4c3721d5 | 315 | char **error_message; |
32090b8e KR |
316 | { |
317 | if (output_bfd != (bfd *) NULL | |
318 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
d1b44e83 ILT |
319 | && (! reloc_entry->howto->partial_inplace |
320 | || reloc_entry->addend == 0)) | |
32090b8e KR |
321 | { |
322 | reloc_entry->address += input_section->output_offset; | |
323 | return bfd_reloc_ok; | |
324 | } | |
325 | ||
326 | return bfd_reloc_continue; | |
327 | } | |
013dec1a | 328 | \f |
27fb8f29 ILT |
329 | /* Print out the program headers. */ |
330 | ||
331 | boolean | |
332 | _bfd_elf_print_private_bfd_data (abfd, farg) | |
333 | bfd *abfd; | |
334 | PTR farg; | |
335 | { | |
336 | FILE *f = (FILE *) farg; | |
337 | Elf_Internal_Phdr *p; | |
02fcd126 ILT |
338 | asection *s; |
339 | bfd_byte *dynbuf = NULL; | |
27fb8f29 ILT |
340 | |
341 | p = elf_tdata (abfd)->phdr; | |
02fcd126 | 342 | if (p != NULL) |
27fb8f29 | 343 | { |
02fcd126 | 344 | unsigned int i, c; |
27fb8f29 | 345 | |
02fcd126 ILT |
346 | fprintf (f, "\nProgram Header:\n"); |
347 | c = elf_elfheader (abfd)->e_phnum; | |
348 | for (i = 0; i < c; i++, p++) | |
27fb8f29 | 349 | { |
02fcd126 ILT |
350 | const char *s; |
351 | char buf[20]; | |
352 | ||
353 | switch (p->p_type) | |
354 | { | |
355 | case PT_NULL: s = "NULL"; break; | |
356 | case PT_LOAD: s = "LOAD"; break; | |
357 | case PT_DYNAMIC: s = "DYNAMIC"; break; | |
358 | case PT_INTERP: s = "INTERP"; break; | |
359 | case PT_NOTE: s = "NOTE"; break; | |
360 | case PT_SHLIB: s = "SHLIB"; break; | |
361 | case PT_PHDR: s = "PHDR"; break; | |
362 | default: sprintf (buf, "0x%lx", p->p_type); s = buf; break; | |
363 | } | |
364 | fprintf (f, "%8s off 0x", s); | |
365 | fprintf_vma (f, p->p_offset); | |
366 | fprintf (f, " vaddr 0x"); | |
367 | fprintf_vma (f, p->p_vaddr); | |
368 | fprintf (f, " paddr 0x"); | |
369 | fprintf_vma (f, p->p_paddr); | |
370 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); | |
371 | fprintf (f, " filesz 0x"); | |
372 | fprintf_vma (f, p->p_filesz); | |
373 | fprintf (f, " memsz 0x"); | |
374 | fprintf_vma (f, p->p_memsz); | |
375 | fprintf (f, " flags %c%c%c", | |
376 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
377 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
378 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
379 | if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0) | |
380 | fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X)); | |
381 | fprintf (f, "\n"); | |
382 | } | |
383 | } | |
384 | ||
385 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
386 | if (s != NULL) | |
387 | { | |
388 | int elfsec; | |
389 | unsigned long link; | |
390 | bfd_byte *extdyn, *extdynend; | |
391 | size_t extdynsize; | |
392 | void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *)); | |
393 | ||
394 | fprintf (f, "\nDynamic Section:\n"); | |
395 | ||
396 | dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size); | |
397 | if (dynbuf == NULL) | |
398 | goto error_return; | |
399 | if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0, | |
400 | s->_raw_size)) | |
401 | goto error_return; | |
402 | ||
403 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
404 | if (elfsec == -1) | |
405 | goto error_return; | |
406 | link = elf_elfsections (abfd)[elfsec]->sh_link; | |
407 | ||
408 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
409 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
410 | ||
411 | extdyn = dynbuf; | |
412 | extdynend = extdyn + s->_raw_size; | |
413 | for (; extdyn < extdynend; extdyn += extdynsize) | |
414 | { | |
415 | Elf_Internal_Dyn dyn; | |
416 | const char *name; | |
417 | char ab[20]; | |
418 | boolean stringp; | |
419 | ||
420 | (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn); | |
421 | ||
422 | if (dyn.d_tag == DT_NULL) | |
423 | break; | |
424 | ||
425 | stringp = false; | |
426 | switch (dyn.d_tag) | |
427 | { | |
428 | default: | |
927d05b5 | 429 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); |
02fcd126 ILT |
430 | name = ab; |
431 | break; | |
432 | ||
433 | case DT_NEEDED: name = "NEEDED"; stringp = true; break; | |
434 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; | |
435 | case DT_PLTGOT: name = "PLTGOT"; break; | |
436 | case DT_HASH: name = "HASH"; break; | |
437 | case DT_STRTAB: name = "STRTAB"; break; | |
438 | case DT_SYMTAB: name = "SYMTAB"; break; | |
439 | case DT_RELA: name = "RELA"; break; | |
440 | case DT_RELASZ: name = "RELASZ"; break; | |
441 | case DT_RELAENT: name = "RELAENT"; break; | |
442 | case DT_STRSZ: name = "STRSZ"; break; | |
443 | case DT_SYMENT: name = "SYMENT"; break; | |
444 | case DT_INIT: name = "INIT"; break; | |
445 | case DT_FINI: name = "FINI"; break; | |
446 | case DT_SONAME: name = "SONAME"; stringp = true; break; | |
447 | case DT_RPATH: name = "RPATH"; stringp = true; break; | |
448 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; | |
449 | case DT_REL: name = "REL"; break; | |
450 | case DT_RELSZ: name = "RELSZ"; break; | |
451 | case DT_RELENT: name = "RELENT"; break; | |
452 | case DT_PLTREL: name = "PLTREL"; break; | |
453 | case DT_DEBUG: name = "DEBUG"; break; | |
454 | case DT_TEXTREL: name = "TEXTREL"; break; | |
455 | case DT_JMPREL: name = "JMPREL"; break; | |
456 | } | |
457 | ||
458 | fprintf (f, " %-11s ", name); | |
459 | if (! stringp) | |
927d05b5 | 460 | fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val); |
02fcd126 ILT |
461 | else |
462 | { | |
463 | const char *string; | |
464 | ||
465 | string = bfd_elf_string_from_elf_section (abfd, link, | |
466 | dyn.d_un.d_val); | |
467 | if (string == NULL) | |
468 | goto error_return; | |
469 | fprintf (f, "%s", string); | |
470 | } | |
471 | fprintf (f, "\n"); | |
27fb8f29 | 472 | } |
02fcd126 ILT |
473 | |
474 | free (dynbuf); | |
475 | dynbuf = NULL; | |
27fb8f29 ILT |
476 | } |
477 | ||
478 | return true; | |
02fcd126 ILT |
479 | |
480 | error_return: | |
481 | if (dynbuf != NULL) | |
482 | free (dynbuf); | |
483 | return false; | |
27fb8f29 ILT |
484 | } |
485 | ||
b176e1e9 ILT |
486 | /* Display ELF-specific fields of a symbol. */ |
487 | void | |
488 | bfd_elf_print_symbol (ignore_abfd, filep, symbol, how) | |
489 | bfd *ignore_abfd; | |
490 | PTR filep; | |
491 | asymbol *symbol; | |
492 | bfd_print_symbol_type how; | |
493 | { | |
494 | FILE *file = (FILE *) filep; | |
495 | switch (how) | |
496 | { | |
497 | case bfd_print_symbol_name: | |
498 | fprintf (file, "%s", symbol->name); | |
499 | break; | |
500 | case bfd_print_symbol_more: | |
501 | fprintf (file, "elf "); | |
502 | fprintf_vma (file, symbol->value); | |
503 | fprintf (file, " %lx", (long) symbol->flags); | |
504 | break; | |
505 | case bfd_print_symbol_all: | |
506 | { | |
507 | CONST char *section_name; | |
508 | section_name = symbol->section ? symbol->section->name : "(*none*)"; | |
509 | bfd_print_symbol_vandf ((PTR) file, symbol); | |
510 | fprintf (file, " %s\t", section_name); | |
511 | /* Print the "other" value for a symbol. For common symbols, | |
512 | we've already printed the size; now print the alignment. | |
513 | For other symbols, we have no specified alignment, and | |
514 | we've printed the address; now print the size. */ | |
515 | fprintf_vma (file, | |
516 | (bfd_is_com_section (symbol->section) | |
517 | ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value | |
518 | : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size)); | |
519 | fprintf (file, " %s", symbol->name); | |
520 | } | |
521 | break; | |
522 | } | |
523 | } | |
524 | \f | |
013dec1a ILT |
525 | /* Create an entry in an ELF linker hash table. */ |
526 | ||
5315c428 ILT |
527 | struct bfd_hash_entry * |
528 | _bfd_elf_link_hash_newfunc (entry, table, string) | |
013dec1a ILT |
529 | struct bfd_hash_entry *entry; |
530 | struct bfd_hash_table *table; | |
531 | const char *string; | |
532 | { | |
533 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
534 | ||
535 | /* Allocate the structure if it has not already been allocated by a | |
536 | subclass. */ | |
537 | if (ret == (struct elf_link_hash_entry *) NULL) | |
538 | ret = ((struct elf_link_hash_entry *) | |
539 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry))); | |
540 | if (ret == (struct elf_link_hash_entry *) NULL) | |
a9713b91 | 541 | return (struct bfd_hash_entry *) ret; |
013dec1a ILT |
542 | |
543 | /* Call the allocation method of the superclass. */ | |
544 | ret = ((struct elf_link_hash_entry *) | |
545 | _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
546 | table, string)); | |
547 | if (ret != (struct elf_link_hash_entry *) NULL) | |
548 | { | |
549 | /* Set local fields. */ | |
550 | ret->indx = -1; | |
551 | ret->size = 0; | |
013dec1a ILT |
552 | ret->dynindx = -1; |
553 | ret->dynstr_index = 0; | |
554 | ret->weakdef = NULL; | |
b176e1e9 ILT |
555 | ret->got_offset = (bfd_vma) -1; |
556 | ret->plt_offset = (bfd_vma) -1; | |
86aac8ea | 557 | ret->linker_section_pointer = (elf_linker_section_pointers_t *)0; |
013dec1a ILT |
558 | ret->type = STT_NOTYPE; |
559 | ret->elf_link_hash_flags = 0; | |
560 | } | |
561 | ||
562 | return (struct bfd_hash_entry *) ret; | |
563 | } | |
564 | ||
5315c428 ILT |
565 | /* Initialize an ELF linker hash table. */ |
566 | ||
567 | boolean | |
568 | _bfd_elf_link_hash_table_init (table, abfd, newfunc) | |
569 | struct elf_link_hash_table *table; | |
570 | bfd *abfd; | |
571 | struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *, | |
572 | struct bfd_hash_table *, | |
573 | const char *)); | |
574 | { | |
b176e1e9 | 575 | table->dynamic_sections_created = false; |
5315c428 | 576 | table->dynobj = NULL; |
b176e1e9 ILT |
577 | /* The first dynamic symbol is a dummy. */ |
578 | table->dynsymcount = 1; | |
5315c428 ILT |
579 | table->dynstr = NULL; |
580 | table->bucketcount = 0; | |
b176e1e9 | 581 | table->needed = NULL; |
5315c428 ILT |
582 | return _bfd_link_hash_table_init (&table->root, abfd, newfunc); |
583 | } | |
584 | ||
013dec1a ILT |
585 | /* Create an ELF linker hash table. */ |
586 | ||
587 | struct bfd_link_hash_table * | |
588 | _bfd_elf_link_hash_table_create (abfd) | |
589 | bfd *abfd; | |
590 | { | |
591 | struct elf_link_hash_table *ret; | |
592 | ||
593 | ret = ((struct elf_link_hash_table *) | |
594 | bfd_alloc (abfd, sizeof (struct elf_link_hash_table))); | |
595 | if (ret == (struct elf_link_hash_table *) NULL) | |
a9713b91 | 596 | return NULL; |
5315c428 ILT |
597 | |
598 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc)) | |
013dec1a ILT |
599 | { |
600 | bfd_release (abfd, ret); | |
601 | return NULL; | |
602 | } | |
603 | ||
013dec1a ILT |
604 | return &ret->root; |
605 | } | |
7c6da9ca ILT |
606 | |
607 | /* This is a hook for the ELF emulation code in the generic linker to | |
608 | tell the backend linker what file name to use for the DT_NEEDED | |
b176e1e9 ILT |
609 | entry for a dynamic object. The generic linker passes name as an |
610 | empty string to indicate that no DT_NEEDED entry should be made. */ | |
7c6da9ca ILT |
611 | |
612 | void | |
613 | bfd_elf_set_dt_needed_name (abfd, name) | |
614 | bfd *abfd; | |
615 | const char *name; | |
616 | { | |
b2193cc5 ILT |
617 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) |
618 | elf_dt_needed_name (abfd) = name; | |
7c6da9ca | 619 | } |
b176e1e9 ILT |
620 | |
621 | /* Get the list of DT_NEEDED entries for a link. */ | |
622 | ||
5fe14a9f | 623 | struct bfd_link_needed_list * |
b176e1e9 ILT |
624 | bfd_elf_get_needed_list (abfd, info) |
625 | bfd *abfd; | |
626 | struct bfd_link_info *info; | |
627 | { | |
b2193cc5 ILT |
628 | if (info->hash->creator->flavour != bfd_target_elf_flavour) |
629 | return NULL; | |
b176e1e9 ILT |
630 | return elf_hash_table (info)->needed; |
631 | } | |
ede4eed4 KR |
632 | \f |
633 | /* Allocate an ELF string table--force the first byte to be zero. */ | |
634 | ||
635 | struct bfd_strtab_hash * | |
636 | _bfd_elf_stringtab_init () | |
637 | { | |
638 | struct bfd_strtab_hash *ret; | |
639 | ||
640 | ret = _bfd_stringtab_init (); | |
641 | if (ret != NULL) | |
642 | { | |
643 | bfd_size_type loc; | |
644 | ||
645 | loc = _bfd_stringtab_add (ret, "", true, false); | |
646 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); | |
647 | if (loc == (bfd_size_type) -1) | |
648 | { | |
649 | _bfd_stringtab_free (ret); | |
650 | ret = NULL; | |
651 | } | |
652 | } | |
653 | return ret; | |
654 | } | |
655 | \f | |
656 | /* ELF .o/exec file reading */ | |
657 | ||
658 | /* Create a new bfd section from an ELF section header. */ | |
659 | ||
660 | boolean | |
661 | bfd_section_from_shdr (abfd, shindex) | |
662 | bfd *abfd; | |
663 | unsigned int shindex; | |
664 | { | |
665 | Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex]; | |
666 | Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); | |
667 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
668 | char *name; | |
669 | ||
670 | name = elf_string_from_elf_strtab (abfd, hdr->sh_name); | |
671 | ||
672 | switch (hdr->sh_type) | |
673 | { | |
674 | case SHT_NULL: | |
675 | /* Inactive section. Throw it away. */ | |
676 | return true; | |
677 | ||
678 | case SHT_PROGBITS: /* Normal section with contents. */ | |
679 | case SHT_DYNAMIC: /* Dynamic linking information. */ | |
680 | case SHT_NOBITS: /* .bss section. */ | |
681 | case SHT_HASH: /* .hash section. */ | |
5b3b9ff6 | 682 | case SHT_NOTE: /* .note section. */ |
ede4eed4 KR |
683 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); |
684 | ||
685 | case SHT_SYMTAB: /* A symbol table */ | |
686 | if (elf_onesymtab (abfd) == shindex) | |
687 | return true; | |
688 | ||
689 | BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); | |
690 | BFD_ASSERT (elf_onesymtab (abfd) == 0); | |
691 | elf_onesymtab (abfd) = shindex; | |
692 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
fd0198f0 | 693 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; |
ede4eed4 KR |
694 | abfd->flags |= HAS_SYMS; |
695 | ||
696 | /* Sometimes a shared object will map in the symbol table. If | |
697 | SHF_ALLOC is set, and this is a shared object, then we also | |
698 | treat this section as a BFD section. We can not base the | |
699 | decision purely on SHF_ALLOC, because that flag is sometimes | |
700 | set in a relocateable object file, which would confuse the | |
701 | linker. */ | |
702 | if ((hdr->sh_flags & SHF_ALLOC) != 0 | |
703 | && (abfd->flags & DYNAMIC) != 0 | |
704 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) | |
705 | return false; | |
706 | ||
707 | return true; | |
708 | ||
709 | case SHT_DYNSYM: /* A dynamic symbol table */ | |
710 | if (elf_dynsymtab (abfd) == shindex) | |
711 | return true; | |
712 | ||
713 | BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); | |
714 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); | |
715 | elf_dynsymtab (abfd) = shindex; | |
716 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
fd0198f0 | 717 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; |
ede4eed4 KR |
718 | abfd->flags |= HAS_SYMS; |
719 | ||
720 | /* Besides being a symbol table, we also treat this as a regular | |
721 | section, so that objcopy can handle it. */ | |
722 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
723 | ||
724 | case SHT_STRTAB: /* A string table */ | |
725 | if (hdr->bfd_section != NULL) | |
726 | return true; | |
727 | if (ehdr->e_shstrndx == shindex) | |
728 | { | |
729 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
730 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
731 | return true; | |
732 | } | |
733 | { | |
734 | unsigned int i; | |
735 | ||
736 | for (i = 1; i < ehdr->e_shnum; i++) | |
737 | { | |
738 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
739 | if (hdr2->sh_link == shindex) | |
740 | { | |
741 | if (! bfd_section_from_shdr (abfd, i)) | |
742 | return false; | |
743 | if (elf_onesymtab (abfd) == i) | |
744 | { | |
745 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
746 | elf_elfsections (abfd)[shindex] = | |
747 | &elf_tdata (abfd)->strtab_hdr; | |
748 | return true; | |
749 | } | |
750 | if (elf_dynsymtab (abfd) == i) | |
751 | { | |
752 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
fd0198f0 | 753 | elf_elfsections (abfd)[shindex] = hdr = |
ede4eed4 KR |
754 | &elf_tdata (abfd)->dynstrtab_hdr; |
755 | /* We also treat this as a regular section, so | |
756 | that objcopy can handle it. */ | |
757 | break; | |
758 | } | |
759 | #if 0 /* Not handling other string tables specially right now. */ | |
760 | hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */ | |
761 | /* We have a strtab for some random other section. */ | |
762 | newsect = (asection *) hdr2->bfd_section; | |
763 | if (!newsect) | |
764 | break; | |
765 | hdr->bfd_section = newsect; | |
766 | hdr2 = &elf_section_data (newsect)->str_hdr; | |
767 | *hdr2 = *hdr; | |
768 | elf_elfsections (abfd)[shindex] = hdr2; | |
769 | #endif | |
770 | } | |
771 | } | |
772 | } | |
773 | ||
774 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
775 | ||
776 | case SHT_REL: | |
777 | case SHT_RELA: | |
778 | /* *These* do a lot of work -- but build no sections! */ | |
779 | { | |
780 | asection *target_sect; | |
781 | Elf_Internal_Shdr *hdr2; | |
782 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; | |
783 | ||
ae115e51 ILT |
784 | /* For some incomprehensible reason Oracle distributes |
785 | libraries for Solaris in which some of the objects have | |
786 | bogus sh_link fields. It would be nice if we could just | |
787 | reject them, but, unfortunately, some people need to use | |
788 | them. We scan through the section headers; if we find only | |
789 | one suitable symbol table, we clobber the sh_link to point | |
790 | to it. I hope this doesn't break anything. */ | |
791 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
792 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) | |
793 | { | |
794 | int scan; | |
795 | int found; | |
796 | ||
797 | found = 0; | |
798 | for (scan = 1; scan < ehdr->e_shnum; scan++) | |
799 | { | |
800 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
801 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
802 | { | |
803 | if (found != 0) | |
804 | { | |
805 | found = 0; | |
806 | break; | |
807 | } | |
808 | found = scan; | |
809 | } | |
810 | } | |
811 | if (found != 0) | |
812 | hdr->sh_link = found; | |
813 | } | |
814 | ||
ede4eed4 | 815 | /* Get the symbol table. */ |
ae115e51 ILT |
816 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
817 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) | |
ede4eed4 KR |
818 | return false; |
819 | ||
820 | /* If this reloc section does not use the main symbol table we | |
821 | don't treat it as a reloc section. BFD can't adequately | |
822 | represent such a section, so at least for now, we don't | |
823 | try. We just present it as a normal section. */ | |
824 | if (hdr->sh_link != elf_onesymtab (abfd)) | |
825 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
826 | ||
827 | /* Don't allow REL relocations on a machine that uses RELA and | |
828 | vice versa. */ | |
829 | /* @@ Actually, the generic ABI does suggest that both might be | |
830 | used in one file. But the four ABI Processor Supplements I | |
831 | have access to right now all specify that only one is used on | |
832 | each of those architectures. It's conceivable that, e.g., a | |
833 | bunch of absolute 32-bit relocs might be more compact in REL | |
834 | form even on a RELA machine... */ | |
835 | BFD_ASSERT (use_rela_p | |
836 | ? (hdr->sh_type == SHT_RELA | |
837 | && hdr->sh_entsize == bed->s->sizeof_rela) | |
838 | : (hdr->sh_type == SHT_REL | |
839 | && hdr->sh_entsize == bed->s->sizeof_rel)); | |
840 | ||
841 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
842 | return false; | |
843 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); | |
844 | if (target_sect == NULL) | |
845 | return false; | |
846 | ||
847 | hdr2 = &elf_section_data (target_sect)->rel_hdr; | |
848 | *hdr2 = *hdr; | |
849 | elf_elfsections (abfd)[shindex] = hdr2; | |
850 | target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize; | |
851 | target_sect->flags |= SEC_RELOC; | |
852 | target_sect->relocation = NULL; | |
853 | target_sect->rel_filepos = hdr->sh_offset; | |
854 | abfd->flags |= HAS_RELOC; | |
855 | return true; | |
856 | } | |
857 | break; | |
858 | ||
ede4eed4 | 859 | case SHT_SHLIB: |
ede4eed4 KR |
860 | return true; |
861 | ||
862 | default: | |
863 | /* Check for any processor-specific section types. */ | |
864 | { | |
865 | if (bed->elf_backend_section_from_shdr) | |
866 | (*bed->elf_backend_section_from_shdr) (abfd, hdr, name); | |
867 | } | |
868 | break; | |
869 | } | |
870 | ||
871 | return true; | |
872 | } | |
873 | ||
874 | /* Given an ELF section number, retrieve the corresponding BFD | |
875 | section. */ | |
876 | ||
877 | asection * | |
878 | bfd_section_from_elf_index (abfd, index) | |
879 | bfd *abfd; | |
880 | unsigned int index; | |
881 | { | |
882 | BFD_ASSERT (index > 0 && index < SHN_LORESERVE); | |
883 | if (index >= elf_elfheader (abfd)->e_shnum) | |
884 | return NULL; | |
885 | return elf_elfsections (abfd)[index]->bfd_section; | |
886 | } | |
887 | ||
888 | boolean | |
889 | _bfd_elf_new_section_hook (abfd, sec) | |
890 | bfd *abfd; | |
891 | asection *sec; | |
892 | { | |
893 | struct bfd_elf_section_data *sdata; | |
894 | ||
895 | sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata)); | |
896 | if (!sdata) | |
a9713b91 | 897 | return false; |
ede4eed4 KR |
898 | sec->used_by_bfd = (PTR) sdata; |
899 | memset (sdata, 0, sizeof (*sdata)); | |
900 | return true; | |
901 | } | |
902 | ||
903 | /* Create a new bfd section from an ELF program header. | |
904 | ||
905 | Since program segments have no names, we generate a synthetic name | |
906 | of the form segment<NUM>, where NUM is generally the index in the | |
907 | program header table. For segments that are split (see below) we | |
908 | generate the names segment<NUM>a and segment<NUM>b. | |
909 | ||
910 | Note that some program segments may have a file size that is different than | |
911 | (less than) the memory size. All this means is that at execution the | |
912 | system must allocate the amount of memory specified by the memory size, | |
913 | but only initialize it with the first "file size" bytes read from the | |
914 | file. This would occur for example, with program segments consisting | |
915 | of combined data+bss. | |
916 | ||
917 | To handle the above situation, this routine generates TWO bfd sections | |
918 | for the single program segment. The first has the length specified by | |
919 | the file size of the segment, and the second has the length specified | |
920 | by the difference between the two sizes. In effect, the segment is split | |
921 | into it's initialized and uninitialized parts. | |
922 | ||
923 | */ | |
924 | ||
925 | boolean | |
926 | bfd_section_from_phdr (abfd, hdr, index) | |
927 | bfd *abfd; | |
928 | Elf_Internal_Phdr *hdr; | |
929 | int index; | |
930 | { | |
931 | asection *newsect; | |
932 | char *name; | |
933 | char namebuf[64]; | |
934 | int split; | |
935 | ||
936 | split = ((hdr->p_memsz > 0) && | |
937 | (hdr->p_filesz > 0) && | |
938 | (hdr->p_memsz > hdr->p_filesz)); | |
939 | sprintf (namebuf, split ? "segment%da" : "segment%d", index); | |
940 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
941 | if (!name) | |
a9713b91 | 942 | return false; |
ede4eed4 KR |
943 | strcpy (name, namebuf); |
944 | newsect = bfd_make_section (abfd, name); | |
945 | if (newsect == NULL) | |
946 | return false; | |
947 | newsect->vma = hdr->p_vaddr; | |
ae115e51 | 948 | newsect->lma = hdr->p_paddr; |
ede4eed4 KR |
949 | newsect->_raw_size = hdr->p_filesz; |
950 | newsect->filepos = hdr->p_offset; | |
951 | newsect->flags |= SEC_HAS_CONTENTS; | |
952 | if (hdr->p_type == PT_LOAD) | |
953 | { | |
954 | newsect->flags |= SEC_ALLOC; | |
955 | newsect->flags |= SEC_LOAD; | |
956 | if (hdr->p_flags & PF_X) | |
957 | { | |
958 | /* FIXME: all we known is that it has execute PERMISSION, | |
959 | may be data. */ | |
960 | newsect->flags |= SEC_CODE; | |
961 | } | |
962 | } | |
963 | if (!(hdr->p_flags & PF_W)) | |
964 | { | |
965 | newsect->flags |= SEC_READONLY; | |
966 | } | |
967 | ||
968 | if (split) | |
969 | { | |
970 | sprintf (namebuf, "segment%db", index); | |
971 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
972 | if (!name) | |
a9713b91 | 973 | return false; |
ede4eed4 KR |
974 | strcpy (name, namebuf); |
975 | newsect = bfd_make_section (abfd, name); | |
976 | if (newsect == NULL) | |
977 | return false; | |
978 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; | |
ae115e51 | 979 | newsect->lma = hdr->p_paddr + hdr->p_filesz; |
ede4eed4 KR |
980 | newsect->_raw_size = hdr->p_memsz - hdr->p_filesz; |
981 | if (hdr->p_type == PT_LOAD) | |
982 | { | |
983 | newsect->flags |= SEC_ALLOC; | |
984 | if (hdr->p_flags & PF_X) | |
985 | newsect->flags |= SEC_CODE; | |
986 | } | |
987 | if (!(hdr->p_flags & PF_W)) | |
988 | newsect->flags |= SEC_READONLY; | |
989 | } | |
990 | ||
991 | return true; | |
992 | } | |
993 | ||
994 | /* Set up an ELF internal section header for a section. */ | |
995 | ||
996 | /*ARGSUSED*/ | |
997 | static void | |
998 | elf_fake_sections (abfd, asect, failedptrarg) | |
999 | bfd *abfd; | |
1000 | asection *asect; | |
1001 | PTR failedptrarg; | |
1002 | { | |
1003 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1004 | boolean *failedptr = (boolean *) failedptrarg; | |
1005 | Elf_Internal_Shdr *this_hdr; | |
1006 | ||
1007 | if (*failedptr) | |
1008 | { | |
1009 | /* We already failed; just get out of the bfd_map_over_sections | |
1010 | loop. */ | |
1011 | return; | |
1012 | } | |
1013 | ||
1014 | this_hdr = &elf_section_data (asect)->this_hdr; | |
1015 | ||
1016 | this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd), | |
1017 | asect->name, | |
1018 | true, false); | |
1019 | if (this_hdr->sh_name == (unsigned long) -1) | |
1020 | { | |
1021 | *failedptr = true; | |
1022 | return; | |
1023 | } | |
1024 | ||
1025 | this_hdr->sh_flags = 0; | |
ae115e51 | 1026 | |
ede4eed4 | 1027 | if ((asect->flags & SEC_ALLOC) != 0) |
fd0198f0 | 1028 | this_hdr->sh_addr = asect->vma; |
ede4eed4 KR |
1029 | else |
1030 | this_hdr->sh_addr = 0; | |
ae115e51 | 1031 | |
ede4eed4 KR |
1032 | this_hdr->sh_offset = 0; |
1033 | this_hdr->sh_size = asect->_raw_size; | |
1034 | this_hdr->sh_link = 0; | |
ede4eed4 | 1035 | this_hdr->sh_addralign = 1 << asect->alignment_power; |
fd0198f0 ILT |
1036 | /* The sh_entsize and sh_info fields may have been set already by |
1037 | copy_private_section_data. */ | |
ede4eed4 KR |
1038 | |
1039 | this_hdr->bfd_section = asect; | |
1040 | this_hdr->contents = NULL; | |
1041 | ||
1042 | /* FIXME: This should not be based on section names. */ | |
1043 | if (strcmp (asect->name, ".dynstr") == 0) | |
1044 | this_hdr->sh_type = SHT_STRTAB; | |
1045 | else if (strcmp (asect->name, ".hash") == 0) | |
1046 | { | |
1047 | this_hdr->sh_type = SHT_HASH; | |
1048 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
1049 | } | |
1050 | else if (strcmp (asect->name, ".dynsym") == 0) | |
1051 | { | |
1052 | this_hdr->sh_type = SHT_DYNSYM; | |
1053 | this_hdr->sh_entsize = bed->s->sizeof_sym; | |
1054 | } | |
1055 | else if (strcmp (asect->name, ".dynamic") == 0) | |
1056 | { | |
1057 | this_hdr->sh_type = SHT_DYNAMIC; | |
1058 | this_hdr->sh_entsize = bed->s->sizeof_dyn; | |
1059 | } | |
1060 | else if (strncmp (asect->name, ".rela", 5) == 0 | |
1061 | && get_elf_backend_data (abfd)->use_rela_p) | |
1062 | { | |
1063 | this_hdr->sh_type = SHT_RELA; | |
1064 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
1065 | } | |
1066 | else if (strncmp (asect->name, ".rel", 4) == 0 | |
1067 | && ! get_elf_backend_data (abfd)->use_rela_p) | |
1068 | { | |
1069 | this_hdr->sh_type = SHT_REL; | |
1070 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
1071 | } | |
1072 | else if (strcmp (asect->name, ".note") == 0) | |
1073 | this_hdr->sh_type = SHT_NOTE; | |
1074 | else if (strncmp (asect->name, ".stab", 5) == 0 | |
1075 | && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0) | |
1076 | this_hdr->sh_type = SHT_STRTAB; | |
1077 | else if ((asect->flags & SEC_ALLOC) != 0 | |
1078 | && (asect->flags & SEC_LOAD) != 0) | |
1079 | this_hdr->sh_type = SHT_PROGBITS; | |
1080 | else if ((asect->flags & SEC_ALLOC) != 0 | |
1081 | && ((asect->flags & SEC_LOAD) == 0)) | |
5fe14a9f | 1082 | this_hdr->sh_type = SHT_NOBITS; |
ede4eed4 KR |
1083 | else |
1084 | { | |
1085 | /* Who knows? */ | |
1086 | this_hdr->sh_type = SHT_PROGBITS; | |
1087 | } | |
1088 | ||
1089 | if ((asect->flags & SEC_ALLOC) != 0) | |
1090 | this_hdr->sh_flags |= SHF_ALLOC; | |
1091 | if ((asect->flags & SEC_READONLY) == 0) | |
1092 | this_hdr->sh_flags |= SHF_WRITE; | |
1093 | if ((asect->flags & SEC_CODE) != 0) | |
1094 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
1095 | ||
1096 | /* Check for processor-specific section types. */ | |
1097 | { | |
1098 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1099 | ||
1100 | if (bed->elf_backend_fake_sections) | |
1101 | (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect); | |
1102 | } | |
1103 | ||
1104 | /* If the section has relocs, set up a section header for the | |
1105 | SHT_REL[A] section. */ | |
1106 | if ((asect->flags & SEC_RELOC) != 0) | |
1107 | { | |
1108 | Elf_Internal_Shdr *rela_hdr; | |
1109 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; | |
1110 | char *name; | |
1111 | ||
1112 | rela_hdr = &elf_section_data (asect)->rel_hdr; | |
1113 | name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name)); | |
1114 | if (name == NULL) | |
1115 | { | |
ede4eed4 KR |
1116 | *failedptr = true; |
1117 | return; | |
1118 | } | |
1119 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); | |
1120 | rela_hdr->sh_name = | |
1121 | (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name, | |
1122 | true, false); | |
1123 | if (rela_hdr->sh_name == (unsigned int) -1) | |
1124 | { | |
1125 | *failedptr = true; | |
1126 | return; | |
1127 | } | |
1128 | rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; | |
1129 | rela_hdr->sh_entsize = (use_rela_p | |
1130 | ? bed->s->sizeof_rela | |
1131 | : bed->s->sizeof_rel); | |
1132 | rela_hdr->sh_addralign = bed->s->file_align; | |
1133 | rela_hdr->sh_flags = 0; | |
1134 | rela_hdr->sh_addr = 0; | |
1135 | rela_hdr->sh_size = 0; | |
1136 | rela_hdr->sh_offset = 0; | |
1137 | } | |
1138 | } | |
1139 | ||
1140 | /* Assign all ELF section numbers. The dummy first section is handled here | |
1141 | too. The link/info pointers for the standard section types are filled | |
1142 | in here too, while we're at it. */ | |
1143 | ||
1144 | static boolean | |
1145 | assign_section_numbers (abfd) | |
1146 | bfd *abfd; | |
1147 | { | |
1148 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
1149 | asection *sec; | |
1150 | unsigned int section_number; | |
1151 | Elf_Internal_Shdr **i_shdrp; | |
1152 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1153 | ||
1154 | section_number = 1; | |
1155 | ||
1156 | for (sec = abfd->sections; sec; sec = sec->next) | |
1157 | { | |
1158 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
1159 | ||
1160 | d->this_idx = section_number++; | |
1161 | if ((sec->flags & SEC_RELOC) == 0) | |
1162 | d->rel_idx = 0; | |
1163 | else | |
1164 | d->rel_idx = section_number++; | |
1165 | } | |
1166 | ||
1167 | t->shstrtab_section = section_number++; | |
1168 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; | |
1169 | t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd)); | |
1170 | ||
1171 | if (abfd->symcount > 0) | |
1172 | { | |
1173 | t->symtab_section = section_number++; | |
1174 | t->strtab_section = section_number++; | |
1175 | } | |
1176 | ||
1177 | elf_elfheader (abfd)->e_shnum = section_number; | |
1178 | ||
1179 | /* Set up the list of section header pointers, in agreement with the | |
1180 | indices. */ | |
1181 | i_shdrp = ((Elf_Internal_Shdr **) | |
1182 | bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *))); | |
1183 | if (i_shdrp == NULL) | |
a9713b91 | 1184 | return false; |
ede4eed4 KR |
1185 | |
1186 | i_shdrp[0] = ((Elf_Internal_Shdr *) | |
1187 | bfd_alloc (abfd, sizeof (Elf_Internal_Shdr))); | |
1188 | if (i_shdrp[0] == NULL) | |
1189 | { | |
1190 | bfd_release (abfd, i_shdrp); | |
ede4eed4 KR |
1191 | return false; |
1192 | } | |
1193 | memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr)); | |
1194 | ||
1195 | elf_elfsections (abfd) = i_shdrp; | |
1196 | ||
1197 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; | |
1198 | if (abfd->symcount > 0) | |
1199 | { | |
1200 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
1201 | i_shdrp[t->strtab_section] = &t->strtab_hdr; | |
1202 | t->symtab_hdr.sh_link = t->strtab_section; | |
1203 | } | |
1204 | for (sec = abfd->sections; sec; sec = sec->next) | |
1205 | { | |
1206 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
1207 | asection *s; | |
1208 | const char *name; | |
1209 | ||
1210 | i_shdrp[d->this_idx] = &d->this_hdr; | |
1211 | if (d->rel_idx != 0) | |
1212 | i_shdrp[d->rel_idx] = &d->rel_hdr; | |
1213 | ||
1214 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
1215 | ||
1216 | /* sh_link of a reloc section is the section index of the symbol | |
1217 | table. sh_info is the section index of the section to which | |
1218 | the relocation entries apply. */ | |
1219 | if (d->rel_idx != 0) | |
1220 | { | |
1221 | d->rel_hdr.sh_link = t->symtab_section; | |
1222 | d->rel_hdr.sh_info = d->this_idx; | |
1223 | } | |
1224 | ||
1225 | switch (d->this_hdr.sh_type) | |
1226 | { | |
1227 | case SHT_REL: | |
1228 | case SHT_RELA: | |
1229 | /* A reloc section which we are treating as a normal BFD | |
1230 | section. sh_link is the section index of the symbol | |
1231 | table. sh_info is the section index of the section to | |
1232 | which the relocation entries apply. We assume that an | |
1233 | allocated reloc section uses the dynamic symbol table. | |
1234 | FIXME: How can we be sure? */ | |
1235 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
1236 | if (s != NULL) | |
1237 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1238 | ||
1239 | /* We look up the section the relocs apply to by name. */ | |
1240 | name = sec->name; | |
1241 | if (d->this_hdr.sh_type == SHT_REL) | |
1242 | name += 4; | |
1243 | else | |
1244 | name += 5; | |
1245 | s = bfd_get_section_by_name (abfd, name); | |
1246 | if (s != NULL) | |
1247 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
1248 | break; | |
1249 | ||
1250 | case SHT_STRTAB: | |
1251 | /* We assume that a section named .stab*str is a stabs | |
1252 | string section. We look for a section with the same name | |
1253 | but without the trailing ``str'', and set its sh_link | |
1254 | field to point to this section. */ | |
1255 | if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0 | |
1256 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) | |
1257 | { | |
1258 | size_t len; | |
1259 | char *alc; | |
1260 | ||
1261 | len = strlen (sec->name); | |
58142f10 | 1262 | alc = (char *) bfd_malloc (len - 2); |
ede4eed4 | 1263 | if (alc == NULL) |
58142f10 | 1264 | return false; |
ede4eed4 KR |
1265 | strncpy (alc, sec->name, len - 3); |
1266 | alc[len - 3] = '\0'; | |
1267 | s = bfd_get_section_by_name (abfd, alc); | |
1268 | free (alc); | |
1269 | if (s != NULL) | |
1270 | { | |
1271 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
1272 | ||
1273 | /* This is a .stab section. */ | |
1274 | elf_section_data (s)->this_hdr.sh_entsize = | |
1275 | 4 + 2 * (bed->s->arch_size / 8); | |
1276 | } | |
1277 | } | |
1278 | break; | |
1279 | ||
1280 | case SHT_DYNAMIC: | |
1281 | case SHT_DYNSYM: | |
1282 | /* sh_link is the section header index of the string table | |
1283 | used for the dynamic entries or symbol table. */ | |
1284 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
1285 | if (s != NULL) | |
1286 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1287 | break; | |
1288 | ||
1289 | case SHT_HASH: | |
1290 | /* sh_link is the section header index of the symbol table | |
1291 | this hash table is for. */ | |
1292 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
1293 | if (s != NULL) | |
1294 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1295 | break; | |
1296 | } | |
1297 | } | |
1298 | ||
1299 | return true; | |
1300 | } | |
1301 | ||
1302 | /* Map symbol from it's internal number to the external number, moving | |
1303 | all local symbols to be at the head of the list. */ | |
1304 | ||
1305 | static INLINE int | |
1306 | sym_is_global (abfd, sym) | |
1307 | bfd *abfd; | |
1308 | asymbol *sym; | |
1309 | { | |
1310 | /* If the backend has a special mapping, use it. */ | |
1311 | if (get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1312 | return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1313 | (abfd, sym)); | |
1314 | ||
1315 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0 | |
1316 | || bfd_is_und_section (bfd_get_section (sym)) | |
1317 | || bfd_is_com_section (bfd_get_section (sym))); | |
1318 | } | |
1319 | ||
1320 | static boolean | |
1321 | elf_map_symbols (abfd) | |
1322 | bfd *abfd; | |
1323 | { | |
1324 | int symcount = bfd_get_symcount (abfd); | |
1325 | asymbol **syms = bfd_get_outsymbols (abfd); | |
1326 | asymbol **sect_syms; | |
1327 | int num_locals = 0; | |
1328 | int num_globals = 0; | |
1329 | int num_locals2 = 0; | |
1330 | int num_globals2 = 0; | |
1331 | int max_index = 0; | |
1332 | int num_sections = 0; | |
1333 | int idx; | |
1334 | asection *asect; | |
1335 | asymbol **new_syms; | |
1336 | ||
1337 | #ifdef DEBUG | |
1338 | fprintf (stderr, "elf_map_symbols\n"); | |
1339 | fflush (stderr); | |
1340 | #endif | |
1341 | ||
1342 | /* Add a section symbol for each BFD section. FIXME: Is this really | |
1343 | necessary? */ | |
1344 | for (asect = abfd->sections; asect; asect = asect->next) | |
1345 | { | |
1346 | if (max_index < asect->index) | |
1347 | max_index = asect->index; | |
1348 | } | |
1349 | ||
1350 | max_index++; | |
1351 | sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *)); | |
1352 | if (sect_syms == NULL) | |
a9713b91 | 1353 | return false; |
ede4eed4 KR |
1354 | elf_section_syms (abfd) = sect_syms; |
1355 | ||
1356 | for (idx = 0; idx < symcount; idx++) | |
1357 | { | |
1358 | if ((syms[idx]->flags & BSF_SECTION_SYM) != 0 | |
fd0198f0 | 1359 | && (syms[idx]->value + syms[idx]->section->vma) == 0) |
ede4eed4 KR |
1360 | { |
1361 | asection *sec; | |
1362 | ||
1363 | sec = syms[idx]->section; | |
1364 | if (sec->owner != NULL) | |
1365 | { | |
1366 | if (sec->owner != abfd) | |
1367 | { | |
1368 | if (sec->output_offset != 0) | |
1369 | continue; | |
1370 | sec = sec->output_section; | |
1371 | BFD_ASSERT (sec->owner == abfd); | |
1372 | } | |
1373 | sect_syms[sec->index] = syms[idx]; | |
1374 | } | |
1375 | } | |
1376 | } | |
1377 | ||
1378 | for (asect = abfd->sections; asect; asect = asect->next) | |
1379 | { | |
1380 | asymbol *sym; | |
1381 | ||
1382 | if (sect_syms[asect->index] != NULL) | |
1383 | continue; | |
1384 | ||
1385 | sym = bfd_make_empty_symbol (abfd); | |
1386 | if (sym == NULL) | |
1387 | return false; | |
1388 | sym->the_bfd = abfd; | |
1389 | sym->name = asect->name; | |
1390 | sym->value = 0; | |
1391 | /* Set the flags to 0 to indicate that this one was newly added. */ | |
1392 | sym->flags = 0; | |
1393 | sym->section = asect; | |
1394 | sect_syms[asect->index] = sym; | |
1395 | num_sections++; | |
1396 | #ifdef DEBUG | |
1397 | fprintf (stderr, | |
1398 | "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n", | |
1399 | asect->name, (long) asect->vma, asect->index, (long) asect); | |
1400 | #endif | |
1401 | } | |
1402 | ||
1403 | /* Classify all of the symbols. */ | |
1404 | for (idx = 0; idx < symcount; idx++) | |
1405 | { | |
1406 | if (!sym_is_global (abfd, syms[idx])) | |
1407 | num_locals++; | |
1408 | else | |
1409 | num_globals++; | |
1410 | } | |
1411 | for (asect = abfd->sections; asect; asect = asect->next) | |
1412 | { | |
1413 | if (sect_syms[asect->index] != NULL | |
1414 | && sect_syms[asect->index]->flags == 0) | |
1415 | { | |
1416 | sect_syms[asect->index]->flags = BSF_SECTION_SYM; | |
1417 | if (!sym_is_global (abfd, sect_syms[asect->index])) | |
1418 | num_locals++; | |
1419 | else | |
1420 | num_globals++; | |
1421 | sect_syms[asect->index]->flags = 0; | |
1422 | } | |
1423 | } | |
1424 | ||
1425 | /* Now sort the symbols so the local symbols are first. */ | |
1426 | new_syms = ((asymbol **) | |
1427 | bfd_alloc (abfd, | |
1428 | (num_locals + num_globals) * sizeof (asymbol *))); | |
1429 | if (new_syms == NULL) | |
a9713b91 | 1430 | return false; |
ede4eed4 KR |
1431 | |
1432 | for (idx = 0; idx < symcount; idx++) | |
1433 | { | |
1434 | asymbol *sym = syms[idx]; | |
1435 | int i; | |
1436 | ||
1437 | if (!sym_is_global (abfd, sym)) | |
1438 | i = num_locals2++; | |
1439 | else | |
1440 | i = num_locals + num_globals2++; | |
1441 | new_syms[i] = sym; | |
1442 | sym->udata.i = i + 1; | |
1443 | } | |
1444 | for (asect = abfd->sections; asect; asect = asect->next) | |
1445 | { | |
1446 | if (sect_syms[asect->index] != NULL | |
1447 | && sect_syms[asect->index]->flags == 0) | |
1448 | { | |
1449 | asymbol *sym = sect_syms[asect->index]; | |
1450 | int i; | |
1451 | ||
1452 | sym->flags = BSF_SECTION_SYM; | |
1453 | if (!sym_is_global (abfd, sym)) | |
1454 | i = num_locals2++; | |
1455 | else | |
1456 | i = num_locals + num_globals2++; | |
1457 | new_syms[i] = sym; | |
1458 | sym->udata.i = i + 1; | |
1459 | } | |
1460 | } | |
1461 | ||
1462 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
1463 | ||
1464 | elf_num_locals (abfd) = num_locals; | |
1465 | elf_num_globals (abfd) = num_globals; | |
1466 | return true; | |
1467 | } | |
1468 | ||
fd0198f0 ILT |
1469 | /* Align to the maximum file alignment that could be required for any |
1470 | ELF data structure. */ | |
1471 | ||
1472 | static INLINE file_ptr align_file_position PARAMS ((file_ptr, int)); | |
1473 | static INLINE file_ptr | |
1474 | align_file_position (off, align) | |
1475 | file_ptr off; | |
1476 | int align; | |
1477 | { | |
1478 | return (off + align - 1) & ~(align - 1); | |
1479 | } | |
1480 | ||
1481 | /* Assign a file position to a section, optionally aligning to the | |
1482 | required section alignment. */ | |
1483 | ||
1484 | INLINE file_ptr | |
1485 | _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align) | |
1486 | Elf_Internal_Shdr *i_shdrp; | |
1487 | file_ptr offset; | |
1488 | boolean align; | |
1489 | { | |
1490 | if (align) | |
1491 | { | |
1492 | unsigned int al; | |
1493 | ||
1494 | al = i_shdrp->sh_addralign; | |
1495 | if (al > 1) | |
1496 | offset = BFD_ALIGN (offset, al); | |
1497 | } | |
1498 | i_shdrp->sh_offset = offset; | |
1499 | if (i_shdrp->bfd_section != NULL) | |
1500 | i_shdrp->bfd_section->filepos = offset; | |
1501 | if (i_shdrp->sh_type != SHT_NOBITS) | |
1502 | offset += i_shdrp->sh_size; | |
1503 | return offset; | |
1504 | } | |
1505 | ||
ede4eed4 KR |
1506 | /* Compute the file positions we are going to put the sections at, and |
1507 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
1508 | is not NULL, this is being called by the ELF backend linker. */ | |
1509 | ||
1510 | boolean | |
1511 | _bfd_elf_compute_section_file_positions (abfd, link_info) | |
1512 | bfd *abfd; | |
1513 | struct bfd_link_info *link_info; | |
1514 | { | |
1515 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1516 | boolean failed; | |
1517 | struct bfd_strtab_hash *strtab; | |
1518 | Elf_Internal_Shdr *shstrtab_hdr; | |
1519 | ||
1520 | if (abfd->output_has_begun) | |
1521 | return true; | |
1522 | ||
1523 | /* Do any elf backend specific processing first. */ | |
1524 | if (bed->elf_backend_begin_write_processing) | |
1525 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
1526 | ||
1527 | if (! prep_headers (abfd)) | |
1528 | return false; | |
1529 | ||
1530 | failed = false; | |
1531 | bfd_map_over_sections (abfd, elf_fake_sections, &failed); | |
1532 | if (failed) | |
1533 | return false; | |
1534 | ||
1535 | if (!assign_section_numbers (abfd)) | |
1536 | return false; | |
1537 | ||
1538 | /* The backend linker builds symbol table information itself. */ | |
fd0198f0 | 1539 | if (link_info == NULL && abfd->symcount > 0) |
ede4eed4 KR |
1540 | { |
1541 | if (! swap_out_syms (abfd, &strtab)) | |
1542 | return false; | |
1543 | } | |
1544 | ||
1545 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; | |
1546 | /* sh_name was set in prep_headers. */ | |
1547 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
1548 | shstrtab_hdr->sh_flags = 0; | |
1549 | shstrtab_hdr->sh_addr = 0; | |
1550 | shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd)); | |
1551 | shstrtab_hdr->sh_entsize = 0; | |
1552 | shstrtab_hdr->sh_link = 0; | |
1553 | shstrtab_hdr->sh_info = 0; | |
fd0198f0 | 1554 | /* sh_offset is set in assign_file_positions_except_relocs. */ |
ede4eed4 KR |
1555 | shstrtab_hdr->sh_addralign = 1; |
1556 | ||
fd0198f0 | 1557 | if (!assign_file_positions_except_relocs (abfd)) |
ede4eed4 KR |
1558 | return false; |
1559 | ||
fd0198f0 | 1560 | if (link_info == NULL && abfd->symcount > 0) |
ede4eed4 | 1561 | { |
fd0198f0 ILT |
1562 | file_ptr off; |
1563 | Elf_Internal_Shdr *hdr; | |
1564 | ||
1565 | off = elf_tdata (abfd)->next_file_pos; | |
1566 | ||
1567 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
1568 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
1569 | ||
1570 | hdr = &elf_tdata (abfd)->strtab_hdr; | |
1571 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
1572 | ||
1573 | elf_tdata (abfd)->next_file_pos = off; | |
1574 | ||
ede4eed4 KR |
1575 | /* Now that we know where the .strtab section goes, write it |
1576 | out. */ | |
fd0198f0 | 1577 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ede4eed4 KR |
1578 | || ! _bfd_stringtab_emit (abfd, strtab)) |
1579 | return false; | |
1580 | _bfd_stringtab_free (strtab); | |
1581 | } | |
1582 | ||
1583 | abfd->output_has_begun = true; | |
1584 | ||
1585 | return true; | |
1586 | } | |
1587 | ||
fd0198f0 | 1588 | /* Create a mapping from a set of sections to a program segment. */ |
ede4eed4 | 1589 | |
fd0198f0 ILT |
1590 | static INLINE struct elf_segment_map * |
1591 | make_mapping (abfd, sections, from, to) | |
1592 | bfd *abfd; | |
1593 | asection **sections; | |
1594 | unsigned int from; | |
1595 | unsigned int to; | |
ede4eed4 | 1596 | { |
fd0198f0 ILT |
1597 | struct elf_segment_map *m; |
1598 | unsigned int i; | |
1599 | asection **hdrpp; | |
1600 | ||
1601 | m = ((struct elf_segment_map *) | |
1602 | bfd_zalloc (abfd, | |
1603 | (sizeof (struct elf_segment_map) | |
1604 | + (to - from - 1) * sizeof (asection *)))); | |
1605 | if (m == NULL) | |
a9713b91 | 1606 | return NULL; |
fd0198f0 ILT |
1607 | m->next = NULL; |
1608 | m->p_type = PT_LOAD; | |
1609 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
1610 | m->sections[i - from] = *hdrpp; | |
1611 | m->count = to - from; | |
1612 | ||
6933148a ILT |
1613 | if (from == 0) |
1614 | { | |
1615 | /* Include the headers in the first PT_LOAD segment. */ | |
1616 | m->includes_filehdr = 1; | |
1617 | m->includes_phdrs = 1; | |
1618 | } | |
1619 | ||
fd0198f0 | 1620 | return m; |
ede4eed4 KR |
1621 | } |
1622 | ||
fd0198f0 | 1623 | /* Set up a mapping from BFD sections to program segments. */ |
ede4eed4 | 1624 | |
fd0198f0 ILT |
1625 | static boolean |
1626 | map_sections_to_segments (abfd) | |
1627 | bfd *abfd; | |
ede4eed4 | 1628 | { |
fd0198f0 ILT |
1629 | asection **sections = NULL; |
1630 | asection *s; | |
1631 | unsigned int i; | |
1632 | unsigned int count; | |
1633 | struct elf_segment_map *mfirst; | |
1634 | struct elf_segment_map **pm; | |
1635 | struct elf_segment_map *m; | |
1636 | asection *last_hdr; | |
1637 | unsigned int phdr_index; | |
1638 | bfd_vma maxpagesize; | |
1639 | asection **hdrpp; | |
1640 | ||
1641 | if (elf_tdata (abfd)->segment_map != NULL) | |
1642 | return true; | |
1643 | ||
1644 | if (bfd_count_sections (abfd) == 0) | |
1645 | return true; | |
1646 | ||
1647 | /* Select the allocated sections, and sort them. */ | |
1648 | ||
58142f10 ILT |
1649 | sections = (asection **) bfd_malloc (bfd_count_sections (abfd) |
1650 | * sizeof (asection *)); | |
fd0198f0 | 1651 | if (sections == NULL) |
58142f10 | 1652 | goto error_return; |
ede4eed4 | 1653 | |
fd0198f0 ILT |
1654 | i = 0; |
1655 | for (s = abfd->sections; s != NULL; s = s->next) | |
1656 | { | |
1657 | if ((s->flags & SEC_ALLOC) != 0) | |
1658 | { | |
1659 | sections[i] = s; | |
1660 | ++i; | |
1661 | } | |
5fe14a9f | 1662 | } |
fd0198f0 ILT |
1663 | BFD_ASSERT (i <= bfd_count_sections (abfd)); |
1664 | count = i; | |
ede4eed4 | 1665 | |
fd0198f0 | 1666 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
ede4eed4 | 1667 | |
fd0198f0 | 1668 | /* Build the mapping. */ |
ede4eed4 | 1669 | |
fd0198f0 ILT |
1670 | mfirst = NULL; |
1671 | pm = &mfirst; | |
ede4eed4 | 1672 | |
fd0198f0 ILT |
1673 | /* If we have a .interp section, then create a PT_PHDR segment for |
1674 | the program headers and a PT_INTERP segment for the .interp | |
1675 | section. */ | |
1676 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1677 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
1678 | { | |
1679 | m = ((struct elf_segment_map *) | |
1680 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
1681 | if (m == NULL) | |
a9713b91 | 1682 | goto error_return; |
fd0198f0 ILT |
1683 | m->next = NULL; |
1684 | m->p_type = PT_PHDR; | |
1685 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
1686 | m->p_flags = PF_R | PF_X; | |
1687 | m->p_flags_valid = 1; | |
6933148a | 1688 | m->includes_phdrs = 1; |
ede4eed4 | 1689 | |
fd0198f0 ILT |
1690 | *pm = m; |
1691 | pm = &m->next; | |
ede4eed4 | 1692 | |
fd0198f0 ILT |
1693 | m = ((struct elf_segment_map *) |
1694 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
1695 | if (m == NULL) | |
a9713b91 | 1696 | goto error_return; |
fd0198f0 ILT |
1697 | m->next = NULL; |
1698 | m->p_type = PT_INTERP; | |
1699 | m->count = 1; | |
1700 | m->sections[0] = s; | |
ede4eed4 | 1701 | |
fd0198f0 ILT |
1702 | *pm = m; |
1703 | pm = &m->next; | |
1704 | } | |
ede4eed4 | 1705 | |
fd0198f0 ILT |
1706 | /* Look through the sections. We put sections in the same program |
1707 | segment when the start of the second section can be placed within | |
1708 | a few bytes of the end of the first section. */ | |
1709 | last_hdr = NULL; | |
1710 | phdr_index = 0; | |
1711 | maxpagesize = get_elf_backend_data (abfd)->maxpagesize; | |
1712 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) | |
ede4eed4 | 1713 | { |
fd0198f0 | 1714 | asection *hdr; |
ede4eed4 | 1715 | |
fd0198f0 | 1716 | hdr = *hdrpp; |
ede4eed4 | 1717 | |
fd0198f0 ILT |
1718 | /* See if this section and the last one will fit in the same |
1719 | segment. */ | |
1720 | if (last_hdr == NULL | |
1721 | || ((BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize) | |
1722 | >= hdr->lma) | |
1723 | && ((last_hdr->flags & SEC_LOAD) != 0 | |
1724 | || (hdr->flags & SEC_LOAD) == 0))) | |
1725 | { | |
1726 | last_hdr = hdr; | |
1727 | continue; | |
1728 | } | |
ede4eed4 | 1729 | |
fd0198f0 ILT |
1730 | /* This section won't fit in the program segment. We must |
1731 | create a new program header holding all the sections from | |
1732 | phdr_index until hdr. */ | |
ede4eed4 | 1733 | |
fd0198f0 ILT |
1734 | m = make_mapping (abfd, sections, phdr_index, i); |
1735 | if (m == NULL) | |
1736 | goto error_return; | |
ede4eed4 | 1737 | |
fd0198f0 ILT |
1738 | *pm = m; |
1739 | pm = &m->next; | |
ede4eed4 | 1740 | |
fd0198f0 ILT |
1741 | last_hdr = hdr; |
1742 | phdr_index = i; | |
ede4eed4 | 1743 | } |
fd0198f0 ILT |
1744 | |
1745 | /* Create a final PT_LOAD program segment. */ | |
1746 | if (last_hdr != NULL) | |
ede4eed4 | 1747 | { |
fd0198f0 ILT |
1748 | m = make_mapping (abfd, sections, phdr_index, i); |
1749 | if (m == NULL) | |
1750 | goto error_return; | |
1751 | ||
1752 | *pm = m; | |
1753 | pm = &m->next; | |
ede4eed4 KR |
1754 | } |
1755 | ||
fd0198f0 ILT |
1756 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
1757 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
ede4eed4 KR |
1758 | if (s != NULL && (s->flags & SEC_LOAD) != 0) |
1759 | { | |
fd0198f0 ILT |
1760 | m = ((struct elf_segment_map *) |
1761 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
1762 | if (m == NULL) | |
a9713b91 | 1763 | goto error_return; |
fd0198f0 ILT |
1764 | m->next = NULL; |
1765 | m->p_type = PT_DYNAMIC; | |
1766 | m->count = 1; | |
1767 | m->sections[0] = s; | |
ede4eed4 | 1768 | |
fd0198f0 ILT |
1769 | *pm = m; |
1770 | pm = &m->next; | |
ede4eed4 KR |
1771 | } |
1772 | ||
fd0198f0 ILT |
1773 | free (sections); |
1774 | sections = NULL; | |
ae115e51 | 1775 | |
fd0198f0 ILT |
1776 | elf_tdata (abfd)->segment_map = mfirst; |
1777 | return true; | |
1778 | ||
1779 | error_return: | |
1780 | if (sections != NULL) | |
1781 | free (sections); | |
1782 | return false; | |
ede4eed4 KR |
1783 | } |
1784 | ||
fd0198f0 | 1785 | /* Sort sections by VMA. */ |
ede4eed4 | 1786 | |
fd0198f0 ILT |
1787 | static int |
1788 | elf_sort_sections (arg1, arg2) | |
1789 | const PTR arg1; | |
1790 | const PTR arg2; | |
ede4eed4 | 1791 | { |
fd0198f0 ILT |
1792 | const asection *sec1 = *(const asection **) arg1; |
1793 | const asection *sec2 = *(const asection **) arg2; | |
ede4eed4 | 1794 | |
fd0198f0 ILT |
1795 | if (sec1->vma < sec2->vma) |
1796 | return -1; | |
1797 | else if (sec1->vma > sec2->vma) | |
1798 | return 1; | |
ede4eed4 | 1799 | |
fd0198f0 | 1800 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ |
ede4eed4 | 1801 | |
fd0198f0 | 1802 | #define TOEND(x) (((x)->flags & SEC_LOAD) == 0) |
ede4eed4 | 1803 | |
fd0198f0 ILT |
1804 | if (TOEND (sec1)) |
1805 | if (TOEND (sec2)) | |
1806 | return sec1->target_index - sec2->target_index; | |
1807 | else | |
1808 | return 1; | |
ede4eed4 | 1809 | |
fd0198f0 ILT |
1810 | if (TOEND (sec2)) |
1811 | return -1; | |
ede4eed4 | 1812 | |
fd0198f0 | 1813 | #undef TOEND |
ede4eed4 | 1814 | |
fd0198f0 ILT |
1815 | /* Sort by size, to put zero sized sections before others at the |
1816 | same address. */ | |
ede4eed4 | 1817 | |
fd0198f0 ILT |
1818 | if (sec1->_raw_size < sec2->_raw_size) |
1819 | return -1; | |
1820 | if (sec1->_raw_size > sec2->_raw_size) | |
1821 | return 1; | |
ede4eed4 | 1822 | |
fd0198f0 ILT |
1823 | return sec1->target_index - sec2->target_index; |
1824 | } | |
ede4eed4 | 1825 | |
fd0198f0 ILT |
1826 | /* Assign file positions to the sections based on the mapping from |
1827 | sections to segments. This function also sets up some fields in | |
1828 | the file header, and writes out the program headers. */ | |
ede4eed4 | 1829 | |
fd0198f0 ILT |
1830 | static boolean |
1831 | assign_file_positions_for_segments (abfd) | |
1832 | bfd *abfd; | |
1833 | { | |
1834 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1835 | unsigned int count; | |
1836 | struct elf_segment_map *m; | |
1837 | unsigned int alloc; | |
1838 | Elf_Internal_Phdr *phdrs; | |
1839 | file_ptr off; | |
6933148a ILT |
1840 | bfd_vma filehdr_vaddr, filehdr_paddr; |
1841 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
fd0198f0 ILT |
1842 | Elf_Internal_Phdr *p; |
1843 | ||
1844 | if (elf_tdata (abfd)->segment_map == NULL) | |
1845 | { | |
1846 | if (! map_sections_to_segments (abfd)) | |
1847 | return false; | |
1848 | } | |
ede4eed4 | 1849 | |
5b3b9ff6 ILT |
1850 | if (bed->elf_backend_modify_segment_map) |
1851 | { | |
1852 | if (! (*bed->elf_backend_modify_segment_map) (abfd)) | |
1853 | return false; | |
1854 | } | |
1855 | ||
fd0198f0 ILT |
1856 | count = 0; |
1857 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
1858 | ++count; | |
ede4eed4 | 1859 | |
fd0198f0 ILT |
1860 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; |
1861 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
1862 | elf_elfheader (abfd)->e_phnum = count; | |
ede4eed4 | 1863 | |
fd0198f0 ILT |
1864 | if (count == 0) |
1865 | return true; | |
ede4eed4 | 1866 | |
fd0198f0 ILT |
1867 | /* If we already counted the number of program segments, make sure |
1868 | that we allocated enough space. This happens when SIZEOF_HEADERS | |
1869 | is used in a linker script. */ | |
1870 | alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr; | |
1871 | if (alloc != 0 && count > alloc) | |
1872 | { | |
1873 | ((*_bfd_error_handler) | |
1874 | ("%s: Not enough room for program headers (allocated %u, need %u)", | |
1875 | bfd_get_filename (abfd), alloc, count)); | |
1876 | bfd_set_error (bfd_error_bad_value); | |
1877 | return false; | |
ede4eed4 KR |
1878 | } |
1879 | ||
fd0198f0 ILT |
1880 | if (alloc == 0) |
1881 | alloc = count; | |
1882 | ||
1883 | phdrs = ((Elf_Internal_Phdr *) | |
1884 | bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr))); | |
1885 | if (phdrs == NULL) | |
a9713b91 | 1886 | return false; |
ede4eed4 | 1887 | |
fd0198f0 ILT |
1888 | off = bed->s->sizeof_ehdr; |
1889 | off += alloc * bed->s->sizeof_phdr; | |
ede4eed4 | 1890 | |
6933148a ILT |
1891 | filehdr_vaddr = 0; |
1892 | filehdr_paddr = 0; | |
1893 | phdrs_vaddr = 0; | |
1894 | phdrs_paddr = 0; | |
fd0198f0 ILT |
1895 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; |
1896 | m != NULL; | |
1897 | m = m->next, p++) | |
1898 | { | |
1899 | unsigned int i; | |
1900 | asection **secpp; | |
fd0198f0 | 1901 | |
3b950780 ILT |
1902 | /* If elf_segment_map is not from map_sections_to_segments, the |
1903 | sections may not be correctly ordered. */ | |
1904 | if (m->count > 0) | |
1905 | qsort (m->sections, (size_t) m->count, sizeof (asection *), | |
1906 | elf_sort_sections); | |
1907 | ||
fd0198f0 ILT |
1908 | p->p_type = m->p_type; |
1909 | ||
1910 | if (m->p_flags_valid) | |
1911 | p->p_flags = m->p_flags; | |
14899eb7 ILT |
1912 | else |
1913 | p->p_flags = 0; | |
fd0198f0 | 1914 | |
d49ddb85 ILT |
1915 | if (p->p_type == PT_LOAD |
1916 | && m->count > 0 | |
1917 | && (m->sections[0]->flags & SEC_LOAD) != 0) | |
44ef8897 ILT |
1918 | off += (m->sections[0]->vma - off) % bed->maxpagesize; |
1919 | ||
fd0198f0 ILT |
1920 | if (m->count == 0) |
1921 | p->p_vaddr = 0; | |
1922 | else | |
1923 | p->p_vaddr = m->sections[0]->vma; | |
ede4eed4 | 1924 | |
fd0198f0 ILT |
1925 | if (m->p_paddr_valid) |
1926 | p->p_paddr = m->p_paddr; | |
1927 | else if (m->count == 0) | |
1928 | p->p_paddr = 0; | |
1929 | else | |
1930 | p->p_paddr = m->sections[0]->lma; | |
1931 | ||
1932 | if (p->p_type == PT_LOAD) | |
1933 | p->p_align = bed->maxpagesize; | |
1934 | else if (m->count == 0) | |
1935 | p->p_align = bed->s->file_align; | |
1936 | else | |
1937 | p->p_align = 0; | |
1938 | ||
6933148a | 1939 | p->p_offset = 0; |
fd0198f0 ILT |
1940 | p->p_filesz = 0; |
1941 | p->p_memsz = 0; | |
1942 | ||
6933148a | 1943 | if (m->includes_filehdr) |
ede4eed4 | 1944 | { |
14899eb7 ILT |
1945 | if (! m->p_flags_valid) |
1946 | p->p_flags |= PF_R; | |
6933148a ILT |
1947 | p->p_offset = 0; |
1948 | p->p_filesz = bed->s->sizeof_ehdr; | |
1949 | p->p_memsz = bed->s->sizeof_ehdr; | |
1950 | if (m->count > 0) | |
1951 | { | |
1952 | BFD_ASSERT (p->p_type == PT_LOAD); | |
1953 | p->p_vaddr -= off; | |
1954 | if (! m->p_paddr_valid) | |
1955 | p->p_paddr -= off; | |
1956 | } | |
1957 | if (p->p_type == PT_LOAD) | |
1958 | { | |
1959 | filehdr_vaddr = p->p_vaddr; | |
1960 | filehdr_paddr = p->p_paddr; | |
1961 | } | |
1962 | } | |
fd0198f0 | 1963 | |
6933148a ILT |
1964 | if (m->includes_phdrs) |
1965 | { | |
14899eb7 ILT |
1966 | if (! m->p_flags_valid) |
1967 | p->p_flags |= PF_R; | |
6933148a | 1968 | if (m->includes_filehdr) |
fd0198f0 | 1969 | { |
6933148a | 1970 | if (p->p_type == PT_LOAD) |
fd0198f0 | 1971 | { |
6933148a ILT |
1972 | phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr; |
1973 | phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr; | |
fd0198f0 | 1974 | } |
6933148a ILT |
1975 | } |
1976 | else | |
1977 | { | |
1978 | p->p_offset = bed->s->sizeof_ehdr; | |
1979 | if (m->count > 0) | |
1980 | { | |
1981 | BFD_ASSERT (p->p_type == PT_LOAD); | |
1982 | p->p_vaddr -= off - p->p_offset; | |
1983 | if (! m->p_paddr_valid) | |
1984 | p->p_paddr -= off - p->p_offset; | |
1985 | } | |
1986 | if (p->p_type == PT_LOAD) | |
fd0198f0 | 1987 | { |
6933148a ILT |
1988 | phdrs_vaddr = p->p_vaddr; |
1989 | phdrs_paddr = p->p_paddr; | |
fd0198f0 | 1990 | } |
6933148a ILT |
1991 | } |
1992 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
1993 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
1994 | } | |
1995 | ||
1996 | if (p->p_type == PT_LOAD) | |
1997 | { | |
1998 | if (! m->includes_filehdr && ! m->includes_phdrs) | |
1999 | p->p_offset = off; | |
2000 | else | |
2001 | { | |
2002 | file_ptr adjust; | |
fd0198f0 | 2003 | |
6933148a ILT |
2004 | adjust = off - (p->p_offset + p->p_filesz); |
2005 | p->p_filesz += adjust; | |
2006 | p->p_memsz += adjust; | |
fd0198f0 | 2007 | } |
ede4eed4 KR |
2008 | } |
2009 | ||
fd0198f0 | 2010 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
ede4eed4 | 2011 | { |
fd0198f0 ILT |
2012 | asection *sec; |
2013 | flagword flags; | |
2014 | bfd_size_type align; | |
2015 | ||
2016 | sec = *secpp; | |
2017 | flags = sec->flags; | |
2018 | ||
2019 | if (p->p_type == PT_LOAD) | |
2020 | { | |
2021 | bfd_vma adjust; | |
2022 | ||
2023 | /* The section VMA must equal the file position modulo | |
2024 | the page size. */ | |
d49ddb85 | 2025 | if ((flags & SEC_LOAD) != 0) |
fd0198f0 | 2026 | { |
d49ddb85 ILT |
2027 | adjust = (sec->vma - off) % bed->maxpagesize; |
2028 | if (adjust != 0) | |
2029 | { | |
2030 | if (i == 0) | |
2031 | abort (); | |
2032 | p->p_memsz += adjust; | |
2033 | if ((flags & SEC_LOAD) != 0) | |
2034 | p->p_filesz += adjust; | |
2035 | off += adjust; | |
2036 | } | |
fd0198f0 ILT |
2037 | } |
2038 | ||
2039 | sec->filepos = off; | |
2040 | ||
2041 | if ((flags & SEC_LOAD) != 0) | |
2042 | off += sec->_raw_size; | |
2043 | } | |
2044 | ||
2045 | p->p_memsz += sec->_raw_size; | |
2046 | ||
2047 | if ((flags & SEC_LOAD) != 0) | |
2048 | p->p_filesz += sec->_raw_size; | |
2049 | ||
2050 | align = 1 << bfd_get_section_alignment (abfd, sec); | |
2051 | if (align > p->p_align) | |
2052 | p->p_align = align; | |
2053 | ||
2054 | if (! m->p_flags_valid) | |
2055 | { | |
14899eb7 | 2056 | p->p_flags |= PF_R; |
fd0198f0 ILT |
2057 | if ((flags & SEC_CODE) != 0) |
2058 | p->p_flags |= PF_X; | |
2059 | if ((flags & SEC_READONLY) == 0) | |
2060 | p->p_flags |= PF_W; | |
2061 | } | |
ede4eed4 | 2062 | } |
fd0198f0 | 2063 | } |
ede4eed4 | 2064 | |
fd0198f0 ILT |
2065 | /* Now that we have set the section file positions, we can set up |
2066 | the file positions for the non PT_LOAD segments. */ | |
2067 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
2068 | m != NULL; | |
2069 | m = m->next, p++) | |
2070 | { | |
2071 | if (p->p_type != PT_LOAD && m->count > 0) | |
ede4eed4 | 2072 | { |
6933148a ILT |
2073 | BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs); |
2074 | p->p_offset = m->sections[0]->filepos; | |
2075 | } | |
2076 | if (m->count == 0) | |
2077 | { | |
2078 | if (m->includes_filehdr) | |
2079 | { | |
2080 | p->p_vaddr = filehdr_vaddr; | |
2081 | if (! m->p_paddr_valid) | |
2082 | p->p_paddr = filehdr_paddr; | |
2083 | } | |
2084 | else if (m->includes_phdrs) | |
2085 | { | |
2086 | p->p_vaddr = phdrs_vaddr; | |
2087 | if (! m->p_paddr_valid) | |
2088 | p->p_paddr = phdrs_paddr; | |
2089 | } | |
ede4eed4 | 2090 | } |
ede4eed4 KR |
2091 | } |
2092 | ||
fd0198f0 ILT |
2093 | /* Clear out any program headers we allocated but did not use. */ |
2094 | for (; count < alloc; count++, p++) | |
ede4eed4 | 2095 | { |
fd0198f0 ILT |
2096 | memset (p, 0, sizeof *p); |
2097 | p->p_type = PT_NULL; | |
ede4eed4 KR |
2098 | } |
2099 | ||
fd0198f0 | 2100 | elf_tdata (abfd)->phdr = phdrs; |
ede4eed4 | 2101 | |
fd0198f0 | 2102 | elf_tdata (abfd)->next_file_pos = off; |
ede4eed4 | 2103 | |
fd0198f0 ILT |
2104 | /* Write out the program headers. */ |
2105 | if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0 | |
2106 | || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0) | |
2107 | return false; | |
2108 | ||
2109 | return true; | |
2110 | } | |
2111 | ||
2112 | /* Get the size of the program header. | |
2113 | ||
2114 | If this is called by the linker before any of the section VMA's are set, it | |
2115 | can't calculate the correct value for a strange memory layout. This only | |
2116 | happens when SIZEOF_HEADERS is used in a linker script. In this case, | |
2117 | SORTED_HDRS is NULL and we assume the normal scenario of one text and one | |
2118 | data segment (exclusive of .interp and .dynamic). | |
2119 | ||
2120 | ??? User written scripts must either not use SIZEOF_HEADERS, or assume there | |
2121 | will be two segments. */ | |
2122 | ||
2123 | static bfd_size_type | |
2124 | get_program_header_size (abfd) | |
2125 | bfd *abfd; | |
2126 | { | |
2127 | size_t segs; | |
2128 | asection *s; | |
2129 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2130 | ||
2131 | /* We can't return a different result each time we're called. */ | |
2132 | if (elf_tdata (abfd)->program_header_size != 0) | |
2133 | return elf_tdata (abfd)->program_header_size; | |
ae115e51 | 2134 | |
3b950780 ILT |
2135 | if (elf_tdata (abfd)->segment_map != NULL) |
2136 | { | |
2137 | struct elf_segment_map *m; | |
2138 | ||
2139 | segs = 0; | |
2140 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
2141 | ++segs; | |
2142 | elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; | |
2143 | return elf_tdata (abfd)->program_header_size; | |
2144 | } | |
2145 | ||
fd0198f0 ILT |
2146 | /* Assume we will need exactly two PT_LOAD segments: one for text |
2147 | and one for data. */ | |
2148 | segs = 2; | |
2149 | ||
2150 | s = bfd_get_section_by_name (abfd, ".interp"); | |
2151 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
ede4eed4 | 2152 | { |
fd0198f0 ILT |
2153 | /* If we have a loadable interpreter section, we need a |
2154 | PT_INTERP segment. In this case, assume we also need a | |
2155 | PT_PHDR segment, although that may not be true for all | |
2156 | targets. */ | |
2157 | segs += 2; | |
ede4eed4 KR |
2158 | } |
2159 | ||
fd0198f0 | 2160 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) |
ede4eed4 | 2161 | { |
fd0198f0 ILT |
2162 | /* We need a PT_DYNAMIC segment. */ |
2163 | ++segs; | |
ede4eed4 | 2164 | } |
ede4eed4 | 2165 | |
fd0198f0 | 2166 | /* Let the backend count up any program headers it might need. */ |
5b3b9ff6 ILT |
2167 | if (bed->elf_backend_additional_program_headers) |
2168 | { | |
2169 | int a; | |
2170 | ||
2171 | a = (*bed->elf_backend_additional_program_headers) (abfd); | |
2172 | if (a == -1) | |
2173 | abort (); | |
2174 | segs += a; | |
2175 | } | |
ede4eed4 | 2176 | |
fd0198f0 ILT |
2177 | elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; |
2178 | return elf_tdata (abfd)->program_header_size; | |
ede4eed4 KR |
2179 | } |
2180 | ||
2181 | /* Work out the file positions of all the sections. This is called by | |
2182 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
2183 | VMAs must be known before this is called. | |
2184 | ||
2185 | We do not consider reloc sections at this point, unless they form | |
2186 | part of the loadable image. Reloc sections are assigned file | |
2187 | positions in assign_file_positions_for_relocs, which is called by | |
2188 | write_object_contents and final_link. | |
2189 | ||
fd0198f0 | 2190 | We also don't set the positions of the .symtab and .strtab here. */ |
ede4eed4 KR |
2191 | |
2192 | static boolean | |
fd0198f0 | 2193 | assign_file_positions_except_relocs (abfd) |
ede4eed4 | 2194 | bfd *abfd; |
ede4eed4 KR |
2195 | { |
2196 | struct elf_obj_tdata * const tdata = elf_tdata (abfd); | |
2197 | Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd); | |
2198 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); | |
2199 | file_ptr off; | |
2200 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2201 | ||
ede4eed4 KR |
2202 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) |
2203 | { | |
2204 | Elf_Internal_Shdr **hdrpp; | |
2205 | unsigned int i; | |
2206 | ||
fd0198f0 ILT |
2207 | /* Start after the ELF header. */ |
2208 | off = i_ehdrp->e_ehsize; | |
2209 | ||
ede4eed4 KR |
2210 | /* We are not creating an executable, which means that we are |
2211 | not creating a program header, and that the actual order of | |
2212 | the sections in the file is unimportant. */ | |
2213 | for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++) | |
2214 | { | |
2215 | Elf_Internal_Shdr *hdr; | |
2216 | ||
2217 | hdr = *hdrpp; | |
2218 | if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
2219 | { | |
2220 | hdr->sh_offset = -1; | |
2221 | continue; | |
2222 | } | |
fd0198f0 ILT |
2223 | if (i == tdata->symtab_section |
2224 | || i == tdata->strtab_section) | |
ede4eed4 KR |
2225 | { |
2226 | hdr->sh_offset = -1; | |
2227 | continue; | |
2228 | } | |
2229 | ||
5fe14a9f | 2230 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); |
ede4eed4 KR |
2231 | } |
2232 | } | |
2233 | else | |
2234 | { | |
ede4eed4 | 2235 | unsigned int i; |
fd0198f0 | 2236 | Elf_Internal_Shdr **hdrpp; |
ede4eed4 | 2237 | |
fd0198f0 ILT |
2238 | /* Assign file positions for the loaded sections based on the |
2239 | assignment of sections to segments. */ | |
2240 | if (! assign_file_positions_for_segments (abfd)) | |
ede4eed4 KR |
2241 | return false; |
2242 | ||
fd0198f0 ILT |
2243 | /* Assign file positions for the other sections. */ |
2244 | ||
2245 | off = elf_tdata (abfd)->next_file_pos; | |
2246 | for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++) | |
ede4eed4 KR |
2247 | { |
2248 | Elf_Internal_Shdr *hdr; | |
2249 | ||
2250 | hdr = *hdrpp; | |
fd0198f0 ILT |
2251 | if (hdr->bfd_section != NULL |
2252 | && hdr->bfd_section->filepos != 0) | |
2253 | hdr->sh_offset = hdr->bfd_section->filepos; | |
2254 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
ede4eed4 | 2255 | { |
fd0198f0 ILT |
2256 | ((*_bfd_error_handler) |
2257 | ("%s: warning: allocated section `%s' not in segment", | |
2258 | bfd_get_filename (abfd), | |
2259 | (hdr->bfd_section == NULL | |
2260 | ? "*unknown*" | |
2261 | : hdr->bfd_section->name))); | |
2262 | off += (hdr->sh_addr - off) % bed->maxpagesize; | |
5fe14a9f ILT |
2263 | off = _bfd_elf_assign_file_position_for_section (hdr, off, |
2264 | false); | |
ede4eed4 | 2265 | } |
fd0198f0 ILT |
2266 | else if (hdr->sh_type == SHT_REL |
2267 | || hdr->sh_type == SHT_RELA | |
2268 | || hdr == i_shdrpp[tdata->symtab_section] | |
2269 | || hdr == i_shdrpp[tdata->strtab_section]) | |
2270 | hdr->sh_offset = -1; | |
2271 | else | |
2272 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
2273 | } | |
ede4eed4 KR |
2274 | } |
2275 | ||
2276 | /* Place the section headers. */ | |
2277 | off = align_file_position (off, bed->s->file_align); | |
2278 | i_ehdrp->e_shoff = off; | |
2279 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
2280 | ||
2281 | elf_tdata (abfd)->next_file_pos = off; | |
2282 | ||
2283 | return true; | |
2284 | } | |
2285 | ||
ede4eed4 KR |
2286 | static boolean |
2287 | prep_headers (abfd) | |
2288 | bfd *abfd; | |
2289 | { | |
2290 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
2291 | Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ | |
2292 | Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ | |
2293 | int count; | |
2294 | struct bfd_strtab_hash *shstrtab; | |
2295 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2296 | ||
2297 | i_ehdrp = elf_elfheader (abfd); | |
2298 | i_shdrp = elf_elfsections (abfd); | |
2299 | ||
2300 | shstrtab = _bfd_elf_stringtab_init (); | |
2301 | if (shstrtab == NULL) | |
2302 | return false; | |
2303 | ||
2304 | elf_shstrtab (abfd) = shstrtab; | |
2305 | ||
2306 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
2307 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
2308 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
2309 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
2310 | ||
2311 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
2312 | i_ehdrp->e_ident[EI_DATA] = | |
86587dd4 | 2313 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; |
ede4eed4 KR |
2314 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; |
2315 | ||
2316 | for (count = EI_PAD; count < EI_NIDENT; count++) | |
2317 | i_ehdrp->e_ident[count] = 0; | |
2318 | ||
2319 | if ((abfd->flags & DYNAMIC) != 0) | |
2320 | i_ehdrp->e_type = ET_DYN; | |
2321 | else if ((abfd->flags & EXEC_P) != 0) | |
2322 | i_ehdrp->e_type = ET_EXEC; | |
2323 | else | |
2324 | i_ehdrp->e_type = ET_REL; | |
2325 | ||
2326 | switch (bfd_get_arch (abfd)) | |
2327 | { | |
2328 | case bfd_arch_unknown: | |
2329 | i_ehdrp->e_machine = EM_NONE; | |
2330 | break; | |
2331 | case bfd_arch_sparc: | |
2332 | if (bed->s->arch_size == 64) | |
2333 | i_ehdrp->e_machine = EM_SPARC64; | |
2334 | else | |
2335 | i_ehdrp->e_machine = EM_SPARC; | |
2336 | break; | |
2337 | case bfd_arch_i386: | |
2338 | i_ehdrp->e_machine = EM_386; | |
2339 | break; | |
2340 | case bfd_arch_m68k: | |
2341 | i_ehdrp->e_machine = EM_68K; | |
2342 | break; | |
2343 | case bfd_arch_m88k: | |
2344 | i_ehdrp->e_machine = EM_88K; | |
2345 | break; | |
2346 | case bfd_arch_i860: | |
2347 | i_ehdrp->e_machine = EM_860; | |
2348 | break; | |
2349 | case bfd_arch_mips: /* MIPS Rxxxx */ | |
2350 | i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */ | |
2351 | break; | |
2352 | case bfd_arch_hppa: | |
2353 | i_ehdrp->e_machine = EM_PARISC; | |
2354 | break; | |
2355 | case bfd_arch_powerpc: | |
2356 | i_ehdrp->e_machine = EM_PPC; | |
2357 | break; | |
2358 | /* start-sanitize-arc */ | |
2359 | case bfd_arch_arc: | |
2360 | i_ehdrp->e_machine = EM_CYGNUS_ARC; | |
2361 | break; | |
2362 | /* end-sanitize-arc */ | |
2363 | /* also note that EM_M32, AT&T WE32100 is unknown to bfd */ | |
2364 | default: | |
2365 | i_ehdrp->e_machine = EM_NONE; | |
2366 | } | |
2367 | i_ehdrp->e_version = bed->s->ev_current; | |
2368 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
2369 | ||
2370 | /* no program header, for now. */ | |
2371 | i_ehdrp->e_phoff = 0; | |
2372 | i_ehdrp->e_phentsize = 0; | |
2373 | i_ehdrp->e_phnum = 0; | |
2374 | ||
2375 | /* each bfd section is section header entry */ | |
2376 | i_ehdrp->e_entry = bfd_get_start_address (abfd); | |
2377 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
2378 | ||
2379 | /* if we're building an executable, we'll need a program header table */ | |
2380 | if (abfd->flags & EXEC_P) | |
2381 | { | |
2382 | /* it all happens later */ | |
2383 | #if 0 | |
2384 | i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); | |
2385 | ||
2386 | /* elf_build_phdrs() returns a (NULL-terminated) array of | |
2387 | Elf_Internal_Phdrs */ | |
2388 | i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum); | |
2389 | i_ehdrp->e_phoff = outbase; | |
2390 | outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum; | |
2391 | #endif | |
2392 | } | |
2393 | else | |
2394 | { | |
2395 | i_ehdrp->e_phentsize = 0; | |
2396 | i_phdrp = 0; | |
2397 | i_ehdrp->e_phoff = 0; | |
2398 | } | |
2399 | ||
2400 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
2401 | (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false); | |
2402 | elf_tdata (abfd)->strtab_hdr.sh_name = | |
2403 | (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false); | |
2404 | elf_tdata (abfd)->shstrtab_hdr.sh_name = | |
2405 | (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false); | |
2406 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
2407 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
2408 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
2409 | return false; | |
2410 | ||
2411 | return true; | |
2412 | } | |
2413 | ||
2414 | /* Assign file positions for all the reloc sections which are not part | |
2415 | of the loadable file image. */ | |
2416 | ||
2417 | void | |
2418 | _bfd_elf_assign_file_positions_for_relocs (abfd) | |
2419 | bfd *abfd; | |
2420 | { | |
2421 | file_ptr off; | |
2422 | unsigned int i; | |
2423 | Elf_Internal_Shdr **shdrpp; | |
2424 | ||
2425 | off = elf_tdata (abfd)->next_file_pos; | |
2426 | ||
2427 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; | |
2428 | i < elf_elfheader (abfd)->e_shnum; | |
2429 | i++, shdrpp++) | |
2430 | { | |
2431 | Elf_Internal_Shdr *shdrp; | |
2432 | ||
2433 | shdrp = *shdrpp; | |
2434 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
2435 | && shdrp->sh_offset == -1) | |
5fe14a9f | 2436 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, true); |
ede4eed4 KR |
2437 | } |
2438 | ||
2439 | elf_tdata (abfd)->next_file_pos = off; | |
2440 | } | |
2441 | ||
2442 | boolean | |
2443 | _bfd_elf_write_object_contents (abfd) | |
2444 | bfd *abfd; | |
2445 | { | |
2446 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2447 | Elf_Internal_Ehdr *i_ehdrp; | |
2448 | Elf_Internal_Shdr **i_shdrp; | |
2449 | boolean failed; | |
2450 | unsigned int count; | |
2451 | ||
2452 | if (! abfd->output_has_begun | |
2453 | && ! _bfd_elf_compute_section_file_positions (abfd, | |
2454 | (struct bfd_link_info *) NULL)) | |
2455 | return false; | |
2456 | ||
2457 | i_shdrp = elf_elfsections (abfd); | |
2458 | i_ehdrp = elf_elfheader (abfd); | |
2459 | ||
2460 | failed = false; | |
2461 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); | |
2462 | if (failed) | |
2463 | return false; | |
2464 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
2465 | ||
2466 | /* After writing the headers, we need to write the sections too... */ | |
2467 | for (count = 1; count < i_ehdrp->e_shnum; count++) | |
2468 | { | |
2469 | if (bed->elf_backend_section_processing) | |
2470 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
2471 | if (i_shdrp[count]->contents) | |
2472 | { | |
2473 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 | |
2474 | || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size, | |
2475 | 1, abfd) | |
2476 | != i_shdrp[count]->sh_size)) | |
2477 | return false; | |
2478 | } | |
2479 | } | |
2480 | ||
2481 | /* Write out the section header names. */ | |
2482 | if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
2483 | || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd))) | |
2484 | return false; | |
2485 | ||
2486 | if (bed->elf_backend_final_write_processing) | |
2487 | (*bed->elf_backend_final_write_processing) (abfd, | |
2488 | elf_tdata (abfd)->linker); | |
2489 | ||
2490 | return bed->s->write_shdrs_and_ehdr (abfd); | |
2491 | } | |
2492 | ||
2493 | /* given a section, search the header to find them... */ | |
2494 | int | |
2495 | _bfd_elf_section_from_bfd_section (abfd, asect) | |
2496 | bfd *abfd; | |
2497 | struct sec *asect; | |
2498 | { | |
2499 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2500 | Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd); | |
2501 | int index; | |
2502 | Elf_Internal_Shdr *hdr; | |
2503 | int maxindex = elf_elfheader (abfd)->e_shnum; | |
2504 | ||
2505 | for (index = 0; index < maxindex; index++) | |
2506 | { | |
2507 | hdr = i_shdrp[index]; | |
2508 | if (hdr->bfd_section == asect) | |
2509 | return index; | |
2510 | } | |
2511 | ||
2512 | if (bed->elf_backend_section_from_bfd_section) | |
2513 | { | |
2514 | for (index = 0; index < maxindex; index++) | |
2515 | { | |
2516 | int retval; | |
2517 | ||
2518 | hdr = i_shdrp[index]; | |
2519 | retval = index; | |
2520 | if ((*bed->elf_backend_section_from_bfd_section) | |
2521 | (abfd, hdr, asect, &retval)) | |
2522 | return retval; | |
2523 | } | |
2524 | } | |
2525 | ||
2526 | if (bfd_is_abs_section (asect)) | |
2527 | return SHN_ABS; | |
2528 | if (bfd_is_com_section (asect)) | |
2529 | return SHN_COMMON; | |
2530 | if (bfd_is_und_section (asect)) | |
2531 | return SHN_UNDEF; | |
2532 | ||
2533 | return -1; | |
2534 | } | |
2535 | ||
2536 | /* given a symbol, return the bfd index for that symbol. */ | |
2537 | int | |
2538 | _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr) | |
2539 | bfd *abfd; | |
2540 | struct symbol_cache_entry **asym_ptr_ptr; | |
2541 | { | |
2542 | struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr; | |
2543 | int idx; | |
2544 | flagword flags = asym_ptr->flags; | |
2545 | ||
2546 | /* When gas creates relocations against local labels, it creates its | |
2547 | own symbol for the section, but does put the symbol into the | |
2548 | symbol chain, so udata is 0. When the linker is generating | |
2549 | relocatable output, this section symbol may be for one of the | |
2550 | input sections rather than the output section. */ | |
2551 | if (asym_ptr->udata.i == 0 | |
2552 | && (flags & BSF_SECTION_SYM) | |
2553 | && asym_ptr->section) | |
2554 | { | |
2555 | int indx; | |
2556 | ||
2557 | if (asym_ptr->section->output_section != NULL) | |
2558 | indx = asym_ptr->section->output_section->index; | |
2559 | else | |
2560 | indx = asym_ptr->section->index; | |
2561 | if (elf_section_syms (abfd)[indx]) | |
2562 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; | |
2563 | } | |
2564 | ||
2565 | idx = asym_ptr->udata.i; | |
2566 | BFD_ASSERT (idx != 0); | |
2567 | ||
2568 | #if DEBUG & 4 | |
2569 | { | |
2570 | fprintf (stderr, | |
2571 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n", | |
2572 | (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags)); | |
2573 | fflush (stderr); | |
2574 | } | |
2575 | #endif | |
2576 | ||
2577 | return idx; | |
2578 | } | |
2579 | ||
3dbf33ee ILT |
2580 | /* Copy private BFD data. This copies any program header information. */ |
2581 | ||
2582 | static boolean | |
2583 | copy_private_bfd_data (ibfd, obfd) | |
2584 | bfd *ibfd; | |
2585 | bfd *obfd; | |
2586 | { | |
6933148a | 2587 | Elf_Internal_Ehdr *iehdr; |
3dbf33ee ILT |
2588 | struct elf_segment_map *mfirst; |
2589 | struct elf_segment_map **pm; | |
2590 | Elf_Internal_Phdr *p; | |
2591 | unsigned int i, c; | |
2592 | ||
2593 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
2594 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
2595 | return true; | |
2596 | ||
2597 | if (elf_tdata (ibfd)->phdr == NULL) | |
2598 | return true; | |
2599 | ||
6933148a ILT |
2600 | iehdr = elf_elfheader (ibfd); |
2601 | ||
3dbf33ee ILT |
2602 | mfirst = NULL; |
2603 | pm = &mfirst; | |
2604 | ||
2605 | c = elf_elfheader (ibfd)->e_phnum; | |
2606 | for (i = 0, p = elf_tdata (ibfd)->phdr; i < c; i++, p++) | |
2607 | { | |
3dbf33ee | 2608 | unsigned int csecs; |
6933148a ILT |
2609 | asection *s; |
2610 | struct elf_segment_map *m; | |
2611 | unsigned int isec; | |
3dbf33ee ILT |
2612 | |
2613 | csecs = 0; | |
3dbf33ee | 2614 | |
6933148a ILT |
2615 | /* The complicated case when p_vaddr is 0 is to handle the |
2616 | Solaris linker, which generates a PT_INTERP section with | |
2617 | p_vaddr and p_memsz set to 0. */ | |
2618 | for (s = ibfd->sections; s != NULL; s = s->next) | |
2619 | if (((s->vma >= p->p_vaddr | |
2620 | && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz | |
2621 | || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz)) | |
2622 | || (p->p_vaddr == 0 | |
2623 | && p->p_filesz > 0 | |
2624 | && (s->flags & SEC_HAS_CONTENTS) != 0 | |
2625 | && (bfd_vma) s->filepos >= p->p_offset | |
2626 | && ((bfd_vma) s->filepos + s->_raw_size | |
2627 | <= p->p_offset + p->p_filesz))) | |
86587dd4 | 2628 | && (s->flags & SEC_ALLOC) != 0 |
6933148a ILT |
2629 | && s->output_section != NULL) |
2630 | ++csecs; | |
3dbf33ee ILT |
2631 | |
2632 | m = ((struct elf_segment_map *) | |
2633 | bfd_alloc (obfd, | |
2634 | (sizeof (struct elf_segment_map) | |
2635 | + (csecs - 1) * sizeof (asection *)))); | |
2636 | if (m == NULL) | |
a9713b91 | 2637 | return false; |
3dbf33ee ILT |
2638 | |
2639 | m->next = NULL; | |
2640 | m->p_type = p->p_type; | |
2641 | m->p_flags = p->p_flags; | |
2642 | m->p_flags_valid = 1; | |
2643 | m->p_paddr = p->p_paddr; | |
2644 | m->p_paddr_valid = 1; | |
2645 | ||
6933148a ILT |
2646 | m->includes_filehdr = (p->p_offset == 0 |
2647 | && p->p_filesz >= iehdr->e_ehsize); | |
2648 | ||
2649 | m->includes_phdrs = (p->p_offset <= (bfd_vma) iehdr->e_phoff | |
2650 | && (p->p_offset + p->p_filesz | |
2651 | >= ((bfd_vma) iehdr->e_phoff | |
2652 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
3dbf33ee | 2653 | |
6933148a ILT |
2654 | isec = 0; |
2655 | for (s = ibfd->sections; s != NULL; s = s->next) | |
2656 | { | |
2657 | if (((s->vma >= p->p_vaddr | |
2658 | && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz | |
2659 | || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz)) | |
2660 | || (p->p_vaddr == 0 | |
2661 | && p->p_filesz > 0 | |
2662 | && (s->flags & SEC_HAS_CONTENTS) != 0 | |
2663 | && (bfd_vma) s->filepos >= p->p_offset | |
2664 | && ((bfd_vma) s->filepos + s->_raw_size | |
2665 | <= p->p_offset + p->p_filesz))) | |
86587dd4 | 2666 | && (s->flags & SEC_ALLOC) != 0 |
6933148a | 2667 | && s->output_section != NULL) |
3dbf33ee | 2668 | { |
6933148a ILT |
2669 | m->sections[isec] = s->output_section; |
2670 | ++isec; | |
3dbf33ee | 2671 | } |
3dbf33ee | 2672 | } |
6933148a | 2673 | BFD_ASSERT (isec == csecs); |
6933148a | 2674 | m->count = csecs; |
3dbf33ee ILT |
2675 | |
2676 | *pm = m; | |
2677 | pm = &m->next; | |
2678 | } | |
2679 | ||
2680 | elf_tdata (obfd)->segment_map = mfirst; | |
2681 | ||
2682 | return true; | |
2683 | } | |
2684 | ||
fd0198f0 ILT |
2685 | /* Copy private section information. This copies over the entsize |
2686 | field, and sometimes the info field. */ | |
2687 | ||
2688 | boolean | |
2689 | _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec) | |
2690 | bfd *ibfd; | |
2691 | asection *isec; | |
2692 | bfd *obfd; | |
2693 | asection *osec; | |
2694 | { | |
2695 | Elf_Internal_Shdr *ihdr, *ohdr; | |
2696 | ||
2697 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
2698 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
2699 | return true; | |
2700 | ||
3dbf33ee ILT |
2701 | /* Copy over private BFD data if it has not already been copied. |
2702 | This must be done here, rather than in the copy_private_bfd_data | |
2703 | entry point, because the latter is called after the section | |
2704 | contents have been set, which means that the program headers have | |
2705 | already been worked out. */ | |
2706 | if (elf_tdata (obfd)->segment_map == NULL | |
2707 | && elf_tdata (ibfd)->phdr != NULL) | |
2708 | { | |
2709 | asection *s; | |
2710 | ||
2711 | /* Only set up the segments when all the sections have been set | |
2712 | up. */ | |
2713 | for (s = ibfd->sections; s != NULL; s = s->next) | |
2714 | if (s->output_section == NULL) | |
2715 | break; | |
2716 | if (s == NULL) | |
2717 | { | |
2718 | if (! copy_private_bfd_data (ibfd, obfd)) | |
2719 | return false; | |
2720 | } | |
2721 | } | |
2722 | ||
fd0198f0 ILT |
2723 | ihdr = &elf_section_data (isec)->this_hdr; |
2724 | ohdr = &elf_section_data (osec)->this_hdr; | |
2725 | ||
2726 | ohdr->sh_entsize = ihdr->sh_entsize; | |
2727 | ||
2728 | if (ihdr->sh_type == SHT_SYMTAB | |
2729 | || ihdr->sh_type == SHT_DYNSYM) | |
2730 | ohdr->sh_info = ihdr->sh_info; | |
2731 | ||
2732 | return true; | |
2733 | } | |
2734 | ||
2735 | /* Copy private symbol information. If this symbol is in a section | |
2736 | which we did not map into a BFD section, try to map the section | |
2737 | index correctly. We use special macro definitions for the mapped | |
2738 | section indices; these definitions are interpreted by the | |
2739 | swap_out_syms function. */ | |
2740 | ||
2741 | #define MAP_ONESYMTAB (SHN_LORESERVE - 1) | |
2742 | #define MAP_DYNSYMTAB (SHN_LORESERVE - 2) | |
2743 | #define MAP_STRTAB (SHN_LORESERVE - 3) | |
2744 | #define MAP_SHSTRTAB (SHN_LORESERVE - 4) | |
2745 | ||
2746 | boolean | |
2747 | _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg) | |
2748 | bfd *ibfd; | |
2749 | asymbol *isymarg; | |
2750 | bfd *obfd; | |
2751 | asymbol *osymarg; | |
2752 | { | |
2753 | elf_symbol_type *isym, *osym; | |
2754 | ||
2755 | isym = elf_symbol_from (ibfd, isymarg); | |
2756 | osym = elf_symbol_from (obfd, osymarg); | |
2757 | ||
2758 | if (isym != NULL | |
2759 | && osym != NULL | |
2760 | && bfd_is_abs_section (isym->symbol.section)) | |
2761 | { | |
2762 | unsigned int shndx; | |
2763 | ||
2764 | shndx = isym->internal_elf_sym.st_shndx; | |
2765 | if (shndx == elf_onesymtab (ibfd)) | |
2766 | shndx = MAP_ONESYMTAB; | |
2767 | else if (shndx == elf_dynsymtab (ibfd)) | |
2768 | shndx = MAP_DYNSYMTAB; | |
2769 | else if (shndx == elf_tdata (ibfd)->strtab_section) | |
2770 | shndx = MAP_STRTAB; | |
2771 | else if (shndx == elf_tdata (ibfd)->shstrtab_section) | |
2772 | shndx = MAP_SHSTRTAB; | |
2773 | osym->internal_elf_sym.st_shndx = shndx; | |
2774 | } | |
2775 | ||
2776 | return true; | |
2777 | } | |
2778 | ||
2779 | /* Swap out the symbols. */ | |
2780 | ||
ede4eed4 KR |
2781 | static boolean |
2782 | swap_out_syms (abfd, sttp) | |
2783 | bfd *abfd; | |
2784 | struct bfd_strtab_hash **sttp; | |
2785 | { | |
2786 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2787 | ||
2788 | if (!elf_map_symbols (abfd)) | |
2789 | return false; | |
2790 | ||
2791 | /* Dump out the symtabs. */ | |
2792 | { | |
2793 | int symcount = bfd_get_symcount (abfd); | |
2794 | asymbol **syms = bfd_get_outsymbols (abfd); | |
2795 | struct bfd_strtab_hash *stt; | |
2796 | Elf_Internal_Shdr *symtab_hdr; | |
2797 | Elf_Internal_Shdr *symstrtab_hdr; | |
2798 | char *outbound_syms; | |
2799 | int idx; | |
2800 | ||
2801 | stt = _bfd_elf_stringtab_init (); | |
2802 | if (stt == NULL) | |
2803 | return false; | |
2804 | ||
2805 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
2806 | symtab_hdr->sh_type = SHT_SYMTAB; | |
2807 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
2808 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
2809 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
2810 | symtab_hdr->sh_addralign = bed->s->file_align; | |
2811 | ||
2812 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
2813 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
2814 | ||
2815 | outbound_syms = bfd_alloc (abfd, | |
2816 | (1 + symcount) * bed->s->sizeof_sym); | |
2817 | if (outbound_syms == NULL) | |
a9713b91 | 2818 | return false; |
ede4eed4 KR |
2819 | symtab_hdr->contents = (PTR) outbound_syms; |
2820 | ||
2821 | /* now generate the data (for "contents") */ | |
2822 | { | |
2823 | /* Fill in zeroth symbol and swap it out. */ | |
2824 | Elf_Internal_Sym sym; | |
2825 | sym.st_name = 0; | |
2826 | sym.st_value = 0; | |
2827 | sym.st_size = 0; | |
2828 | sym.st_info = 0; | |
2829 | sym.st_other = 0; | |
2830 | sym.st_shndx = SHN_UNDEF; | |
cf9fb9f2 | 2831 | bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms); |
ede4eed4 KR |
2832 | outbound_syms += bed->s->sizeof_sym; |
2833 | } | |
2834 | for (idx = 0; idx < symcount; idx++) | |
2835 | { | |
2836 | Elf_Internal_Sym sym; | |
2837 | bfd_vma value = syms[idx]->value; | |
2838 | elf_symbol_type *type_ptr; | |
2839 | flagword flags = syms[idx]->flags; | |
2840 | ||
2841 | if (flags & BSF_SECTION_SYM) | |
2842 | /* Section symbols have no names. */ | |
2843 | sym.st_name = 0; | |
2844 | else | |
2845 | { | |
2846 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
2847 | syms[idx]->name, | |
2848 | true, false); | |
2849 | if (sym.st_name == (unsigned long) -1) | |
2850 | return false; | |
2851 | } | |
2852 | ||
2853 | type_ptr = elf_symbol_from (abfd, syms[idx]); | |
2854 | ||
2855 | if (bfd_is_com_section (syms[idx]->section)) | |
2856 | { | |
2857 | /* ELF common symbols put the alignment into the `value' field, | |
2858 | and the size into the `size' field. This is backwards from | |
2859 | how BFD handles it, so reverse it here. */ | |
2860 | sym.st_size = value; | |
2861 | if (type_ptr == NULL | |
2862 | || type_ptr->internal_elf_sym.st_value == 0) | |
2863 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
2864 | else | |
2865 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
2866 | sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, | |
2867 | syms[idx]->section); | |
2868 | } | |
2869 | else | |
2870 | { | |
2871 | asection *sec = syms[idx]->section; | |
2872 | int shndx; | |
2873 | ||
2874 | if (sec->output_section) | |
2875 | { | |
2876 | value += sec->output_offset; | |
2877 | sec = sec->output_section; | |
2878 | } | |
2879 | value += sec->vma; | |
2880 | sym.st_value = value; | |
2881 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
fd0198f0 ILT |
2882 | |
2883 | if (bfd_is_abs_section (sec) | |
2884 | && type_ptr != NULL | |
2885 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
ede4eed4 | 2886 | { |
fd0198f0 ILT |
2887 | /* This symbol is in a real ELF section which we did |
2888 | not create as a BFD section. Undo the mapping done | |
2889 | by copy_private_symbol_data. */ | |
2890 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
2891 | switch (shndx) | |
2892 | { | |
2893 | case MAP_ONESYMTAB: | |
2894 | shndx = elf_onesymtab (abfd); | |
2895 | break; | |
2896 | case MAP_DYNSYMTAB: | |
2897 | shndx = elf_dynsymtab (abfd); | |
2898 | break; | |
2899 | case MAP_STRTAB: | |
2900 | shndx = elf_tdata (abfd)->strtab_section; | |
2901 | break; | |
2902 | case MAP_SHSTRTAB: | |
2903 | shndx = elf_tdata (abfd)->shstrtab_section; | |
2904 | break; | |
2905 | default: | |
2906 | break; | |
2907 | } | |
2908 | } | |
2909 | else | |
2910 | { | |
2911 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
2912 | ||
2913 | if (shndx == -1) | |
2914 | { | |
2915 | asection *sec2; | |
2916 | ||
2917 | /* Writing this would be a hell of a lot easier if | |
2918 | we had some decent documentation on bfd, and | |
2919 | knew what to expect of the library, and what to | |
2920 | demand of applications. For example, it | |
2921 | appears that `objcopy' might not set the | |
2922 | section of a symbol to be a section that is | |
2923 | actually in the output file. */ | |
2924 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
2925 | BFD_ASSERT (sec2 != 0); | |
2926 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
2927 | BFD_ASSERT (shndx != -1); | |
2928 | } | |
ede4eed4 | 2929 | } |
fd0198f0 ILT |
2930 | |
2931 | sym.st_shndx = shndx; | |
ede4eed4 KR |
2932 | } |
2933 | ||
2934 | if (bfd_is_com_section (syms[idx]->section)) | |
2935 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT); | |
2936 | else if (bfd_is_und_section (syms[idx]->section)) | |
2937 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
2938 | ? STB_WEAK | |
2939 | : STB_GLOBAL), | |
2940 | ((flags & BSF_FUNCTION) | |
2941 | ? STT_FUNC | |
2942 | : STT_NOTYPE)); | |
2943 | else if (flags & BSF_SECTION_SYM) | |
2944 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
2945 | else if (flags & BSF_FILE) | |
2946 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
2947 | else | |
2948 | { | |
2949 | int bind = STB_LOCAL; | |
2950 | int type = STT_OBJECT; | |
2951 | ||
2952 | if (flags & BSF_LOCAL) | |
2953 | bind = STB_LOCAL; | |
2954 | else if (flags & BSF_WEAK) | |
2955 | bind = STB_WEAK; | |
2956 | else if (flags & BSF_GLOBAL) | |
2957 | bind = STB_GLOBAL; | |
2958 | ||
2959 | if (flags & BSF_FUNCTION) | |
2960 | type = STT_FUNC; | |
2961 | ||
2962 | sym.st_info = ELF_ST_INFO (bind, type); | |
2963 | } | |
2964 | ||
2965 | sym.st_other = 0; | |
cf9fb9f2 | 2966 | bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms); |
ede4eed4 KR |
2967 | outbound_syms += bed->s->sizeof_sym; |
2968 | } | |
2969 | ||
2970 | *sttp = stt; | |
2971 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
2972 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
2973 | ||
2974 | symstrtab_hdr->sh_flags = 0; | |
2975 | symstrtab_hdr->sh_addr = 0; | |
2976 | symstrtab_hdr->sh_entsize = 0; | |
2977 | symstrtab_hdr->sh_link = 0; | |
2978 | symstrtab_hdr->sh_info = 0; | |
2979 | symstrtab_hdr->sh_addralign = 1; | |
2980 | } | |
2981 | ||
2982 | return true; | |
2983 | } | |
2984 | ||
2985 | /* Return the number of bytes required to hold the symtab vector. | |
2986 | ||
2987 | Note that we base it on the count plus 1, since we will null terminate | |
2988 | the vector allocated based on this size. However, the ELF symbol table | |
2989 | always has a dummy entry as symbol #0, so it ends up even. */ | |
2990 | ||
2991 | long | |
2992 | _bfd_elf_get_symtab_upper_bound (abfd) | |
2993 | bfd *abfd; | |
2994 | { | |
2995 | long symcount; | |
2996 | long symtab_size; | |
2997 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
2998 | ||
2999 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3000 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); | |
3001 | ||
3002 | return symtab_size; | |
3003 | } | |
3004 | ||
3005 | long | |
3006 | _bfd_elf_get_dynamic_symtab_upper_bound (abfd) | |
3007 | bfd *abfd; | |
3008 | { | |
3009 | long symcount; | |
3010 | long symtab_size; | |
3011 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3012 | ||
3013 | if (elf_dynsymtab (abfd) == 0) | |
3014 | { | |
3015 | bfd_set_error (bfd_error_invalid_operation); | |
3016 | return -1; | |
3017 | } | |
3018 | ||
3019 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3020 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); | |
3021 | ||
3022 | return symtab_size; | |
3023 | } | |
3024 | ||
3025 | long | |
3026 | _bfd_elf_get_reloc_upper_bound (abfd, asect) | |
3027 | bfd *abfd; | |
3028 | sec_ptr asect; | |
3029 | { | |
3030 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
3031 | } | |
3032 | ||
3033 | /* Canonicalize the relocs. */ | |
3034 | ||
3035 | long | |
3036 | _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols) | |
3037 | bfd *abfd; | |
3038 | sec_ptr section; | |
3039 | arelent **relptr; | |
3040 | asymbol **symbols; | |
3041 | { | |
3042 | arelent *tblptr; | |
3043 | unsigned int i; | |
3044 | ||
3045 | if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd, section, symbols)) | |
3046 | return -1; | |
3047 | ||
3048 | tblptr = section->relocation; | |
3049 | for (i = 0; i < section->reloc_count; i++) | |
3050 | *relptr++ = tblptr++; | |
3051 | ||
3052 | *relptr = NULL; | |
3053 | ||
3054 | return section->reloc_count; | |
3055 | } | |
3056 | ||
3057 | long | |
3058 | _bfd_elf_get_symtab (abfd, alocation) | |
3059 | bfd *abfd; | |
3060 | asymbol **alocation; | |
3061 | { | |
3062 | long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false); | |
3063 | ||
3064 | if (symcount >= 0) | |
3065 | bfd_get_symcount (abfd) = symcount; | |
3066 | return symcount; | |
3067 | } | |
3068 | ||
3069 | long | |
3070 | _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation) | |
3071 | bfd *abfd; | |
3072 | asymbol **alocation; | |
3073 | { | |
3074 | return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true); | |
3075 | } | |
3076 | ||
3077 | asymbol * | |
3078 | _bfd_elf_make_empty_symbol (abfd) | |
3079 | bfd *abfd; | |
3080 | { | |
3081 | elf_symbol_type *newsym; | |
3082 | ||
3083 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type)); | |
3084 | if (!newsym) | |
a9713b91 | 3085 | return NULL; |
ede4eed4 KR |
3086 | else |
3087 | { | |
3088 | newsym->symbol.the_bfd = abfd; | |
3089 | return &newsym->symbol; | |
3090 | } | |
3091 | } | |
3092 | ||
3093 | void | |
3094 | _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret) | |
3095 | bfd *ignore_abfd; | |
3096 | asymbol *symbol; | |
3097 | symbol_info *ret; | |
3098 | { | |
3099 | bfd_symbol_info (symbol, ret); | |
3100 | } | |
3101 | ||
3102 | alent * | |
3103 | _bfd_elf_get_lineno (ignore_abfd, symbol) | |
3104 | bfd *ignore_abfd; | |
3105 | asymbol *symbol; | |
3106 | { | |
8cd2f4fe | 3107 | abort (); |
ede4eed4 KR |
3108 | return NULL; |
3109 | } | |
3110 | ||
3111 | boolean | |
3112 | _bfd_elf_set_arch_mach (abfd, arch, machine) | |
3113 | bfd *abfd; | |
3114 | enum bfd_architecture arch; | |
3115 | unsigned long machine; | |
3116 | { | |
3117 | /* If this isn't the right architecture for this backend, and this | |
3118 | isn't the generic backend, fail. */ | |
3119 | if (arch != get_elf_backend_data (abfd)->arch | |
3120 | && arch != bfd_arch_unknown | |
3121 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
3122 | return false; | |
3123 | ||
3124 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
3125 | } | |
3126 | ||
6f904fce ILT |
3127 | /* Find the nearest line to a particular section and offset, for error |
3128 | reporting. */ | |
3129 | ||
ede4eed4 KR |
3130 | boolean |
3131 | _bfd_elf_find_nearest_line (abfd, | |
6f904fce ILT |
3132 | section, |
3133 | symbols, | |
3134 | offset, | |
3135 | filename_ptr, | |
3136 | functionname_ptr, | |
3137 | line_ptr) | |
ede4eed4 KR |
3138 | bfd *abfd; |
3139 | asection *section; | |
3140 | asymbol **symbols; | |
3141 | bfd_vma offset; | |
3142 | CONST char **filename_ptr; | |
3143 | CONST char **functionname_ptr; | |
3144 | unsigned int *line_ptr; | |
3145 | { | |
86aac8ea | 3146 | boolean found; |
6f904fce ILT |
3147 | const char *filename; |
3148 | asymbol *func; | |
86aac8ea | 3149 | bfd_vma low_func; |
6f904fce ILT |
3150 | asymbol **p; |
3151 | ||
86aac8ea ILT |
3152 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, |
3153 | &found, filename_ptr, | |
3154 | functionname_ptr, line_ptr, | |
3155 | &elf_tdata (abfd)->line_info)) | |
3156 | return false; | |
3157 | if (found) | |
3158 | return true; | |
3159 | ||
6f904fce ILT |
3160 | if (symbols == NULL) |
3161 | return false; | |
3162 | ||
3163 | filename = NULL; | |
3164 | func = NULL; | |
86aac8ea | 3165 | low_func = 0; |
6f904fce ILT |
3166 | |
3167 | for (p = symbols; *p != NULL; p++) | |
3168 | { | |
3169 | elf_symbol_type *q; | |
3170 | ||
3171 | q = (elf_symbol_type *) *p; | |
3172 | ||
3173 | if (bfd_get_section (&q->symbol) != section) | |
3174 | continue; | |
3175 | ||
3176 | switch (ELF_ST_TYPE (q->internal_elf_sym.st_info)) | |
3177 | { | |
3178 | default: | |
3179 | break; | |
3180 | case STT_FILE: | |
3181 | filename = bfd_asymbol_name (&q->symbol); | |
3182 | break; | |
3183 | case STT_FUNC: | |
86aac8ea ILT |
3184 | if (q->symbol.section == section |
3185 | && q->symbol.value >= low_func | |
3186 | && q->symbol.value <= offset) | |
3187 | { | |
3188 | func = (asymbol *) q; | |
3189 | low_func = q->symbol.value; | |
3190 | } | |
6f904fce ILT |
3191 | break; |
3192 | } | |
3193 | } | |
3194 | ||
3195 | if (func == NULL) | |
3196 | return false; | |
3197 | ||
3198 | *filename_ptr = filename; | |
3199 | *functionname_ptr = bfd_asymbol_name (func); | |
3200 | *line_ptr = 0; | |
3201 | return true; | |
ede4eed4 KR |
3202 | } |
3203 | ||
3204 | int | |
3205 | _bfd_elf_sizeof_headers (abfd, reloc) | |
3206 | bfd *abfd; | |
3207 | boolean reloc; | |
3208 | { | |
3209 | int ret; | |
3210 | ||
3211 | ret = get_elf_backend_data (abfd)->s->sizeof_ehdr; | |
3212 | if (! reloc) | |
fd0198f0 | 3213 | ret += get_program_header_size (abfd); |
ede4eed4 KR |
3214 | return ret; |
3215 | } | |
3216 | ||
3217 | boolean | |
3218 | _bfd_elf_set_section_contents (abfd, section, location, offset, count) | |
3219 | bfd *abfd; | |
3220 | sec_ptr section; | |
3221 | PTR location; | |
3222 | file_ptr offset; | |
3223 | bfd_size_type count; | |
3224 | { | |
3225 | Elf_Internal_Shdr *hdr; | |
3226 | ||
3227 | if (! abfd->output_has_begun | |
3228 | && ! _bfd_elf_compute_section_file_positions (abfd, | |
3229 | (struct bfd_link_info *) NULL)) | |
3230 | return false; | |
3231 | ||
3232 | hdr = &elf_section_data (section)->this_hdr; | |
3233 | ||
3234 | if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1) | |
3235 | return false; | |
3236 | if (bfd_write (location, 1, count, abfd) != count) | |
3237 | return false; | |
3238 | ||
3239 | return true; | |
3240 | } | |
3241 | ||
3242 | void | |
3243 | _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst) | |
3244 | bfd *abfd; | |
3245 | arelent *cache_ptr; | |
3246 | Elf_Internal_Rela *dst; | |
3247 | { | |
8cd2f4fe | 3248 | abort (); |
ede4eed4 KR |
3249 | } |
3250 | ||
3251 | #if 0 | |
3252 | void | |
3253 | _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst) | |
3254 | bfd *abfd; | |
3255 | arelent *cache_ptr; | |
3256 | Elf_Internal_Rel *dst; | |
3257 | { | |
8cd2f4fe | 3258 | abort (); |
ede4eed4 KR |
3259 | } |
3260 | #endif |