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