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