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 | 41 | static INLINE struct elf_segment_map *make_mapping |
edf3fe48 | 42 | PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean)); |
fd0198f0 ILT |
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 | 558 | ret->type = STT_NOTYPE; |
869b7d80 ILT |
559 | /* Assume that we have been called by a non-ELF symbol reader. |
560 | This flag is then reset by the code which reads an ELF input | |
561 | file. This ensures that a symbol created by a non-ELF symbol | |
562 | reader will have the flag set correctly. */ | |
563 | ret->elf_link_hash_flags = ELF_LINK_NON_ELF; | |
013dec1a ILT |
564 | } |
565 | ||
566 | return (struct bfd_hash_entry *) ret; | |
567 | } | |
568 | ||
5315c428 ILT |
569 | /* Initialize an ELF linker hash table. */ |
570 | ||
571 | boolean | |
572 | _bfd_elf_link_hash_table_init (table, abfd, newfunc) | |
573 | struct elf_link_hash_table *table; | |
574 | bfd *abfd; | |
575 | struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *, | |
576 | struct bfd_hash_table *, | |
577 | const char *)); | |
578 | { | |
b176e1e9 | 579 | table->dynamic_sections_created = false; |
5315c428 | 580 | table->dynobj = NULL; |
b176e1e9 ILT |
581 | /* The first dynamic symbol is a dummy. */ |
582 | table->dynsymcount = 1; | |
5315c428 ILT |
583 | table->dynstr = NULL; |
584 | table->bucketcount = 0; | |
b176e1e9 | 585 | table->needed = NULL; |
19bfbcbe | 586 | table->hgot = NULL; |
d1bf45aa | 587 | table->stab_info = NULL; |
5315c428 ILT |
588 | return _bfd_link_hash_table_init (&table->root, abfd, newfunc); |
589 | } | |
590 | ||
013dec1a ILT |
591 | /* Create an ELF linker hash table. */ |
592 | ||
593 | struct bfd_link_hash_table * | |
594 | _bfd_elf_link_hash_table_create (abfd) | |
595 | bfd *abfd; | |
596 | { | |
597 | struct elf_link_hash_table *ret; | |
598 | ||
599 | ret = ((struct elf_link_hash_table *) | |
600 | bfd_alloc (abfd, sizeof (struct elf_link_hash_table))); | |
601 | if (ret == (struct elf_link_hash_table *) NULL) | |
a9713b91 | 602 | return NULL; |
5315c428 ILT |
603 | |
604 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc)) | |
013dec1a ILT |
605 | { |
606 | bfd_release (abfd, ret); | |
607 | return NULL; | |
608 | } | |
609 | ||
013dec1a ILT |
610 | return &ret->root; |
611 | } | |
7c6da9ca ILT |
612 | |
613 | /* This is a hook for the ELF emulation code in the generic linker to | |
614 | tell the backend linker what file name to use for the DT_NEEDED | |
b176e1e9 ILT |
615 | entry for a dynamic object. The generic linker passes name as an |
616 | empty string to indicate that no DT_NEEDED entry should be made. */ | |
7c6da9ca ILT |
617 | |
618 | void | |
619 | bfd_elf_set_dt_needed_name (abfd, name) | |
620 | bfd *abfd; | |
621 | const char *name; | |
622 | { | |
053ae1d7 ILT |
623 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour |
624 | && bfd_get_format (abfd) == bfd_object) | |
625 | elf_dt_name (abfd) = name; | |
7c6da9ca | 626 | } |
b176e1e9 | 627 | |
053ae1d7 ILT |
628 | /* Get the list of DT_NEEDED entries for a link. This is a hook for |
629 | the ELF emulation code. */ | |
b176e1e9 | 630 | |
5fe14a9f | 631 | struct bfd_link_needed_list * |
b176e1e9 ILT |
632 | bfd_elf_get_needed_list (abfd, info) |
633 | bfd *abfd; | |
634 | struct bfd_link_info *info; | |
635 | { | |
b2193cc5 ILT |
636 | if (info->hash->creator->flavour != bfd_target_elf_flavour) |
637 | return NULL; | |
b176e1e9 ILT |
638 | return elf_hash_table (info)->needed; |
639 | } | |
053ae1d7 ILT |
640 | |
641 | /* Get the name actually used for a dynamic object for a link. This | |
642 | is the SONAME entry if there is one. Otherwise, it is the string | |
643 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
644 | ||
645 | const char * | |
646 | bfd_elf_get_dt_soname (abfd) | |
647 | bfd *abfd; | |
648 | { | |
649 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
650 | && bfd_get_format (abfd) == bfd_object) | |
651 | return elf_dt_name (abfd); | |
652 | return NULL; | |
653 | } | |
ede4eed4 KR |
654 | \f |
655 | /* Allocate an ELF string table--force the first byte to be zero. */ | |
656 | ||
657 | struct bfd_strtab_hash * | |
658 | _bfd_elf_stringtab_init () | |
659 | { | |
660 | struct bfd_strtab_hash *ret; | |
661 | ||
662 | ret = _bfd_stringtab_init (); | |
663 | if (ret != NULL) | |
664 | { | |
665 | bfd_size_type loc; | |
666 | ||
667 | loc = _bfd_stringtab_add (ret, "", true, false); | |
668 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); | |
669 | if (loc == (bfd_size_type) -1) | |
670 | { | |
671 | _bfd_stringtab_free (ret); | |
672 | ret = NULL; | |
673 | } | |
674 | } | |
675 | return ret; | |
676 | } | |
677 | \f | |
678 | /* ELF .o/exec file reading */ | |
679 | ||
680 | /* Create a new bfd section from an ELF section header. */ | |
681 | ||
682 | boolean | |
683 | bfd_section_from_shdr (abfd, shindex) | |
684 | bfd *abfd; | |
685 | unsigned int shindex; | |
686 | { | |
687 | Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex]; | |
688 | Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); | |
689 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
690 | char *name; | |
691 | ||
692 | name = elf_string_from_elf_strtab (abfd, hdr->sh_name); | |
693 | ||
694 | switch (hdr->sh_type) | |
695 | { | |
696 | case SHT_NULL: | |
697 | /* Inactive section. Throw it away. */ | |
698 | return true; | |
699 | ||
700 | case SHT_PROGBITS: /* Normal section with contents. */ | |
701 | case SHT_DYNAMIC: /* Dynamic linking information. */ | |
702 | case SHT_NOBITS: /* .bss section. */ | |
703 | case SHT_HASH: /* .hash section. */ | |
5b3b9ff6 | 704 | case SHT_NOTE: /* .note section. */ |
ede4eed4 KR |
705 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); |
706 | ||
707 | case SHT_SYMTAB: /* A symbol table */ | |
708 | if (elf_onesymtab (abfd) == shindex) | |
709 | return true; | |
710 | ||
711 | BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); | |
712 | BFD_ASSERT (elf_onesymtab (abfd) == 0); | |
713 | elf_onesymtab (abfd) = shindex; | |
714 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
fd0198f0 | 715 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; |
ede4eed4 KR |
716 | abfd->flags |= HAS_SYMS; |
717 | ||
718 | /* Sometimes a shared object will map in the symbol table. If | |
719 | SHF_ALLOC is set, and this is a shared object, then we also | |
720 | treat this section as a BFD section. We can not base the | |
721 | decision purely on SHF_ALLOC, because that flag is sometimes | |
722 | set in a relocateable object file, which would confuse the | |
723 | linker. */ | |
724 | if ((hdr->sh_flags & SHF_ALLOC) != 0 | |
725 | && (abfd->flags & DYNAMIC) != 0 | |
726 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) | |
727 | return false; | |
728 | ||
729 | return true; | |
730 | ||
731 | case SHT_DYNSYM: /* A dynamic symbol table */ | |
732 | if (elf_dynsymtab (abfd) == shindex) | |
733 | return true; | |
734 | ||
735 | BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); | |
736 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); | |
737 | elf_dynsymtab (abfd) = shindex; | |
738 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
fd0198f0 | 739 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; |
ede4eed4 KR |
740 | abfd->flags |= HAS_SYMS; |
741 | ||
742 | /* Besides being a symbol table, we also treat this as a regular | |
743 | section, so that objcopy can handle it. */ | |
744 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
745 | ||
746 | case SHT_STRTAB: /* A string table */ | |
747 | if (hdr->bfd_section != NULL) | |
748 | return true; | |
749 | if (ehdr->e_shstrndx == shindex) | |
750 | { | |
751 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
752 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
753 | return true; | |
754 | } | |
755 | { | |
756 | unsigned int i; | |
757 | ||
758 | for (i = 1; i < ehdr->e_shnum; i++) | |
759 | { | |
760 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
761 | if (hdr2->sh_link == shindex) | |
762 | { | |
763 | if (! bfd_section_from_shdr (abfd, i)) | |
764 | return false; | |
765 | if (elf_onesymtab (abfd) == i) | |
766 | { | |
767 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
768 | elf_elfsections (abfd)[shindex] = | |
769 | &elf_tdata (abfd)->strtab_hdr; | |
770 | return true; | |
771 | } | |
772 | if (elf_dynsymtab (abfd) == i) | |
773 | { | |
774 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
fd0198f0 | 775 | elf_elfsections (abfd)[shindex] = hdr = |
ede4eed4 KR |
776 | &elf_tdata (abfd)->dynstrtab_hdr; |
777 | /* We also treat this as a regular section, so | |
778 | that objcopy can handle it. */ | |
779 | break; | |
780 | } | |
781 | #if 0 /* Not handling other string tables specially right now. */ | |
782 | hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */ | |
783 | /* We have a strtab for some random other section. */ | |
784 | newsect = (asection *) hdr2->bfd_section; | |
785 | if (!newsect) | |
786 | break; | |
787 | hdr->bfd_section = newsect; | |
788 | hdr2 = &elf_section_data (newsect)->str_hdr; | |
789 | *hdr2 = *hdr; | |
790 | elf_elfsections (abfd)[shindex] = hdr2; | |
791 | #endif | |
792 | } | |
793 | } | |
794 | } | |
795 | ||
796 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
797 | ||
798 | case SHT_REL: | |
799 | case SHT_RELA: | |
800 | /* *These* do a lot of work -- but build no sections! */ | |
801 | { | |
802 | asection *target_sect; | |
803 | Elf_Internal_Shdr *hdr2; | |
ede4eed4 | 804 | |
ae115e51 ILT |
805 | /* For some incomprehensible reason Oracle distributes |
806 | libraries for Solaris in which some of the objects have | |
807 | bogus sh_link fields. It would be nice if we could just | |
808 | reject them, but, unfortunately, some people need to use | |
809 | them. We scan through the section headers; if we find only | |
810 | one suitable symbol table, we clobber the sh_link to point | |
811 | to it. I hope this doesn't break anything. */ | |
812 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
813 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) | |
814 | { | |
815 | int scan; | |
816 | int found; | |
817 | ||
818 | found = 0; | |
819 | for (scan = 1; scan < ehdr->e_shnum; scan++) | |
820 | { | |
821 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
822 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
823 | { | |
824 | if (found != 0) | |
825 | { | |
826 | found = 0; | |
827 | break; | |
828 | } | |
829 | found = scan; | |
830 | } | |
831 | } | |
832 | if (found != 0) | |
833 | hdr->sh_link = found; | |
834 | } | |
835 | ||
ede4eed4 | 836 | /* Get the symbol table. */ |
ae115e51 ILT |
837 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
838 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) | |
ede4eed4 KR |
839 | return false; |
840 | ||
841 | /* If this reloc section does not use the main symbol table we | |
842 | don't treat it as a reloc section. BFD can't adequately | |
843 | represent such a section, so at least for now, we don't | |
844 | try. We just present it as a normal section. */ | |
845 | if (hdr->sh_link != elf_onesymtab (abfd)) | |
846 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
847 | ||
ede4eed4 KR |
848 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) |
849 | return false; | |
850 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); | |
851 | if (target_sect == NULL) | |
852 | return false; | |
853 | ||
d1bf45aa ILT |
854 | if ((target_sect->flags & SEC_RELOC) == 0 |
855 | || target_sect->reloc_count == 0) | |
856 | hdr2 = &elf_section_data (target_sect)->rel_hdr; | |
857 | else | |
858 | { | |
859 | BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL); | |
860 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); | |
861 | elf_section_data (target_sect)->rel_hdr2 = hdr2; | |
862 | } | |
ede4eed4 KR |
863 | *hdr2 = *hdr; |
864 | elf_elfsections (abfd)[shindex] = hdr2; | |
d1bf45aa | 865 | target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize; |
ede4eed4 KR |
866 | target_sect->flags |= SEC_RELOC; |
867 | target_sect->relocation = NULL; | |
868 | target_sect->rel_filepos = hdr->sh_offset; | |
869 | abfd->flags |= HAS_RELOC; | |
870 | return true; | |
871 | } | |
872 | break; | |
873 | ||
ede4eed4 | 874 | case SHT_SHLIB: |
ede4eed4 KR |
875 | return true; |
876 | ||
877 | default: | |
878 | /* Check for any processor-specific section types. */ | |
879 | { | |
880 | if (bed->elf_backend_section_from_shdr) | |
881 | (*bed->elf_backend_section_from_shdr) (abfd, hdr, name); | |
882 | } | |
883 | break; | |
884 | } | |
885 | ||
886 | return true; | |
887 | } | |
888 | ||
889 | /* Given an ELF section number, retrieve the corresponding BFD | |
890 | section. */ | |
891 | ||
892 | asection * | |
893 | bfd_section_from_elf_index (abfd, index) | |
894 | bfd *abfd; | |
895 | unsigned int index; | |
896 | { | |
897 | BFD_ASSERT (index > 0 && index < SHN_LORESERVE); | |
898 | if (index >= elf_elfheader (abfd)->e_shnum) | |
899 | return NULL; | |
900 | return elf_elfsections (abfd)[index]->bfd_section; | |
901 | } | |
902 | ||
903 | boolean | |
904 | _bfd_elf_new_section_hook (abfd, sec) | |
905 | bfd *abfd; | |
906 | asection *sec; | |
907 | { | |
908 | struct bfd_elf_section_data *sdata; | |
909 | ||
910 | sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata)); | |
911 | if (!sdata) | |
a9713b91 | 912 | return false; |
ede4eed4 KR |
913 | sec->used_by_bfd = (PTR) sdata; |
914 | memset (sdata, 0, sizeof (*sdata)); | |
915 | return true; | |
916 | } | |
917 | ||
918 | /* Create a new bfd section from an ELF program header. | |
919 | ||
920 | Since program segments have no names, we generate a synthetic name | |
921 | of the form segment<NUM>, where NUM is generally the index in the | |
922 | program header table. For segments that are split (see below) we | |
923 | generate the names segment<NUM>a and segment<NUM>b. | |
924 | ||
925 | Note that some program segments may have a file size that is different than | |
926 | (less than) the memory size. All this means is that at execution the | |
927 | system must allocate the amount of memory specified by the memory size, | |
928 | but only initialize it with the first "file size" bytes read from the | |
929 | file. This would occur for example, with program segments consisting | |
930 | of combined data+bss. | |
931 | ||
932 | To handle the above situation, this routine generates TWO bfd sections | |
933 | for the single program segment. The first has the length specified by | |
934 | the file size of the segment, and the second has the length specified | |
935 | by the difference between the two sizes. In effect, the segment is split | |
936 | into it's initialized and uninitialized parts. | |
937 | ||
938 | */ | |
939 | ||
940 | boolean | |
941 | bfd_section_from_phdr (abfd, hdr, index) | |
942 | bfd *abfd; | |
943 | Elf_Internal_Phdr *hdr; | |
944 | int index; | |
945 | { | |
946 | asection *newsect; | |
947 | char *name; | |
948 | char namebuf[64]; | |
949 | int split; | |
950 | ||
951 | split = ((hdr->p_memsz > 0) && | |
952 | (hdr->p_filesz > 0) && | |
953 | (hdr->p_memsz > hdr->p_filesz)); | |
954 | sprintf (namebuf, split ? "segment%da" : "segment%d", index); | |
955 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
956 | if (!name) | |
a9713b91 | 957 | return false; |
ede4eed4 KR |
958 | strcpy (name, namebuf); |
959 | newsect = bfd_make_section (abfd, name); | |
960 | if (newsect == NULL) | |
961 | return false; | |
962 | newsect->vma = hdr->p_vaddr; | |
ae115e51 | 963 | newsect->lma = hdr->p_paddr; |
ede4eed4 KR |
964 | newsect->_raw_size = hdr->p_filesz; |
965 | newsect->filepos = hdr->p_offset; | |
966 | newsect->flags |= SEC_HAS_CONTENTS; | |
967 | if (hdr->p_type == PT_LOAD) | |
968 | { | |
969 | newsect->flags |= SEC_ALLOC; | |
970 | newsect->flags |= SEC_LOAD; | |
971 | if (hdr->p_flags & PF_X) | |
972 | { | |
973 | /* FIXME: all we known is that it has execute PERMISSION, | |
974 | may be data. */ | |
975 | newsect->flags |= SEC_CODE; | |
976 | } | |
977 | } | |
978 | if (!(hdr->p_flags & PF_W)) | |
979 | { | |
980 | newsect->flags |= SEC_READONLY; | |
981 | } | |
982 | ||
983 | if (split) | |
984 | { | |
985 | sprintf (namebuf, "segment%db", index); | |
986 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
987 | if (!name) | |
a9713b91 | 988 | return false; |
ede4eed4 KR |
989 | strcpy (name, namebuf); |
990 | newsect = bfd_make_section (abfd, name); | |
991 | if (newsect == NULL) | |
992 | return false; | |
993 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; | |
ae115e51 | 994 | newsect->lma = hdr->p_paddr + hdr->p_filesz; |
ede4eed4 KR |
995 | newsect->_raw_size = hdr->p_memsz - hdr->p_filesz; |
996 | if (hdr->p_type == PT_LOAD) | |
997 | { | |
998 | newsect->flags |= SEC_ALLOC; | |
999 | if (hdr->p_flags & PF_X) | |
1000 | newsect->flags |= SEC_CODE; | |
1001 | } | |
1002 | if (!(hdr->p_flags & PF_W)) | |
1003 | newsect->flags |= SEC_READONLY; | |
1004 | } | |
1005 | ||
1006 | return true; | |
1007 | } | |
1008 | ||
1009 | /* Set up an ELF internal section header for a section. */ | |
1010 | ||
1011 | /*ARGSUSED*/ | |
1012 | static void | |
1013 | elf_fake_sections (abfd, asect, failedptrarg) | |
1014 | bfd *abfd; | |
1015 | asection *asect; | |
1016 | PTR failedptrarg; | |
1017 | { | |
1018 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1019 | boolean *failedptr = (boolean *) failedptrarg; | |
1020 | Elf_Internal_Shdr *this_hdr; | |
1021 | ||
1022 | if (*failedptr) | |
1023 | { | |
1024 | /* We already failed; just get out of the bfd_map_over_sections | |
1025 | loop. */ | |
1026 | return; | |
1027 | } | |
1028 | ||
1029 | this_hdr = &elf_section_data (asect)->this_hdr; | |
1030 | ||
1031 | this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd), | |
1032 | asect->name, | |
1033 | true, false); | |
1034 | if (this_hdr->sh_name == (unsigned long) -1) | |
1035 | { | |
1036 | *failedptr = true; | |
1037 | return; | |
1038 | } | |
1039 | ||
1040 | this_hdr->sh_flags = 0; | |
ae115e51 | 1041 | |
ede4eed4 | 1042 | if ((asect->flags & SEC_ALLOC) != 0) |
fd0198f0 | 1043 | this_hdr->sh_addr = asect->vma; |
ede4eed4 KR |
1044 | else |
1045 | this_hdr->sh_addr = 0; | |
ae115e51 | 1046 | |
ede4eed4 KR |
1047 | this_hdr->sh_offset = 0; |
1048 | this_hdr->sh_size = asect->_raw_size; | |
1049 | this_hdr->sh_link = 0; | |
ede4eed4 | 1050 | this_hdr->sh_addralign = 1 << asect->alignment_power; |
fd0198f0 ILT |
1051 | /* The sh_entsize and sh_info fields may have been set already by |
1052 | copy_private_section_data. */ | |
ede4eed4 KR |
1053 | |
1054 | this_hdr->bfd_section = asect; | |
1055 | this_hdr->contents = NULL; | |
1056 | ||
1057 | /* FIXME: This should not be based on section names. */ | |
1058 | if (strcmp (asect->name, ".dynstr") == 0) | |
1059 | this_hdr->sh_type = SHT_STRTAB; | |
1060 | else if (strcmp (asect->name, ".hash") == 0) | |
1061 | { | |
1062 | this_hdr->sh_type = SHT_HASH; | |
1063 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
1064 | } | |
1065 | else if (strcmp (asect->name, ".dynsym") == 0) | |
1066 | { | |
1067 | this_hdr->sh_type = SHT_DYNSYM; | |
1068 | this_hdr->sh_entsize = bed->s->sizeof_sym; | |
1069 | } | |
1070 | else if (strcmp (asect->name, ".dynamic") == 0) | |
1071 | { | |
1072 | this_hdr->sh_type = SHT_DYNAMIC; | |
1073 | this_hdr->sh_entsize = bed->s->sizeof_dyn; | |
1074 | } | |
1075 | else if (strncmp (asect->name, ".rela", 5) == 0 | |
1076 | && get_elf_backend_data (abfd)->use_rela_p) | |
1077 | { | |
1078 | this_hdr->sh_type = SHT_RELA; | |
1079 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
1080 | } | |
1081 | else if (strncmp (asect->name, ".rel", 4) == 0 | |
1082 | && ! get_elf_backend_data (abfd)->use_rela_p) | |
1083 | { | |
1084 | this_hdr->sh_type = SHT_REL; | |
1085 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
1086 | } | |
1087 | else if (strcmp (asect->name, ".note") == 0) | |
1088 | this_hdr->sh_type = SHT_NOTE; | |
1089 | else if (strncmp (asect->name, ".stab", 5) == 0 | |
1090 | && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0) | |
1091 | this_hdr->sh_type = SHT_STRTAB; | |
1092 | else if ((asect->flags & SEC_ALLOC) != 0 | |
1093 | && (asect->flags & SEC_LOAD) != 0) | |
1094 | this_hdr->sh_type = SHT_PROGBITS; | |
1095 | else if ((asect->flags & SEC_ALLOC) != 0 | |
1096 | && ((asect->flags & SEC_LOAD) == 0)) | |
5fe14a9f | 1097 | this_hdr->sh_type = SHT_NOBITS; |
ede4eed4 KR |
1098 | else |
1099 | { | |
1100 | /* Who knows? */ | |
1101 | this_hdr->sh_type = SHT_PROGBITS; | |
1102 | } | |
1103 | ||
1104 | if ((asect->flags & SEC_ALLOC) != 0) | |
1105 | this_hdr->sh_flags |= SHF_ALLOC; | |
1106 | if ((asect->flags & SEC_READONLY) == 0) | |
1107 | this_hdr->sh_flags |= SHF_WRITE; | |
1108 | if ((asect->flags & SEC_CODE) != 0) | |
1109 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
1110 | ||
1111 | /* Check for processor-specific section types. */ | |
1112 | { | |
1113 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1114 | ||
1115 | if (bed->elf_backend_fake_sections) | |
1116 | (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect); | |
1117 | } | |
1118 | ||
1119 | /* If the section has relocs, set up a section header for the | |
1120 | SHT_REL[A] section. */ | |
1121 | if ((asect->flags & SEC_RELOC) != 0) | |
1122 | { | |
1123 | Elf_Internal_Shdr *rela_hdr; | |
1124 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; | |
1125 | char *name; | |
1126 | ||
1127 | rela_hdr = &elf_section_data (asect)->rel_hdr; | |
1128 | name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name)); | |
1129 | if (name == NULL) | |
1130 | { | |
ede4eed4 KR |
1131 | *failedptr = true; |
1132 | return; | |
1133 | } | |
1134 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); | |
1135 | rela_hdr->sh_name = | |
1136 | (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name, | |
1137 | true, false); | |
1138 | if (rela_hdr->sh_name == (unsigned int) -1) | |
1139 | { | |
1140 | *failedptr = true; | |
1141 | return; | |
1142 | } | |
1143 | rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; | |
1144 | rela_hdr->sh_entsize = (use_rela_p | |
1145 | ? bed->s->sizeof_rela | |
1146 | : bed->s->sizeof_rel); | |
1147 | rela_hdr->sh_addralign = bed->s->file_align; | |
1148 | rela_hdr->sh_flags = 0; | |
1149 | rela_hdr->sh_addr = 0; | |
1150 | rela_hdr->sh_size = 0; | |
1151 | rela_hdr->sh_offset = 0; | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | /* Assign all ELF section numbers. The dummy first section is handled here | |
1156 | too. The link/info pointers for the standard section types are filled | |
1157 | in here too, while we're at it. */ | |
1158 | ||
1159 | static boolean | |
1160 | assign_section_numbers (abfd) | |
1161 | bfd *abfd; | |
1162 | { | |
1163 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
1164 | asection *sec; | |
1165 | unsigned int section_number; | |
1166 | Elf_Internal_Shdr **i_shdrp; | |
1167 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1168 | ||
1169 | section_number = 1; | |
1170 | ||
1171 | for (sec = abfd->sections; sec; sec = sec->next) | |
1172 | { | |
1173 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
1174 | ||
1175 | d->this_idx = section_number++; | |
1176 | if ((sec->flags & SEC_RELOC) == 0) | |
1177 | d->rel_idx = 0; | |
1178 | else | |
1179 | d->rel_idx = section_number++; | |
1180 | } | |
1181 | ||
1182 | t->shstrtab_section = section_number++; | |
1183 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; | |
1184 | t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd)); | |
1185 | ||
1186 | if (abfd->symcount > 0) | |
1187 | { | |
1188 | t->symtab_section = section_number++; | |
1189 | t->strtab_section = section_number++; | |
1190 | } | |
1191 | ||
1192 | elf_elfheader (abfd)->e_shnum = section_number; | |
1193 | ||
1194 | /* Set up the list of section header pointers, in agreement with the | |
1195 | indices. */ | |
1196 | i_shdrp = ((Elf_Internal_Shdr **) | |
1197 | bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *))); | |
1198 | if (i_shdrp == NULL) | |
a9713b91 | 1199 | return false; |
ede4eed4 KR |
1200 | |
1201 | i_shdrp[0] = ((Elf_Internal_Shdr *) | |
1202 | bfd_alloc (abfd, sizeof (Elf_Internal_Shdr))); | |
1203 | if (i_shdrp[0] == NULL) | |
1204 | { | |
1205 | bfd_release (abfd, i_shdrp); | |
ede4eed4 KR |
1206 | return false; |
1207 | } | |
1208 | memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr)); | |
1209 | ||
1210 | elf_elfsections (abfd) = i_shdrp; | |
1211 | ||
1212 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; | |
1213 | if (abfd->symcount > 0) | |
1214 | { | |
1215 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
1216 | i_shdrp[t->strtab_section] = &t->strtab_hdr; | |
1217 | t->symtab_hdr.sh_link = t->strtab_section; | |
1218 | } | |
1219 | for (sec = abfd->sections; sec; sec = sec->next) | |
1220 | { | |
1221 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
1222 | asection *s; | |
1223 | const char *name; | |
1224 | ||
1225 | i_shdrp[d->this_idx] = &d->this_hdr; | |
1226 | if (d->rel_idx != 0) | |
1227 | i_shdrp[d->rel_idx] = &d->rel_hdr; | |
1228 | ||
1229 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
1230 | ||
1231 | /* sh_link of a reloc section is the section index of the symbol | |
1232 | table. sh_info is the section index of the section to which | |
1233 | the relocation entries apply. */ | |
1234 | if (d->rel_idx != 0) | |
1235 | { | |
1236 | d->rel_hdr.sh_link = t->symtab_section; | |
1237 | d->rel_hdr.sh_info = d->this_idx; | |
1238 | } | |
1239 | ||
1240 | switch (d->this_hdr.sh_type) | |
1241 | { | |
1242 | case SHT_REL: | |
1243 | case SHT_RELA: | |
1244 | /* A reloc section which we are treating as a normal BFD | |
1245 | section. sh_link is the section index of the symbol | |
1246 | table. sh_info is the section index of the section to | |
1247 | which the relocation entries apply. We assume that an | |
1248 | allocated reloc section uses the dynamic symbol table. | |
1249 | FIXME: How can we be sure? */ | |
1250 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
1251 | if (s != NULL) | |
1252 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1253 | ||
1254 | /* We look up the section the relocs apply to by name. */ | |
1255 | name = sec->name; | |
1256 | if (d->this_hdr.sh_type == SHT_REL) | |
1257 | name += 4; | |
1258 | else | |
1259 | name += 5; | |
1260 | s = bfd_get_section_by_name (abfd, name); | |
1261 | if (s != NULL) | |
1262 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
1263 | break; | |
1264 | ||
1265 | case SHT_STRTAB: | |
1266 | /* We assume that a section named .stab*str is a stabs | |
1267 | string section. We look for a section with the same name | |
1268 | but without the trailing ``str'', and set its sh_link | |
1269 | field to point to this section. */ | |
1270 | if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0 | |
1271 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) | |
1272 | { | |
1273 | size_t len; | |
1274 | char *alc; | |
1275 | ||
1276 | len = strlen (sec->name); | |
58142f10 | 1277 | alc = (char *) bfd_malloc (len - 2); |
ede4eed4 | 1278 | if (alc == NULL) |
58142f10 | 1279 | return false; |
ede4eed4 KR |
1280 | strncpy (alc, sec->name, len - 3); |
1281 | alc[len - 3] = '\0'; | |
1282 | s = bfd_get_section_by_name (abfd, alc); | |
1283 | free (alc); | |
1284 | if (s != NULL) | |
1285 | { | |
1286 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
1287 | ||
1288 | /* This is a .stab section. */ | |
1289 | elf_section_data (s)->this_hdr.sh_entsize = | |
1290 | 4 + 2 * (bed->s->arch_size / 8); | |
1291 | } | |
1292 | } | |
1293 | break; | |
1294 | ||
1295 | case SHT_DYNAMIC: | |
1296 | case SHT_DYNSYM: | |
1297 | /* sh_link is the section header index of the string table | |
1298 | used for the dynamic entries or symbol table. */ | |
1299 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
1300 | if (s != NULL) | |
1301 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1302 | break; | |
1303 | ||
1304 | case SHT_HASH: | |
1305 | /* sh_link is the section header index of the symbol table | |
1306 | this hash table is for. */ | |
1307 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
1308 | if (s != NULL) | |
1309 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1310 | break; | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | return true; | |
1315 | } | |
1316 | ||
1317 | /* Map symbol from it's internal number to the external number, moving | |
1318 | all local symbols to be at the head of the list. */ | |
1319 | ||
1320 | static INLINE int | |
1321 | sym_is_global (abfd, sym) | |
1322 | bfd *abfd; | |
1323 | asymbol *sym; | |
1324 | { | |
1325 | /* If the backend has a special mapping, use it. */ | |
1326 | if (get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1327 | return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1328 | (abfd, sym)); | |
1329 | ||
1330 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0 | |
1331 | || bfd_is_und_section (bfd_get_section (sym)) | |
1332 | || bfd_is_com_section (bfd_get_section (sym))); | |
1333 | } | |
1334 | ||
1335 | static boolean | |
1336 | elf_map_symbols (abfd) | |
1337 | bfd *abfd; | |
1338 | { | |
1339 | int symcount = bfd_get_symcount (abfd); | |
1340 | asymbol **syms = bfd_get_outsymbols (abfd); | |
1341 | asymbol **sect_syms; | |
1342 | int num_locals = 0; | |
1343 | int num_globals = 0; | |
1344 | int num_locals2 = 0; | |
1345 | int num_globals2 = 0; | |
1346 | int max_index = 0; | |
1347 | int num_sections = 0; | |
1348 | int idx; | |
1349 | asection *asect; | |
1350 | asymbol **new_syms; | |
1351 | ||
1352 | #ifdef DEBUG | |
1353 | fprintf (stderr, "elf_map_symbols\n"); | |
1354 | fflush (stderr); | |
1355 | #endif | |
1356 | ||
1357 | /* Add a section symbol for each BFD section. FIXME: Is this really | |
1358 | necessary? */ | |
1359 | for (asect = abfd->sections; asect; asect = asect->next) | |
1360 | { | |
1361 | if (max_index < asect->index) | |
1362 | max_index = asect->index; | |
1363 | } | |
1364 | ||
1365 | max_index++; | |
1366 | sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *)); | |
1367 | if (sect_syms == NULL) | |
a9713b91 | 1368 | return false; |
ede4eed4 KR |
1369 | elf_section_syms (abfd) = sect_syms; |
1370 | ||
1371 | for (idx = 0; idx < symcount; idx++) | |
1372 | { | |
1373 | if ((syms[idx]->flags & BSF_SECTION_SYM) != 0 | |
fd0198f0 | 1374 | && (syms[idx]->value + syms[idx]->section->vma) == 0) |
ede4eed4 KR |
1375 | { |
1376 | asection *sec; | |
1377 | ||
1378 | sec = syms[idx]->section; | |
1379 | if (sec->owner != NULL) | |
1380 | { | |
1381 | if (sec->owner != abfd) | |
1382 | { | |
1383 | if (sec->output_offset != 0) | |
1384 | continue; | |
1385 | sec = sec->output_section; | |
1386 | BFD_ASSERT (sec->owner == abfd); | |
1387 | } | |
1388 | sect_syms[sec->index] = syms[idx]; | |
1389 | } | |
1390 | } | |
1391 | } | |
1392 | ||
1393 | for (asect = abfd->sections; asect; asect = asect->next) | |
1394 | { | |
1395 | asymbol *sym; | |
1396 | ||
1397 | if (sect_syms[asect->index] != NULL) | |
1398 | continue; | |
1399 | ||
1400 | sym = bfd_make_empty_symbol (abfd); | |
1401 | if (sym == NULL) | |
1402 | return false; | |
1403 | sym->the_bfd = abfd; | |
1404 | sym->name = asect->name; | |
1405 | sym->value = 0; | |
1406 | /* Set the flags to 0 to indicate that this one was newly added. */ | |
1407 | sym->flags = 0; | |
1408 | sym->section = asect; | |
1409 | sect_syms[asect->index] = sym; | |
1410 | num_sections++; | |
1411 | #ifdef DEBUG | |
1412 | fprintf (stderr, | |
1413 | "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n", | |
1414 | asect->name, (long) asect->vma, asect->index, (long) asect); | |
1415 | #endif | |
1416 | } | |
1417 | ||
1418 | /* Classify all of the symbols. */ | |
1419 | for (idx = 0; idx < symcount; idx++) | |
1420 | { | |
1421 | if (!sym_is_global (abfd, syms[idx])) | |
1422 | num_locals++; | |
1423 | else | |
1424 | num_globals++; | |
1425 | } | |
1426 | for (asect = abfd->sections; asect; asect = asect->next) | |
1427 | { | |
1428 | if (sect_syms[asect->index] != NULL | |
1429 | && sect_syms[asect->index]->flags == 0) | |
1430 | { | |
1431 | sect_syms[asect->index]->flags = BSF_SECTION_SYM; | |
1432 | if (!sym_is_global (abfd, sect_syms[asect->index])) | |
1433 | num_locals++; | |
1434 | else | |
1435 | num_globals++; | |
1436 | sect_syms[asect->index]->flags = 0; | |
1437 | } | |
1438 | } | |
1439 | ||
1440 | /* Now sort the symbols so the local symbols are first. */ | |
1441 | new_syms = ((asymbol **) | |
1442 | bfd_alloc (abfd, | |
1443 | (num_locals + num_globals) * sizeof (asymbol *))); | |
1444 | if (new_syms == NULL) | |
a9713b91 | 1445 | return false; |
ede4eed4 KR |
1446 | |
1447 | for (idx = 0; idx < symcount; idx++) | |
1448 | { | |
1449 | asymbol *sym = syms[idx]; | |
1450 | int i; | |
1451 | ||
1452 | if (!sym_is_global (abfd, sym)) | |
1453 | i = num_locals2++; | |
1454 | else | |
1455 | i = num_locals + num_globals2++; | |
1456 | new_syms[i] = sym; | |
1457 | sym->udata.i = i + 1; | |
1458 | } | |
1459 | for (asect = abfd->sections; asect; asect = asect->next) | |
1460 | { | |
1461 | if (sect_syms[asect->index] != NULL | |
1462 | && sect_syms[asect->index]->flags == 0) | |
1463 | { | |
1464 | asymbol *sym = sect_syms[asect->index]; | |
1465 | int i; | |
1466 | ||
1467 | sym->flags = BSF_SECTION_SYM; | |
1468 | if (!sym_is_global (abfd, sym)) | |
1469 | i = num_locals2++; | |
1470 | else | |
1471 | i = num_locals + num_globals2++; | |
1472 | new_syms[i] = sym; | |
1473 | sym->udata.i = i + 1; | |
1474 | } | |
1475 | } | |
1476 | ||
1477 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
1478 | ||
1479 | elf_num_locals (abfd) = num_locals; | |
1480 | elf_num_globals (abfd) = num_globals; | |
1481 | return true; | |
1482 | } | |
1483 | ||
fd0198f0 ILT |
1484 | /* Align to the maximum file alignment that could be required for any |
1485 | ELF data structure. */ | |
1486 | ||
1487 | static INLINE file_ptr align_file_position PARAMS ((file_ptr, int)); | |
1488 | static INLINE file_ptr | |
1489 | align_file_position (off, align) | |
1490 | file_ptr off; | |
1491 | int align; | |
1492 | { | |
1493 | return (off + align - 1) & ~(align - 1); | |
1494 | } | |
1495 | ||
1496 | /* Assign a file position to a section, optionally aligning to the | |
1497 | required section alignment. */ | |
1498 | ||
1499 | INLINE file_ptr | |
1500 | _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align) | |
1501 | Elf_Internal_Shdr *i_shdrp; | |
1502 | file_ptr offset; | |
1503 | boolean align; | |
1504 | { | |
1505 | if (align) | |
1506 | { | |
1507 | unsigned int al; | |
1508 | ||
1509 | al = i_shdrp->sh_addralign; | |
1510 | if (al > 1) | |
1511 | offset = BFD_ALIGN (offset, al); | |
1512 | } | |
1513 | i_shdrp->sh_offset = offset; | |
1514 | if (i_shdrp->bfd_section != NULL) | |
1515 | i_shdrp->bfd_section->filepos = offset; | |
1516 | if (i_shdrp->sh_type != SHT_NOBITS) | |
1517 | offset += i_shdrp->sh_size; | |
1518 | return offset; | |
1519 | } | |
1520 | ||
ede4eed4 KR |
1521 | /* Compute the file positions we are going to put the sections at, and |
1522 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
1523 | is not NULL, this is being called by the ELF backend linker. */ | |
1524 | ||
1525 | boolean | |
1526 | _bfd_elf_compute_section_file_positions (abfd, link_info) | |
1527 | bfd *abfd; | |
1528 | struct bfd_link_info *link_info; | |
1529 | { | |
1530 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1531 | boolean failed; | |
1532 | struct bfd_strtab_hash *strtab; | |
1533 | Elf_Internal_Shdr *shstrtab_hdr; | |
1534 | ||
1535 | if (abfd->output_has_begun) | |
1536 | return true; | |
1537 | ||
1538 | /* Do any elf backend specific processing first. */ | |
1539 | if (bed->elf_backend_begin_write_processing) | |
1540 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
1541 | ||
1542 | if (! prep_headers (abfd)) | |
1543 | return false; | |
1544 | ||
1545 | failed = false; | |
1546 | bfd_map_over_sections (abfd, elf_fake_sections, &failed); | |
1547 | if (failed) | |
1548 | return false; | |
1549 | ||
1550 | if (!assign_section_numbers (abfd)) | |
1551 | return false; | |
1552 | ||
1553 | /* The backend linker builds symbol table information itself. */ | |
fd0198f0 | 1554 | if (link_info == NULL && abfd->symcount > 0) |
ede4eed4 KR |
1555 | { |
1556 | if (! swap_out_syms (abfd, &strtab)) | |
1557 | return false; | |
1558 | } | |
1559 | ||
1560 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; | |
1561 | /* sh_name was set in prep_headers. */ | |
1562 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
1563 | shstrtab_hdr->sh_flags = 0; | |
1564 | shstrtab_hdr->sh_addr = 0; | |
1565 | shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd)); | |
1566 | shstrtab_hdr->sh_entsize = 0; | |
1567 | shstrtab_hdr->sh_link = 0; | |
1568 | shstrtab_hdr->sh_info = 0; | |
fd0198f0 | 1569 | /* sh_offset is set in assign_file_positions_except_relocs. */ |
ede4eed4 KR |
1570 | shstrtab_hdr->sh_addralign = 1; |
1571 | ||
fd0198f0 | 1572 | if (!assign_file_positions_except_relocs (abfd)) |
ede4eed4 KR |
1573 | return false; |
1574 | ||
fd0198f0 | 1575 | if (link_info == NULL && abfd->symcount > 0) |
ede4eed4 | 1576 | { |
fd0198f0 ILT |
1577 | file_ptr off; |
1578 | Elf_Internal_Shdr *hdr; | |
1579 | ||
1580 | off = elf_tdata (abfd)->next_file_pos; | |
1581 | ||
1582 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
1583 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
1584 | ||
1585 | hdr = &elf_tdata (abfd)->strtab_hdr; | |
1586 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
1587 | ||
1588 | elf_tdata (abfd)->next_file_pos = off; | |
1589 | ||
ede4eed4 KR |
1590 | /* Now that we know where the .strtab section goes, write it |
1591 | out. */ | |
fd0198f0 | 1592 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ede4eed4 KR |
1593 | || ! _bfd_stringtab_emit (abfd, strtab)) |
1594 | return false; | |
1595 | _bfd_stringtab_free (strtab); | |
1596 | } | |
1597 | ||
1598 | abfd->output_has_begun = true; | |
1599 | ||
1600 | return true; | |
1601 | } | |
1602 | ||
fd0198f0 | 1603 | /* Create a mapping from a set of sections to a program segment. */ |
ede4eed4 | 1604 | |
fd0198f0 | 1605 | static INLINE struct elf_segment_map * |
edf3fe48 | 1606 | make_mapping (abfd, sections, from, to, phdr) |
fd0198f0 ILT |
1607 | bfd *abfd; |
1608 | asection **sections; | |
1609 | unsigned int from; | |
1610 | unsigned int to; | |
edf3fe48 | 1611 | boolean phdr; |
ede4eed4 | 1612 | { |
fd0198f0 ILT |
1613 | struct elf_segment_map *m; |
1614 | unsigned int i; | |
1615 | asection **hdrpp; | |
1616 | ||
1617 | m = ((struct elf_segment_map *) | |
1618 | bfd_zalloc (abfd, | |
1619 | (sizeof (struct elf_segment_map) | |
1620 | + (to - from - 1) * sizeof (asection *)))); | |
1621 | if (m == NULL) | |
a9713b91 | 1622 | return NULL; |
fd0198f0 ILT |
1623 | m->next = NULL; |
1624 | m->p_type = PT_LOAD; | |
1625 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
1626 | m->sections[i - from] = *hdrpp; | |
1627 | m->count = to - from; | |
1628 | ||
edf3fe48 | 1629 | if (from == 0 && phdr) |
6933148a ILT |
1630 | { |
1631 | /* Include the headers in the first PT_LOAD segment. */ | |
1632 | m->includes_filehdr = 1; | |
1633 | m->includes_phdrs = 1; | |
1634 | } | |
1635 | ||
fd0198f0 | 1636 | return m; |
ede4eed4 KR |
1637 | } |
1638 | ||
fd0198f0 | 1639 | /* Set up a mapping from BFD sections to program segments. */ |
ede4eed4 | 1640 | |
fd0198f0 ILT |
1641 | static boolean |
1642 | map_sections_to_segments (abfd) | |
1643 | bfd *abfd; | |
ede4eed4 | 1644 | { |
fd0198f0 ILT |
1645 | asection **sections = NULL; |
1646 | asection *s; | |
1647 | unsigned int i; | |
1648 | unsigned int count; | |
1649 | struct elf_segment_map *mfirst; | |
1650 | struct elf_segment_map **pm; | |
1651 | struct elf_segment_map *m; | |
1652 | asection *last_hdr; | |
1653 | unsigned int phdr_index; | |
1654 | bfd_vma maxpagesize; | |
1655 | asection **hdrpp; | |
edf3fe48 ILT |
1656 | boolean phdr_in_section = true; |
1657 | boolean writable; | |
1658 | asection *dynsec; | |
fd0198f0 ILT |
1659 | |
1660 | if (elf_tdata (abfd)->segment_map != NULL) | |
1661 | return true; | |
1662 | ||
1663 | if (bfd_count_sections (abfd) == 0) | |
1664 | return true; | |
1665 | ||
1666 | /* Select the allocated sections, and sort them. */ | |
1667 | ||
58142f10 ILT |
1668 | sections = (asection **) bfd_malloc (bfd_count_sections (abfd) |
1669 | * sizeof (asection *)); | |
fd0198f0 | 1670 | if (sections == NULL) |
58142f10 | 1671 | goto error_return; |
ede4eed4 | 1672 | |
fd0198f0 ILT |
1673 | i = 0; |
1674 | for (s = abfd->sections; s != NULL; s = s->next) | |
1675 | { | |
1676 | if ((s->flags & SEC_ALLOC) != 0) | |
1677 | { | |
1678 | sections[i] = s; | |
1679 | ++i; | |
1680 | } | |
5fe14a9f | 1681 | } |
fd0198f0 ILT |
1682 | BFD_ASSERT (i <= bfd_count_sections (abfd)); |
1683 | count = i; | |
ede4eed4 | 1684 | |
fd0198f0 | 1685 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
ede4eed4 | 1686 | |
fd0198f0 | 1687 | /* Build the mapping. */ |
ede4eed4 | 1688 | |
fd0198f0 ILT |
1689 | mfirst = NULL; |
1690 | pm = &mfirst; | |
ede4eed4 | 1691 | |
fd0198f0 ILT |
1692 | /* If we have a .interp section, then create a PT_PHDR segment for |
1693 | the program headers and a PT_INTERP segment for the .interp | |
1694 | section. */ | |
1695 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1696 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
1697 | { | |
1698 | m = ((struct elf_segment_map *) | |
1699 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
1700 | if (m == NULL) | |
a9713b91 | 1701 | goto error_return; |
fd0198f0 ILT |
1702 | m->next = NULL; |
1703 | m->p_type = PT_PHDR; | |
1704 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
1705 | m->p_flags = PF_R | PF_X; | |
1706 | m->p_flags_valid = 1; | |
6933148a | 1707 | m->includes_phdrs = 1; |
ede4eed4 | 1708 | |
fd0198f0 ILT |
1709 | *pm = m; |
1710 | pm = &m->next; | |
ede4eed4 | 1711 | |
fd0198f0 ILT |
1712 | m = ((struct elf_segment_map *) |
1713 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
1714 | if (m == NULL) | |
a9713b91 | 1715 | goto error_return; |
fd0198f0 ILT |
1716 | m->next = NULL; |
1717 | m->p_type = PT_INTERP; | |
1718 | m->count = 1; | |
1719 | m->sections[0] = s; | |
ede4eed4 | 1720 | |
fd0198f0 ILT |
1721 | *pm = m; |
1722 | pm = &m->next; | |
1723 | } | |
ede4eed4 | 1724 | |
fd0198f0 ILT |
1725 | /* Look through the sections. We put sections in the same program |
1726 | segment when the start of the second section can be placed within | |
1727 | a few bytes of the end of the first section. */ | |
1728 | last_hdr = NULL; | |
1729 | phdr_index = 0; | |
1730 | maxpagesize = get_elf_backend_data (abfd)->maxpagesize; | |
edf3fe48 ILT |
1731 | writable = false; |
1732 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
1733 | if (dynsec != NULL | |
1734 | && (dynsec->flags & SEC_LOAD) == 0) | |
1735 | dynsec = NULL; | |
1736 | ||
7fc6a16a ILT |
1737 | /* Deal with -Ttext or something similar such that the first section |
1738 | is not adjacent to the program headers. This is an | |
1739 | approximation, since at this point we don't know exactly how many | |
1740 | program headers we will need. */ | |
1741 | if (count > 0) | |
1742 | { | |
1743 | bfd_size_type phdr_size; | |
1744 | ||
1745 | phdr_size = elf_tdata (abfd)->program_header_size; | |
1746 | if (phdr_size == 0) | |
1747 | phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr; | |
1748 | if (sections[0]->lma % maxpagesize < phdr_size % maxpagesize) | |
1749 | phdr_in_section = false; | |
1750 | } | |
edf3fe48 | 1751 | |
fd0198f0 | 1752 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
ede4eed4 | 1753 | { |
fd0198f0 | 1754 | asection *hdr; |
ede4eed4 | 1755 | |
fd0198f0 | 1756 | hdr = *hdrpp; |
ede4eed4 | 1757 | |
fd0198f0 | 1758 | /* See if this section and the last one will fit in the same |
edf3fe48 ILT |
1759 | segment. Don't put a loadable section after a non-loadable |
1760 | section. If we are building a dynamic executable, don't put | |
1761 | a writable section in a read only segment (we don't do this | |
1762 | for a non-dynamic executable because some people prefer to | |
1763 | have only one program segment; anybody can use PHDRS in their | |
1764 | linker script to control what happens anyhow). */ | |
fd0198f0 ILT |
1765 | if (last_hdr == NULL |
1766 | || ((BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize) | |
1767 | >= hdr->lma) | |
1768 | && ((last_hdr->flags & SEC_LOAD) != 0 | |
edf3fe48 ILT |
1769 | || (hdr->flags & SEC_LOAD) == 0) |
1770 | && (dynsec == NULL | |
1771 | || writable | |
1772 | || (hdr->flags & SEC_READONLY) != 0))) | |
fd0198f0 ILT |
1773 | { |
1774 | last_hdr = hdr; | |
1775 | continue; | |
1776 | } | |
ede4eed4 | 1777 | |
fd0198f0 ILT |
1778 | /* This section won't fit in the program segment. We must |
1779 | create a new program header holding all the sections from | |
1780 | phdr_index until hdr. */ | |
ede4eed4 | 1781 | |
edf3fe48 | 1782 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section); |
fd0198f0 ILT |
1783 | if (m == NULL) |
1784 | goto error_return; | |
ede4eed4 | 1785 | |
fd0198f0 ILT |
1786 | *pm = m; |
1787 | pm = &m->next; | |
ede4eed4 | 1788 | |
edf3fe48 ILT |
1789 | if ((hdr->flags & SEC_READONLY) == 0) |
1790 | writable = true; | |
1791 | ||
fd0198f0 ILT |
1792 | last_hdr = hdr; |
1793 | phdr_index = i; | |
edf3fe48 | 1794 | phdr_in_section = false; |
ede4eed4 | 1795 | } |
fd0198f0 ILT |
1796 | |
1797 | /* Create a final PT_LOAD program segment. */ | |
1798 | if (last_hdr != NULL) | |
ede4eed4 | 1799 | { |
edf3fe48 | 1800 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section); |
fd0198f0 ILT |
1801 | if (m == NULL) |
1802 | goto error_return; | |
1803 | ||
1804 | *pm = m; | |
1805 | pm = &m->next; | |
ede4eed4 KR |
1806 | } |
1807 | ||
fd0198f0 | 1808 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
edf3fe48 | 1809 | if (dynsec != NULL) |
ede4eed4 | 1810 | { |
fd0198f0 ILT |
1811 | m = ((struct elf_segment_map *) |
1812 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
1813 | if (m == NULL) | |
a9713b91 | 1814 | goto error_return; |
fd0198f0 ILT |
1815 | m->next = NULL; |
1816 | m->p_type = PT_DYNAMIC; | |
1817 | m->count = 1; | |
edf3fe48 | 1818 | m->sections[0] = dynsec; |
ede4eed4 | 1819 | |
fd0198f0 ILT |
1820 | *pm = m; |
1821 | pm = &m->next; | |
ede4eed4 KR |
1822 | } |
1823 | ||
fd0198f0 ILT |
1824 | free (sections); |
1825 | sections = NULL; | |
ae115e51 | 1826 | |
fd0198f0 ILT |
1827 | elf_tdata (abfd)->segment_map = mfirst; |
1828 | return true; | |
1829 | ||
1830 | error_return: | |
1831 | if (sections != NULL) | |
1832 | free (sections); | |
1833 | return false; | |
ede4eed4 KR |
1834 | } |
1835 | ||
fd0198f0 | 1836 | /* Sort sections by VMA. */ |
ede4eed4 | 1837 | |
fd0198f0 ILT |
1838 | static int |
1839 | elf_sort_sections (arg1, arg2) | |
1840 | const PTR arg1; | |
1841 | const PTR arg2; | |
ede4eed4 | 1842 | { |
fd0198f0 ILT |
1843 | const asection *sec1 = *(const asection **) arg1; |
1844 | const asection *sec2 = *(const asection **) arg2; | |
ede4eed4 | 1845 | |
fd0198f0 ILT |
1846 | if (sec1->vma < sec2->vma) |
1847 | return -1; | |
1848 | else if (sec1->vma > sec2->vma) | |
1849 | return 1; | |
ede4eed4 | 1850 | |
fd0198f0 | 1851 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ |
ede4eed4 | 1852 | |
fd0198f0 | 1853 | #define TOEND(x) (((x)->flags & SEC_LOAD) == 0) |
ede4eed4 | 1854 | |
fd0198f0 ILT |
1855 | if (TOEND (sec1)) |
1856 | if (TOEND (sec2)) | |
1857 | return sec1->target_index - sec2->target_index; | |
1858 | else | |
1859 | return 1; | |
ede4eed4 | 1860 | |
fd0198f0 ILT |
1861 | if (TOEND (sec2)) |
1862 | return -1; | |
ede4eed4 | 1863 | |
fd0198f0 | 1864 | #undef TOEND |
ede4eed4 | 1865 | |
fd0198f0 ILT |
1866 | /* Sort by size, to put zero sized sections before others at the |
1867 | same address. */ | |
ede4eed4 | 1868 | |
fd0198f0 ILT |
1869 | if (sec1->_raw_size < sec2->_raw_size) |
1870 | return -1; | |
1871 | if (sec1->_raw_size > sec2->_raw_size) | |
1872 | return 1; | |
ede4eed4 | 1873 | |
fd0198f0 ILT |
1874 | return sec1->target_index - sec2->target_index; |
1875 | } | |
ede4eed4 | 1876 | |
fd0198f0 ILT |
1877 | /* Assign file positions to the sections based on the mapping from |
1878 | sections to segments. This function also sets up some fields in | |
1879 | the file header, and writes out the program headers. */ | |
ede4eed4 | 1880 | |
fd0198f0 ILT |
1881 | static boolean |
1882 | assign_file_positions_for_segments (abfd) | |
1883 | bfd *abfd; | |
1884 | { | |
1885 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1886 | unsigned int count; | |
1887 | struct elf_segment_map *m; | |
1888 | unsigned int alloc; | |
1889 | Elf_Internal_Phdr *phdrs; | |
1890 | file_ptr off; | |
6933148a ILT |
1891 | bfd_vma filehdr_vaddr, filehdr_paddr; |
1892 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
fd0198f0 ILT |
1893 | Elf_Internal_Phdr *p; |
1894 | ||
1895 | if (elf_tdata (abfd)->segment_map == NULL) | |
1896 | { | |
1897 | if (! map_sections_to_segments (abfd)) | |
1898 | return false; | |
1899 | } | |
ede4eed4 | 1900 | |
5b3b9ff6 ILT |
1901 | if (bed->elf_backend_modify_segment_map) |
1902 | { | |
1903 | if (! (*bed->elf_backend_modify_segment_map) (abfd)) | |
1904 | return false; | |
1905 | } | |
1906 | ||
fd0198f0 ILT |
1907 | count = 0; |
1908 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
1909 | ++count; | |
ede4eed4 | 1910 | |
fd0198f0 ILT |
1911 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; |
1912 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
1913 | elf_elfheader (abfd)->e_phnum = count; | |
ede4eed4 | 1914 | |
fd0198f0 ILT |
1915 | if (count == 0) |
1916 | return true; | |
ede4eed4 | 1917 | |
fd0198f0 ILT |
1918 | /* If we already counted the number of program segments, make sure |
1919 | that we allocated enough space. This happens when SIZEOF_HEADERS | |
1920 | is used in a linker script. */ | |
1921 | alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr; | |
1922 | if (alloc != 0 && count > alloc) | |
1923 | { | |
1924 | ((*_bfd_error_handler) | |
1925 | ("%s: Not enough room for program headers (allocated %u, need %u)", | |
1926 | bfd_get_filename (abfd), alloc, count)); | |
1927 | bfd_set_error (bfd_error_bad_value); | |
1928 | return false; | |
ede4eed4 KR |
1929 | } |
1930 | ||
fd0198f0 ILT |
1931 | if (alloc == 0) |
1932 | alloc = count; | |
1933 | ||
1934 | phdrs = ((Elf_Internal_Phdr *) | |
1935 | bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr))); | |
1936 | if (phdrs == NULL) | |
a9713b91 | 1937 | return false; |
ede4eed4 | 1938 | |
fd0198f0 ILT |
1939 | off = bed->s->sizeof_ehdr; |
1940 | off += alloc * bed->s->sizeof_phdr; | |
ede4eed4 | 1941 | |
6933148a ILT |
1942 | filehdr_vaddr = 0; |
1943 | filehdr_paddr = 0; | |
1944 | phdrs_vaddr = 0; | |
1945 | phdrs_paddr = 0; | |
fd0198f0 ILT |
1946 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; |
1947 | m != NULL; | |
1948 | m = m->next, p++) | |
1949 | { | |
1950 | unsigned int i; | |
1951 | asection **secpp; | |
fd0198f0 | 1952 | |
3b950780 ILT |
1953 | /* If elf_segment_map is not from map_sections_to_segments, the |
1954 | sections may not be correctly ordered. */ | |
1955 | if (m->count > 0) | |
1956 | qsort (m->sections, (size_t) m->count, sizeof (asection *), | |
1957 | elf_sort_sections); | |
1958 | ||
fd0198f0 ILT |
1959 | p->p_type = m->p_type; |
1960 | ||
1961 | if (m->p_flags_valid) | |
1962 | p->p_flags = m->p_flags; | |
14899eb7 ILT |
1963 | else |
1964 | p->p_flags = 0; | |
fd0198f0 | 1965 | |
d49ddb85 ILT |
1966 | if (p->p_type == PT_LOAD |
1967 | && m->count > 0 | |
1968 | && (m->sections[0]->flags & SEC_LOAD) != 0) | |
44ef8897 ILT |
1969 | off += (m->sections[0]->vma - off) % bed->maxpagesize; |
1970 | ||
fd0198f0 ILT |
1971 | if (m->count == 0) |
1972 | p->p_vaddr = 0; | |
1973 | else | |
1974 | p->p_vaddr = m->sections[0]->vma; | |
ede4eed4 | 1975 | |
fd0198f0 ILT |
1976 | if (m->p_paddr_valid) |
1977 | p->p_paddr = m->p_paddr; | |
1978 | else if (m->count == 0) | |
1979 | p->p_paddr = 0; | |
1980 | else | |
1981 | p->p_paddr = m->sections[0]->lma; | |
1982 | ||
1983 | if (p->p_type == PT_LOAD) | |
1984 | p->p_align = bed->maxpagesize; | |
1985 | else if (m->count == 0) | |
1986 | p->p_align = bed->s->file_align; | |
1987 | else | |
1988 | p->p_align = 0; | |
1989 | ||
6933148a | 1990 | p->p_offset = 0; |
fd0198f0 ILT |
1991 | p->p_filesz = 0; |
1992 | p->p_memsz = 0; | |
1993 | ||
6933148a | 1994 | if (m->includes_filehdr) |
ede4eed4 | 1995 | { |
14899eb7 ILT |
1996 | if (! m->p_flags_valid) |
1997 | p->p_flags |= PF_R; | |
6933148a ILT |
1998 | p->p_offset = 0; |
1999 | p->p_filesz = bed->s->sizeof_ehdr; | |
2000 | p->p_memsz = bed->s->sizeof_ehdr; | |
2001 | if (m->count > 0) | |
2002 | { | |
2003 | BFD_ASSERT (p->p_type == PT_LOAD); | |
2004 | p->p_vaddr -= off; | |
2005 | if (! m->p_paddr_valid) | |
2006 | p->p_paddr -= off; | |
2007 | } | |
2008 | if (p->p_type == PT_LOAD) | |
2009 | { | |
2010 | filehdr_vaddr = p->p_vaddr; | |
2011 | filehdr_paddr = p->p_paddr; | |
2012 | } | |
2013 | } | |
fd0198f0 | 2014 | |
6933148a ILT |
2015 | if (m->includes_phdrs) |
2016 | { | |
14899eb7 ILT |
2017 | if (! m->p_flags_valid) |
2018 | p->p_flags |= PF_R; | |
6933148a | 2019 | if (m->includes_filehdr) |
fd0198f0 | 2020 | { |
6933148a | 2021 | if (p->p_type == PT_LOAD) |
fd0198f0 | 2022 | { |
6933148a ILT |
2023 | phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr; |
2024 | phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr; | |
fd0198f0 | 2025 | } |
6933148a ILT |
2026 | } |
2027 | else | |
2028 | { | |
2029 | p->p_offset = bed->s->sizeof_ehdr; | |
2030 | if (m->count > 0) | |
2031 | { | |
2032 | BFD_ASSERT (p->p_type == PT_LOAD); | |
2033 | p->p_vaddr -= off - p->p_offset; | |
2034 | if (! m->p_paddr_valid) | |
2035 | p->p_paddr -= off - p->p_offset; | |
2036 | } | |
2037 | if (p->p_type == PT_LOAD) | |
fd0198f0 | 2038 | { |
6933148a ILT |
2039 | phdrs_vaddr = p->p_vaddr; |
2040 | phdrs_paddr = p->p_paddr; | |
fd0198f0 | 2041 | } |
6933148a ILT |
2042 | } |
2043 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
2044 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2045 | } | |
2046 | ||
2047 | if (p->p_type == PT_LOAD) | |
2048 | { | |
2049 | if (! m->includes_filehdr && ! m->includes_phdrs) | |
2050 | p->p_offset = off; | |
2051 | else | |
2052 | { | |
2053 | file_ptr adjust; | |
fd0198f0 | 2054 | |
6933148a ILT |
2055 | adjust = off - (p->p_offset + p->p_filesz); |
2056 | p->p_filesz += adjust; | |
2057 | p->p_memsz += adjust; | |
fd0198f0 | 2058 | } |
ede4eed4 KR |
2059 | } |
2060 | ||
fd0198f0 | 2061 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
ede4eed4 | 2062 | { |
fd0198f0 ILT |
2063 | asection *sec; |
2064 | flagword flags; | |
2065 | bfd_size_type align; | |
2066 | ||
2067 | sec = *secpp; | |
2068 | flags = sec->flags; | |
2069 | ||
2070 | if (p->p_type == PT_LOAD) | |
2071 | { | |
2072 | bfd_vma adjust; | |
2073 | ||
2074 | /* The section VMA must equal the file position modulo | |
2075 | the page size. */ | |
09609415 | 2076 | if ((flags & SEC_ALLOC) != 0) |
fd0198f0 | 2077 | { |
d49ddb85 ILT |
2078 | adjust = (sec->vma - off) % bed->maxpagesize; |
2079 | if (adjust != 0) | |
2080 | { | |
2081 | if (i == 0) | |
2082 | abort (); | |
2083 | p->p_memsz += adjust; | |
19bfbcbe | 2084 | off += adjust; |
d49ddb85 | 2085 | if ((flags & SEC_LOAD) != 0) |
19bfbcbe | 2086 | p->p_filesz += adjust; |
d49ddb85 | 2087 | } |
fd0198f0 ILT |
2088 | } |
2089 | ||
2090 | sec->filepos = off; | |
2091 | ||
2092 | if ((flags & SEC_LOAD) != 0) | |
2093 | off += sec->_raw_size; | |
2094 | } | |
2095 | ||
2096 | p->p_memsz += sec->_raw_size; | |
2097 | ||
2098 | if ((flags & SEC_LOAD) != 0) | |
2099 | p->p_filesz += sec->_raw_size; | |
2100 | ||
2101 | align = 1 << bfd_get_section_alignment (abfd, sec); | |
2102 | if (align > p->p_align) | |
2103 | p->p_align = align; | |
2104 | ||
2105 | if (! m->p_flags_valid) | |
2106 | { | |
14899eb7 | 2107 | p->p_flags |= PF_R; |
fd0198f0 ILT |
2108 | if ((flags & SEC_CODE) != 0) |
2109 | p->p_flags |= PF_X; | |
2110 | if ((flags & SEC_READONLY) == 0) | |
2111 | p->p_flags |= PF_W; | |
2112 | } | |
ede4eed4 | 2113 | } |
fd0198f0 | 2114 | } |
ede4eed4 | 2115 | |
fd0198f0 ILT |
2116 | /* Now that we have set the section file positions, we can set up |
2117 | the file positions for the non PT_LOAD segments. */ | |
2118 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
2119 | m != NULL; | |
2120 | m = m->next, p++) | |
2121 | { | |
2122 | if (p->p_type != PT_LOAD && m->count > 0) | |
ede4eed4 | 2123 | { |
6933148a ILT |
2124 | BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs); |
2125 | p->p_offset = m->sections[0]->filepos; | |
2126 | } | |
2127 | if (m->count == 0) | |
2128 | { | |
2129 | if (m->includes_filehdr) | |
2130 | { | |
2131 | p->p_vaddr = filehdr_vaddr; | |
2132 | if (! m->p_paddr_valid) | |
2133 | p->p_paddr = filehdr_paddr; | |
2134 | } | |
2135 | else if (m->includes_phdrs) | |
2136 | { | |
2137 | p->p_vaddr = phdrs_vaddr; | |
2138 | if (! m->p_paddr_valid) | |
2139 | p->p_paddr = phdrs_paddr; | |
2140 | } | |
ede4eed4 | 2141 | } |
ede4eed4 KR |
2142 | } |
2143 | ||
fd0198f0 ILT |
2144 | /* Clear out any program headers we allocated but did not use. */ |
2145 | for (; count < alloc; count++, p++) | |
ede4eed4 | 2146 | { |
fd0198f0 ILT |
2147 | memset (p, 0, sizeof *p); |
2148 | p->p_type = PT_NULL; | |
ede4eed4 KR |
2149 | } |
2150 | ||
fd0198f0 | 2151 | elf_tdata (abfd)->phdr = phdrs; |
ede4eed4 | 2152 | |
fd0198f0 | 2153 | elf_tdata (abfd)->next_file_pos = off; |
ede4eed4 | 2154 | |
fd0198f0 ILT |
2155 | /* Write out the program headers. */ |
2156 | if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0 | |
2157 | || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0) | |
2158 | return false; | |
2159 | ||
2160 | return true; | |
2161 | } | |
2162 | ||
2163 | /* Get the size of the program header. | |
2164 | ||
2165 | If this is called by the linker before any of the section VMA's are set, it | |
2166 | can't calculate the correct value for a strange memory layout. This only | |
2167 | happens when SIZEOF_HEADERS is used in a linker script. In this case, | |
2168 | SORTED_HDRS is NULL and we assume the normal scenario of one text and one | |
2169 | data segment (exclusive of .interp and .dynamic). | |
2170 | ||
2171 | ??? User written scripts must either not use SIZEOF_HEADERS, or assume there | |
2172 | will be two segments. */ | |
2173 | ||
2174 | static bfd_size_type | |
2175 | get_program_header_size (abfd) | |
2176 | bfd *abfd; | |
2177 | { | |
2178 | size_t segs; | |
2179 | asection *s; | |
2180 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2181 | ||
2182 | /* We can't return a different result each time we're called. */ | |
2183 | if (elf_tdata (abfd)->program_header_size != 0) | |
2184 | return elf_tdata (abfd)->program_header_size; | |
ae115e51 | 2185 | |
3b950780 ILT |
2186 | if (elf_tdata (abfd)->segment_map != NULL) |
2187 | { | |
2188 | struct elf_segment_map *m; | |
2189 | ||
2190 | segs = 0; | |
2191 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
2192 | ++segs; | |
2193 | elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; | |
2194 | return elf_tdata (abfd)->program_header_size; | |
2195 | } | |
2196 | ||
fd0198f0 ILT |
2197 | /* Assume we will need exactly two PT_LOAD segments: one for text |
2198 | and one for data. */ | |
2199 | segs = 2; | |
2200 | ||
2201 | s = bfd_get_section_by_name (abfd, ".interp"); | |
2202 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
ede4eed4 | 2203 | { |
fd0198f0 ILT |
2204 | /* If we have a loadable interpreter section, we need a |
2205 | PT_INTERP segment. In this case, assume we also need a | |
2206 | PT_PHDR segment, although that may not be true for all | |
2207 | targets. */ | |
2208 | segs += 2; | |
ede4eed4 KR |
2209 | } |
2210 | ||
fd0198f0 | 2211 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) |
ede4eed4 | 2212 | { |
fd0198f0 ILT |
2213 | /* We need a PT_DYNAMIC segment. */ |
2214 | ++segs; | |
ede4eed4 | 2215 | } |
ede4eed4 | 2216 | |
fd0198f0 | 2217 | /* Let the backend count up any program headers it might need. */ |
5b3b9ff6 ILT |
2218 | if (bed->elf_backend_additional_program_headers) |
2219 | { | |
2220 | int a; | |
2221 | ||
2222 | a = (*bed->elf_backend_additional_program_headers) (abfd); | |
2223 | if (a == -1) | |
2224 | abort (); | |
2225 | segs += a; | |
2226 | } | |
ede4eed4 | 2227 | |
fd0198f0 ILT |
2228 | elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; |
2229 | return elf_tdata (abfd)->program_header_size; | |
ede4eed4 KR |
2230 | } |
2231 | ||
2232 | /* Work out the file positions of all the sections. This is called by | |
2233 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
2234 | VMAs must be known before this is called. | |
2235 | ||
2236 | We do not consider reloc sections at this point, unless they form | |
2237 | part of the loadable image. Reloc sections are assigned file | |
2238 | positions in assign_file_positions_for_relocs, which is called by | |
2239 | write_object_contents and final_link. | |
2240 | ||
fd0198f0 | 2241 | We also don't set the positions of the .symtab and .strtab here. */ |
ede4eed4 KR |
2242 | |
2243 | static boolean | |
fd0198f0 | 2244 | assign_file_positions_except_relocs (abfd) |
ede4eed4 | 2245 | bfd *abfd; |
ede4eed4 KR |
2246 | { |
2247 | struct elf_obj_tdata * const tdata = elf_tdata (abfd); | |
2248 | Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd); | |
2249 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); | |
2250 | file_ptr off; | |
2251 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2252 | ||
ede4eed4 KR |
2253 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) |
2254 | { | |
2255 | Elf_Internal_Shdr **hdrpp; | |
2256 | unsigned int i; | |
2257 | ||
fd0198f0 ILT |
2258 | /* Start after the ELF header. */ |
2259 | off = i_ehdrp->e_ehsize; | |
2260 | ||
ede4eed4 KR |
2261 | /* We are not creating an executable, which means that we are |
2262 | not creating a program header, and that the actual order of | |
2263 | the sections in the file is unimportant. */ | |
2264 | for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++) | |
2265 | { | |
2266 | Elf_Internal_Shdr *hdr; | |
2267 | ||
2268 | hdr = *hdrpp; | |
2269 | if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
2270 | { | |
2271 | hdr->sh_offset = -1; | |
2272 | continue; | |
2273 | } | |
fd0198f0 ILT |
2274 | if (i == tdata->symtab_section |
2275 | || i == tdata->strtab_section) | |
ede4eed4 KR |
2276 | { |
2277 | hdr->sh_offset = -1; | |
2278 | continue; | |
2279 | } | |
2280 | ||
5fe14a9f | 2281 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); |
ede4eed4 KR |
2282 | } |
2283 | } | |
2284 | else | |
2285 | { | |
ede4eed4 | 2286 | unsigned int i; |
fd0198f0 | 2287 | Elf_Internal_Shdr **hdrpp; |
ede4eed4 | 2288 | |
fd0198f0 ILT |
2289 | /* Assign file positions for the loaded sections based on the |
2290 | assignment of sections to segments. */ | |
2291 | if (! assign_file_positions_for_segments (abfd)) | |
ede4eed4 KR |
2292 | return false; |
2293 | ||
fd0198f0 ILT |
2294 | /* Assign file positions for the other sections. */ |
2295 | ||
2296 | off = elf_tdata (abfd)->next_file_pos; | |
2297 | for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++) | |
ede4eed4 KR |
2298 | { |
2299 | Elf_Internal_Shdr *hdr; | |
2300 | ||
2301 | hdr = *hdrpp; | |
fd0198f0 ILT |
2302 | if (hdr->bfd_section != NULL |
2303 | && hdr->bfd_section->filepos != 0) | |
2304 | hdr->sh_offset = hdr->bfd_section->filepos; | |
2305 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
ede4eed4 | 2306 | { |
fd0198f0 ILT |
2307 | ((*_bfd_error_handler) |
2308 | ("%s: warning: allocated section `%s' not in segment", | |
2309 | bfd_get_filename (abfd), | |
2310 | (hdr->bfd_section == NULL | |
2311 | ? "*unknown*" | |
2312 | : hdr->bfd_section->name))); | |
2313 | off += (hdr->sh_addr - off) % bed->maxpagesize; | |
5fe14a9f ILT |
2314 | off = _bfd_elf_assign_file_position_for_section (hdr, off, |
2315 | false); | |
ede4eed4 | 2316 | } |
fd0198f0 ILT |
2317 | else if (hdr->sh_type == SHT_REL |
2318 | || hdr->sh_type == SHT_RELA | |
2319 | || hdr == i_shdrpp[tdata->symtab_section] | |
2320 | || hdr == i_shdrpp[tdata->strtab_section]) | |
2321 | hdr->sh_offset = -1; | |
2322 | else | |
2323 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
2324 | } | |
ede4eed4 KR |
2325 | } |
2326 | ||
2327 | /* Place the section headers. */ | |
2328 | off = align_file_position (off, bed->s->file_align); | |
2329 | i_ehdrp->e_shoff = off; | |
2330 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
2331 | ||
2332 | elf_tdata (abfd)->next_file_pos = off; | |
2333 | ||
2334 | return true; | |
2335 | } | |
2336 | ||
ede4eed4 KR |
2337 | static boolean |
2338 | prep_headers (abfd) | |
2339 | bfd *abfd; | |
2340 | { | |
2341 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
2342 | Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ | |
2343 | Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ | |
2344 | int count; | |
2345 | struct bfd_strtab_hash *shstrtab; | |
2346 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2347 | ||
2348 | i_ehdrp = elf_elfheader (abfd); | |
2349 | i_shdrp = elf_elfsections (abfd); | |
2350 | ||
2351 | shstrtab = _bfd_elf_stringtab_init (); | |
2352 | if (shstrtab == NULL) | |
2353 | return false; | |
2354 | ||
2355 | elf_shstrtab (abfd) = shstrtab; | |
2356 | ||
2357 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
2358 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
2359 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
2360 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
2361 | ||
2362 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
2363 | i_ehdrp->e_ident[EI_DATA] = | |
86587dd4 | 2364 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; |
ede4eed4 KR |
2365 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; |
2366 | ||
2367 | for (count = EI_PAD; count < EI_NIDENT; count++) | |
2368 | i_ehdrp->e_ident[count] = 0; | |
2369 | ||
2370 | if ((abfd->flags & DYNAMIC) != 0) | |
2371 | i_ehdrp->e_type = ET_DYN; | |
2372 | else if ((abfd->flags & EXEC_P) != 0) | |
2373 | i_ehdrp->e_type = ET_EXEC; | |
2374 | else | |
2375 | i_ehdrp->e_type = ET_REL; | |
2376 | ||
2377 | switch (bfd_get_arch (abfd)) | |
2378 | { | |
2379 | case bfd_arch_unknown: | |
2380 | i_ehdrp->e_machine = EM_NONE; | |
2381 | break; | |
2382 | case bfd_arch_sparc: | |
2383 | if (bed->s->arch_size == 64) | |
2384 | i_ehdrp->e_machine = EM_SPARC64; | |
2385 | else | |
2386 | i_ehdrp->e_machine = EM_SPARC; | |
2387 | break; | |
2388 | case bfd_arch_i386: | |
2389 | i_ehdrp->e_machine = EM_386; | |
2390 | break; | |
2391 | case bfd_arch_m68k: | |
2392 | i_ehdrp->e_machine = EM_68K; | |
2393 | break; | |
2394 | case bfd_arch_m88k: | |
2395 | i_ehdrp->e_machine = EM_88K; | |
2396 | break; | |
2397 | case bfd_arch_i860: | |
2398 | i_ehdrp->e_machine = EM_860; | |
2399 | break; | |
2400 | case bfd_arch_mips: /* MIPS Rxxxx */ | |
2401 | i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */ | |
2402 | break; | |
2403 | case bfd_arch_hppa: | |
2404 | i_ehdrp->e_machine = EM_PARISC; | |
2405 | break; | |
2406 | case bfd_arch_powerpc: | |
2407 | i_ehdrp->e_machine = EM_PPC; | |
2408 | break; | |
2409 | /* start-sanitize-arc */ | |
2410 | case bfd_arch_arc: | |
2411 | i_ehdrp->e_machine = EM_CYGNUS_ARC; | |
2412 | break; | |
2413 | /* end-sanitize-arc */ | |
2414 | /* also note that EM_M32, AT&T WE32100 is unknown to bfd */ | |
2415 | default: | |
2416 | i_ehdrp->e_machine = EM_NONE; | |
2417 | } | |
2418 | i_ehdrp->e_version = bed->s->ev_current; | |
2419 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
2420 | ||
2421 | /* no program header, for now. */ | |
2422 | i_ehdrp->e_phoff = 0; | |
2423 | i_ehdrp->e_phentsize = 0; | |
2424 | i_ehdrp->e_phnum = 0; | |
2425 | ||
2426 | /* each bfd section is section header entry */ | |
2427 | i_ehdrp->e_entry = bfd_get_start_address (abfd); | |
2428 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
2429 | ||
2430 | /* if we're building an executable, we'll need a program header table */ | |
2431 | if (abfd->flags & EXEC_P) | |
2432 | { | |
2433 | /* it all happens later */ | |
2434 | #if 0 | |
2435 | i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); | |
2436 | ||
2437 | /* elf_build_phdrs() returns a (NULL-terminated) array of | |
2438 | Elf_Internal_Phdrs */ | |
2439 | i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum); | |
2440 | i_ehdrp->e_phoff = outbase; | |
2441 | outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum; | |
2442 | #endif | |
2443 | } | |
2444 | else | |
2445 | { | |
2446 | i_ehdrp->e_phentsize = 0; | |
2447 | i_phdrp = 0; | |
2448 | i_ehdrp->e_phoff = 0; | |
2449 | } | |
2450 | ||
2451 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
2452 | (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false); | |
2453 | elf_tdata (abfd)->strtab_hdr.sh_name = | |
2454 | (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false); | |
2455 | elf_tdata (abfd)->shstrtab_hdr.sh_name = | |
2456 | (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false); | |
2457 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
2458 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
2459 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
2460 | return false; | |
2461 | ||
2462 | return true; | |
2463 | } | |
2464 | ||
2465 | /* Assign file positions for all the reloc sections which are not part | |
2466 | of the loadable file image. */ | |
2467 | ||
2468 | void | |
2469 | _bfd_elf_assign_file_positions_for_relocs (abfd) | |
2470 | bfd *abfd; | |
2471 | { | |
2472 | file_ptr off; | |
2473 | unsigned int i; | |
2474 | Elf_Internal_Shdr **shdrpp; | |
2475 | ||
2476 | off = elf_tdata (abfd)->next_file_pos; | |
2477 | ||
2478 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; | |
2479 | i < elf_elfheader (abfd)->e_shnum; | |
2480 | i++, shdrpp++) | |
2481 | { | |
2482 | Elf_Internal_Shdr *shdrp; | |
2483 | ||
2484 | shdrp = *shdrpp; | |
2485 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
2486 | && shdrp->sh_offset == -1) | |
5fe14a9f | 2487 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, true); |
ede4eed4 KR |
2488 | } |
2489 | ||
2490 | elf_tdata (abfd)->next_file_pos = off; | |
2491 | } | |
2492 | ||
2493 | boolean | |
2494 | _bfd_elf_write_object_contents (abfd) | |
2495 | bfd *abfd; | |
2496 | { | |
2497 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2498 | Elf_Internal_Ehdr *i_ehdrp; | |
2499 | Elf_Internal_Shdr **i_shdrp; | |
2500 | boolean failed; | |
2501 | unsigned int count; | |
2502 | ||
2503 | if (! abfd->output_has_begun | |
2504 | && ! _bfd_elf_compute_section_file_positions (abfd, | |
2505 | (struct bfd_link_info *) NULL)) | |
2506 | return false; | |
2507 | ||
2508 | i_shdrp = elf_elfsections (abfd); | |
2509 | i_ehdrp = elf_elfheader (abfd); | |
2510 | ||
2511 | failed = false; | |
2512 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); | |
2513 | if (failed) | |
2514 | return false; | |
2515 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
2516 | ||
2517 | /* After writing the headers, we need to write the sections too... */ | |
2518 | for (count = 1; count < i_ehdrp->e_shnum; count++) | |
2519 | { | |
2520 | if (bed->elf_backend_section_processing) | |
2521 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
2522 | if (i_shdrp[count]->contents) | |
2523 | { | |
2524 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 | |
2525 | || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size, | |
2526 | 1, abfd) | |
2527 | != i_shdrp[count]->sh_size)) | |
2528 | return false; | |
2529 | } | |
2530 | } | |
2531 | ||
2532 | /* Write out the section header names. */ | |
2533 | if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
2534 | || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd))) | |
2535 | return false; | |
2536 | ||
2537 | if (bed->elf_backend_final_write_processing) | |
2538 | (*bed->elf_backend_final_write_processing) (abfd, | |
2539 | elf_tdata (abfd)->linker); | |
2540 | ||
2541 | return bed->s->write_shdrs_and_ehdr (abfd); | |
2542 | } | |
2543 | ||
2544 | /* given a section, search the header to find them... */ | |
2545 | int | |
2546 | _bfd_elf_section_from_bfd_section (abfd, asect) | |
2547 | bfd *abfd; | |
2548 | struct sec *asect; | |
2549 | { | |
2550 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2551 | Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd); | |
2552 | int index; | |
2553 | Elf_Internal_Shdr *hdr; | |
2554 | int maxindex = elf_elfheader (abfd)->e_shnum; | |
2555 | ||
2556 | for (index = 0; index < maxindex; index++) | |
2557 | { | |
2558 | hdr = i_shdrp[index]; | |
2559 | if (hdr->bfd_section == asect) | |
2560 | return index; | |
2561 | } | |
2562 | ||
2563 | if (bed->elf_backend_section_from_bfd_section) | |
2564 | { | |
2565 | for (index = 0; index < maxindex; index++) | |
2566 | { | |
2567 | int retval; | |
2568 | ||
2569 | hdr = i_shdrp[index]; | |
2570 | retval = index; | |
2571 | if ((*bed->elf_backend_section_from_bfd_section) | |
2572 | (abfd, hdr, asect, &retval)) | |
2573 | return retval; | |
2574 | } | |
2575 | } | |
2576 | ||
2577 | if (bfd_is_abs_section (asect)) | |
2578 | return SHN_ABS; | |
2579 | if (bfd_is_com_section (asect)) | |
2580 | return SHN_COMMON; | |
2581 | if (bfd_is_und_section (asect)) | |
2582 | return SHN_UNDEF; | |
2583 | ||
2584 | return -1; | |
2585 | } | |
2586 | ||
cb84f028 ILT |
2587 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 |
2588 | on error. */ | |
2589 | ||
2590 | int | |
ede4eed4 KR |
2591 | _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr) |
2592 | bfd *abfd; | |
7fc6a16a | 2593 | asymbol **asym_ptr_ptr; |
ede4eed4 | 2594 | { |
7fc6a16a | 2595 | asymbol *asym_ptr = *asym_ptr_ptr; |
ede4eed4 KR |
2596 | int idx; |
2597 | flagword flags = asym_ptr->flags; | |
2598 | ||
2599 | /* When gas creates relocations against local labels, it creates its | |
2600 | own symbol for the section, but does put the symbol into the | |
2601 | symbol chain, so udata is 0. When the linker is generating | |
2602 | relocatable output, this section symbol may be for one of the | |
2603 | input sections rather than the output section. */ | |
2604 | if (asym_ptr->udata.i == 0 | |
2605 | && (flags & BSF_SECTION_SYM) | |
2606 | && asym_ptr->section) | |
2607 | { | |
2608 | int indx; | |
2609 | ||
2610 | if (asym_ptr->section->output_section != NULL) | |
2611 | indx = asym_ptr->section->output_section->index; | |
2612 | else | |
2613 | indx = asym_ptr->section->index; | |
2614 | if (elf_section_syms (abfd)[indx]) | |
2615 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; | |
2616 | } | |
2617 | ||
2618 | idx = asym_ptr->udata.i; | |
cb84f028 ILT |
2619 | |
2620 | if (idx == 0) | |
2621 | { | |
2622 | /* This case can occur when using --strip-symbol on a symbol | |
2623 | which is used in a relocation entry. */ | |
2624 | (*_bfd_error_handler) | |
2625 | ("%s: symbol `%s' required but not present", | |
2626 | bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr)); | |
2627 | bfd_set_error (bfd_error_no_symbols); | |
2628 | return -1; | |
2629 | } | |
ede4eed4 KR |
2630 | |
2631 | #if DEBUG & 4 | |
2632 | { | |
2633 | fprintf (stderr, | |
2634 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n", | |
cb84f028 ILT |
2635 | (long) asym_ptr, asym_ptr->name, idx, flags, |
2636 | elf_symbol_flags (flags)); | |
ede4eed4 KR |
2637 | fflush (stderr); |
2638 | } | |
2639 | #endif | |
2640 | ||
2641 | return idx; | |
2642 | } | |
2643 | ||
3dbf33ee ILT |
2644 | /* Copy private BFD data. This copies any program header information. */ |
2645 | ||
2646 | static boolean | |
2647 | copy_private_bfd_data (ibfd, obfd) | |
2648 | bfd *ibfd; | |
2649 | bfd *obfd; | |
2650 | { | |
6933148a | 2651 | Elf_Internal_Ehdr *iehdr; |
3dbf33ee ILT |
2652 | struct elf_segment_map *mfirst; |
2653 | struct elf_segment_map **pm; | |
2654 | Elf_Internal_Phdr *p; | |
2655 | unsigned int i, c; | |
2656 | ||
2657 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
2658 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
2659 | return true; | |
2660 | ||
2661 | if (elf_tdata (ibfd)->phdr == NULL) | |
2662 | return true; | |
2663 | ||
6933148a ILT |
2664 | iehdr = elf_elfheader (ibfd); |
2665 | ||
3dbf33ee ILT |
2666 | mfirst = NULL; |
2667 | pm = &mfirst; | |
2668 | ||
2669 | c = elf_elfheader (ibfd)->e_phnum; | |
2670 | for (i = 0, p = elf_tdata (ibfd)->phdr; i < c; i++, p++) | |
2671 | { | |
3dbf33ee | 2672 | unsigned int csecs; |
6933148a ILT |
2673 | asection *s; |
2674 | struct elf_segment_map *m; | |
2675 | unsigned int isec; | |
3dbf33ee ILT |
2676 | |
2677 | csecs = 0; | |
3dbf33ee | 2678 | |
6933148a ILT |
2679 | /* The complicated case when p_vaddr is 0 is to handle the |
2680 | Solaris linker, which generates a PT_INTERP section with | |
2681 | p_vaddr and p_memsz set to 0. */ | |
2682 | for (s = ibfd->sections; s != NULL; s = s->next) | |
2683 | if (((s->vma >= p->p_vaddr | |
2684 | && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz | |
2685 | || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz)) | |
2686 | || (p->p_vaddr == 0 | |
2687 | && p->p_filesz > 0 | |
2688 | && (s->flags & SEC_HAS_CONTENTS) != 0 | |
2689 | && (bfd_vma) s->filepos >= p->p_offset | |
2690 | && ((bfd_vma) s->filepos + s->_raw_size | |
2691 | <= p->p_offset + p->p_filesz))) | |
86587dd4 | 2692 | && (s->flags & SEC_ALLOC) != 0 |
6933148a ILT |
2693 | && s->output_section != NULL) |
2694 | ++csecs; | |
3dbf33ee ILT |
2695 | |
2696 | m = ((struct elf_segment_map *) | |
2697 | bfd_alloc (obfd, | |
2698 | (sizeof (struct elf_segment_map) | |
2699 | + (csecs - 1) * sizeof (asection *)))); | |
2700 | if (m == NULL) | |
a9713b91 | 2701 | return false; |
3dbf33ee ILT |
2702 | |
2703 | m->next = NULL; | |
2704 | m->p_type = p->p_type; | |
2705 | m->p_flags = p->p_flags; | |
2706 | m->p_flags_valid = 1; | |
2707 | m->p_paddr = p->p_paddr; | |
2708 | m->p_paddr_valid = 1; | |
2709 | ||
6933148a ILT |
2710 | m->includes_filehdr = (p->p_offset == 0 |
2711 | && p->p_filesz >= iehdr->e_ehsize); | |
2712 | ||
2713 | m->includes_phdrs = (p->p_offset <= (bfd_vma) iehdr->e_phoff | |
2714 | && (p->p_offset + p->p_filesz | |
2715 | >= ((bfd_vma) iehdr->e_phoff | |
2716 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
3dbf33ee | 2717 | |
6933148a ILT |
2718 | isec = 0; |
2719 | for (s = ibfd->sections; s != NULL; s = s->next) | |
2720 | { | |
2721 | if (((s->vma >= p->p_vaddr | |
2722 | && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz | |
2723 | || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz)) | |
2724 | || (p->p_vaddr == 0 | |
2725 | && p->p_filesz > 0 | |
2726 | && (s->flags & SEC_HAS_CONTENTS) != 0 | |
2727 | && (bfd_vma) s->filepos >= p->p_offset | |
2728 | && ((bfd_vma) s->filepos + s->_raw_size | |
2729 | <= p->p_offset + p->p_filesz))) | |
86587dd4 | 2730 | && (s->flags & SEC_ALLOC) != 0 |
6933148a | 2731 | && s->output_section != NULL) |
3dbf33ee | 2732 | { |
6933148a ILT |
2733 | m->sections[isec] = s->output_section; |
2734 | ++isec; | |
3dbf33ee | 2735 | } |
3dbf33ee | 2736 | } |
6933148a | 2737 | BFD_ASSERT (isec == csecs); |
6933148a | 2738 | m->count = csecs; |
3dbf33ee ILT |
2739 | |
2740 | *pm = m; | |
2741 | pm = &m->next; | |
2742 | } | |
2743 | ||
2744 | elf_tdata (obfd)->segment_map = mfirst; | |
2745 | ||
2746 | return true; | |
2747 | } | |
2748 | ||
fd0198f0 ILT |
2749 | /* Copy private section information. This copies over the entsize |
2750 | field, and sometimes the info field. */ | |
2751 | ||
2752 | boolean | |
2753 | _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec) | |
2754 | bfd *ibfd; | |
2755 | asection *isec; | |
2756 | bfd *obfd; | |
2757 | asection *osec; | |
2758 | { | |
2759 | Elf_Internal_Shdr *ihdr, *ohdr; | |
2760 | ||
2761 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
2762 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
2763 | return true; | |
2764 | ||
3dbf33ee ILT |
2765 | /* Copy over private BFD data if it has not already been copied. |
2766 | This must be done here, rather than in the copy_private_bfd_data | |
2767 | entry point, because the latter is called after the section | |
2768 | contents have been set, which means that the program headers have | |
2769 | already been worked out. */ | |
2770 | if (elf_tdata (obfd)->segment_map == NULL | |
2771 | && elf_tdata (ibfd)->phdr != NULL) | |
2772 | { | |
2773 | asection *s; | |
2774 | ||
2775 | /* Only set up the segments when all the sections have been set | |
2776 | up. */ | |
2777 | for (s = ibfd->sections; s != NULL; s = s->next) | |
2778 | if (s->output_section == NULL) | |
2779 | break; | |
2780 | if (s == NULL) | |
2781 | { | |
2782 | if (! copy_private_bfd_data (ibfd, obfd)) | |
2783 | return false; | |
2784 | } | |
2785 | } | |
2786 | ||
fd0198f0 ILT |
2787 | ihdr = &elf_section_data (isec)->this_hdr; |
2788 | ohdr = &elf_section_data (osec)->this_hdr; | |
2789 | ||
2790 | ohdr->sh_entsize = ihdr->sh_entsize; | |
2791 | ||
2792 | if (ihdr->sh_type == SHT_SYMTAB | |
2793 | || ihdr->sh_type == SHT_DYNSYM) | |
2794 | ohdr->sh_info = ihdr->sh_info; | |
2795 | ||
2796 | return true; | |
2797 | } | |
2798 | ||
2799 | /* Copy private symbol information. If this symbol is in a section | |
2800 | which we did not map into a BFD section, try to map the section | |
2801 | index correctly. We use special macro definitions for the mapped | |
2802 | section indices; these definitions are interpreted by the | |
2803 | swap_out_syms function. */ | |
2804 | ||
2805 | #define MAP_ONESYMTAB (SHN_LORESERVE - 1) | |
2806 | #define MAP_DYNSYMTAB (SHN_LORESERVE - 2) | |
2807 | #define MAP_STRTAB (SHN_LORESERVE - 3) | |
2808 | #define MAP_SHSTRTAB (SHN_LORESERVE - 4) | |
2809 | ||
2810 | boolean | |
2811 | _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg) | |
2812 | bfd *ibfd; | |
2813 | asymbol *isymarg; | |
2814 | bfd *obfd; | |
2815 | asymbol *osymarg; | |
2816 | { | |
2817 | elf_symbol_type *isym, *osym; | |
2818 | ||
2819 | isym = elf_symbol_from (ibfd, isymarg); | |
2820 | osym = elf_symbol_from (obfd, osymarg); | |
2821 | ||
2822 | if (isym != NULL | |
2823 | && osym != NULL | |
2824 | && bfd_is_abs_section (isym->symbol.section)) | |
2825 | { | |
2826 | unsigned int shndx; | |
2827 | ||
2828 | shndx = isym->internal_elf_sym.st_shndx; | |
2829 | if (shndx == elf_onesymtab (ibfd)) | |
2830 | shndx = MAP_ONESYMTAB; | |
2831 | else if (shndx == elf_dynsymtab (ibfd)) | |
2832 | shndx = MAP_DYNSYMTAB; | |
2833 | else if (shndx == elf_tdata (ibfd)->strtab_section) | |
2834 | shndx = MAP_STRTAB; | |
2835 | else if (shndx == elf_tdata (ibfd)->shstrtab_section) | |
2836 | shndx = MAP_SHSTRTAB; | |
2837 | osym->internal_elf_sym.st_shndx = shndx; | |
2838 | } | |
2839 | ||
2840 | return true; | |
2841 | } | |
2842 | ||
2843 | /* Swap out the symbols. */ | |
2844 | ||
ede4eed4 KR |
2845 | static boolean |
2846 | swap_out_syms (abfd, sttp) | |
2847 | bfd *abfd; | |
2848 | struct bfd_strtab_hash **sttp; | |
2849 | { | |
2850 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2851 | ||
2852 | if (!elf_map_symbols (abfd)) | |
2853 | return false; | |
2854 | ||
2855 | /* Dump out the symtabs. */ | |
2856 | { | |
2857 | int symcount = bfd_get_symcount (abfd); | |
2858 | asymbol **syms = bfd_get_outsymbols (abfd); | |
2859 | struct bfd_strtab_hash *stt; | |
2860 | Elf_Internal_Shdr *symtab_hdr; | |
2861 | Elf_Internal_Shdr *symstrtab_hdr; | |
2862 | char *outbound_syms; | |
2863 | int idx; | |
2864 | ||
2865 | stt = _bfd_elf_stringtab_init (); | |
2866 | if (stt == NULL) | |
2867 | return false; | |
2868 | ||
2869 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
2870 | symtab_hdr->sh_type = SHT_SYMTAB; | |
2871 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
2872 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
2873 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
2874 | symtab_hdr->sh_addralign = bed->s->file_align; | |
2875 | ||
2876 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
2877 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
2878 | ||
2879 | outbound_syms = bfd_alloc (abfd, | |
2880 | (1 + symcount) * bed->s->sizeof_sym); | |
2881 | if (outbound_syms == NULL) | |
a9713b91 | 2882 | return false; |
ede4eed4 KR |
2883 | symtab_hdr->contents = (PTR) outbound_syms; |
2884 | ||
2885 | /* now generate the data (for "contents") */ | |
2886 | { | |
2887 | /* Fill in zeroth symbol and swap it out. */ | |
2888 | Elf_Internal_Sym sym; | |
2889 | sym.st_name = 0; | |
2890 | sym.st_value = 0; | |
2891 | sym.st_size = 0; | |
2892 | sym.st_info = 0; | |
2893 | sym.st_other = 0; | |
2894 | sym.st_shndx = SHN_UNDEF; | |
cf9fb9f2 | 2895 | bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms); |
ede4eed4 KR |
2896 | outbound_syms += bed->s->sizeof_sym; |
2897 | } | |
2898 | for (idx = 0; idx < symcount; idx++) | |
2899 | { | |
2900 | Elf_Internal_Sym sym; | |
2901 | bfd_vma value = syms[idx]->value; | |
2902 | elf_symbol_type *type_ptr; | |
2903 | flagword flags = syms[idx]->flags; | |
052b35d2 | 2904 | int type; |
ede4eed4 KR |
2905 | |
2906 | if (flags & BSF_SECTION_SYM) | |
2907 | /* Section symbols have no names. */ | |
2908 | sym.st_name = 0; | |
2909 | else | |
2910 | { | |
2911 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
2912 | syms[idx]->name, | |
2913 | true, false); | |
2914 | if (sym.st_name == (unsigned long) -1) | |
2915 | return false; | |
2916 | } | |
2917 | ||
2918 | type_ptr = elf_symbol_from (abfd, syms[idx]); | |
2919 | ||
2920 | if (bfd_is_com_section (syms[idx]->section)) | |
2921 | { | |
2922 | /* ELF common symbols put the alignment into the `value' field, | |
2923 | and the size into the `size' field. This is backwards from | |
2924 | how BFD handles it, so reverse it here. */ | |
2925 | sym.st_size = value; | |
2926 | if (type_ptr == NULL | |
2927 | || type_ptr->internal_elf_sym.st_value == 0) | |
2928 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
2929 | else | |
2930 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
2931 | sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, | |
2932 | syms[idx]->section); | |
2933 | } | |
2934 | else | |
2935 | { | |
2936 | asection *sec = syms[idx]->section; | |
2937 | int shndx; | |
2938 | ||
2939 | if (sec->output_section) | |
2940 | { | |
2941 | value += sec->output_offset; | |
2942 | sec = sec->output_section; | |
2943 | } | |
2944 | value += sec->vma; | |
2945 | sym.st_value = value; | |
2946 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
fd0198f0 ILT |
2947 | |
2948 | if (bfd_is_abs_section (sec) | |
2949 | && type_ptr != NULL | |
2950 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
ede4eed4 | 2951 | { |
fd0198f0 ILT |
2952 | /* This symbol is in a real ELF section which we did |
2953 | not create as a BFD section. Undo the mapping done | |
2954 | by copy_private_symbol_data. */ | |
2955 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
2956 | switch (shndx) | |
2957 | { | |
2958 | case MAP_ONESYMTAB: | |
2959 | shndx = elf_onesymtab (abfd); | |
2960 | break; | |
2961 | case MAP_DYNSYMTAB: | |
2962 | shndx = elf_dynsymtab (abfd); | |
2963 | break; | |
2964 | case MAP_STRTAB: | |
2965 | shndx = elf_tdata (abfd)->strtab_section; | |
2966 | break; | |
2967 | case MAP_SHSTRTAB: | |
2968 | shndx = elf_tdata (abfd)->shstrtab_section; | |
2969 | break; | |
2970 | default: | |
2971 | break; | |
2972 | } | |
2973 | } | |
2974 | else | |
2975 | { | |
2976 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
2977 | ||
2978 | if (shndx == -1) | |
2979 | { | |
2980 | asection *sec2; | |
2981 | ||
2982 | /* Writing this would be a hell of a lot easier if | |
2983 | we had some decent documentation on bfd, and | |
2984 | knew what to expect of the library, and what to | |
2985 | demand of applications. For example, it | |
2986 | appears that `objcopy' might not set the | |
2987 | section of a symbol to be a section that is | |
2988 | actually in the output file. */ | |
2989 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
2990 | BFD_ASSERT (sec2 != 0); | |
2991 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
2992 | BFD_ASSERT (shndx != -1); | |
2993 | } | |
ede4eed4 | 2994 | } |
fd0198f0 ILT |
2995 | |
2996 | sym.st_shndx = shndx; | |
ede4eed4 KR |
2997 | } |
2998 | ||
052b35d2 ILT |
2999 | if ((flags & BSF_FUNCTION) != 0) |
3000 | type = STT_FUNC; | |
3001 | else if ((flags & BSF_OBJECT) != 0) | |
3002 | type = STT_OBJECT; | |
3003 | else | |
3004 | type = STT_NOTYPE; | |
3005 | ||
ede4eed4 | 3006 | if (bfd_is_com_section (syms[idx]->section)) |
052b35d2 | 3007 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); |
ede4eed4 KR |
3008 | else if (bfd_is_und_section (syms[idx]->section)) |
3009 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
3010 | ? STB_WEAK | |
3011 | : STB_GLOBAL), | |
052b35d2 | 3012 | type); |
ede4eed4 KR |
3013 | else if (flags & BSF_SECTION_SYM) |
3014 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
3015 | else if (flags & BSF_FILE) | |
3016 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
3017 | else | |
3018 | { | |
3019 | int bind = STB_LOCAL; | |
ede4eed4 KR |
3020 | |
3021 | if (flags & BSF_LOCAL) | |
3022 | bind = STB_LOCAL; | |
3023 | else if (flags & BSF_WEAK) | |
3024 | bind = STB_WEAK; | |
3025 | else if (flags & BSF_GLOBAL) | |
3026 | bind = STB_GLOBAL; | |
3027 | ||
ede4eed4 KR |
3028 | sym.st_info = ELF_ST_INFO (bind, type); |
3029 | } | |
3030 | ||
3031 | sym.st_other = 0; | |
cf9fb9f2 | 3032 | bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms); |
ede4eed4 KR |
3033 | outbound_syms += bed->s->sizeof_sym; |
3034 | } | |
3035 | ||
3036 | *sttp = stt; | |
3037 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
3038 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
3039 | ||
3040 | symstrtab_hdr->sh_flags = 0; | |
3041 | symstrtab_hdr->sh_addr = 0; | |
3042 | symstrtab_hdr->sh_entsize = 0; | |
3043 | symstrtab_hdr->sh_link = 0; | |
3044 | symstrtab_hdr->sh_info = 0; | |
3045 | symstrtab_hdr->sh_addralign = 1; | |
3046 | } | |
3047 | ||
3048 | return true; | |
3049 | } | |
3050 | ||
3051 | /* Return the number of bytes required to hold the symtab vector. | |
3052 | ||
3053 | Note that we base it on the count plus 1, since we will null terminate | |
3054 | the vector allocated based on this size. However, the ELF symbol table | |
3055 | always has a dummy entry as symbol #0, so it ends up even. */ | |
3056 | ||
3057 | long | |
3058 | _bfd_elf_get_symtab_upper_bound (abfd) | |
3059 | bfd *abfd; | |
3060 | { | |
3061 | long symcount; | |
3062 | long symtab_size; | |
3063 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
3064 | ||
3065 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3066 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); | |
3067 | ||
3068 | return symtab_size; | |
3069 | } | |
3070 | ||
3071 | long | |
3072 | _bfd_elf_get_dynamic_symtab_upper_bound (abfd) | |
3073 | bfd *abfd; | |
3074 | { | |
3075 | long symcount; | |
3076 | long symtab_size; | |
3077 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3078 | ||
3079 | if (elf_dynsymtab (abfd) == 0) | |
3080 | { | |
3081 | bfd_set_error (bfd_error_invalid_operation); | |
3082 | return -1; | |
3083 | } | |
3084 | ||
3085 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3086 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); | |
3087 | ||
3088 | return symtab_size; | |
3089 | } | |
3090 | ||
3091 | long | |
3092 | _bfd_elf_get_reloc_upper_bound (abfd, asect) | |
3093 | bfd *abfd; | |
3094 | sec_ptr asect; | |
3095 | { | |
3096 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
3097 | } | |
3098 | ||
3099 | /* Canonicalize the relocs. */ | |
3100 | ||
3101 | long | |
3102 | _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols) | |
3103 | bfd *abfd; | |
3104 | sec_ptr section; | |
3105 | arelent **relptr; | |
3106 | asymbol **symbols; | |
3107 | { | |
3108 | arelent *tblptr; | |
3109 | unsigned int i; | |
3110 | ||
3111 | if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd, section, symbols)) | |
3112 | return -1; | |
3113 | ||
3114 | tblptr = section->relocation; | |
3115 | for (i = 0; i < section->reloc_count; i++) | |
3116 | *relptr++ = tblptr++; | |
3117 | ||
3118 | *relptr = NULL; | |
3119 | ||
3120 | return section->reloc_count; | |
3121 | } | |
3122 | ||
3123 | long | |
3124 | _bfd_elf_get_symtab (abfd, alocation) | |
3125 | bfd *abfd; | |
3126 | asymbol **alocation; | |
3127 | { | |
3128 | long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false); | |
3129 | ||
3130 | if (symcount >= 0) | |
3131 | bfd_get_symcount (abfd) = symcount; | |
3132 | return symcount; | |
3133 | } | |
3134 | ||
3135 | long | |
3136 | _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation) | |
3137 | bfd *abfd; | |
3138 | asymbol **alocation; | |
3139 | { | |
3140 | return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true); | |
3141 | } | |
3142 | ||
3143 | asymbol * | |
3144 | _bfd_elf_make_empty_symbol (abfd) | |
3145 | bfd *abfd; | |
3146 | { | |
3147 | elf_symbol_type *newsym; | |
3148 | ||
3149 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type)); | |
3150 | if (!newsym) | |
a9713b91 | 3151 | return NULL; |
ede4eed4 KR |
3152 | else |
3153 | { | |
3154 | newsym->symbol.the_bfd = abfd; | |
3155 | return &newsym->symbol; | |
3156 | } | |
3157 | } | |
3158 | ||
3159 | void | |
3160 | _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret) | |
3161 | bfd *ignore_abfd; | |
3162 | asymbol *symbol; | |
3163 | symbol_info *ret; | |
3164 | { | |
3165 | bfd_symbol_info (symbol, ret); | |
3166 | } | |
3167 | ||
3168 | alent * | |
3169 | _bfd_elf_get_lineno (ignore_abfd, symbol) | |
3170 | bfd *ignore_abfd; | |
3171 | asymbol *symbol; | |
3172 | { | |
8cd2f4fe | 3173 | abort (); |
ede4eed4 KR |
3174 | return NULL; |
3175 | } | |
3176 | ||
3177 | boolean | |
3178 | _bfd_elf_set_arch_mach (abfd, arch, machine) | |
3179 | bfd *abfd; | |
3180 | enum bfd_architecture arch; | |
3181 | unsigned long machine; | |
3182 | { | |
3183 | /* If this isn't the right architecture for this backend, and this | |
3184 | isn't the generic backend, fail. */ | |
3185 | if (arch != get_elf_backend_data (abfd)->arch | |
3186 | && arch != bfd_arch_unknown | |
3187 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
3188 | return false; | |
3189 | ||
3190 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
3191 | } | |
3192 | ||
6f904fce ILT |
3193 | /* Find the nearest line to a particular section and offset, for error |
3194 | reporting. */ | |
3195 | ||
ede4eed4 KR |
3196 | boolean |
3197 | _bfd_elf_find_nearest_line (abfd, | |
6f904fce ILT |
3198 | section, |
3199 | symbols, | |
3200 | offset, | |
3201 | filename_ptr, | |
3202 | functionname_ptr, | |
3203 | line_ptr) | |
ede4eed4 KR |
3204 | bfd *abfd; |
3205 | asection *section; | |
3206 | asymbol **symbols; | |
3207 | bfd_vma offset; | |
3208 | CONST char **filename_ptr; | |
3209 | CONST char **functionname_ptr; | |
3210 | unsigned int *line_ptr; | |
3211 | { | |
86aac8ea | 3212 | boolean found; |
6f904fce ILT |
3213 | const char *filename; |
3214 | asymbol *func; | |
86aac8ea | 3215 | bfd_vma low_func; |
6f904fce ILT |
3216 | asymbol **p; |
3217 | ||
86aac8ea ILT |
3218 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, |
3219 | &found, filename_ptr, | |
3220 | functionname_ptr, line_ptr, | |
3221 | &elf_tdata (abfd)->line_info)) | |
3222 | return false; | |
3223 | if (found) | |
3224 | return true; | |
3225 | ||
6f904fce ILT |
3226 | if (symbols == NULL) |
3227 | return false; | |
3228 | ||
3229 | filename = NULL; | |
3230 | func = NULL; | |
86aac8ea | 3231 | low_func = 0; |
6f904fce ILT |
3232 | |
3233 | for (p = symbols; *p != NULL; p++) | |
3234 | { | |
3235 | elf_symbol_type *q; | |
3236 | ||
3237 | q = (elf_symbol_type *) *p; | |
3238 | ||
3239 | if (bfd_get_section (&q->symbol) != section) | |
3240 | continue; | |
3241 | ||
3242 | switch (ELF_ST_TYPE (q->internal_elf_sym.st_info)) | |
3243 | { | |
3244 | default: | |
3245 | break; | |
3246 | case STT_FILE: | |
3247 | filename = bfd_asymbol_name (&q->symbol); | |
3248 | break; | |
3249 | case STT_FUNC: | |
86aac8ea ILT |
3250 | if (q->symbol.section == section |
3251 | && q->symbol.value >= low_func | |
3252 | && q->symbol.value <= offset) | |
3253 | { | |
3254 | func = (asymbol *) q; | |
3255 | low_func = q->symbol.value; | |
3256 | } | |
6f904fce ILT |
3257 | break; |
3258 | } | |
3259 | } | |
3260 | ||
3261 | if (func == NULL) | |
3262 | return false; | |
3263 | ||
3264 | *filename_ptr = filename; | |
3265 | *functionname_ptr = bfd_asymbol_name (func); | |
3266 | *line_ptr = 0; | |
3267 | return true; | |
ede4eed4 KR |
3268 | } |
3269 | ||
3270 | int | |
3271 | _bfd_elf_sizeof_headers (abfd, reloc) | |
3272 | bfd *abfd; | |
3273 | boolean reloc; | |
3274 | { | |
3275 | int ret; | |
3276 | ||
3277 | ret = get_elf_backend_data (abfd)->s->sizeof_ehdr; | |
3278 | if (! reloc) | |
fd0198f0 | 3279 | ret += get_program_header_size (abfd); |
ede4eed4 KR |
3280 | return ret; |
3281 | } | |
3282 | ||
3283 | boolean | |
3284 | _bfd_elf_set_section_contents (abfd, section, location, offset, count) | |
3285 | bfd *abfd; | |
3286 | sec_ptr section; | |
3287 | PTR location; | |
3288 | file_ptr offset; | |
3289 | bfd_size_type count; | |
3290 | { | |
3291 | Elf_Internal_Shdr *hdr; | |
3292 | ||
3293 | if (! abfd->output_has_begun | |
3294 | && ! _bfd_elf_compute_section_file_positions (abfd, | |
3295 | (struct bfd_link_info *) NULL)) | |
3296 | return false; | |
3297 | ||
3298 | hdr = &elf_section_data (section)->this_hdr; | |
3299 | ||
3300 | if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1) | |
3301 | return false; | |
3302 | if (bfd_write (location, 1, count, abfd) != count) | |
3303 | return false; | |
3304 | ||
3305 | return true; | |
3306 | } | |
3307 | ||
3308 | void | |
3309 | _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst) | |
3310 | bfd *abfd; | |
3311 | arelent *cache_ptr; | |
3312 | Elf_Internal_Rela *dst; | |
3313 | { | |
8cd2f4fe | 3314 | abort (); |
ede4eed4 KR |
3315 | } |
3316 | ||
3317 | #if 0 | |
3318 | void | |
3319 | _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst) | |
3320 | bfd *abfd; | |
3321 | arelent *cache_ptr; | |
3322 | Elf_Internal_Rel *dst; | |
3323 | { | |
8cd2f4fe | 3324 | abort (); |
ede4eed4 KR |
3325 | } |
3326 | #endif | |
7fc6a16a ILT |
3327 | |
3328 | /* Try to convert a non-ELF reloc into an ELF one. */ | |
3329 | ||
3330 | boolean | |
3331 | _bfd_elf_validate_reloc (abfd, areloc) | |
3332 | bfd *abfd; | |
3333 | arelent *areloc; | |
3334 | { | |
3335 | /* Check whether we really have an ELF howto. */ | |
3336 | ||
3337 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
3338 | { | |
3339 | bfd_reloc_code_real_type code; | |
3340 | reloc_howto_type *howto; | |
3341 | ||
3342 | /* Alien reloc: Try to determine its type to replace it with an | |
3343 | equivalent ELF reloc. */ | |
3344 | ||
3345 | if (areloc->howto->pc_relative) | |
3346 | { | |
3347 | switch (areloc->howto->bitsize) | |
3348 | { | |
3349 | case 8: | |
3350 | code = BFD_RELOC_8_PCREL; | |
3351 | break; | |
3352 | case 12: | |
3353 | code = BFD_RELOC_12_PCREL; | |
3354 | break; | |
3355 | case 16: | |
3356 | code = BFD_RELOC_16_PCREL; | |
3357 | break; | |
3358 | case 24: | |
3359 | code = BFD_RELOC_24_PCREL; | |
3360 | break; | |
3361 | case 32: | |
3362 | code = BFD_RELOC_32_PCREL; | |
3363 | break; | |
3364 | case 64: | |
3365 | code = BFD_RELOC_64_PCREL; | |
3366 | break; | |
3367 | default: | |
3368 | goto fail; | |
3369 | } | |
3370 | ||
3371 | howto = bfd_reloc_type_lookup (abfd, code); | |
3372 | ||
3373 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
3374 | { | |
3375 | if (howto->pcrel_offset) | |
3376 | areloc->addend += areloc->address; | |
3377 | else | |
3378 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
3379 | } | |
3380 | } | |
3381 | else | |
3382 | { | |
3383 | switch (areloc->howto->bitsize) | |
3384 | { | |
3385 | case 8: | |
3386 | code = BFD_RELOC_8; | |
3387 | break; | |
3388 | case 14: | |
3389 | code = BFD_RELOC_14; | |
3390 | break; | |
3391 | case 16: | |
3392 | code = BFD_RELOC_16; | |
3393 | break; | |
3394 | case 26: | |
3395 | code = BFD_RELOC_26; | |
3396 | break; | |
3397 | case 32: | |
3398 | code = BFD_RELOC_32; | |
3399 | break; | |
3400 | case 64: | |
3401 | code = BFD_RELOC_64; | |
3402 | break; | |
3403 | default: | |
3404 | goto fail; | |
3405 | } | |
3406 | ||
3407 | howto = bfd_reloc_type_lookup (abfd, code); | |
3408 | } | |
3409 | ||
3410 | if (howto) | |
3411 | areloc->howto = howto; | |
3412 | else | |
3413 | goto fail; | |
3414 | } | |
3415 | ||
3416 | return true; | |
3417 | ||
3418 | fail: | |
3419 | (*_bfd_error_handler) | |
3420 | ("%s: unsupported relocation type %s", | |
3421 | bfd_get_filename (abfd), areloc->howto->name); | |
3422 | bfd_set_error (bfd_error_bad_value); | |
3423 | return false; | |
3424 | } |