* elfcode.h (assign_file_positions_except_relocs): Don't require
[deliverable/binutils-gdb.git] / bfd / elfcode.h
1 /* ELF executable support for BFD.
2 Copyright 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
3
4 Written by Fred Fish @ Cygnus Support, from information published
5 in "UNIX System V Release 4, Programmers Guide: ANSI C and
6 Programming Support Tools". Sufficient support for gdb.
7
8 Rewritten by Mark Eichin @ Cygnus Support, from information
9 published in "System V Application Binary Interface", chapters 4
10 and 5, as well as the various "Processor Supplement" documents
11 derived from it. Added support for assembler and other object file
12 utilities. Further work done by Ken Raeburn (Cygnus Support), Michael
13 Meissner (Open Software Foundation), and Peter Hoogenboom (University
14 of Utah) to finish and extend this.
15
16 This file is part of BFD, the Binary File Descriptor library.
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
31
32 /* Problems and other issues to resolve.
33
34 (1) BFD expects there to be some fixed number of "sections" in
35 the object file. I.E. there is a "section_count" variable in the
36 bfd structure which contains the number of sections. However, ELF
37 supports multiple "views" of a file. In particular, with current
38 implementations, executable files typically have two tables, a
39 program header table and a section header table, both of which
40 partition the executable.
41
42 In ELF-speak, the "linking view" of the file uses the section header
43 table to access "sections" within the file, and the "execution view"
44 uses the program header table to access "segments" within the file.
45 "Segments" typically may contain all the data from one or more
46 "sections".
47
48 Note that the section header table is optional in ELF executables,
49 but it is this information that is most useful to gdb. If the
50 section header table is missing, then gdb should probably try
51 to make do with the program header table. (FIXME)
52
53 (2) The code in this file is compiled twice, once in 32-bit mode and
54 once in 64-bit mode. More of it should be made size-independent
55 and moved into elf.c.
56
57 (3) ELF section symbols are handled rather sloppily now. This should
58 be cleaned up, and ELF section symbols reconciled with BFD section
59 symbols.
60 */
61
62 #include <string.h> /* For strrchr and friends */
63 #include "bfd.h"
64 #include "sysdep.h"
65 #include "libbfd.h"
66 #include "libelf.h"
67
68 /* Renaming structures, typedefs, macros and functions to be size-specific. */
69 #define Elf_External_Ehdr NAME(Elf,External_Ehdr)
70 #define Elf_External_Sym NAME(Elf,External_Sym)
71 #define Elf_External_Shdr NAME(Elf,External_Shdr)
72 #define Elf_External_Phdr NAME(Elf,External_Phdr)
73 #define Elf_External_Rel NAME(Elf,External_Rel)
74 #define Elf_External_Rela NAME(Elf,External_Rela)
75
76 #define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command)
77 #define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal)
78 #define elf_core_file_matches_executable_p NAME(bfd_elf,core_file_matches_executable_p)
79 #define elf_object_p NAME(bfd_elf,object_p)
80 #define elf_core_file_p NAME(bfd_elf,core_file_p)
81 #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound)
82 #define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
83 #define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc)
84 #define elf_get_symtab NAME(bfd_elf,get_symtab)
85 #define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol)
86 #define elf_get_symbol_info NAME(bfd_elf,get_symbol_info)
87 #define elf_print_symbol NAME(bfd_elf,print_symbol)
88 #define elf_get_lineno NAME(bfd_elf,get_lineno)
89 #define elf_set_arch_mach NAME(bfd_elf,set_arch_mach)
90 #define elf_find_nearest_line NAME(bfd_elf,find_nearest_line)
91 #define elf_sizeof_headers NAME(bfd_elf,sizeof_headers)
92 #define elf_set_section_contents NAME(bfd_elf,set_section_contents)
93 #define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto)
94 #define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel)
95 #define elf_new_section_hook NAME(bfd_elf,new_section_hook)
96 #define write_relocs NAME(bfd_elf,_write_relocs)
97 #define elf_find_section NAME(bfd_elf,find_section)
98
99 #if ARCH_SIZE == 64
100 #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
101 #define ELF_R_SYM(X) ELF64_R_SYM(X)
102 #define ELFCLASS ELFCLASS64
103 #define FILE_ALIGN 8
104 #endif
105 #if ARCH_SIZE == 32
106 #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
107 #define ELF_R_SYM(X) ELF32_R_SYM(X)
108 #define ELFCLASS ELFCLASS32
109 #define FILE_ALIGN 4
110 #endif
111
112 static int shstrtab_length_fixed;
113
114 struct elf_sect_data
115 {
116 int reloc_sec;
117 /* more? */
118 };
119
120 /* Forward declarations of static functions */
121
122 static struct sec *section_from_elf_index PARAMS ((bfd *, unsigned int));
123
124 static int elf_section_from_bfd_section PARAMS ((bfd *, struct sec *));
125
126 static boolean elf_slurp_symbol_table PARAMS ((bfd *, asymbol **));
127
128 static int elf_symbol_from_bfd_symbol PARAMS ((bfd *,
129 struct symbol_cache_entry **));
130
131 static boolean elf_map_symbols PARAMS ((bfd *));
132 static boolean swap_out_syms PARAMS ((bfd *));
133
134 #ifdef DEBUG
135 static void elf_debug_section PARAMS ((char *, int, Elf_Internal_Shdr *));
136 static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *));
137 #endif
138
139 #define elf_string_from_elf_strtab(abfd,strindex) \
140 elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
141 \f
142
143 /* Structure swapping routines */
144
145 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
146 can be handled by explicitly specifying 32 bits or "the long type". */
147 #if ARCH_SIZE == 64
148 #define put_word bfd_h_put_64
149 #define get_word bfd_h_get_64
150 #endif
151 #if ARCH_SIZE == 32
152 #define put_word bfd_h_put_32
153 #define get_word bfd_h_get_32
154 #endif
155
156 /* Translate an ELF symbol in external format into an ELF symbol in internal
157 format. */
158
159 static void
160 elf_swap_symbol_in (abfd, src, dst)
161 bfd *abfd;
162 Elf_External_Sym *src;
163 Elf_Internal_Sym *dst;
164 {
165 dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name);
166 dst->st_value = get_word (abfd, (bfd_byte *) src->st_value);
167 dst->st_size = get_word (abfd, (bfd_byte *) src->st_size);
168 dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info);
169 dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other);
170 dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx);
171 }
172
173 /* Translate an ELF symbol in internal format into an ELF symbol in external
174 format. */
175
176 static void
177 elf_swap_symbol_out (abfd, src, dst)
178 bfd *abfd;
179 Elf_Internal_Sym *src;
180 Elf_External_Sym *dst;
181 {
182 bfd_h_put_32 (abfd, src->st_name, dst->st_name);
183 put_word (abfd, src->st_value, dst->st_value);
184 put_word (abfd, src->st_size, dst->st_size);
185 bfd_h_put_8 (abfd, src->st_info, dst->st_info);
186 bfd_h_put_8 (abfd, src->st_other, dst->st_other);
187 bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx);
188 }
189
190
191 /* Translate an ELF file header in external format into an ELF file header in
192 internal format. */
193
194 static void
195 elf_swap_ehdr_in (abfd, src, dst)
196 bfd *abfd;
197 Elf_External_Ehdr *src;
198 Elf_Internal_Ehdr *dst;
199 {
200 memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
201 dst->e_type = bfd_h_get_16 (abfd, (bfd_byte *) src->e_type);
202 dst->e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src->e_machine);
203 dst->e_version = bfd_h_get_32 (abfd, (bfd_byte *) src->e_version);
204 dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry);
205 dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff);
206 dst->e_shoff = get_word (abfd, (bfd_byte *) src->e_shoff);
207 dst->e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->e_flags);
208 dst->e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_ehsize);
209 dst->e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phentsize);
210 dst->e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phnum);
211 dst->e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shentsize);
212 dst->e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shnum);
213 dst->e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shstrndx);
214 }
215
216 /* Translate an ELF file header in internal format into an ELF file header in
217 external format. */
218
219 static void
220 elf_swap_ehdr_out (abfd, src, dst)
221 bfd *abfd;
222 Elf_Internal_Ehdr *src;
223 Elf_External_Ehdr *dst;
224 {
225 memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
226 /* note that all elements of dst are *arrays of unsigned char* already... */
227 bfd_h_put_16 (abfd, src->e_type, dst->e_type);
228 bfd_h_put_16 (abfd, src->e_machine, dst->e_machine);
229 bfd_h_put_32 (abfd, src->e_version, dst->e_version);
230 put_word (abfd, src->e_entry, dst->e_entry);
231 put_word (abfd, src->e_phoff, dst->e_phoff);
232 put_word (abfd, src->e_shoff, dst->e_shoff);
233 bfd_h_put_32 (abfd, src->e_flags, dst->e_flags);
234 bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize);
235 bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize);
236 bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum);
237 bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize);
238 bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum);
239 bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx);
240 }
241
242
243 /* Translate an ELF section header table entry in external format into an
244 ELF section header table entry in internal format. */
245
246 static void
247 elf_swap_shdr_in (abfd, src, dst)
248 bfd *abfd;
249 Elf_External_Shdr *src;
250 Elf_Internal_Shdr *dst;
251 {
252 dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name);
253 dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type);
254 dst->sh_flags = get_word (abfd, (bfd_byte *) src->sh_flags);
255 dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr);
256 dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset);
257 dst->sh_size = get_word (abfd, (bfd_byte *) src->sh_size);
258 dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link);
259 dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info);
260 dst->sh_addralign = get_word (abfd, (bfd_byte *) src->sh_addralign);
261 dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize);
262 /* we haven't done any processing on it yet, so... */
263 dst->rawdata = (void *) 0;
264 }
265
266 /* Translate an ELF section header table entry in internal format into an
267 ELF section header table entry in external format. */
268
269 static void
270 elf_swap_shdr_out (abfd, src, dst)
271 bfd *abfd;
272 Elf_Internal_Shdr *src;
273 Elf_External_Shdr *dst;
274 {
275 /* note that all elements of dst are *arrays of unsigned char* already... */
276 bfd_h_put_32 (abfd, src->sh_name, dst->sh_name);
277 bfd_h_put_32 (abfd, src->sh_type, dst->sh_type);
278 put_word (abfd, src->sh_flags, dst->sh_flags);
279 put_word (abfd, src->sh_addr, dst->sh_addr);
280 put_word (abfd, src->sh_offset, dst->sh_offset);
281 put_word (abfd, src->sh_size, dst->sh_size);
282 bfd_h_put_32 (abfd, src->sh_link, dst->sh_link);
283 bfd_h_put_32 (abfd, src->sh_info, dst->sh_info);
284 put_word (abfd, src->sh_addralign, dst->sh_addralign);
285 put_word (abfd, src->sh_entsize, dst->sh_entsize);
286 }
287
288
289 /* Translate an ELF program header table entry in external format into an
290 ELF program header table entry in internal format. */
291
292 static void
293 elf_swap_phdr_in (abfd, src, dst)
294 bfd *abfd;
295 Elf_External_Phdr *src;
296 Elf_Internal_Phdr *dst;
297 {
298 dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);
299 dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags);
300 dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset);
301 dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr);
302 dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr);
303 dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz);
304 dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz);
305 dst->p_align = get_word (abfd, (bfd_byte *) src->p_align);
306 }
307
308 static void
309 elf_swap_phdr_out (abfd, src, dst)
310 bfd *abfd;
311 Elf_Internal_Phdr *src;
312 Elf_External_Phdr *dst;
313 {
314 /* note that all elements of dst are *arrays of unsigned char* already... */
315 bfd_h_put_32 (abfd, src->p_type, dst->p_type);
316 put_word (abfd, src->p_offset, dst->p_offset);
317 put_word (abfd, src->p_vaddr, dst->p_vaddr);
318 put_word (abfd, src->p_paddr, dst->p_paddr);
319 put_word (abfd, src->p_filesz, dst->p_filesz);
320 put_word (abfd, src->p_memsz, dst->p_memsz);
321 bfd_h_put_32 (abfd, src->p_flags, dst->p_flags);
322 put_word (abfd, src->p_align, dst->p_align);
323 }
324
325 /* Translate an ELF reloc from external format to internal format. */
326 static INLINE void
327 elf_swap_reloc_in (abfd, src, dst)
328 bfd *abfd;
329 Elf_External_Rel *src;
330 Elf_Internal_Rel *dst;
331 {
332 dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
333 dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
334 }
335
336 static INLINE void
337 elf_swap_reloca_in (abfd, src, dst)
338 bfd *abfd;
339 Elf_External_Rela *src;
340 Elf_Internal_Rela *dst;
341 {
342 dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
343 dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
344 dst->r_addend = get_word (abfd, (bfd_byte *) src->r_addend);
345 }
346
347 /* Translate an ELF reloc from internal format to external format. */
348 static INLINE void
349 elf_swap_reloc_out (abfd, src, dst)
350 bfd *abfd;
351 Elf_Internal_Rel *src;
352 Elf_External_Rel *dst;
353 {
354 put_word (abfd, src->r_offset, dst->r_offset);
355 put_word (abfd, src->r_info, dst->r_info);
356 }
357
358 static INLINE void
359 elf_swap_reloca_out (abfd, src, dst)
360 bfd *abfd;
361 Elf_Internal_Rela *src;
362 Elf_External_Rela *dst;
363 {
364 put_word (abfd, src->r_offset, dst->r_offset);
365 put_word (abfd, src->r_info, dst->r_info);
366 put_word (abfd, src->r_addend, dst->r_addend);
367 }
368 \f
369
370
371 /* String table creation/manipulation routines */
372
373 static struct strtab *
374 bfd_new_strtab (abfd)
375 bfd *abfd;
376 {
377 struct strtab *ss;
378
379 ss = (struct strtab *) malloc (sizeof (struct strtab));
380 if (!ss)
381 {
382 bfd_set_error (bfd_error_no_memory);
383 return NULL;
384 }
385 ss->tab = malloc (1);
386 if (!ss->tab)
387 {
388 bfd_set_error (bfd_error_no_memory);
389 return NULL;
390 }
391 *ss->tab = 0;
392 ss->nentries = 0;
393 ss->length = 1;
394
395 return ss;
396 }
397
398 static int
399 bfd_add_to_strtab (abfd, ss, str)
400 bfd *abfd;
401 struct strtab *ss;
402 CONST char *str;
403 {
404 /* should search first, but for now: */
405 /* include the trailing NUL */
406 int ln = strlen (str) + 1;
407
408 /* should this be using obstacks? */
409 ss->tab = realloc (ss->tab, ss->length + ln);
410
411 BFD_ASSERT (ss->tab != 0); /* FIXME */
412 strcpy (ss->tab + ss->length, str);
413 ss->nentries++;
414 ss->length += ln;
415
416 return ss->length - ln;
417 }
418
419 static int
420 bfd_add_2_to_strtab (abfd, ss, str, str2)
421 bfd *abfd;
422 struct strtab *ss;
423 char *str;
424 CONST char *str2;
425 {
426 /* should search first, but for now: */
427 /* include the trailing NUL */
428 int ln = strlen (str) + strlen (str2) + 1;
429
430 /* should this be using obstacks? */
431 if (ss->length)
432 ss->tab = realloc (ss->tab, ss->length + ln);
433 else
434 ss->tab = malloc (ln);
435
436 BFD_ASSERT (ss->tab != 0); /* FIXME */
437 strcpy (ss->tab + ss->length, str);
438 strcpy (ss->tab + ss->length + strlen (str), str2);
439 ss->nentries++;
440 ss->length += ln;
441
442 return ss->length - ln;
443 }
444 \f
445
446 /* ELF .o/exec file reading */
447
448 /* Create a new bfd section from an ELF section header. */
449
450 static boolean
451 bfd_section_from_shdr (abfd, shindex)
452 bfd *abfd;
453 unsigned int shindex;
454 {
455 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
456 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
457 asection *newsect;
458 char *name;
459
460 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
461
462 switch (hdr->sh_type)
463 {
464
465 case SHT_NULL:
466 /* inactive section. Throw it away. */
467 return true;
468
469 case SHT_PROGBITS:
470 case SHT_DYNAMIC:
471 /* Bits that get saved. This one is real. */
472 if (!hdr->rawdata)
473 {
474 newsect = bfd_make_section (abfd, name);
475 if (newsect != NULL)
476 {
477 newsect->filepos = hdr->sh_offset; /* so we can read back the bits */
478 newsect->flags |= SEC_HAS_CONTENTS;
479 newsect->vma = hdr->sh_addr;
480 newsect->_raw_size = hdr->sh_size;
481 newsect->alignment_power = bfd_log2 (hdr->sh_addralign);
482
483 if (hdr->sh_flags & SHF_ALLOC)
484 {
485 newsect->flags |= SEC_ALLOC;
486 newsect->flags |= SEC_LOAD;
487 }
488
489 if (!(hdr->sh_flags & SHF_WRITE))
490 newsect->flags |= SEC_READONLY;
491
492 if (hdr->sh_flags & SHF_EXECINSTR)
493 newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */
494 else if (newsect->flags & SEC_ALLOC)
495 newsect->flags |= SEC_DATA;
496
497 /* The debugging sections appear to recognized only by
498 name. */
499 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
500 || strncmp (name, ".line", sizeof ".line" - 1) == 0
501 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
502 newsect->flags |= SEC_DEBUGGING;
503
504 hdr->rawdata = (void *) newsect;
505 }
506 else
507 hdr->rawdata = (void *) bfd_get_section_by_name (abfd, name);
508 }
509 return true;
510
511 case SHT_NOBITS:
512 /* Bits that get saved. This one is real. */
513 if (!hdr->rawdata)
514 {
515 newsect = bfd_make_section (abfd, name);
516 if (newsect != NULL)
517 {
518 newsect->vma = hdr->sh_addr;
519 newsect->_raw_size = hdr->sh_size;
520 newsect->filepos = hdr->sh_offset; /* fake */
521 newsect->alignment_power = bfd_log2 (hdr->sh_addralign);
522 if (hdr->sh_flags & SHF_ALLOC)
523 newsect->flags |= SEC_ALLOC;
524
525 if (!(hdr->sh_flags & SHF_WRITE))
526 newsect->flags |= SEC_READONLY;
527
528 /* FIXME: This section is empty. Does it really make
529 sense to set SEC_CODE for it? */
530 if (hdr->sh_flags & SHF_EXECINSTR)
531 newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */
532
533 hdr->rawdata = (void *) newsect;
534 }
535 }
536 return true;
537
538 case SHT_SYMTAB: /* A symbol table */
539 if (elf_onesymtab (abfd) == shindex)
540 return true;
541
542 BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
543 BFD_ASSERT (elf_onesymtab (abfd) == 0);
544 elf_onesymtab (abfd) = shindex;
545 elf_tdata (abfd)->symtab_hdr = *hdr;
546 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_hdr;
547 abfd->flags |= HAS_SYMS;
548 return true;
549
550 case SHT_STRTAB: /* A string table */
551 if (hdr->rawdata)
552 return true;
553 if (ehdr->e_shstrndx == shindex)
554 {
555 elf_tdata (abfd)->shstrtab_hdr = *hdr;
556 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
557 hdr->rawdata = (PTR) & elf_tdata (abfd)->shstrtab_hdr;
558 return true;
559 }
560 {
561 unsigned int i;
562
563 for (i = 1; i < ehdr->e_shnum; i++)
564 {
565 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
566 if (hdr2->sh_link == shindex)
567 {
568 bfd_section_from_shdr (abfd, i);
569 if (elf_onesymtab (abfd) == i)
570 {
571 elf_tdata (abfd)->strtab_hdr = *hdr;
572 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
573 return true;
574 }
575 #if 0 /* Not handling other string tables specially right now. */
576 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
577 /* We have a strtab for some random other section. */
578 newsect = (asection *) hdr2->rawdata;
579 if (!newsect)
580 break;
581 hdr->rawdata = (PTR) newsect;
582 hdr2 = &elf_section_data (newsect)->str_hdr;
583 *hdr2 = *hdr;
584 elf_elfsections (abfd)[shindex] = hdr2;
585 #endif
586 }
587 }
588 }
589
590 newsect = bfd_make_section (abfd, name);
591 if (newsect)
592 {
593 newsect->flags = SEC_HAS_CONTENTS;
594 hdr->rawdata = (PTR) newsect;
595 newsect->_raw_size = hdr->sh_size;
596 newsect->alignment_power = bfd_log2 (hdr->sh_addralign);
597 newsect->vma = hdr->sh_addr;
598 newsect->filepos = hdr->sh_offset;
599
600 if (hdr->sh_flags & SHF_ALLOC)
601 newsect->flags |= SEC_ALLOC | SEC_LOAD;
602 if (!(hdr->sh_flags & SHF_WRITE))
603 newsect->flags |= SEC_READONLY;
604 if (hdr->sh_flags & SHF_EXECINSTR)
605 newsect->flags |= SEC_CODE;
606 else if (newsect->flags & SEC_ALLOC)
607 newsect->flags |= SEC_DATA;
608
609 /* Check for debugging string tables. */
610 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
611 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
612 newsect->flags |= SEC_DEBUGGING;
613 }
614
615 return true;
616
617 case SHT_REL:
618 case SHT_RELA:
619 /* *These* do a lot of work -- but build no sections!
620 The spec says there can be multiple strtabs, but only one symtab,
621 but there can be lots of REL* sections. */
622 /* FIXME: The above statement is wrong! There are typically at least
623 two symbol tables in a dynamically linked executable, ".dynsym"
624 which is the dynamic linkage symbol table and ".symtab", which is
625 the "traditional" symbol table. -fnf */
626
627 {
628 asection *target_sect;
629 Elf_Internal_Shdr *hdr2;
630 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
631
632 /* Don't allow REL relocations on a machine that uses RELA and
633 vice versa. */
634 /* @@ Actually, the generic ABI does suggest that both might be
635 used in one file. But the four ABI Processor Supplements I
636 have access to right now all specify that only one is used on
637 each of those architectures. It's conceivable that, e.g., a
638 bunch of absolute 32-bit relocs might be more compact in REL
639 form even on a RELA machine... */
640 BFD_ASSERT (!(use_rela_p && (hdr->sh_type == SHT_REL)));
641 BFD_ASSERT (!(!use_rela_p && (hdr->sh_type == SHT_RELA)));
642 BFD_ASSERT (hdr->sh_entsize ==
643 (use_rela_p
644 ? sizeof (Elf_External_Rela)
645 : sizeof (Elf_External_Rel)));
646
647 bfd_section_from_shdr (abfd, hdr->sh_info); /* target */
648 bfd_section_from_shdr (abfd, hdr->sh_link); /* symbol table */
649 target_sect = section_from_elf_index (abfd, hdr->sh_info);
650 if (target_sect == NULL
651 || elf_section_data (target_sect) == NULL)
652 return false;
653
654 hdr2 = &elf_section_data (target_sect)->rel_hdr;
655 *hdr2 = *hdr;
656 elf_elfsections (abfd)[shindex] = hdr2;
657 target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
658 target_sect->flags |= SEC_RELOC;
659 target_sect->relocation = 0;
660 target_sect->rel_filepos = hdr->sh_offset;
661 abfd->flags |= HAS_RELOC;
662 return true;
663 }
664 break;
665
666 case SHT_HASH:
667 case SHT_DYNSYM: /* could treat this like symtab... */
668 #if 0
669 fprintf (stderr, "Dynamic Linking sections not yet supported.\n");
670 BFD_FAIL ();
671 #endif
672 break;
673
674 case SHT_NOTE:
675 #if 0
676 fprintf (stderr, "Note Sections not yet supported.\n");
677 BFD_FAIL ();
678 #endif
679 break;
680
681 case SHT_SHLIB:
682 #if 0
683 fprintf (stderr, "SHLIB Sections not supported (and non conforming.)\n");
684 #endif
685 return true;
686
687 default:
688 /* Check for any processor-specific section types. */
689 {
690 struct elf_backend_data *bed = get_elf_backend_data (abfd);
691
692 if (bed->elf_backend_section_from_shdr)
693 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
694 }
695 break;
696 }
697
698 return true;
699 }
700
701 boolean
702 elf_new_section_hook (abfd, sec)
703 bfd *abfd
704 ;
705 asection *sec;
706 {
707 struct bfd_elf_section_data *sdata;
708
709 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
710 if (!sdata)
711 {
712 bfd_set_error (bfd_error_no_memory);
713 return false;
714 }
715 sec->used_by_bfd = (PTR) sdata;
716 memset (sdata, 0, sizeof (*sdata));
717 return true;
718 }
719
720 /* Create a new bfd section from an ELF program header.
721
722 Since program segments have no names, we generate a synthetic name
723 of the form segment<NUM>, where NUM is generally the index in the
724 program header table. For segments that are split (see below) we
725 generate the names segment<NUM>a and segment<NUM>b.
726
727 Note that some program segments may have a file size that is different than
728 (less than) the memory size. All this means is that at execution the
729 system must allocate the amount of memory specified by the memory size,
730 but only initialize it with the first "file size" bytes read from the
731 file. This would occur for example, with program segments consisting
732 of combined data+bss.
733
734 To handle the above situation, this routine generates TWO bfd sections
735 for the single program segment. The first has the length specified by
736 the file size of the segment, and the second has the length specified
737 by the difference between the two sizes. In effect, the segment is split
738 into it's initialized and uninitialized parts.
739
740 */
741
742 static boolean
743 bfd_section_from_phdr (abfd, hdr, index)
744 bfd *abfd;
745 Elf_Internal_Phdr *hdr;
746 int index;
747 {
748 asection *newsect;
749 char *name;
750 char namebuf[64];
751 int split;
752
753 split = ((hdr->p_memsz > 0) &&
754 (hdr->p_filesz > 0) &&
755 (hdr->p_memsz > hdr->p_filesz));
756 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
757 name = bfd_alloc (abfd, strlen (namebuf) + 1);
758 if (!name)
759 {
760 bfd_set_error (bfd_error_no_memory);
761 return false;
762 }
763 strcpy (name, namebuf);
764 newsect = bfd_make_section (abfd, name);
765 newsect->vma = hdr->p_vaddr;
766 newsect->_raw_size = hdr->p_filesz;
767 newsect->filepos = hdr->p_offset;
768 newsect->flags |= SEC_HAS_CONTENTS;
769 if (hdr->p_type == PT_LOAD)
770 {
771 newsect->flags |= SEC_ALLOC;
772 newsect->flags |= SEC_LOAD;
773 if (hdr->p_flags & PF_X)
774 {
775 /* FIXME: all we known is that it has execute PERMISSION,
776 may be data. */
777 newsect->flags |= SEC_CODE;
778 }
779 }
780 if (!(hdr->p_flags & PF_W))
781 {
782 newsect->flags |= SEC_READONLY;
783 }
784
785 if (split)
786 {
787 sprintf (namebuf, "segment%db", index);
788 name = bfd_alloc (abfd, strlen (namebuf) + 1);
789 if (!name)
790 {
791 bfd_set_error (bfd_error_no_memory);
792 return false;
793 }
794 strcpy (name, namebuf);
795 newsect = bfd_make_section (abfd, name);
796 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
797 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
798 if (hdr->p_type == PT_LOAD)
799 {
800 newsect->flags |= SEC_ALLOC;
801 if (hdr->p_flags & PF_X)
802 newsect->flags |= SEC_CODE;
803 }
804 if (!(hdr->p_flags & PF_W))
805 newsect->flags |= SEC_READONLY;
806 }
807
808 return true;
809 }
810
811 /* Begin processing a given object.
812
813 First we validate the file by reading in the ELF header and checking
814 the magic number. */
815
816 static INLINE boolean
817 elf_file_p (x_ehdrp)
818 Elf_External_Ehdr *x_ehdrp;
819 {
820 return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
821 && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
822 && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
823 && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
824 }
825
826 /* Check to see if the file associated with ABFD matches the target vector
827 that ABFD points to.
828
829 Note that we may be called several times with the same ABFD, but different
830 target vectors, most of which will not match. We have to avoid leaving
831 any side effects in ABFD, or any data it points to (like tdata), if the
832 file does not match the target vector.
833
834 FIXME: There is memory leak if we are called more than once with the same
835 ABFD, and that bfd already has tdata allocated, since we allocate more tdata
836 and the old tdata is orphaned. Since it's in the bfd obstack, there isn't
837 much we can do about this except possibly rewrite the code. There are
838 also other bfd_allocs that may be the source of memory leaks as well. */
839
840 bfd_target *
841 elf_object_p (abfd)
842 bfd *abfd;
843 {
844 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
845 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
846 Elf_External_Shdr x_shdr; /* Section header table entry, external form */
847 Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
848 unsigned int shindex;
849 char *shstrtab; /* Internal copy of section header stringtab */
850 struct elf_backend_data *ebd;
851 struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd);
852
853 /* Read in the ELF header in external format. */
854
855 if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
856 {
857 if (bfd_get_error () != bfd_error_system_call)
858 goto got_wrong_format_error;
859 else
860 goto got_no_match;
861 }
862
863 /* Now check to see if we have a valid ELF file, and one that BFD can
864 make use of. The magic number must match, the address size ('class')
865 and byte-swapping must match our XVEC entry, and it must have a
866 section header table (FIXME: See comments re sections at top of this
867 file). */
868
869 if ((elf_file_p (&x_ehdr) == false) ||
870 (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT) ||
871 (x_ehdr.e_ident[EI_CLASS] != ELFCLASS))
872 goto got_wrong_format_error;
873
874 /* Check that file's byte order matches xvec's */
875 switch (x_ehdr.e_ident[EI_DATA])
876 {
877 case ELFDATA2MSB: /* Big-endian */
878 if (!abfd->xvec->header_byteorder_big_p)
879 goto got_wrong_format_error;
880 break;
881 case ELFDATA2LSB: /* Little-endian */
882 if (abfd->xvec->header_byteorder_big_p)
883 goto got_wrong_format_error;
884 break;
885 case ELFDATANONE: /* No data encoding specified */
886 default: /* Unknown data encoding specified */
887 goto got_wrong_format_error;
888 }
889
890 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
891 the tdata pointer in the bfd. FIXME: memory leak, see above. */
892
893 elf_tdata (abfd) =
894 (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
895 if (elf_tdata (abfd) == NULL)
896 goto got_no_memory_error;
897
898 /* Now that we know the byte order, swap in the rest of the header */
899 i_ehdrp = elf_elfheader (abfd);
900 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
901 #if DEBUG & 1
902 elf_debug_file (i_ehdrp);
903 #endif
904
905 /* If there is no section header table, we're hosed. */
906 if (i_ehdrp->e_shoff == 0)
907 goto got_wrong_format_error;
908
909 /* As a simple sanity check, verify that the what BFD thinks is the
910 size of each section header table entry actually matches the size
911 recorded in the file. */
912 if (i_ehdrp->e_shentsize != sizeof (x_shdr))
913 goto got_wrong_format_error;
914
915 ebd = get_elf_backend_data (abfd);
916
917 /* Check that the ELF e_machine field matches what this particular
918 BFD format expects. */
919 if (ebd->elf_machine_code != i_ehdrp->e_machine)
920 {
921 bfd_target **target_ptr;
922
923 if (ebd->elf_machine_code != EM_NONE)
924 goto got_wrong_format_error;
925
926 /* This is the generic ELF target. Let it match any ELF target
927 for which we do not have a specific backend. */
928 for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
929 {
930 struct elf_backend_data *back;
931
932 if ((*target_ptr)->flavour != bfd_target_elf_flavour)
933 continue;
934 back = (struct elf_backend_data *) (*target_ptr)->backend_data;
935 if (back->elf_machine_code == i_ehdrp->e_machine)
936 {
937 /* target_ptr is an ELF backend which matches this
938 object file, so reject the generic ELF target. */
939 goto got_wrong_format_error;
940 }
941 }
942 }
943
944
945 /* Set the flags and architecture before calling the backend so that
946 it can override them. */
947 if (i_ehdrp->e_type == ET_EXEC)
948 abfd->flags |= EXEC_P;
949 else if (i_ehdrp->e_type == ET_DYN)
950 abfd->flags |= DYNAMIC;
951
952 bfd_default_set_arch_mach (abfd, ebd->arch, 0);
953
954 /* Remember the entry point specified in the ELF file header. */
955 bfd_get_start_address (abfd) = i_ehdrp->e_entry;
956
957 /* Let the backend double check the format and override global
958 information. */
959 if (ebd->elf_backend_object_p)
960 {
961 if ((*ebd->elf_backend_object_p) (abfd) == false)
962 goto got_wrong_format_error;
963 }
964
965 /* Allocate space for a copy of the section header table in
966 internal form, seek to the section header table in the file,
967 read it in, and convert it to internal form. */
968 i_shdrp = (Elf_Internal_Shdr *)
969 bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum);
970 elf_elfsections (abfd) =
971 (Elf_Internal_Shdr **) bfd_alloc (abfd, sizeof (i_shdrp) * i_ehdrp->e_shnum);
972 if (!i_shdrp || !elf_elfsections (abfd))
973 goto got_no_memory_error;
974 if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) == -1)
975 goto got_no_match;
976 for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++)
977 {
978 if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd) != sizeof (x_shdr))
979 goto got_no_match;
980 elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
981 elf_elfsections (abfd)[shindex] = i_shdrp + shindex;
982
983 /* If this is a .dynamic section, mark the object file as being
984 dynamically linked. */
985 if (i_shdrp[shindex].sh_type == SHT_DYNAMIC)
986 abfd->flags |= DYNAMIC;
987 }
988 if (i_ehdrp->e_shstrndx)
989 {
990 bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx);
991 }
992
993 /* Read in the string table containing the names of the sections. We
994 will need the base pointer to this table later. */
995 /* We read this inline now, so that we don't have to go through
996 bfd_section_from_shdr with it (since this particular strtab is
997 used to find all of the ELF section names.) */
998
999 shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
1000 if (!shstrtab)
1001 goto got_wrong_format_error;
1002
1003 /* Once all of the section headers have been read and converted, we
1004 can start processing them. Note that the first section header is
1005 a dummy placeholder entry, so we ignore it.
1006
1007 We also watch for the symbol table section and remember the file
1008 offset and section size for both the symbol table section and the
1009 associated string table section. */
1010
1011 for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
1012 {
1013 bfd_section_from_shdr (abfd, shindex);
1014 }
1015
1016 return (abfd->xvec);
1017
1018 /* If we are going to use goto's to avoid duplicating error setting
1019 and return(NULL) code, then this at least makes it more maintainable. */
1020
1021 got_wrong_format_error:
1022 bfd_set_error (bfd_error_wrong_format);
1023 goto got_no_match;
1024 got_no_memory_error:
1025 bfd_set_error (bfd_error_no_memory);
1026 goto got_no_match;
1027 got_no_match:
1028 elf_tdata (abfd) = preserved_tdata;
1029 return (NULL);
1030 }
1031 \f
1032
1033 /* ELF .o/exec file writing */
1034
1035 /* Takes a bfd and a symbol, returns a pointer to the elf specific area
1036 of the symbol if there is one. */
1037 static INLINE elf_symbol_type *
1038 elf_symbol_from (ignore_abfd, symbol)
1039 bfd *ignore_abfd;
1040 asymbol *symbol;
1041 {
1042 if (symbol->the_bfd->xvec->flavour != bfd_target_elf_flavour)
1043 return 0;
1044
1045 if (symbol->the_bfd->tdata.elf_obj_data == (struct elf_obj_tdata *) NULL)
1046 return 0;
1047
1048 return (elf_symbol_type *) symbol;
1049 }
1050
1051 /* Create ELF output from BFD sections.
1052
1053 Essentially, just create the section header and forget about the program
1054 header for now. */
1055
1056 static void
1057 elf_make_sections (abfd, asect, obj)
1058 bfd *abfd;
1059 asection *asect;
1060 PTR obj;
1061 {
1062 /* most of what is in bfd_shdr_from_section goes in here... */
1063 /* and all of these sections generate at *least* one ELF section. */
1064 Elf_Internal_Shdr *this_hdr;
1065 this_hdr = &elf_section_data (asect)->this_hdr;
1066
1067 this_hdr->sh_addr = asect->vma;
1068 this_hdr->sh_size = asect->_raw_size;
1069 /* contents already set by elf_set_section_contents */
1070
1071 if (asect->flags & SEC_RELOC)
1072 {
1073 /* emit a reloc section, and thus strtab and symtab... */
1074 Elf_Internal_Shdr *rela_hdr;
1075 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1076
1077 rela_hdr = &elf_section_data (asect)->rel_hdr;
1078
1079 /* orelocation has the data, reloc_count has the count... */
1080 if (use_rela_p)
1081 {
1082 rela_hdr->sh_type = SHT_RELA;
1083 rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
1084 }
1085 else
1086 /* REL relocations */
1087 {
1088 rela_hdr->sh_type = SHT_REL;
1089 rela_hdr->sh_entsize = sizeof (Elf_External_Rel);
1090 }
1091 rela_hdr->sh_flags = 0;
1092 rela_hdr->sh_addr = 0;
1093 rela_hdr->sh_offset = 0;
1094
1095 /* FIXME: Systems I've checked use an alignment of 4, but it is
1096 possible that some systems use a different alignment. */
1097 rela_hdr->sh_addralign = 4;
1098
1099 rela_hdr->size = 0;
1100 }
1101 if (asect->flags & SEC_ALLOC)
1102 {
1103 this_hdr->sh_flags |= SHF_ALLOC;
1104 if (asect->flags & SEC_LOAD)
1105 {
1106 /* @@ Do something with sh_type? */
1107 }
1108 }
1109 else
1110 {
1111 /* If this section is not part of the program image during
1112 execution, leave the address fields at 0. */
1113 this_hdr->sh_addr = 0;
1114 asect->vma = 0;
1115 }
1116 if (!(asect->flags & SEC_READONLY))
1117 this_hdr->sh_flags |= SHF_WRITE;
1118
1119 if (asect->flags & SEC_CODE)
1120 this_hdr->sh_flags |= SHF_EXECINSTR;
1121 }
1122
1123 void
1124 write_relocs (abfd, sec, xxx)
1125 bfd *abfd;
1126 asection *sec;
1127 PTR xxx;
1128 {
1129 Elf_Internal_Shdr *rela_hdr;
1130 Elf_External_Rela *outbound_relocas;
1131 Elf_External_Rel *outbound_relocs;
1132 int idx;
1133 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1134 asymbol *last_sym = 0;
1135 int last_sym_idx = 9999999; /* should always be written before use */
1136
1137 if ((sec->flags & SEC_RELOC) == 0)
1138 return;
1139 /* Flags are sometimes inconsistent. */
1140 if (sec->reloc_count == 0)
1141 return;
1142
1143 rela_hdr = &elf_section_data (sec)->rel_hdr;
1144
1145 rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
1146 rela_hdr->contents = (void *) bfd_alloc (abfd, rela_hdr->sh_size);
1147 if (!rela_hdr->contents)
1148 {
1149 bfd_set_error (bfd_error_no_memory);
1150 abort (); /* FIXME */
1151 }
1152
1153 /* orelocation has the data, reloc_count has the count... */
1154 if (use_rela_p)
1155 {
1156 outbound_relocas = (Elf_External_Rela *) rela_hdr->contents;
1157
1158 for (idx = 0; idx < sec->reloc_count; idx++)
1159 {
1160 Elf_Internal_Rela dst_rela;
1161 Elf_External_Rela *src_rela;
1162 arelent *ptr;
1163 asymbol *sym;
1164 int n;
1165
1166 ptr = sec->orelocation[idx];
1167 src_rela = outbound_relocas + idx;
1168 if (!(abfd->flags & EXEC_P))
1169 dst_rela.r_offset = ptr->address - sec->vma;
1170 else
1171 dst_rela.r_offset = ptr->address;
1172
1173 sym = *ptr->sym_ptr_ptr;
1174
1175 /* If SYM is a section symbol for an input section, which
1176 has been combined with other similar input sections (ld -r),
1177 then adjust the addend by the output_offset of sym->section.
1178
1179 Apparently elf_symbol_from_bfd_symbol doesn't provide a 1:1
1180 mapping from bfd symbol to elf symbols in this case. */
1181 if ((sym->flags & BSF_SECTION_SYM)
1182 && sym->section)
1183 ptr->addend += sym->section->output_offset;
1184
1185 if (sym == last_sym)
1186 n = last_sym_idx;
1187 else
1188 {
1189 last_sym = sym;
1190 last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
1191 }
1192 dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1193
1194 dst_rela.r_addend = ptr->addend;
1195 elf_swap_reloca_out (abfd, &dst_rela, src_rela);
1196 }
1197 }
1198 else
1199 /* REL relocations */
1200 {
1201 outbound_relocs = (Elf_External_Rel *) rela_hdr->contents;
1202
1203 for (idx = 0; idx < sec->reloc_count; idx++)
1204 {
1205 Elf_Internal_Rel dst_rel;
1206 Elf_External_Rel *src_rel;
1207 arelent *ptr;
1208 int n;
1209 asymbol *sym;
1210
1211 ptr = sec->orelocation[idx];
1212 sym = *ptr->sym_ptr_ptr;
1213 src_rel = outbound_relocs + idx;
1214 if (!(abfd->flags & EXEC_P))
1215 dst_rel.r_offset = ptr->address - sec->vma;
1216 else
1217 dst_rel.r_offset = ptr->address;
1218
1219 if (sym == last_sym)
1220 n = last_sym_idx;
1221 else
1222 {
1223 last_sym = sym;
1224 last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
1225 }
1226 dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type);
1227
1228 elf_swap_reloc_out (abfd, &dst_rel, src_rel);
1229 }
1230 }
1231 }
1232
1233 static void
1234 fix_up_strtabs (abfd, asect, obj)
1235 bfd *abfd;
1236 asection *asect;
1237 PTR obj;
1238 {
1239 Elf_Internal_Shdr *this_hdr = &elf_section_data (asect)->this_hdr;
1240 int this_idx = elf_section_data (asect)->this_idx;
1241
1242 /* @@ Check flags! */
1243 if (!strncmp (asect->name, ".stab", 5)
1244 && !strcmp ("str", asect->name + strlen (asect->name) - 3))
1245 {
1246 size_t len = strlen (asect->name) + 1;
1247 char *s = (char *) malloc (len);
1248 if (s == NULL)
1249 /* FIXME: Should deal more gracefully with errors. */
1250 abort ();
1251 strcpy (s, asect->name);
1252 s[len - 4] = 0;
1253 asect = bfd_get_section_by_name (abfd, s);
1254 free (s);
1255 if (!asect)
1256 abort ();
1257 elf_section_data (asect)->this_hdr.sh_link = this_idx;
1258 /* @@ Assuming 32 bits! */
1259 elf_section_data (asect)->this_hdr.sh_entsize = 0xc;
1260
1261 this_hdr->sh_type = SHT_STRTAB;
1262 }
1263 }
1264
1265 static void
1266 elf_fake_sections (abfd, asect, obj)
1267 bfd *abfd;
1268 asection *asect;
1269 PTR obj;
1270 {
1271 /* most of what is in bfd_shdr_from_section goes in here... */
1272 /* and all of these sections generate at *least* one ELF section. */
1273
1274 Elf_Internal_Shdr *this_hdr;
1275 this_hdr = &elf_section_data (asect)->this_hdr;
1276 this_hdr->sh_name =
1277 bfd_add_to_strtab (abfd, elf_shstrtab (abfd), asect->name);
1278 /* We need to log the type *now* so that elf_section_from_bfd_section
1279 can find us... have to set rawdata too. */
1280 this_hdr->rawdata = (void *) asect;
1281 this_hdr->sh_addralign = 1 << asect->alignment_power;
1282 if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD))
1283 this_hdr->sh_type = SHT_PROGBITS;
1284 else if ((asect->flags & SEC_ALLOC) && ((asect->flags & SEC_LOAD) == 0))
1285 {
1286 BFD_ASSERT (strcmp (asect->name, ".bss") == 0
1287 || strcmp (asect->name, ".sbss") == 0);
1288 this_hdr->sh_type = SHT_NOBITS;
1289 }
1290 /* FIXME I am not sure how to detect a .note section from the flags
1291 word of an `asection'. */
1292 else if (!strcmp (asect->name, ".note"))
1293 this_hdr->sh_type = SHT_NOTE;
1294 else
1295 this_hdr->sh_type = SHT_PROGBITS;
1296
1297 this_hdr->sh_flags = 0;
1298 this_hdr->sh_addr = 0;
1299 this_hdr->sh_size = 0;
1300 this_hdr->sh_entsize = 0;
1301 this_hdr->sh_info = 0;
1302 this_hdr->sh_link = 0;
1303 this_hdr->sh_offset = 0;
1304 this_hdr->size = 0;
1305
1306 /* Now, check for processor-specific section types. */
1307 {
1308 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1309
1310 if (bed->elf_backend_fake_sections)
1311 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1312 }
1313
1314 {
1315 /* Emit a strtab and symtab, and possibly a reloc section. */
1316 Elf_Internal_Shdr *rela_hdr;
1317
1318 /* Note that only one symtab is used, so just remember it
1319 for now. */
1320
1321 if (asect->flags & SEC_RELOC)
1322 {
1323 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1324
1325 rela_hdr = &elf_section_data (asect)->rel_hdr;
1326 rela_hdr->sh_name =
1327 bfd_add_2_to_strtab (abfd, elf_shstrtab (abfd),
1328 use_rela_p ? ".rela" : ".rel",
1329 asect->name);
1330 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1331 rela_hdr->sh_entsize = (use_rela_p
1332 ? sizeof (Elf_External_Rela)
1333 : sizeof (Elf_External_Rel));
1334
1335 rela_hdr->sh_flags = 0;
1336 rela_hdr->sh_addr = 0;
1337 rela_hdr->sh_size = 0;
1338 rela_hdr->sh_offset = 0;
1339
1340 /* FIXME: Systems I've checked use an alignment of 4, but some
1341 systems may use a different alignment. */
1342 rela_hdr->sh_addralign = 4;
1343
1344 rela_hdr->size = 0;
1345 }
1346 }
1347 if (asect->flags & SEC_ALLOC)
1348 {
1349 this_hdr->sh_flags |= SHF_ALLOC;
1350 if (asect->flags & SEC_LOAD)
1351 {
1352 /* @@ Do something with sh_type? */
1353 }
1354 }
1355 if (!(asect->flags & SEC_READONLY))
1356 this_hdr->sh_flags |= SHF_WRITE;
1357 if (asect->flags & SEC_CODE)
1358 this_hdr->sh_flags |= SHF_EXECINSTR;
1359 }
1360
1361 /* Map symbol from it's internal number to the external number, moving
1362 all local symbols to be at the head of the list. */
1363
1364 static INLINE int
1365 sym_is_global (abfd, sym)
1366 bfd *abfd;
1367 asymbol *sym;
1368 {
1369 /* If the backend has a special mapping, use it. */
1370 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1371 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1372 (abfd, sym));
1373
1374 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
1375 {
1376 if (sym->flags & BSF_LOCAL)
1377 abort ();
1378 return 1;
1379 }
1380 if (sym->section == 0)
1381 {
1382 /* Is this valid? */
1383 abort ();
1384
1385 return 1;
1386 }
1387 if (sym->section == &bfd_und_section)
1388 return 1;
1389 if (bfd_is_com_section (sym->section))
1390 return 1;
1391 if (sym->flags & (BSF_LOCAL | BSF_SECTION_SYM | BSF_FILE))
1392 return 0;
1393 return 0;
1394 }
1395
1396 static boolean
1397 elf_map_symbols (abfd)
1398 bfd *abfd;
1399 {
1400 int symcount = bfd_get_symcount (abfd);
1401 asymbol **syms = bfd_get_outsymbols (abfd);
1402 asymbol **sect_syms;
1403 int num_locals = 0;
1404 int num_globals = 0;
1405 int num_locals2 = 0;
1406 int num_globals2 = 0;
1407 int max_index = 0;
1408 int num_sections = 0;
1409 Elf_Sym_Extra *sym_extra;
1410 int idx;
1411 asection *asect;
1412
1413 #ifdef DEBUG
1414 fprintf (stderr, "elf_map_symbols\n");
1415 fflush (stderr);
1416 #endif
1417
1418 /* Add local symbols for each section for which there are relocs.
1419 FIXME: How can we tell which sections have relocs at this point?
1420 Will reloc_count always be accurate? Actually, I think most ELF
1421 targets create section symbols for all sections anyhow. */
1422 for (asect = abfd->sections; asect; asect = asect->next)
1423 {
1424 if (max_index < asect->index)
1425 max_index = asect->index;
1426 }
1427
1428 max_index++;
1429 elf_num_section_syms (abfd) = max_index;
1430 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1431 elf_section_syms (abfd) = sect_syms;
1432
1433 if (sect_syms == 0)
1434 {
1435 bfd_set_error (bfd_error_no_memory);
1436 return false;
1437 }
1438
1439 for (asect = abfd->sections; asect; asect = asect->next)
1440 {
1441 asymbol *sym = bfd_make_empty_symbol (abfd);
1442 if (!sym)
1443 {
1444 bfd_set_error (bfd_error_no_memory);
1445 return false;
1446 }
1447 sym->the_bfd = abfd;
1448 sym->name = asect->name;
1449 sym->value = asect->vma;
1450 sym->flags = BSF_SECTION_SYM;
1451 sym->section = asect;
1452 sect_syms[asect->index] = sym;
1453 num_sections++;
1454 #ifdef DEBUG
1455 fprintf (stderr,
1456 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1457 asect->name, (long) asect->vma, asect->index, (long) asect);
1458 #endif
1459 }
1460
1461 if (num_sections)
1462 {
1463 if (syms)
1464 syms = (asymbol **) bfd_realloc (abfd, syms,
1465 ((symcount + num_sections + 1)
1466 * sizeof (asymbol *)));
1467 else
1468 syms = (asymbol **) bfd_alloc (abfd,
1469 (num_sections + 1) * sizeof (asymbol *));
1470 if (!syms)
1471 {
1472 bfd_set_error (bfd_error_no_memory);
1473 return false;
1474 }
1475
1476 for (asect = abfd->sections; asect; asect = asect->next)
1477 {
1478 if (sect_syms[asect->index])
1479 syms[symcount++] = sect_syms[asect->index];
1480 }
1481
1482 syms[symcount] = (asymbol *) 0;
1483 bfd_set_symtab (abfd, syms, symcount);
1484 }
1485
1486 elf_sym_extra (abfd) = sym_extra
1487 = (Elf_Sym_Extra *) bfd_alloc (abfd, symcount * sizeof (Elf_Sym_Extra));
1488 if (!sym_extra)
1489 {
1490 bfd_set_error (bfd_error_no_memory);
1491 return false;
1492 }
1493
1494 /* Identify and classify all of the symbols. */
1495 for (idx = 0; idx < symcount; idx++)
1496 {
1497 if (!sym_is_global (abfd, syms[idx]))
1498 num_locals++;
1499 else
1500 num_globals++;
1501 }
1502
1503 /* Now provide mapping information. Add +1 for skipping over the
1504 dummy symbol. */
1505 for (idx = 0; idx < symcount; idx++)
1506 {
1507 syms[idx]->udata = (PTR) & sym_extra[idx];
1508 if (!sym_is_global (abfd, syms[idx]))
1509 sym_extra[idx].elf_sym_num = 1 + num_locals2++;
1510 else
1511 sym_extra[idx].elf_sym_num = 1 + num_locals + num_globals2++;
1512 }
1513
1514 elf_num_locals (abfd) = num_locals;
1515 elf_num_globals (abfd) = num_globals;
1516 return true;
1517 }
1518
1519 static boolean assign_section_numbers ();
1520 static boolean assign_file_positions_except_relocs ();
1521
1522 static boolean
1523 elf_compute_section_file_positions (abfd)
1524 bfd *abfd;
1525 {
1526 bfd_map_over_sections (abfd, elf_fake_sections, 0);
1527
1528 if (!assign_section_numbers (abfd))
1529 return false;
1530
1531 bfd_map_over_sections (abfd, elf_make_sections, 0);
1532
1533 bfd_map_over_sections (abfd, fix_up_strtabs, 0); /* .stab/.stabstr &c */
1534
1535 if (swap_out_syms (abfd) == false)
1536 return false;
1537
1538 if (!assign_file_positions_except_relocs (abfd))
1539 return false;
1540
1541 return true;
1542 }
1543
1544 static boolean
1545 elf_write_phdrs (abfd, i_ehdrp, i_phdrp, phdr_cnt)
1546 bfd *abfd;
1547 Elf_Internal_Ehdr *i_ehdrp;
1548 Elf_Internal_Phdr *i_phdrp;
1549 unsigned short phdr_cnt;
1550 {
1551 /* first program header entry goes after the file header */
1552 int outbase = i_ehdrp->e_phoff;
1553 unsigned int i;
1554 Elf_External_Phdr x_phdr;
1555
1556 for (i = 0; i < phdr_cnt; i++)
1557 {
1558 elf_swap_phdr_out (abfd, i_phdrp + i, &x_phdr);
1559 if (bfd_seek (abfd, outbase, SEEK_SET) != 0
1560 || (bfd_write ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
1561 != sizeof (x_phdr)))
1562 return false;
1563 outbase += sizeof (x_phdr);
1564 }
1565
1566 return true;
1567 }
1568
1569 static const Elf_Internal_Shdr null_shdr;
1570
1571 /* Assign all ELF section numbers. The dummy first section is handled here
1572 too. The link/info pointers for the standard section types are filled
1573 in here too, while we're at it. (Link pointers for .stab sections are
1574 not filled in here.) */
1575 static boolean
1576 assign_section_numbers (abfd)
1577 bfd *abfd;
1578 {
1579 struct elf_obj_tdata *t = elf_tdata (abfd);
1580 asection *sec;
1581 int section_number = 1;
1582 int i;
1583 Elf_Internal_Shdr **i_shdrp;
1584
1585 t->shstrtab_hdr.sh_size = elf_shstrtab (abfd)->length;
1586 t->shstrtab_hdr.contents = (void *) elf_shstrtab (abfd)->tab;
1587 shstrtab_length_fixed = 1;
1588
1589 t->shstrtab_section = section_number++;
1590 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1591 if (abfd->symcount)
1592 {
1593 t->symtab_section = section_number++;
1594 t->strtab_section = section_number++;
1595 t->symtab_hdr.sh_link = t->strtab_section;
1596 }
1597 for (sec = abfd->sections; sec; sec = sec->next)
1598 {
1599 struct bfd_elf_section_data *d = elf_section_data (sec);
1600 d->this_idx = section_number++;
1601 if (sec->flags & SEC_RELOC)
1602 {
1603 d->rel_idx = section_number++;
1604 d->rel_hdr.sh_link = t->symtab_section;
1605 d->rel_hdr.sh_info = d->this_idx;
1606 }
1607 else
1608 d->rel_idx = 0;
1609 /* No handling for per-section string tables currently. */
1610 }
1611 elf_elfheader (abfd)->e_shnum = section_number;
1612
1613 /* Set up the list of section header pointers, in agreement with the
1614 indices. */
1615 i_shdrp = (Elf_Internal_Shdr **)
1616 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *));
1617 if (!i_shdrp)
1618 {
1619 bfd_set_error (bfd_error_no_memory);
1620 return false;
1621 }
1622 elf_elfsections (abfd) = i_shdrp;
1623 for (i = 0; i < section_number; i++)
1624 i_shdrp[i] = 0;
1625
1626 i_shdrp[0] = (Elf_Internal_Shdr *) & null_shdr;
1627 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1628 if (abfd->symcount)
1629 {
1630 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1631 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1632 }
1633 for (sec = abfd->sections; sec; sec = sec->next)
1634 {
1635 struct bfd_elf_section_data *d = elf_section_data (sec);
1636 i_shdrp[d->this_idx] = &d->this_hdr;
1637 if (d->rel_idx)
1638 i_shdrp[d->rel_idx] = &d->rel_hdr;
1639 }
1640 /* Make sure we got everything.... */
1641 for (i = 0; i < section_number; i++)
1642 if (i_shdrp[i] == 0)
1643 abort ();
1644 return true;
1645 }
1646
1647 static INLINE file_ptr
1648 assign_file_position_for_section (i_shdrp, offset)
1649 Elf_Internal_Shdr *i_shdrp;
1650 file_ptr offset;
1651 {
1652 int align;
1653
1654 if (i_shdrp->sh_addralign != 0)
1655 align = i_shdrp->sh_addralign;
1656 else
1657 align = 1;
1658 i_shdrp->sh_offset = offset = BFD_ALIGN (offset, align);
1659 if (i_shdrp->rawdata != NULL)
1660 ((asection *) i_shdrp->rawdata)->filepos = offset;
1661 if (i_shdrp->sh_type != SHT_NOBITS)
1662 offset += i_shdrp->sh_size;
1663 return offset;
1664 }
1665
1666 static INLINE file_ptr
1667 align_file_position (off)
1668 file_ptr off;
1669 {
1670 return (off + FILE_ALIGN - 1) & ~(FILE_ALIGN - 1);
1671 }
1672
1673 static INLINE file_ptr
1674 assign_file_positions_for_symtab_and_strtabs (abfd, off)
1675 bfd *abfd;
1676 file_ptr off;
1677 {
1678 struct elf_obj_tdata *t = elf_tdata (abfd);
1679
1680 off = align_file_position (off);
1681 off = assign_file_position_for_section (&t->symtab_hdr, off);
1682 off = assign_file_position_for_section (&t->shstrtab_hdr, off);
1683 off = assign_file_position_for_section (&t->strtab_hdr, off);
1684 return off;
1685 }
1686
1687 struct seg_info
1688 {
1689 bfd_vma low, mem_size;
1690 file_ptr file_size;
1691 int start_pos;
1692 int sh_flags;
1693 struct seg_info *next;
1694 };
1695
1696 static boolean
1697 map_program_segments (abfd)
1698 bfd *abfd;
1699 {
1700 Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd);
1701 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
1702 Elf_Internal_Shdr *i_shdrp;
1703 Elf_Internal_Phdr *phdr;
1704 char *done = NULL;
1705 unsigned int i, n_left = 0;
1706 file_ptr lowest_offset = 0;
1707 struct seg_info *seg = NULL;
1708
1709 done = (char *) malloc (i_ehdrp->e_shnum);
1710 if (done == NULL && i_ehdrp->e_shnum != 0)
1711 {
1712 bfd_set_error (bfd_error_no_memory);
1713 goto error_return;
1714 }
1715 memset (done, 0, i_ehdrp->e_shnum);
1716 for (i = 1; i < i_ehdrp->e_shnum; i++)
1717 {
1718 i_shdrp = i_shdrpp[i];
1719 /* If it's going to be mapped in, it's been assigned a position. */
1720 if (i_shdrp->sh_offset + 1 == 0)
1721 {
1722 /* Well, not really, but we won't process it here. */
1723 done[i] = 1;
1724 continue;
1725 }
1726 if (i_shdrp->sh_offset < lowest_offset
1727 || lowest_offset == 0)
1728 lowest_offset = i_shdrp->sh_offset;
1729 /* Only interested in PROGBITS or NOBITS for generating segments. */
1730 switch (i_shdrp->sh_type)
1731 {
1732 case SHT_PROGBITS:
1733 case SHT_NOBITS:
1734 break;
1735 default:
1736 done[i] = 1;
1737 }
1738 if (!done[i])
1739 n_left++;
1740 }
1741 while (n_left)
1742 {
1743 bfd_vma lowest_vma = -1, high;
1744 int low_sec = 0;
1745 int mem_size;
1746 int file_size = 0;
1747 struct seg_info *snew;
1748 struct seg_info **s_ptr;
1749
1750 for (i = 1; i < i_ehdrp->e_shnum; i++)
1751 {
1752 i_shdrp = i_shdrpp[i];
1753 if (!done[i] && i_shdrp->sh_addr < lowest_vma)
1754 {
1755 lowest_vma = i_shdrp->sh_addr;
1756 low_sec = i;
1757 }
1758 }
1759 if (low_sec == 0)
1760 abort ();
1761 /* So now we know the lowest vma of any unassigned sections; start
1762 a segment there. */
1763 snew = (struct seg_info *) bfd_alloc (abfd, sizeof (struct seg_info));
1764 if (!snew)
1765 {
1766 bfd_set_error (bfd_error_no_memory);
1767 goto error_return;
1768 }
1769 s_ptr = &seg;
1770 while (*s_ptr != (struct seg_info *) NULL)
1771 s_ptr = &(*s_ptr)->next;
1772 *s_ptr = snew;
1773 snew->next = NULL;
1774 snew->low = lowest_vma;
1775 i_shdrp = i_shdrpp[low_sec];
1776 snew->start_pos = i_shdrp->sh_offset;
1777 snew->sh_flags = i_shdrp->sh_flags;
1778 done[low_sec] = 1, n_left--;
1779 mem_size = i_shdrp->sh_size;
1780 high = lowest_vma + i_shdrp->sh_size;
1781
1782 if (i_shdrp->sh_type == SHT_PROGBITS)
1783 file_size = i_shdrp->sh_size;
1784
1785 for (i = 1; i < i_ehdrp->e_shnum; i++)
1786 {
1787 file_ptr f1;
1788
1789 if (done[i])
1790 continue;
1791 i_shdrp = i_shdrpp[i];
1792 /* position of next byte on disk */
1793 f1 = snew->start_pos + file_size;
1794 if (i_shdrp->sh_type == SHT_PROGBITS)
1795 {
1796 if (i_shdrp->sh_offset - f1 != i_shdrp->sh_addr - high)
1797 continue;
1798 if (file_size != mem_size)
1799 break;
1800 }
1801 else
1802 /* sh_type == NOBITS */
1803 {
1804 /* If the section in question has no contents in the disk
1805 file, we really don't care where it supposedly starts.
1806 But we don't want to bother merging it into this segment
1807 if it doesn't start on this memory page. */
1808 bfd_vma page1, page2;
1809 bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1810
1811 /* page number in address space of current end of snew */
1812 page1 = (high - 1 + maxpagesize - 1) / maxpagesize;
1813 /* page number in address space of start of this section */
1814 page2 = (i_shdrp->sh_addr + maxpagesize - 1) / maxpagesize;
1815
1816 if (page1 != page2)
1817 continue;
1818 }
1819 done[i] = 1, n_left--;
1820 if (i_shdrp->sh_type == SHT_PROGBITS)
1821 file_size = i_shdrp->sh_offset + i_shdrp->sh_size - snew->start_pos;
1822 mem_size = i_shdrp->sh_addr + i_shdrp->sh_size - snew->low;
1823 high = i_shdrp->sh_addr + i_shdrp->sh_size;
1824 i = 0;
1825 }
1826 snew->file_size = file_size;
1827 snew->mem_size = mem_size;
1828 }
1829 /* Now do something with the list of segments we've built up. */
1830 {
1831 bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1832 struct seg_info *s;
1833 int n_segs = 0;
1834 int sz;
1835
1836 for (s = seg; s; s = s->next)
1837 {
1838 n_segs++;
1839 }
1840 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
1841 sz = sizeof (Elf_External_Phdr) * n_segs;
1842 if (align_file_position (i_ehdrp->e_ehsize) + sz <= lowest_offset)
1843 i_ehdrp->e_phoff = align_file_position (i_ehdrp->e_ehsize);
1844 else
1845 {
1846 i_ehdrp->e_phoff = align_file_position (elf_tdata (abfd)->next_file_pos);
1847 elf_tdata (abfd)->next_file_pos = i_ehdrp->e_phoff + sz;
1848 }
1849 phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd,
1850 n_segs * sizeof (Elf_Internal_Phdr));
1851 if (!phdr)
1852 {
1853 bfd_set_error (bfd_error_no_memory);
1854 abort (); /* FIXME */
1855 }
1856 elf_tdata (abfd)->phdr = phdr;
1857 while (seg)
1858 {
1859 phdr->p_type = PT_LOAD; /* only type we really support so far */
1860 phdr->p_offset = seg->start_pos;
1861 phdr->p_vaddr = seg->low;
1862 phdr->p_paddr = 0;
1863 phdr->p_filesz = seg->file_size;
1864 phdr->p_memsz = seg->mem_size;
1865 phdr->p_flags = PF_R;
1866 phdr->p_align = maxpagesize; /* ? */
1867 if (seg->sh_flags & SHF_WRITE)
1868 /* SysVr4 ELF docs say "data segments normally have read, write,
1869 and execute permissions." */
1870 phdr->p_flags |= (PF_W | PF_X);
1871 if (seg->sh_flags & SHF_EXECINSTR)
1872 phdr->p_flags |= PF_X;
1873 phdr++;
1874 seg = seg->next;
1875 }
1876 i_ehdrp->e_phnum = n_segs;
1877 }
1878 elf_write_phdrs (abfd, i_ehdrp, elf_tdata (abfd)->phdr, i_ehdrp->e_phnum);
1879 if (done != NULL)
1880 free (done);
1881 return true;
1882 error_return:
1883 if (done != NULL)
1884 free (done);
1885 return false;
1886 }
1887
1888 static boolean
1889 assign_file_positions_except_relocs (abfd)
1890 bfd *abfd;
1891 {
1892 /* For now, we ignore the possibility of having program segments, which
1893 may require some alignment in the file. That'll require padding, and
1894 some interesting calculations to optimize file space usage.
1895
1896 Also, since the application may change the list of relocations for
1897 a given section, we don't figure them in here. We'll put them at the
1898 end of the file, at positions computed during bfd_close.
1899
1900 The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ...
1901 or: <ehdr> <phdr> <sec1> <sec2> ... <shdr> <rel1> ... */
1902
1903 struct elf_obj_tdata *t = elf_tdata (abfd);
1904 file_ptr off;
1905 unsigned int i;
1906 Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd);
1907 Elf_Internal_Shdr *i_shdrp;
1908 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
1909 int exec_p = (abfd->flags & EXEC_P) != 0;
1910 bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1911
1912 /* Everything starts after the ELF file header. */
1913 off = i_ehdrp->e_ehsize;
1914
1915 if (!exec_p)
1916 {
1917 /* Section headers. */
1918 off = align_file_position (off);
1919 i_ehdrp->e_shoff = off;
1920 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
1921 off = assign_file_positions_for_symtab_and_strtabs (abfd, off);
1922 }
1923 for (i = 1; i < i_ehdrp->e_shnum; i++)
1924 {
1925 /* The symtab and strtab sections are placed by
1926 assign_file_positions_for_symtab_and_strtabs. */
1927 if (i == t->symtab_section
1928 || i == t->strtab_section
1929 || i == t->shstrtab_section)
1930 continue;
1931
1932 i_shdrp = i_shdrpp[i];
1933 if (i_shdrp->sh_type == SHT_REL || i_shdrp->sh_type == SHT_RELA)
1934 {
1935 i_shdrp->sh_offset = -1;
1936 continue;
1937 }
1938 if (exec_p)
1939 {
1940 if (maxpagesize == 0)
1941 maxpagesize = 1; /* make the arithmetic work */
1942 /* This isn't necessarily going to give the best packing, if the
1943 segments require padding between them, but since that isn't
1944 usually the case, this'll do. */
1945 if ((i_shdrp->sh_flags & SHF_ALLOC) == 0)
1946 {
1947 i_shdrp->sh_offset = -1;
1948 continue;
1949 }
1950 /* Blindly assume that the segments are ordered optimally. With
1951 the default LD script, they will be. */
1952 if (i_shdrp->sh_type != SHT_NOBITS)
1953 {
1954 /* need big unsigned type */
1955 bfd_vma addtl_off;
1956 addtl_off = i_shdrp->sh_addr - off;
1957 addtl_off = addtl_off % maxpagesize;
1958 if (addtl_off)
1959 {
1960 off += addtl_off;
1961 }
1962 }
1963 }
1964 off = assign_file_position_for_section (i_shdrp, off);
1965
1966 if (exec_p
1967 && i_shdrp->sh_type == SHT_NOBITS
1968 && (i == i_ehdrp->e_shnum
1969 || i_shdrpp[i + 1]->sh_type != SHT_NOBITS))
1970 {
1971 /* Skip to the next page to ensure that when the file is
1972 loaded the bss section is loaded with zeroes. I don't
1973 know if this is required on all platforms, but it
1974 shouldn't really hurt. */
1975 off = BFD_ALIGN (off, maxpagesize);
1976 }
1977
1978 if (exec_p
1979 && (abfd->flags & D_PAGED) != 0
1980 && get_elf_backend_data (abfd)->maxpagesize > 1
1981 && i_shdrp->sh_type == SHT_PROGBITS
1982 && (i_shdrp->sh_flags & SHF_ALLOC)
1983 && (i_shdrp->sh_offset - i_shdrp->sh_addr) % get_elf_backend_data (abfd)->maxpagesize != 0)
1984 abort ();
1985 }
1986 if (exec_p)
1987 {
1988 elf_tdata (abfd)->next_file_pos = off;
1989 if (!map_program_segments (abfd))
1990 return false;
1991 off = elf_tdata (abfd)->next_file_pos;
1992
1993 /* Section headers. */
1994 off = align_file_position (off);
1995 i_ehdrp->e_shoff = off;
1996 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
1997
1998 off = assign_file_positions_for_symtab_and_strtabs (abfd, off);
1999
2000 for (i = 1; i < i_ehdrp->e_shnum; i++)
2001 {
2002 i_shdrp = i_shdrpp[i];
2003 if (i_shdrp->sh_offset + 1 == 0
2004 && i_shdrp->sh_type != SHT_REL
2005 && i_shdrp->sh_type != SHT_RELA)
2006 off = assign_file_position_for_section (i_shdrp, off);
2007 }
2008 }
2009 elf_tdata (abfd)->next_file_pos = off;
2010 return true;
2011 }
2012
2013 static boolean
2014 prep_headers (abfd)
2015 bfd *abfd;
2016 {
2017 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2018 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2019 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2020 int count;
2021 struct strtab *shstrtab;
2022
2023 i_ehdrp = elf_elfheader (abfd);
2024 i_shdrp = elf_elfsections (abfd);
2025
2026 shstrtab = bfd_new_strtab (abfd);
2027 if (!shstrtab)
2028 return false;
2029
2030 elf_shstrtab (abfd) = shstrtab;
2031
2032 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2033 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2034 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2035 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2036
2037 i_ehdrp->e_ident[EI_CLASS] = ELFCLASS;
2038 i_ehdrp->e_ident[EI_DATA] =
2039 abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
2040 i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT;
2041
2042 for (count = EI_PAD; count < EI_NIDENT; count++)
2043 i_ehdrp->e_ident[count] = 0;
2044
2045 i_ehdrp->e_type = (abfd->flags & EXEC_P) ? ET_EXEC : ET_REL;
2046 switch (bfd_get_arch (abfd))
2047 {
2048 case bfd_arch_unknown:
2049 i_ehdrp->e_machine = EM_NONE;
2050 break;
2051 case bfd_arch_sparc:
2052 i_ehdrp->e_machine = EM_SPARC;
2053 /* start-sanitize-v9 */
2054 #if ARCH_SIZE == 64
2055 i_ehdrp->e_machine = EM_SPARC64;
2056 #endif
2057 /* end-sanitize-v9 */
2058 break;
2059 case bfd_arch_i386:
2060 i_ehdrp->e_machine = EM_386;
2061 break;
2062 case bfd_arch_m68k:
2063 i_ehdrp->e_machine = EM_68K;
2064 break;
2065 case bfd_arch_m88k:
2066 i_ehdrp->e_machine = EM_88K;
2067 break;
2068 case bfd_arch_i860:
2069 i_ehdrp->e_machine = EM_860;
2070 break;
2071 case bfd_arch_mips: /* MIPS Rxxxx */
2072 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2073 break;
2074 case bfd_arch_hppa:
2075 i_ehdrp->e_machine = EM_HPPA;
2076 break;
2077 case bfd_arch_powerpc:
2078 i_ehdrp->e_machine = EM_CYGNUS_POWERPC;
2079 break;
2080 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2081 default:
2082 i_ehdrp->e_machine = EM_NONE;
2083 }
2084 i_ehdrp->e_version = EV_CURRENT;
2085 i_ehdrp->e_ehsize = sizeof (Elf_External_Ehdr);
2086
2087 /* no program header, for now. */
2088 i_ehdrp->e_phoff = 0;
2089 i_ehdrp->e_phentsize = 0;
2090 i_ehdrp->e_phnum = 0;
2091
2092 /* each bfd section is section header entry */
2093 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2094 i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr);
2095
2096 /* if we're building an executable, we'll need a program header table */
2097 if (abfd->flags & EXEC_P)
2098 {
2099 /* it all happens later */
2100 #if 0
2101 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2102
2103 /* elf_build_phdrs() returns a (NULL-terminated) array of
2104 Elf_Internal_Phdrs */
2105 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2106 i_ehdrp->e_phoff = outbase;
2107 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2108 #endif
2109 }
2110 else
2111 {
2112 i_ehdrp->e_phentsize = 0;
2113 i_phdrp = 0;
2114 i_ehdrp->e_phoff = 0;
2115 }
2116
2117 elf_tdata (abfd)->symtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2118 ".symtab");
2119 elf_tdata (abfd)->strtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2120 ".strtab");
2121 elf_tdata (abfd)->shstrtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2122 ".shstrtab");
2123 return true;
2124 }
2125
2126 static boolean
2127 swap_out_syms (abfd)
2128 bfd *abfd;
2129 {
2130 if (!elf_map_symbols (abfd))
2131 return false;
2132
2133 /* Dump out the symtabs. */
2134 {
2135 int symcount = bfd_get_symcount (abfd);
2136 asymbol **syms = bfd_get_outsymbols (abfd);
2137 struct strtab *stt = bfd_new_strtab (abfd);
2138 Elf_Internal_Shdr *symtab_hdr;
2139 Elf_Internal_Shdr *symstrtab_hdr;
2140 Elf_External_Sym *outbound_syms;
2141 int idx;
2142
2143 if (!stt)
2144 return false;
2145 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2146 symtab_hdr->sh_type = SHT_SYMTAB;
2147 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
2148 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2149 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
2150
2151 /* FIXME: Systems I've checked use 4 byte alignment for .symtab,
2152 but it is possible that there are systems which use a different
2153 alignment. */
2154 symtab_hdr->sh_addralign = 4;
2155
2156 /* see assert in elf_fake_sections that supports this: */
2157 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2158 symstrtab_hdr->sh_type = SHT_STRTAB;
2159
2160 outbound_syms = (Elf_External_Sym *)
2161 bfd_alloc (abfd, (1 + symcount) * sizeof (Elf_External_Sym));
2162 if (!outbound_syms)
2163 {
2164 bfd_set_error (bfd_error_no_memory);
2165 return false;
2166 }
2167 /* now generate the data (for "contents") */
2168 {
2169 /* Fill in zeroth symbol and swap it out. */
2170 Elf_Internal_Sym sym;
2171 sym.st_name = 0;
2172 sym.st_value = 0;
2173 sym.st_size = 0;
2174 sym.st_info = 0;
2175 sym.st_other = 0;
2176 sym.st_shndx = SHN_UNDEF;
2177 elf_swap_symbol_out (abfd, &sym, outbound_syms);
2178 }
2179 for (idx = 0; idx < symcount; idx++)
2180 {
2181 Elf_Internal_Sym sym;
2182 bfd_vma value = syms[idx]->value;
2183
2184 if (syms[idx]->flags & BSF_SECTION_SYM)
2185 /* Section symbols have no names. */
2186 sym.st_name = 0;
2187 else
2188 sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name);
2189
2190 if (bfd_is_com_section (syms[idx]->section))
2191 {
2192 /* ELF common symbols put the alignment into the `value' field,
2193 and the size into the `size' field. This is backwards from
2194 how BFD handles it, so reverse it here. */
2195 sym.st_size = value;
2196 /* Should retrieve this from somewhere... */
2197 sym.st_value = 16;
2198 sym.st_shndx = elf_section_from_bfd_section (abfd,
2199 syms[idx]->section);
2200 }
2201 else
2202 {
2203 asection *sec = syms[idx]->section;
2204 elf_symbol_type *type_ptr;
2205 int shndx;
2206
2207 if (sec->output_section)
2208 {
2209 value += sec->output_offset;
2210 sec = sec->output_section;
2211 }
2212 value += sec->vma;
2213 sym.st_value = value;
2214 type_ptr = elf_symbol_from (abfd, syms[idx]);
2215 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
2216 sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec);
2217 if (shndx == -1)
2218 {
2219 asection *sec2;
2220 /* Writing this would be a hell of a lot easier if we had
2221 some decent documentation on bfd, and knew what to expect
2222 of the library, and what to demand of applications. For
2223 example, it appears that `objcopy' might not set the
2224 section of a symbol to be a section that is actually in
2225 the output file. */
2226 sec2 = bfd_get_section_by_name (abfd, sec->name);
2227 BFD_ASSERT (sec2 != 0);
2228 sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec2);
2229 BFD_ASSERT (shndx != -1);
2230 }
2231 }
2232
2233 if (bfd_is_com_section (syms[idx]->section))
2234 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT);
2235 else if (syms[idx]->section == &bfd_und_section)
2236 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_NOTYPE);
2237 else if (syms[idx]->flags & BSF_SECTION_SYM)
2238 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
2239 else if (syms[idx]->flags & BSF_FILE)
2240 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
2241 else
2242 {
2243 int bind = STB_LOCAL;
2244 int type = STT_OBJECT;
2245 unsigned int flags = syms[idx]->flags;
2246
2247 if (flags & BSF_LOCAL)
2248 bind = STB_LOCAL;
2249 else if (flags & BSF_WEAK)
2250 bind = STB_WEAK;
2251 else if (flags & BSF_GLOBAL)
2252 bind = STB_GLOBAL;
2253
2254 if (flags & BSF_FUNCTION)
2255 type = STT_FUNC;
2256
2257 sym.st_info = ELF_ST_INFO (bind, type);
2258 }
2259
2260 sym.st_other = 0;
2261 elf_swap_symbol_out (abfd, &sym,
2262 (outbound_syms
2263 + elf_sym_extra (abfd)[idx].elf_sym_num));
2264 }
2265
2266 symtab_hdr->contents = (PTR) outbound_syms;
2267 symstrtab_hdr->contents = (PTR) stt->tab;
2268 symstrtab_hdr->sh_size = stt->length;
2269 symstrtab_hdr->sh_type = SHT_STRTAB;
2270
2271 symstrtab_hdr->sh_flags = 0;
2272 symstrtab_hdr->sh_addr = 0;
2273 symstrtab_hdr->sh_entsize = 0;
2274 symstrtab_hdr->sh_link = 0;
2275 symstrtab_hdr->sh_info = 0;
2276 symstrtab_hdr->sh_addralign = 1;
2277 symstrtab_hdr->size = 0;
2278 }
2279
2280 /* put the strtab out too... */
2281 {
2282 Elf_Internal_Shdr *this_hdr;
2283
2284 this_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2285 this_hdr->contents = (PTR) elf_shstrtab (abfd)->tab;
2286 this_hdr->sh_size = elf_shstrtab (abfd)->length;
2287 this_hdr->sh_type = SHT_STRTAB;
2288 this_hdr->sh_flags = 0;
2289 this_hdr->sh_addr = 0;
2290 this_hdr->sh_entsize = 0;
2291 this_hdr->sh_addralign = 1;
2292 this_hdr->size = 0;
2293 }
2294 return true;
2295 }
2296
2297 static boolean
2298 write_shdrs_and_ehdr (abfd)
2299 bfd *abfd;
2300 {
2301 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
2302 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2303 Elf_External_Shdr *x_shdrp; /* Section header table, external form */
2304 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2305 unsigned int count;
2306 struct strtab *shstrtab;
2307
2308 i_ehdrp = elf_elfheader (abfd);
2309 i_shdrp = elf_elfsections (abfd);
2310 shstrtab = elf_shstrtab (abfd);
2311
2312 /* swap the header before spitting it out... */
2313
2314 #if DEBUG & 1
2315 elf_debug_file (i_ehdrp);
2316 #endif
2317 elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
2318 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2319 || (bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd)
2320 != sizeof (x_ehdr)))
2321 return false;
2322
2323 /* at this point we've concocted all the ELF sections... */
2324 x_shdrp = (Elf_External_Shdr *)
2325 bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
2326 if (!x_shdrp)
2327 {
2328 bfd_set_error (bfd_error_no_memory);
2329 return false;
2330 }
2331
2332 for (count = 0; count < i_ehdrp->e_shnum; count++)
2333 {
2334 #if DEBUG & 2
2335 elf_debug_section (shstrtab->tab + i_shdrp[count]->sh_name, count,
2336 i_shdrp[count]);
2337 #endif
2338 elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count);
2339 }
2340 if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0
2341 || (bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd)
2342 != sizeof (*x_shdrp) * i_ehdrp->e_shnum))
2343 return false;
2344
2345 /* need to dump the string table too... */
2346
2347 return true;
2348 }
2349
2350 static void
2351 assign_file_positions_for_relocs (abfd)
2352 bfd *abfd;
2353 {
2354 file_ptr off = elf_tdata (abfd)->next_file_pos;
2355 unsigned int i;
2356 Elf_Internal_Shdr **shdrpp = elf_elfsections (abfd);
2357 Elf_Internal_Shdr *shdrp;
2358 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
2359 {
2360 shdrp = shdrpp[i];
2361 if (shdrp->sh_type != SHT_REL && shdrp->sh_type != SHT_RELA)
2362 continue;
2363 off = align_file_position (off);
2364 off = assign_file_position_for_section (shdrp, off);
2365 }
2366 elf_tdata (abfd)->next_file_pos = off;
2367 }
2368
2369 boolean
2370 NAME(bfd_elf,write_object_contents) (abfd)
2371 bfd *abfd;
2372 {
2373 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2374 Elf_Internal_Ehdr *i_ehdrp;
2375 Elf_Internal_Shdr **i_shdrp;
2376 unsigned int count;
2377
2378 /* We don't know how to write dynamic objects. Specifically, we
2379 don't know how to construct the program header. */
2380 if ((abfd->flags & DYNAMIC) != 0)
2381 {
2382 fprintf (stderr, "Writing ELF dynamic objects is not supported\n");
2383 bfd_set_error (bfd_error_wrong_format);
2384 return false;
2385 }
2386
2387 if (abfd->output_has_begun == false)
2388 {
2389 /* Do any elf backend specific processing first. */
2390 if (bed->elf_backend_begin_write_processing)
2391 (*bed->elf_backend_begin_write_processing) (abfd);
2392
2393 if (prep_headers (abfd) == false)
2394 return false;
2395 if (elf_compute_section_file_positions (abfd) == false)
2396 return false;
2397 abfd->output_has_begun = true;
2398 }
2399
2400 i_shdrp = elf_elfsections (abfd);
2401 i_ehdrp = elf_elfheader (abfd);
2402
2403 bfd_map_over_sections (abfd, write_relocs, (PTR) 0);
2404 assign_file_positions_for_relocs (abfd);
2405
2406 /* After writing the headers, we need to write the sections too... */
2407 for (count = 1; count < i_ehdrp->e_shnum; count++)
2408 {
2409 if (bed->elf_backend_section_processing)
2410 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2411 if (i_shdrp[count]->contents)
2412 {
2413 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2414 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2415 1, abfd)
2416 != i_shdrp[count]->sh_size))
2417 return false;
2418 }
2419 }
2420
2421 if (bed->elf_backend_final_write_processing)
2422 (*bed->elf_backend_final_write_processing) (abfd);
2423
2424 return write_shdrs_and_ehdr (abfd);
2425 }
2426
2427 /* Given an index of a section, retrieve a pointer to it. Note
2428 that for our purposes, sections are indexed by {1, 2, ...} with
2429 0 being an illegal index. */
2430
2431 /* In the original, each ELF section went into exactly one BFD
2432 section. This doesn't really make sense, so we need a real mapping.
2433 The mapping has to hide in the Elf_Internal_Shdr since asection
2434 doesn't have anything like a tdata field... */
2435
2436 static struct sec *
2437 section_from_elf_index (abfd, index)
2438 bfd *abfd;
2439 unsigned int index;
2440 {
2441 /* @@ Is bfd_com_section really correct in all the places it could
2442 be returned from this routine? */
2443
2444 if (index == SHN_ABS)
2445 return &bfd_com_section; /* not abs? */
2446 if (index == SHN_COMMON)
2447 return &bfd_com_section;
2448
2449 if (index > elf_elfheader (abfd)->e_shnum)
2450 return 0;
2451
2452 {
2453 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[index];
2454
2455 switch (hdr->sh_type)
2456 {
2457 /* ELF sections that map to BFD sections */
2458 case SHT_PROGBITS:
2459 case SHT_NOBITS:
2460 if (!hdr->rawdata)
2461 bfd_section_from_shdr (abfd, index);
2462 return (struct sec *) hdr->rawdata;
2463
2464 default:
2465 return (struct sec *) &bfd_abs_section;
2466 }
2467 }
2468 }
2469
2470 /* given a section, search the header to find them... */
2471 static int
2472 elf_section_from_bfd_section (abfd, asect)
2473 bfd *abfd;
2474 struct sec *asect;
2475 {
2476 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2477 int index;
2478 Elf_Internal_Shdr *hdr;
2479 int maxindex = elf_elfheader (abfd)->e_shnum;
2480
2481 if (asect == &bfd_abs_section)
2482 return SHN_ABS;
2483 if (asect == &bfd_com_section)
2484 return SHN_COMMON;
2485 if (asect == &bfd_und_section)
2486 return SHN_UNDEF;
2487
2488 for (index = 0; index < maxindex; index++)
2489 {
2490 hdr = i_shdrp[index];
2491 switch (hdr->sh_type)
2492 {
2493 /* ELF sections that map to BFD sections */
2494 case SHT_PROGBITS:
2495 case SHT_NOBITS:
2496 case SHT_NOTE:
2497 if (hdr->rawdata)
2498 {
2499 if (((struct sec *) (hdr->rawdata)) == asect)
2500 return index;
2501 }
2502 break;
2503
2504 case SHT_STRTAB:
2505 /* fix_up_strtabs will generate STRTAB sections with names
2506 of .stab*str. */
2507 if (!strncmp (asect->name, ".stab", 5)
2508 && !strcmp ("str", asect->name + strlen (asect->name) - 3))
2509 {
2510 if (hdr->rawdata)
2511 {
2512 if (((struct sec *) (hdr->rawdata)) == asect)
2513 return index;
2514 }
2515 break;
2516 }
2517 /* FALL THROUGH */
2518 default:
2519 {
2520 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2521
2522 if (bed->elf_backend_section_from_bfd_section)
2523 {
2524 int retval;
2525
2526 retval = index;
2527 if ((*bed->elf_backend_section_from_bfd_section)
2528 (abfd, hdr, asect, &retval))
2529 return retval;
2530 }
2531 }
2532 break;
2533 }
2534 }
2535 return -1;
2536 }
2537
2538 /* given a symbol, return the bfd index for that symbol. */
2539 static int
2540 elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2541 bfd *abfd;
2542 struct symbol_cache_entry **asym_ptr_ptr;
2543 {
2544 struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr;
2545 int idx;
2546 flagword flags = asym_ptr->flags;
2547
2548 /* When gas creates relocations against local labels, it creates its
2549 own symbol for the section, but does put the symbol into the
2550 symbol chain, so udata is 0. When the linker is generating
2551 relocatable output, this section symbol may be for one of the
2552 input sections rather than the output section. */
2553 if (asym_ptr->udata == (PTR) 0
2554 && (flags & BSF_SECTION_SYM)
2555 && asym_ptr->section)
2556 {
2557 int indx;
2558
2559 if (asym_ptr->section->output_section != NULL)
2560 indx = asym_ptr->section->output_section->index;
2561 else
2562 indx = asym_ptr->section->index;
2563 if (elf_section_syms (abfd)[indx])
2564 asym_ptr->udata = elf_section_syms (abfd)[indx]->udata;
2565 }
2566
2567 if (asym_ptr->udata)
2568 idx = ((Elf_Sym_Extra *) asym_ptr->udata)->elf_sym_num;
2569 else
2570 {
2571 abort ();
2572 }
2573
2574 #if DEBUG & 4
2575 {
2576
2577 fprintf (stderr,
2578 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
2579 (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags));
2580 fflush (stderr);
2581 }
2582 #endif
2583
2584 return idx;
2585 }
2586
2587 static boolean
2588 elf_slurp_symbol_table (abfd, symptrs)
2589 bfd *abfd;
2590 asymbol **symptrs; /* Buffer for generated bfd symbols */
2591 {
2592 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
2593 long symcount; /* Number of external ELF symbols */
2594 elf_symbol_type *sym; /* Pointer to current bfd symbol */
2595 elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
2596 Elf_Internal_Sym i_sym;
2597 Elf_External_Sym *x_symp = NULL;
2598
2599 /* this is only valid because there is only one symtab... */
2600 /* FIXME: This is incorrect, there may also be a dynamic symbol
2601 table which is a subset of the full symbol table. We either need
2602 to be prepared to read both (and merge them) or ensure that we
2603 only read the full symbol table. Currently we only get called to
2604 read the full symbol table. -fnf */
2605
2606 /* Read each raw ELF symbol, converting from external ELF form to
2607 internal ELF form, and then using the information to create a
2608 canonical bfd symbol table entry.
2609
2610 Note that we allocate the initial bfd canonical symbol buffer
2611 based on a one-to-one mapping of the ELF symbols to canonical
2612 symbols. We actually use all the ELF symbols, so there will be no
2613 space left over at the end. When we have all the symbols, we
2614 build the caller's pointer vector. */
2615
2616 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
2617 return false;
2618
2619 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2620
2621 if (symcount == 0)
2622 sym = symbase = NULL;
2623 else
2624 {
2625 long i;
2626
2627 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
2628 return false;
2629
2630 symbase = ((elf_symbol_type *)
2631 bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type)));
2632 if (symbase == (elf_symbol_type *) NULL)
2633 {
2634 bfd_set_error (bfd_error_no_memory);
2635 return false;
2636 }
2637 sym = symbase;
2638
2639 /* Temporarily allocate room for the raw ELF symbols. */
2640 x_symp = ((Elf_External_Sym *)
2641 malloc (symcount * sizeof (Elf_External_Sym)));
2642 if (x_symp == NULL && symcount != 0)
2643 {
2644 bfd_set_error (bfd_error_no_memory);
2645 goto error_return;
2646 }
2647
2648 if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
2649 != symcount * sizeof (Elf_External_Sym))
2650 goto error_return;
2651 /* Skip first symbol, which is a null dummy. */
2652 for (i = 1; i < symcount; i++)
2653 {
2654 elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
2655 memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym));
2656 #ifdef ELF_KEEP_EXTSYM
2657 memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym));
2658 #endif
2659 sym->symbol.the_bfd = abfd;
2660
2661 sym->symbol.name = elf_string_from_elf_section (abfd, hdr->sh_link,
2662 i_sym.st_name);
2663
2664 sym->symbol.value = i_sym.st_value;
2665
2666 if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV)
2667 {
2668 sym->symbol.section = section_from_elf_index (abfd,
2669 i_sym.st_shndx);
2670 }
2671 else if (i_sym.st_shndx == SHN_ABS)
2672 {
2673 sym->symbol.section = &bfd_abs_section;
2674 }
2675 else if (i_sym.st_shndx == SHN_COMMON)
2676 {
2677 sym->symbol.section = &bfd_com_section;
2678 /* Elf puts the alignment into the `value' field, and
2679 the size into the `size' field. BFD wants to see the
2680 size in the value field, and doesn't care (at the
2681 moment) about the alignment. */
2682 sym->symbol.value = i_sym.st_size;
2683 }
2684 else if (i_sym.st_shndx == SHN_UNDEF)
2685 {
2686 sym->symbol.section = &bfd_und_section;
2687 }
2688 else
2689 sym->symbol.section = &bfd_abs_section;
2690
2691 sym->symbol.value -= sym->symbol.section->vma;
2692
2693 switch (ELF_ST_BIND (i_sym.st_info))
2694 {
2695 case STB_LOCAL:
2696 sym->symbol.flags |= BSF_LOCAL;
2697 break;
2698 case STB_GLOBAL:
2699 sym->symbol.flags |= BSF_GLOBAL;
2700 break;
2701 case STB_WEAK:
2702 sym->symbol.flags |= BSF_WEAK;
2703 break;
2704 }
2705
2706 switch (ELF_ST_TYPE (i_sym.st_info))
2707 {
2708 case STT_SECTION:
2709 sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
2710 break;
2711 case STT_FILE:
2712 sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
2713 break;
2714 case STT_FUNC:
2715 sym->symbol.flags |= BSF_FUNCTION;
2716 break;
2717 }
2718
2719 /* Do some backend-specific processing on this symbol. */
2720 {
2721 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2722 if (ebd->elf_backend_symbol_processing)
2723 (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
2724 }
2725
2726 sym++;
2727 }
2728 }
2729
2730 /* Do some backend-specific processing on this symbol table. */
2731 {
2732 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2733 if (ebd->elf_backend_symbol_table_processing)
2734 (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
2735 }
2736
2737 /* We rely on the zalloc to clear out the final symbol entry. */
2738
2739 bfd_get_symcount (abfd) = symcount = sym - symbase;
2740
2741 /* Fill in the user's symbol pointer vector if needed. */
2742 if (symptrs)
2743 {
2744 sym = symbase;
2745 while (symcount-- > 0)
2746 {
2747 *symptrs++ = &sym->symbol;
2748 sym++;
2749 }
2750 *symptrs = 0; /* Final null pointer */
2751 }
2752
2753 if (x_symp != NULL)
2754 free (x_symp);
2755 return true;
2756 error_return:
2757 if (x_symp != NULL)
2758 free (x_symp);
2759 return false;
2760 }
2761
2762 /* Return the number of bytes required to hold the symtab vector.
2763
2764 Note that we base it on the count plus 1, since we will null terminate
2765 the vector allocated based on this size. However, the ELF symbol table
2766 always has a dummy entry as symbol #0, so it ends up even. */
2767
2768 long
2769 elf_get_symtab_upper_bound (abfd)
2770 bfd *abfd;
2771 {
2772 long symcount;
2773 long symtab_size;
2774 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
2775
2776 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2777 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
2778
2779 return symtab_size;
2780 }
2781
2782 /*
2783 This function return the number of bytes required to store the
2784 relocation information associated with section <<sect>>
2785 attached to bfd <<abfd>>
2786
2787 */
2788 long
2789 elf_get_reloc_upper_bound (abfd, asect)
2790 bfd *abfd;
2791 sec_ptr asect;
2792 {
2793 if (asect->flags & SEC_RELOC)
2794 {
2795 /* either rel or rela */
2796 return elf_section_data (asect)->rel_hdr.sh_size;
2797 }
2798 else
2799 return 0;
2800 }
2801
2802 static boolean
2803 elf_slurp_reloca_table (abfd, asect, symbols)
2804 bfd *abfd;
2805 sec_ptr asect;
2806 asymbol **symbols;
2807 {
2808 Elf_External_Rela *native_relocs;
2809 arelent *reloc_cache;
2810 arelent *cache_ptr;
2811
2812 unsigned int idx;
2813
2814 if (asect->relocation)
2815 return true;
2816 if (asect->reloc_count == 0)
2817 return true;
2818 if (asect->flags & SEC_CONSTRUCTOR)
2819 return true;
2820
2821 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2822 return false;
2823 native_relocs = (Elf_External_Rela *)
2824 bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rela));
2825 if (!native_relocs)
2826 {
2827 bfd_set_error (bfd_error_no_memory);
2828 return false;
2829 }
2830 if (bfd_read ((PTR) native_relocs,
2831 sizeof (Elf_External_Rela), asect->reloc_count, abfd)
2832 != sizeof (Elf_External_Rela) * asect->reloc_count)
2833 return false;
2834
2835 reloc_cache = (arelent *)
2836 bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
2837
2838 if (!reloc_cache)
2839 {
2840 bfd_set_error (bfd_error_no_memory);
2841 return false;
2842 }
2843
2844 for (idx = 0; idx < asect->reloc_count; idx++)
2845 {
2846 Elf_Internal_Rela dst;
2847 Elf_External_Rela *src;
2848
2849 cache_ptr = reloc_cache + idx;
2850 src = native_relocs + idx;
2851 elf_swap_reloca_in (abfd, src, &dst);
2852
2853 #ifdef RELOC_PROCESSING
2854 RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
2855 #else
2856 if (asect->flags & SEC_RELOC)
2857 {
2858 /* relocatable, so the offset is off of the section */
2859 cache_ptr->address = dst.r_offset + asect->vma;
2860 }
2861 else
2862 {
2863 /* non-relocatable, so the offset a virtual address */
2864 cache_ptr->address = dst.r_offset;
2865 }
2866
2867 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
2868 of zero points to the dummy symbol, which was not read into
2869 the symbol table SYMBOLS. */
2870 if (ELF_R_SYM (dst.r_info) == 0)
2871 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
2872 else
2873 {
2874 asymbol *s;
2875
2876 cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
2877
2878 /* Translate any ELF section symbol into a BFD section
2879 symbol. */
2880 s = *(cache_ptr->sym_ptr_ptr);
2881 if (s->flags & BSF_SECTION_SYM)
2882 {
2883 cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr;
2884 s = *cache_ptr->sym_ptr_ptr;
2885 if (s->name == 0 || s->name[0] == 0)
2886 abort ();
2887 }
2888 }
2889 cache_ptr->addend = dst.r_addend;
2890
2891 /* Fill in the cache_ptr->howto field from dst.r_type */
2892 {
2893 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2894 (*ebd->elf_info_to_howto) (abfd, cache_ptr, &dst);
2895 }
2896 #endif
2897 }
2898
2899 asect->relocation = reloc_cache;
2900 return true;
2901 }
2902
2903 #ifdef DEBUG
2904 static void
2905 elf_debug_section (str, num, hdr)
2906 char *str;
2907 int num;
2908 Elf_Internal_Shdr *hdr;
2909 {
2910 fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, str, (long) hdr);
2911 fprintf (stderr,
2912 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
2913 (long) hdr->sh_name,
2914 (long) hdr->sh_type,
2915 (long) hdr->sh_flags);
2916 fprintf (stderr,
2917 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
2918 (long) hdr->sh_addr,
2919 (long) hdr->sh_offset,
2920 (long) hdr->sh_size);
2921 fprintf (stderr,
2922 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
2923 (long) hdr->sh_link,
2924 (long) hdr->sh_info,
2925 (long) hdr->sh_addralign);
2926 fprintf (stderr, "sh_entsize = %ld\n",
2927 (long) hdr->sh_entsize);
2928 fprintf (stderr, "rawdata = 0x%.8lx\n", (long) hdr->rawdata);
2929 fprintf (stderr, "contents = 0x%.8lx\n", (long) hdr->contents);
2930 fprintf (stderr, "size = %ld\n", (long) hdr->size);
2931 fflush (stderr);
2932 }
2933
2934 static void
2935 elf_debug_file (ehdrp)
2936 Elf_Internal_Ehdr *ehdrp;
2937 {
2938 fprintf (stderr, "e_entry = 0x%.8lx\n", (long) ehdrp->e_entry);
2939 fprintf (stderr, "e_phoff = %ld\n", (long) ehdrp->e_phoff);
2940 fprintf (stderr, "e_phnum = %ld\n", (long) ehdrp->e_phnum);
2941 fprintf (stderr, "e_phentsize = %ld\n", (long) ehdrp->e_phentsize);
2942 fprintf (stderr, "e_shoff = %ld\n", (long) ehdrp->e_shoff);
2943 fprintf (stderr, "e_shnum = %ld\n", (long) ehdrp->e_shnum);
2944 fprintf (stderr, "e_shentsize = %ld\n", (long) ehdrp->e_shentsize);
2945 }
2946 #endif
2947
2948 static boolean
2949 elf_slurp_reloc_table (abfd, asect, symbols)
2950 bfd *abfd;
2951 sec_ptr asect;
2952 asymbol **symbols;
2953 {
2954 Elf_External_Rel *native_relocs;
2955 arelent *reloc_cache;
2956 arelent *cache_ptr;
2957 Elf_Internal_Shdr *data_hdr;
2958 bfd_vma data_off;
2959 unsigned long data_max;
2960 char buf[4]; /* FIXME -- might be elf64 */
2961
2962 unsigned int idx;
2963
2964 if (asect->relocation)
2965 return true;
2966 if (asect->reloc_count == 0)
2967 return true;
2968 if (asect->flags & SEC_CONSTRUCTOR)
2969 return true;
2970
2971 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2972 return false;
2973 native_relocs = (Elf_External_Rel *)
2974 bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rel));
2975 if (!native_relocs)
2976 {
2977 bfd_set_error (bfd_error_no_memory);
2978 return false;
2979 }
2980 if (bfd_read ((PTR) native_relocs,
2981 sizeof (Elf_External_Rel), asect->reloc_count, abfd)
2982 != sizeof (Elf_External_Rel) * asect->reloc_count)
2983 return false;
2984
2985 reloc_cache = (arelent *)
2986 bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
2987
2988 if (!reloc_cache)
2989 {
2990 bfd_set_error (bfd_error_no_memory);
2991 return false;
2992 }
2993
2994 /* Get the offset of the start of the segment we are relocating to read in
2995 the implicit addend. */
2996 data_hdr = &elf_section_data (asect)->this_hdr;
2997 data_off = data_hdr->sh_offset;
2998 data_max = data_hdr->sh_size - sizeof (buf) + 1;
2999
3000 #if DEBUG & 2
3001 elf_debug_section ("data section", -1, data_hdr);
3002 #endif
3003
3004 for (idx = 0; idx < asect->reloc_count; idx++)
3005 {
3006 #ifdef RELOC_PROCESSING
3007 Elf_Internal_Rel dst;
3008 Elf_External_Rel *src;
3009
3010 cache_ptr = reloc_cache + idx;
3011 src = native_relocs + idx;
3012 elf_swap_reloc_in (abfd, src, &dst);
3013
3014 RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
3015 #else
3016 Elf_Internal_Rel dst;
3017 Elf_External_Rel *src;
3018
3019 cache_ptr = reloc_cache + idx;
3020 src = native_relocs + idx;
3021
3022 elf_swap_reloc_in (abfd, src, &dst);
3023
3024 if (asect->flags & SEC_RELOC)
3025 {
3026 /* relocatable, so the offset is off of the section */
3027 cache_ptr->address = dst.r_offset + asect->vma;
3028 }
3029 else
3030 {
3031 /* non-relocatable, so the offset a virtual address */
3032 cache_ptr->address = dst.r_offset;
3033 }
3034
3035 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
3036 of zero points to the dummy symbol, which was not read into
3037 the symbol table SYMBOLS. */
3038 if (ELF_R_SYM (dst.r_info) == 0)
3039 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
3040 else
3041 {
3042 asymbol *s;
3043
3044 cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
3045
3046 /* Translate any ELF section symbol into a BFD section
3047 symbol. */
3048 s = *(cache_ptr->sym_ptr_ptr);
3049 if (s->flags & BSF_SECTION_SYM)
3050 {
3051 cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr;
3052 s = *cache_ptr->sym_ptr_ptr;
3053 if (s->name == 0 || s->name[0] == 0)
3054 abort ();
3055 }
3056 }
3057 BFD_ASSERT (dst.r_offset <= data_max);
3058 cache_ptr->addend = 0;
3059
3060 /* Fill in the cache_ptr->howto field from dst.r_type */
3061 {
3062 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
3063 (*ebd->elf_info_to_howto_rel) (abfd, cache_ptr, &dst);
3064 }
3065 #endif
3066 }
3067
3068 asect->relocation = reloc_cache;
3069 return true;
3070 }
3071
3072 long
3073 elf_canonicalize_reloc (abfd, section, relptr, symbols)
3074 bfd *abfd;
3075 sec_ptr section;
3076 arelent **relptr;
3077 asymbol **symbols;
3078 {
3079 arelent *tblptr = section->relocation;
3080 unsigned int count = 0;
3081 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
3082
3083 /* snarfed from coffcode.h */
3084 if (use_rela_p)
3085 {
3086 if (! elf_slurp_reloca_table (abfd, section, symbols))
3087 return -1;
3088 }
3089 else
3090 {
3091 if (! elf_slurp_reloc_table (abfd, section, symbols))
3092 return -1;
3093 }
3094
3095 tblptr = section->relocation;
3096
3097 for (; count++ < section->reloc_count;)
3098 *relptr++ = tblptr++;
3099
3100 *relptr = 0;
3101 return section->reloc_count;
3102 }
3103
3104 long
3105 elf_get_symtab (abfd, alocation)
3106 bfd *abfd;
3107 asymbol **alocation;
3108 {
3109 if (!elf_slurp_symbol_table (abfd, alocation))
3110 return -1;
3111
3112 return bfd_get_symcount (abfd);
3113 }
3114
3115 asymbol *
3116 elf_make_empty_symbol (abfd)
3117 bfd *abfd;
3118 {
3119 elf_symbol_type *newsym;
3120
3121 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3122 if (!newsym)
3123 {
3124 bfd_set_error (bfd_error_no_memory);
3125 return NULL;
3126 }
3127 else
3128 {
3129 newsym->symbol.the_bfd = abfd;
3130 return &newsym->symbol;
3131 }
3132 }
3133
3134 void
3135 elf_get_symbol_info (ignore_abfd, symbol, ret)
3136 bfd *ignore_abfd;
3137 asymbol *symbol;
3138 symbol_info *ret;
3139 {
3140 bfd_symbol_info (symbol, ret);
3141 }
3142
3143 void
3144 elf_print_symbol (ignore_abfd, filep, symbol, how)
3145 bfd *ignore_abfd;
3146 PTR filep;
3147 asymbol *symbol;
3148 bfd_print_symbol_type how;
3149 {
3150 FILE *file = (FILE *) filep;
3151 switch (how)
3152 {
3153 case bfd_print_symbol_name:
3154 fprintf (file, "%s", symbol->name);
3155 break;
3156 case bfd_print_symbol_more:
3157 fprintf (file, "elf ");
3158 fprintf_vma (file, symbol->value);
3159 fprintf (file, " %lx", (long) symbol->flags);
3160 break;
3161 case bfd_print_symbol_all:
3162 {
3163 CONST char *section_name;
3164 section_name = symbol->section ? symbol->section->name : "(*none*)";
3165 bfd_print_symbol_vandf ((PTR) file, symbol);
3166 fprintf (file, " %s\t%s",
3167 section_name,
3168 symbol->name);
3169 }
3170 break;
3171 }
3172
3173 }
3174
3175 alent *
3176 elf_get_lineno (ignore_abfd, symbol)
3177 bfd *ignore_abfd;
3178 asymbol *symbol;
3179 {
3180 fprintf (stderr, "elf_get_lineno unimplemented\n");
3181 fflush (stderr);
3182 BFD_FAIL ();
3183 return NULL;
3184 }
3185
3186 boolean
3187 elf_set_arch_mach (abfd, arch, machine)
3188 bfd *abfd;
3189 enum bfd_architecture arch;
3190 unsigned long machine;
3191 {
3192 /* Allow any architecture to be supported by the elf backend */
3193 switch (arch)
3194 {
3195 case bfd_arch_unknown: /* EM_NONE */
3196 case bfd_arch_sparc: /* EM_SPARC */
3197 case bfd_arch_i386: /* EM_386 */
3198 case bfd_arch_m68k: /* EM_68K */
3199 case bfd_arch_m88k: /* EM_88K */
3200 case bfd_arch_i860: /* EM_860 */
3201 case bfd_arch_mips: /* EM_MIPS (MIPS R3000) */
3202 case bfd_arch_hppa: /* EM_HPPA (HP PA_RISC) */
3203 case bfd_arch_powerpc: /* EM_CYGNUS_POWERPC */
3204 return bfd_default_set_arch_mach (abfd, arch, machine);
3205 default:
3206 return false;
3207 }
3208 }
3209
3210 boolean
3211 elf_find_nearest_line (abfd,
3212 section,
3213 symbols,
3214 offset,
3215 filename_ptr,
3216 functionname_ptr,
3217 line_ptr)
3218 bfd *abfd;
3219 asection *section;
3220 asymbol **symbols;
3221 bfd_vma offset;
3222 CONST char **filename_ptr;
3223 CONST char **functionname_ptr;
3224 unsigned int *line_ptr;
3225 {
3226 return false;
3227 }
3228
3229 int
3230 elf_sizeof_headers (abfd, reloc)
3231 bfd *abfd;
3232 boolean reloc;
3233 {
3234 fprintf (stderr, "elf_sizeof_headers unimplemented\n");
3235 fflush (stderr);
3236 BFD_FAIL ();
3237 return 0;
3238 }
3239
3240 boolean
3241 elf_set_section_contents (abfd, section, location, offset, count)
3242 bfd *abfd;
3243 sec_ptr section;
3244 PTR location;
3245 file_ptr offset;
3246 bfd_size_type count;
3247 {
3248 Elf_Internal_Shdr *hdr;
3249
3250 if (abfd->output_has_begun == false) /* set by bfd.c handler? */
3251 {
3252 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3253
3254 /* Do any elf backend specific processing first. */
3255 if (bed->elf_backend_begin_write_processing)
3256 (*bed->elf_backend_begin_write_processing) (abfd);
3257
3258 /* do setup calculations (FIXME) */
3259 if (prep_headers (abfd) == false)
3260 return false;
3261 if (elf_compute_section_file_positions (abfd) == false)
3262 return false;
3263 abfd->output_has_begun = true;
3264 }
3265
3266 hdr = &elf_section_data (section)->this_hdr;
3267
3268 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
3269 return false;
3270 if (bfd_write (location, 1, count, abfd) != count)
3271 return false;
3272
3273 return true;
3274 }
3275
3276 void
3277 elf_no_info_to_howto (abfd, cache_ptr, dst)
3278 bfd *abfd;
3279 arelent *cache_ptr;
3280 Elf_Internal_Rela *dst;
3281 {
3282 fprintf (stderr, "elf RELA relocation support for target machine unimplemented\n");
3283 fflush (stderr);
3284 BFD_FAIL ();
3285 }
3286
3287 void
3288 elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
3289 bfd *abfd;
3290 arelent *cache_ptr;
3291 Elf_Internal_Rel *dst;
3292 {
3293 fprintf (stderr, "elf REL relocation support for target machine unimplemented\n");
3294 fflush (stderr);
3295 BFD_FAIL ();
3296 }
3297 \f
3298
3299 /* Core file support */
3300
3301 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3302 #include <sys/procfs.h>
3303 #else
3304 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
3305 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
3306 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
3307 #endif
3308
3309 #ifdef HAVE_PROCFS
3310
3311 static void
3312 bfd_prstatus (abfd, descdata, descsz, filepos)
3313 bfd *abfd;
3314 char *descdata;
3315 int descsz;
3316 long filepos;
3317 {
3318 asection *newsect;
3319 prstatus_t *status = (prstatus_t *) 0;
3320
3321 if (descsz == sizeof (prstatus_t))
3322 {
3323 newsect = bfd_make_section (abfd, ".reg");
3324 newsect->_raw_size = sizeof (status->pr_reg);
3325 newsect->filepos = filepos + (long) &status->pr_reg;
3326 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3327 newsect->alignment_power = 2;
3328 if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
3329 {
3330 memcpy (core_prstatus (abfd), descdata, descsz);
3331 }
3332 }
3333 }
3334
3335 /* Stash a copy of the prpsinfo structure away for future use. */
3336
3337 static void
3338 bfd_prpsinfo (abfd, descdata, descsz, filepos)
3339 bfd *abfd;
3340 char *descdata;
3341 int descsz;
3342 long filepos;
3343 {
3344 asection *newsect;
3345
3346 if (descsz == sizeof (prpsinfo_t))
3347 {
3348 if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) != NULL)
3349 {
3350 memcpy (core_prpsinfo (abfd), descdata, descsz);
3351 }
3352 }
3353 }
3354
3355 static void
3356 bfd_fpregset (abfd, descdata, descsz, filepos)
3357 bfd *abfd;
3358 char *descdata;
3359 int descsz;
3360 long filepos;
3361 {
3362 asection *newsect;
3363
3364 newsect = bfd_make_section (abfd, ".reg2");
3365 newsect->_raw_size = descsz;
3366 newsect->filepos = filepos;
3367 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3368 newsect->alignment_power = 2;
3369 }
3370
3371 #endif /* HAVE_PROCFS */
3372
3373 /* Return a pointer to the args (including the command name) that were
3374 seen by the program that generated the core dump. Note that for
3375 some reason, a spurious space is tacked onto the end of the args
3376 in some (at least one anyway) implementations, so strip it off if
3377 it exists. */
3378
3379 char *
3380 elf_core_file_failing_command (abfd)
3381 bfd *abfd;
3382 {
3383 #ifdef HAVE_PROCFS
3384 if (core_prpsinfo (abfd))
3385 {
3386 prpsinfo_t *p = core_prpsinfo (abfd);
3387 char *scan = p->pr_psargs;
3388 while (*scan++)
3389 {;
3390 }
3391 scan -= 2;
3392 if ((scan > p->pr_psargs) && (*scan == ' '))
3393 {
3394 *scan = '\000';
3395 }
3396 return p->pr_psargs;
3397 }
3398 #endif
3399 return NULL;
3400 }
3401
3402 /* Return the number of the signal that caused the core dump. Presumably,
3403 since we have a core file, we got a signal of some kind, so don't bother
3404 checking the other process status fields, just return the signal number.
3405 */
3406
3407 int
3408 elf_core_file_failing_signal (abfd)
3409 bfd *abfd;
3410 {
3411 #ifdef HAVE_PROCFS
3412 if (core_prstatus (abfd))
3413 {
3414 return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig;
3415 }
3416 #endif
3417 return -1;
3418 }
3419
3420 /* Check to see if the core file could reasonably be expected to have
3421 come for the current executable file. Note that by default we return
3422 true unless we find something that indicates that there might be a
3423 problem.
3424 */
3425
3426 boolean
3427 elf_core_file_matches_executable_p (core_bfd, exec_bfd)
3428 bfd *core_bfd;
3429 bfd *exec_bfd;
3430 {
3431 #ifdef HAVE_PROCFS
3432 char *corename;
3433 char *execname;
3434 #endif
3435
3436 /* First, xvecs must match since both are ELF files for the same target. */
3437
3438 if (core_bfd->xvec != exec_bfd->xvec)
3439 {
3440 bfd_set_error (bfd_error_system_call);
3441 return false;
3442 }
3443
3444 #ifdef HAVE_PROCFS
3445
3446 /* If no prpsinfo, just return true. Otherwise, grab the last component
3447 of the exec'd pathname from the prpsinfo. */
3448
3449 if (core_prpsinfo (core_bfd))
3450 {
3451 corename = (((struct prpsinfo *) core_prpsinfo (core_bfd))->pr_fname);
3452 }
3453 else
3454 {
3455 return true;
3456 }
3457
3458 /* Find the last component of the executable pathname. */
3459
3460 if ((execname = strrchr (exec_bfd->filename, '/')) != NULL)
3461 {
3462 execname++;
3463 }
3464 else
3465 {
3466 execname = (char *) exec_bfd->filename;
3467 }
3468
3469 /* See if they match */
3470
3471 return strcmp (execname, corename) ? false : true;
3472
3473 #else
3474
3475 return true;
3476
3477 #endif /* HAVE_PROCFS */
3478 }
3479
3480 /* ELF core files contain a segment of type PT_NOTE, that holds much of
3481 the information that would normally be available from the /proc interface
3482 for the process, at the time the process dumped core. Currently this
3483 includes copies of the prstatus, prpsinfo, and fpregset structures.
3484
3485 Since these structures are potentially machine dependent in size and
3486 ordering, bfd provides two levels of support for them. The first level,
3487 available on all machines since it does not require that the host
3488 have /proc support or the relevant include files, is to create a bfd
3489 section for each of the prstatus, prpsinfo, and fpregset structures,
3490 without any interpretation of their contents. With just this support,
3491 the bfd client will have to interpret the structures itself. Even with
3492 /proc support, it might want these full structures for it's own reasons.
3493
3494 In the second level of support, where HAVE_PROCFS is defined, bfd will
3495 pick apart the structures to gather some additional information that
3496 clients may want, such as the general register set, the name of the
3497 exec'ed file and its arguments, the signal (if any) that caused the
3498 core dump, etc.
3499
3500 */
3501
3502 static boolean
3503 elf_corefile_note (abfd, hdr)
3504 bfd *abfd;
3505 Elf_Internal_Phdr *hdr;
3506 {
3507 Elf_External_Note *x_note_p; /* Elf note, external form */
3508 Elf_Internal_Note i_note; /* Elf note, internal form */
3509 char *buf = NULL; /* Entire note segment contents */
3510 char *namedata; /* Name portion of the note */
3511 char *descdata; /* Descriptor portion of the note */
3512 char *sectname; /* Name to use for new section */
3513 long filepos; /* File offset to descriptor data */
3514 asection *newsect;
3515
3516 if (hdr->p_filesz > 0
3517 && (buf = (char *) malloc (hdr->p_filesz)) != NULL
3518 && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1
3519 && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz)
3520 {
3521 x_note_p = (Elf_External_Note *) buf;
3522 while ((char *) x_note_p < (buf + hdr->p_filesz))
3523 {
3524 i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz);
3525 i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz);
3526 i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type);
3527 namedata = x_note_p->name;
3528 descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
3529 filepos = hdr->p_offset + (descdata - buf);
3530 switch (i_note.type)
3531 {
3532 case NT_PRSTATUS:
3533 /* process descdata as prstatus info */
3534 bfd_prstatus (abfd, descdata, i_note.descsz, filepos);
3535 sectname = ".prstatus";
3536 break;
3537 case NT_FPREGSET:
3538 /* process descdata as fpregset info */
3539 bfd_fpregset (abfd, descdata, i_note.descsz, filepos);
3540 sectname = ".fpregset";
3541 break;
3542 case NT_PRPSINFO:
3543 /* process descdata as prpsinfo */
3544 bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos);
3545 sectname = ".prpsinfo";
3546 break;
3547 default:
3548 /* Unknown descriptor, just ignore it. */
3549 sectname = NULL;
3550 break;
3551 }
3552 if (sectname != NULL)
3553 {
3554 newsect = bfd_make_section (abfd, sectname);
3555 newsect->_raw_size = i_note.descsz;
3556 newsect->filepos = filepos;
3557 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3558 newsect->alignment_power = 2;
3559 }
3560 x_note_p = (Elf_External_Note *)
3561 (descdata + BFD_ALIGN (i_note.descsz, 4));
3562 }
3563 }
3564 if (buf != NULL)
3565 {
3566 free (buf);
3567 }
3568 else if (hdr->p_filesz > 0)
3569 {
3570 bfd_set_error (bfd_error_no_memory);
3571 return false;
3572 }
3573 return true;
3574
3575 }
3576
3577 /* Core files are simply standard ELF formatted files that partition
3578 the file using the execution view of the file (program header table)
3579 rather than the linking view. In fact, there is no section header
3580 table in a core file.
3581
3582 The process status information (including the contents of the general
3583 register set) and the floating point register set are stored in a
3584 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
3585 that allow standard bfd access to the general registers (.reg) and the
3586 floating point registers (.reg2).
3587
3588 */
3589
3590 bfd_target *
3591 elf_core_file_p (abfd)
3592 bfd *abfd;
3593 {
3594 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
3595 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3596 Elf_External_Phdr x_phdr; /* Program header table entry, external form */
3597 Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */
3598 unsigned int phindex;
3599 struct elf_backend_data *ebd;
3600
3601 /* Read in the ELF header in external format. */
3602
3603 if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
3604 {
3605 if (bfd_get_error () != bfd_error_system_call)
3606 bfd_set_error (bfd_error_wrong_format);
3607 return NULL;
3608 }
3609
3610 /* Now check to see if we have a valid ELF file, and one that BFD can
3611 make use of. The magic number must match, the address size ('class')
3612 and byte-swapping must match our XVEC entry, and it must have a
3613 program header table (FIXME: See comments re segments at top of this
3614 file). */
3615
3616 if (elf_file_p (&x_ehdr) == false)
3617 {
3618 wrong:
3619 bfd_set_error (bfd_error_wrong_format);
3620 return NULL;
3621 }
3622
3623 /* FIXME, Check EI_VERSION here ! */
3624
3625 {
3626 #if ARCH_SIZE == 32
3627 int desired_address_size = ELFCLASS32;
3628 #endif
3629 #if ARCH_SIZE == 64
3630 int desired_address_size = ELFCLASS64;
3631 #endif
3632
3633 if (x_ehdr.e_ident[EI_CLASS] != desired_address_size)
3634 goto wrong;
3635 }
3636
3637 /* Switch xvec to match the specified byte order. */
3638 switch (x_ehdr.e_ident[EI_DATA])
3639 {
3640 case ELFDATA2MSB: /* Big-endian */
3641 if (abfd->xvec->byteorder_big_p == false)
3642 goto wrong;
3643 break;
3644 case ELFDATA2LSB: /* Little-endian */
3645 if (abfd->xvec->byteorder_big_p == true)
3646 goto wrong;
3647 break;
3648 case ELFDATANONE: /* No data encoding specified */
3649 default: /* Unknown data encoding specified */
3650 goto wrong;
3651 }
3652
3653 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3654 the tdata pointer in the bfd. */
3655
3656 elf_tdata (abfd) =
3657 (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
3658 if (elf_tdata (abfd) == NULL)
3659 {
3660 bfd_set_error (bfd_error_no_memory);
3661 return NULL;
3662 }
3663
3664 /* FIXME, `wrong' returns from this point onward, leak memory. */
3665
3666 /* Now that we know the byte order, swap in the rest of the header */
3667 i_ehdrp = elf_elfheader (abfd);
3668 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
3669 #if DEBUG & 1
3670 elf_debug_file (i_ehdrp);
3671 #endif
3672
3673 ebd = get_elf_backend_data (abfd);
3674
3675 /* Check that the ELF e_machine field matches what this particular
3676 BFD format expects. */
3677 if (ebd->elf_machine_code != i_ehdrp->e_machine)
3678 {
3679 bfd_target **target_ptr;
3680
3681 if (ebd->elf_machine_code != EM_NONE)
3682 goto wrong;
3683
3684 /* This is the generic ELF target. Let it match any ELF target
3685 for which we do not have a specific backend. */
3686 for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
3687 {
3688 struct elf_backend_data *back;
3689
3690 if ((*target_ptr)->flavour != bfd_target_elf_flavour)
3691 continue;
3692 back = (struct elf_backend_data *) (*target_ptr)->backend_data;
3693 if (back->elf_machine_code == i_ehdrp->e_machine)
3694 {
3695 /* target_ptr is an ELF backend which matches this
3696 object file, so reject the generic ELF target. */
3697 goto wrong;
3698 }
3699 }
3700 }
3701
3702 /* If there is no program header, or the type is not a core file, then
3703 we are hosed. */
3704 if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
3705 goto wrong;
3706
3707 /* Allocate space for a copy of the program header table in
3708 internal form, seek to the program header table in the file,
3709 read it in, and convert it to internal form. As a simple sanity
3710 check, verify that the what BFD thinks is the size of each program
3711 header table entry actually matches the size recorded in the file. */
3712
3713 if (i_ehdrp->e_phentsize != sizeof (x_phdr))
3714 goto wrong;
3715 i_phdrp = (Elf_Internal_Phdr *)
3716 bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
3717 if (!i_phdrp)
3718 {
3719 bfd_set_error (bfd_error_no_memory);
3720 return NULL;
3721 }
3722 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
3723 return NULL;
3724 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
3725 {
3726 if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
3727 != sizeof (x_phdr))
3728 return NULL;
3729 elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
3730 }
3731
3732 /* Once all of the program headers have been read and converted, we
3733 can start processing them. */
3734
3735 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
3736 {
3737 bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
3738 if ((i_phdrp + phindex)->p_type == PT_NOTE)
3739 {
3740 elf_corefile_note (abfd, i_phdrp + phindex);
3741 }
3742 }
3743
3744 /* Remember the entry point specified in the ELF file header. */
3745
3746 bfd_get_start_address (abfd) = i_ehdrp->e_entry;
3747
3748 return abfd->xvec;
3749 }
This page took 0.163132 seconds and 5 git commands to generate.