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