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