Back from Intel with Steve
[deliverable/binutils-gdb.git] / bfd / aout.c
1 /*** bfd backend for sunos binaries */
2
3 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Diddler.
6
7 BFD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 1, or (at your option)
10 any later version.
11
12 BFD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with BFD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 /* $Id$
22 * $Log$
23 * Revision 1.1.1.1 1991/03/21 21:11:23 gumby
24 * Back from Intel with Steve
25 *
26 * Revision 1.1 1991/03/21 21:11:23 gumby
27 * Initial revision
28 *
29 * Revision 1.2 1991/03/15 18:16:52 rich
30 * *** empty log message ***
31 *
32 * Revision 1.12 1991/03/10 19:11:41 rich
33 * Modified Files:
34 * bfd.c coff-code.h libbfd.c libbfd.h srec.c sunos.c
35 *
36 * Working bugs out of coff support.
37 *
38 * Revision 1.11 1991/03/09 03:40:04 rich
39 * Modified Files:
40 * Makefile b.out.c liba.out.h libbfd.c sunos.c sysdep.h
41 *
42 * Changes dictated by porting binutils.
43 *
44 * Revision 1.10 1991/03/08 07:52:02 sac
45 * Reinstalled things which went away after latest merge from Intel.
46 *
47 * Fixed a couple of problems in symbol handling too.
48 *
49 * Revision 1.9 1991/03/08 04:18:16 rich
50 * *** empty log message ***
51 *
52 * Revision 1.8 1991/03/07 21:57:26 sac
53 * Moved type info out of the asymbol into the private space.
54 * Cleaned up C++ stuff
55 *
56 * Revision 1.7 1991/03/06 21:49:02 sac
57 * Modified bfd_find_filename to return name of function too.
58 *
59 * Revision 1.6 1991/03/06 02:19:36 sac
60 * Moved howto table, added support for constructor sections and provided
61 * sunos4_find_nearest_line
62 *
63 * Revision 1.5 1991/03/05 16:25:44 sac
64 * Modified howto vector to include inplace and mask fields.
65 *
66 */
67
68 #define TARGET_BYTE_ORDER_BIG_P 1
69 #define TARGET TARGET_SPARC
70
71 #include "sysdep.h"
72 #include "bfd.h"
73 #include "libbfd.h"
74 #include <stdio.h>
75
76
77 /*SUPPRESS558*/
78 /*SUPPRESS529*/
79
80
81
82
83 typedef void generic_symbol_type;
84 /* These values are correct for the SPARC. I dunno about anything else */
85 #define PAGE_SIZE 0x02000
86 #define SEGMENT_SIZE PAGE_SIZE
87 #define TEXT_START_ADDR PAGE_SIZE
88 #include "a.out.gnu.h"
89 #include "stab.gnu.h"
90 #include "ar.h"
91 #include "liba.out.h" /* BFD a.out internal data structures */
92
93 #include "a.out.sun4.h"
94
95 #define CTOR_TABLE_RELOC_IDX 2
96 static reloc_howto_type howto_table[] =
97 {
98 /* type rs size bsz pcrel bitpos abs ovrf sf name partial inplace mask*/
99 { (unsigned int) RELOC_8, 0, 0, 8, false, 0, true, true,0,"8", false, 0x000000ff},
100 { (unsigned int) RELOC_16, 0, 1, 16, false, 0, true, true,0,"16", false, 0x0000ffff},
101 { (unsigned int) RELOC_32, 0, 2, 32, false, 0, true, true,0,"32", false, 0xffffffff},
102 { (unsigned int) RELOC_DISP8, 0, 0, 8, true, 0, false, true,0,"DISP8", false, 0x000000ff},
103 { (unsigned int) RELOC_DISP16, 0, 1, 16, true, 0, false, true,0,"DISP16", false, 0x0000ffff},
104 { (unsigned int) RELOC_DISP32, 0, 2, 32, true, 0, false, true,0,"DISP32", false, 0xffffffff},
105 { (unsigned int) RELOC_WDISP30,2, 2, 30, true, 0, false, true,0,"WDISP30", false, 0x3fffffff},
106 { (unsigned int) RELOC_WDISP22,2, 2, 22, true, 0, false, true,0,"WDISP22", false, 0x003fffff},
107 { (unsigned int) RELOC_HI22, 10, 2, 22, false, 0, false, true,0,"HI22", false, 0x003fffff},
108 { (unsigned int) RELOC_22, 0, 2, 22, false, 0, false, true,0,"22", false, 0x003fffff},
109 { (unsigned int) RELOC_13, 0, 2, 13, false, 0, false, true,0,"13", false, 0x00001fff},
110 { (unsigned int) RELOC_LO10, 0, 2, 10, false, 0, false, true,0,"LO10", false, 0x000003ff},
111 { (unsigned int) RELOC_SFA_BASE,0, 2, 32, false, 0, false, true,0,"SFA_BASE", false, 0xffffffff},
112 { (unsigned int) RELOC_SFA_OFF13,0,2, 32, false, 0, false, true,0,"SFA_OFF13",false, 0xffffffff},
113 { (unsigned int) RELOC_BASE10, 0, 2, 16, false, 0, false, true,0,"BASE10", false, 0x0000ffff},
114 { (unsigned int) RELOC_BASE13, 0, 2, 13, false, 0, false, true,0,"BASE13", false, 0x00001fff},
115 { (unsigned int) RELOC_BASE22, 0, 2, 0, false, 0, false, true,0,"BASE22", false, 0x00000000},
116 { (unsigned int) RELOC_PC10, 0, 2, 10, false, 0, false, true,0,"PC10", false, 0x000003ff},
117 { (unsigned int) RELOC_PC22, 0, 2, 22, false, 0, false, true,0,"PC22", false, 0x003fffff},
118 { (unsigned int) RELOC_JMP_TBL,0, 2, 32, false, 0, false, true,0,"JMP_TBL", false, 0xffffffff},
119 { (unsigned int) RELOC_SEGOFF16,0, 2, 0, false, 0, false, true,0,"SEGOFF16", false, 0x00000000},
120 { (unsigned int) RELOC_GLOB_DAT,0, 2, 0, false, 0, false, true,0,"GLOB_DAT", false, 0x00000000},
121 { (unsigned int) RELOC_JMP_SLOT,0, 2, 0, false, 0, false, true,0,"JMP_SLOT", false, 0x00000000},
122 { (unsigned int) RELOC_RELATIVE,0, 2, 0, false, 0, false, true,0,"RELATIVE", false, 0x00000000},
123 { (unsigned int) RELOC_JUMPTARG,2, 13, 16, true, 0, false, true,0,"JUMPTARG", false, 0x0000ffff},
124 { (unsigned int) RELOC_CONST, 0, 13, 16, false, 0, false, true,0,"CONST", false, 0x0000ffff},
125 { (unsigned int) RELOC_CONSTH, 16, 13, 16, false, 0, false, true,0,"CONSTH", false, 0x0000ffff},
126 };
127
128 /** a.out files */
129
130 PROTO (static void, swap_exec_header, (bfd *abfd, struct exec *execp));
131 PROTO (void , sunos4_write_syms, ());
132 PROTO (static boolean,sunos4_squirt_out_relocs,(bfd *abfd, asection *section));
133
134 /* Steve wants some way to frob this stuff from Saber while he's debugging
135 ld, so we have these funny shadow functions */
136 /* ZMAGIC's start at 0 (making the exec part of the text section),
137 other formats start after the exec
138 */
139 unsigned int n_txtoff(ptr)
140 struct exec *ptr;
141 {return N_MAGIC(*ptr)== ZMAGIC ? 0: sizeof(struct exec);}
142
143 unsigned int n_datoff(ptr)
144 struct exec *ptr;
145 {return n_txtoff(ptr) + ptr->a_text;}
146
147 unsigned int n_treloff(ptr)
148 struct exec *ptr;
149 {return n_datoff(ptr) + ptr->a_data;}
150
151 unsigned int n_dreloff(ptr)
152 struct exec *ptr;
153 {return n_treloff(ptr) + ptr->a_trsize;}
154
155 unsigned int n_symoff(ptr)
156 struct exec *ptr;
157 {return n_dreloff(ptr) + ptr->a_drsize;}
158
159 unsigned int n_stroff(ptr)
160 struct exec *ptr;
161 {return n_symoff(ptr) + ptr->a_syms;}
162
163 unsigned int n_badmag(ptr)
164 struct exec *ptr;
165 {
166 switch (N_MAGIC(*ptr)) {
167 case OMAGIC: case NMAGIC: case ZMAGIC: return 0;
168 default: return 1;
169 }
170 }
171
172
173 bfd_target *
174 sunos4_object_p (abfd)
175 bfd *abfd;
176 {
177 unsigned long magic;
178 struct exec anexec; /* save consing when you don't have to. */
179 struct exec *execp = &anexec;
180 void *rawptr;
181
182 bfd_error = system_call_error;
183
184 if (bfd_read ((void *)&magic, 1, sizeof (magic), abfd) != sizeof (magic))
185 return 0;
186 magic = bfd_h_getlong (abfd, &magic);
187
188 /* Baroque syntax to mask deficiencies of the Sun compiler */
189 /* if (N_BADMAG (*((struct exec *) &magic))) return 0; */
190 if (n_badmag ((struct exec *) &magic)) return 0;
191
192 if (bfd_seek (abfd, 0L, SEEK_SET) < 0) return 0;
193
194 if (bfd_read ((void *) execp, 1, sizeof (struct exec), abfd)
195 != sizeof (struct exec)) {
196 bfd_error = wrong_format;
197 return 0;
198 }
199
200 /* Use an intermediate variable for clarity */
201 rawptr = (void *) zalloc (sizeof (struct sunexdata) + sizeof (struct exec));
202
203 if (rawptr == NULL) {
204 bfd_error = no_memory;
205 return 0;
206 }
207
208 abfd->tdata =(void *)( (struct sunexdata *) rawptr);
209 exec_hdr (abfd) =
210 (struct exec *) ((char *)rawptr + sizeof (struct sunexdata));
211
212 swap_exec_header (abfd, execp);
213 memcpy (exec_hdr (abfd), execp, sizeof (struct exec));
214
215 /* Set the file flags */
216 abfd->flags = NO_FLAGS;
217 if (execp->a_drsize || execp->a_trsize)
218 abfd->flags |= HAS_RELOC;
219 if (execp->a_entry)
220 abfd->flags |= EXEC_P;
221 if (execp->a_syms)
222 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
223
224
225 if (N_MAGIC (anexec) == ZMAGIC) abfd->flags |= D_PAGED;
226 if (N_MAGIC (anexec) == NMAGIC) abfd->flags |= WP_TEXT;
227
228 /* Determine the architecture and machine type of the object file. */
229 abfd->obj_arch = bfd_arch_unknown; /* Default values */
230 abfd->obj_machine = 0;
231 switch (N_MACHTYPE (anexec)) {
232
233 case M_UNKNOWN:
234 break;
235
236 case M_68010:
237 abfd->obj_arch = bfd_arch_m68k;
238 abfd->obj_machine = 68010;
239 break;
240
241 case M_68020:
242 abfd->obj_arch = bfd_arch_m68k;
243 abfd->obj_machine = 68020;
244 break;
245
246 case M_SPARC:
247 abfd->obj_arch = bfd_arch_sparc;
248 break;
249
250 case M_386:
251 abfd->obj_arch = bfd_arch_i386;
252 break;
253
254 case M_29K:
255 abfd->obj_arch = bfd_arch_a29k;
256 break;
257
258 default:
259 abfd->obj_arch = bfd_arch_obscure;
260 break;
261 }
262
263 bfd_get_start_address (abfd) = execp->a_entry;
264
265 /* Remember the positions of the string table and symbol table. */
266 obj_str_filepos (abfd) = n_stroff (&anexec);
267 obj_sym_filepos (abfd) = n_symoff (&anexec);
268
269 /* create the sections. This is raunchy, but bfd_close wants to reclaim
270 them */
271 obj_textsec (abfd) = (asection *)NULL;
272 obj_datasec (abfd) = (asection *)NULL;
273 obj_bsssec (abfd) = (asection *)NULL;
274 obj_aout_symbols(abfd) = (aout_symbol_type *)NULL;
275 (void)bfd_make_section(abfd, ".text");
276 (void)bfd_make_section(abfd, ".data");
277 (void)bfd_make_section(abfd, ".bss");
278
279 obj_datasec (abfd)->size = execp->a_data;
280 obj_bsssec (abfd)->size = execp->a_bss;
281 obj_textsec (abfd)->size = execp->a_text;
282 obj_datasec (abfd)->vma = N_DATADDR(anexec);
283 obj_bsssec (abfd)->vma = N_BSSADDR(anexec);
284 obj_textsec (abfd)->vma = N_TXTADDR(anexec);
285
286 obj_textsec (abfd)->filepos = n_txtoff(&anexec);
287 obj_datasec (abfd)->filepos = n_datoff(&anexec);
288
289 obj_textsec (abfd)->rel_filepos = n_treloff(&anexec);
290 obj_datasec (abfd)->rel_filepos = n_dreloff(&anexec);
291
292 obj_textsec (abfd)->flags =
293 (execp->a_trsize != 0 ?
294 (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_HAS_CONTENTS) :
295 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS));
296 obj_datasec (abfd)->flags =
297 (execp->a_drsize != 0 ?
298 (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_HAS_CONTENTS) :
299 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS));
300 obj_bsssec (abfd)->flags = SEC_ALLOC;
301
302 abfd->sections = obj_textsec (abfd);
303 obj_textsec (abfd)->next = obj_datasec (abfd);
304 obj_datasec (abfd)->next = obj_bsssec (abfd);
305 return abfd->xvec;
306 }
307
308
309 boolean
310 sunos4_mkobject (abfd)
311 bfd *abfd;
312 {
313 char *rawptr;
314
315 bfd_error = system_call_error;
316
317 /* Use an intermediate variable for clarity */
318 rawptr = zalloc (sizeof (struct sunexdata) + sizeof (struct exec));
319
320 if (rawptr == NULL) {
321 bfd_error = no_memory;
322 return false;
323 }
324
325 abfd->tdata = (void *)((struct sunexdata *) rawptr);
326 exec_hdr (abfd) = (struct exec *) (rawptr + sizeof (struct sunexdata));
327
328 /* For simplicity's sake we just make all the sections right here. */
329
330 obj_textsec (abfd) = (asection *)NULL;
331 obj_datasec (abfd) = (asection *)NULL;
332 obj_bsssec (abfd) = (asection *)NULL;
333 bfd_make_section (abfd, ".text");
334 bfd_make_section (abfd, ".data");
335 bfd_make_section (abfd, ".bss");
336
337 return true;
338 }
339
340 /* Keep track of machine architecture and machine type for a.out's.
341 Return the machine_type for a particular arch&machine, or M_UNKNOWN
342 if that exact arch&machine can't be represented in a.out format.
343
344 If the architecture is understood, machine type 0 (default) should
345 always be understood. */
346
347 static enum machine_type
348 aout_machine_type (arch, machine)
349 enum bfd_architecture arch;
350 unsigned long machine;
351 {
352 enum machine_type arch_flags;
353
354 arch_flags = M_UNKNOWN;
355
356 switch (arch) {
357 case bfd_arch_sparc:
358 if (machine == 0) arch_flags = M_SPARC;
359 break;
360
361 case bfd_arch_m68k:
362 switch (machine) {
363 case 0: arch_flags = M_UNKNOWN; break;
364 case 68000: arch_flags = M_UNKNOWN; break;
365 case 68010: arch_flags = M_68010; break;
366 case 68020: arch_flags = M_68020; break;
367 default: arch_flags = M_UNKNOWN; break;
368 }
369 break;
370
371 case bfd_arch_i386:
372 if (machine == 0) arch_flags = M_386;
373 break;
374
375 case bfd_arch_a29k:
376 if (machine == 0) arch_flags = M_29K;
377 break;
378
379 default:
380 arch_flags = M_UNKNOWN;
381 break;
382 }
383 return arch_flags;
384 }
385
386 boolean
387 sunos4_set_arch_mach (abfd, arch, machine)
388 bfd *abfd;
389 enum bfd_architecture arch;
390 unsigned long machine;
391 {
392 abfd->obj_arch = arch;
393 abfd->obj_machine = machine;
394 if (arch != bfd_arch_unknown &&
395 aout_machine_type (arch, machine) == M_UNKNOWN)
396 return false; /* We can't represent this type */
397 return true; /* We're easy ... */
398 }
399
400 boolean
401 sunos4_write_object_contents (abfd)
402 bfd *abfd;
403 {
404 int data_pad = 0;
405 struct exec *execp = exec_hdr (abfd);
406
407
408
409 /* Magic number, maestro, please! */
410 switch (bfd_get_architecture(abfd)) {
411 case bfd_arch_m68k:
412 switch (bfd_get_machine(abfd)) {
413 case 68010:
414 N_SET_MACHTYPE(*execp, M_68010);
415 break;
416 default:
417 case 68020:
418 N_SET_MACHTYPE(*execp, M_68020);
419 break;
420 }
421 break;
422 case bfd_arch_sparc:
423 N_SET_MACHTYPE(*execp, M_SPARC);
424 break;
425 case bfd_arch_i386:
426 N_SET_MACHTYPE(*execp, M_386);
427 break;
428 case bfd_arch_a29k:
429 N_SET_MACHTYPE(*execp, M_29K);
430 break;
431 default:
432 N_SET_MACHTYPE(*execp, M_UNKNOWN);
433 }
434 execp->a_text = obj_textsec (abfd)->size;
435 N_SET_MAGIC (*execp, OMAGIC);
436 if (abfd->flags & D_PAGED) {
437 execp->a_text = obj_textsec (abfd)->size + sizeof(struct exec);
438 N_SET_MAGIC (*execp, ZMAGIC);
439 } else if (abfd->flags & WP_TEXT) {
440 N_SET_MAGIC (*execp, NMAGIC);
441 }
442 N_SET_FLAGS (*execp, 0x1); /* copied from ld.c; who the hell knows? */
443
444 if (abfd->flags & D_PAGED)
445 {
446 data_pad = ((obj_datasec(abfd)->size + PAGE_SIZE -1)
447 & (- PAGE_SIZE)) - obj_datasec(abfd)->size;
448
449 if (data_pad > obj_bsssec(abfd)->size)
450 execp->a_bss = 0;
451 else
452 execp->a_bss = obj_bsssec(abfd)->size - data_pad;
453 execp->a_data = obj_datasec(abfd)->size + data_pad;
454
455 }
456 else {
457 execp->a_data = obj_datasec (abfd)->size;
458 execp->a_bss = obj_bsssec (abfd)->size;
459 }
460
461 execp->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist);
462 execp->a_entry = bfd_get_start_address (abfd);
463 execp->a_trsize = ((obj_textsec (abfd)->reloc_count) *
464 sizeof (struct reloc_info_extended));
465 execp->a_drsize = ((obj_datasec (abfd)->reloc_count) *
466 sizeof (struct reloc_info_extended));;
467
468 swap_exec_header (abfd, execp);
469
470 bfd_seek (abfd, 0L, SEEK_SET);
471 bfd_write ((void *) execp, 1, sizeof (struct exec), abfd);
472
473 /* Now write out reloc info, followed by syms and strings */
474
475 if (bfd_get_symcount (abfd) != 0)
476 {
477 bfd_seek (abfd,
478 (long)(N_SYMOFF(*execp)), SEEK_SET);
479
480 sunos4_write_syms (abfd);
481
482 bfd_seek (abfd, (long)(N_TROFF(*execp)), SEEK_SET);
483
484 if (!sunos4_squirt_out_relocs (abfd, obj_textsec (abfd))) return false;
485 bfd_seek (abfd, (long)(N_DROFF(*execp)), SEEK_SET);
486
487 if (!sunos4_squirt_out_relocs (abfd, obj_datasec (abfd))) return false;
488 }
489 return true;
490 }
491
492 static void
493 swap_exec_header (abfd, execp)
494 bfd *abfd;
495 struct exec *execp;
496 {
497 if (bfd_header_twiddle_required(abfd)) {
498 /* execp->a_info = bfd_h_getlong (abfd, &execp->a_info); */
499 *(unsigned long *) execp =
500 bfd_h_getlong (abfd, (unsigned long *) execp);
501 execp->a_text = bfd_h_getlong (abfd, &execp->a_text);
502 execp->a_data = bfd_h_getlong (abfd, &execp->a_data);
503 execp->a_bss = bfd_h_getlong (abfd, &execp->a_bss);
504 execp->a_syms = bfd_h_getlong (abfd, &execp->a_syms);
505 execp->a_entry = bfd_h_getlong (abfd, &execp->a_entry);
506 execp->a_trsize = bfd_h_getlong (abfd, &execp->a_trsize);
507 execp->a_drsize = bfd_h_getlong (abfd, &execp->a_drsize);
508 }
509 } /* swap_exec_header() */
510 \f
511 /** core files */
512
513 #define CORE_MAGIC 0x080456
514 #define CORE_NAMELEN 16
515
516 /* The core structure is taken from the Sun documentation.
517 Unfortunately, they don't document the FPA structure, or at least I
518 can't find it easily. Fortunately the core header contains its own
519 length. So this shouldn't cause problems, except for c_ucode, which
520 so far we don't use but is easy to find with a little arithmetic. */
521
522 /* But the reg structure can be gotten from the SPARC processor handbook.
523 This really should be in a GNU include file though so that gdb can use
524 the same info. */
525 struct regs {
526 int r_psr;
527 int r_pc;
528 int r_npc;
529 int r_y;
530 int r_g1;
531 int r_g2;
532 int r_g3;
533 int r_g4;
534 int r_g5;
535 int r_g6;
536 int r_g7;
537 int r_o0;
538 int r_o1;
539 int r_o2;
540 int r_o3;
541 int r_o4;
542 int r_o5;
543 int r_o6;
544 int r_o7;
545 };
546
547 /* Taken from Sun documentation: */
548
549 struct core {
550 int c_magic; /* Corefile magic number */
551 int c_len; /* Sizeof (struct core) */
552 struct regs c_regs; /* General purpose registers */
553 struct exec c_aouthdr; /* A.out header */
554 int c_signo; /* Killing signal, if any */
555 int c_tsize; /* Text size (bytes) */
556 int c_dsize; /* Data size (bytes) */
557 int c_ssize; /* Stack size (bytes) */
558 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
559 double fp_stuff[1]; /* external FPU state (size unknown by us) */
560 /* The type "double" is critical here, for alignment.
561 SunOS declares a struct here, but the struct's
562 alignment is double since it contains doubles. */
563 int c_ucode; /* Exception no. from u_code */
564 /* (this member not accessible by name since we don't
565 portably know the size of fp_stuff.) */
566 };
567
568 /* Supposedly the user stack grows downward from the bottom of kernel memory.
569 Presuming that this remains true, this definition will work. */
570 #define USRSTACK (-(128*1024*1024))
571
572 PROTO (static void, swapcore, (bfd *abfd, struct core *core));
573
574 /* need this cast b/c ptr is really void * */
575 #define core_hdr(bfd) (((struct suncordata *) (bfd->tdata))->hdr)
576 #define core_datasec(bfd) (((struct suncordata *) ((bfd)->tdata))->data_section)
577 #define core_stacksec(bfd) (((struct suncordata*)((bfd)->tdata))->stack_section)
578 #define core_regsec(bfd) (((struct suncordata *) ((bfd)->tdata))->reg_section)
579 #define core_regsec2(bfd) (((struct suncordata *) ((bfd)->tdata))->reg2_section)
580
581 /* These are stored in the bfd's tdata */
582 struct suncordata {
583 struct core *hdr; /* core file header */
584 asection *data_section;
585 asection *stack_section;
586 asection *reg_section;
587 asection *reg2_section;
588 };
589
590 bfd_target *
591 sunos4_core_file_p (abfd)
592 bfd *abfd;
593 {
594 /* includes redundent variables for code clarity */
595 int core_size;
596 int core_mag;
597 struct core *core;
598 char *rawptr;
599
600 bfd_error = system_call_error;
601
602 if (bfd_read ((void *)&core_mag, 1, sizeof (int), abfd) != sizeof (int))
603 return 0;
604 core_mag = bfd_h_getlong(abfd, &core_mag);
605
606 if (core_mag != CORE_MAGIC) return 0;
607
608 /* SunOS core headers can vary in length; second word is size; */
609 if (bfd_read ((void *)&core_size, 1, sizeof (int), abfd) != sizeof (int))
610 return 0;
611 core_size = bfd_h_getlong(abfd, &core_size);
612
613 if (bfd_seek (abfd, 0L, SEEK_SET) < 0) return 0;
614
615 rawptr = zalloc (core_size + sizeof (struct suncordata));
616 if (rawptr == NULL) {
617 bfd_error = no_memory;
618 return 0;
619 }
620
621 core = (struct core *) (rawptr + sizeof (struct suncordata));
622
623 if ((bfd_read ((void *) core, 1, core_size, abfd)) != core_size) {
624 bfd_error = system_call_error;
625 free ((void *)rawptr);
626 return 0;
627 }
628
629 swapcore (abfd, core);
630 abfd->tdata = (void *)((struct suncordata *) rawptr);
631 core_hdr (abfd) = core;
632
633 /* create the sections. This is raunchy, but bfd_close wants to reclaim
634 them */
635 core_stacksec (abfd) = (asection *) zalloc (sizeof (asection));
636 if (core_stacksec (abfd) == NULL) {
637 loser:
638 bfd_error = no_memory;
639 free ((void *)rawptr);
640 return 0;
641 }
642 core_datasec (abfd) = (asection *) zalloc (sizeof (asection));
643 if (core_datasec (abfd) == NULL) {
644 loser1:
645 free ((void *)core_stacksec (abfd));
646 goto loser;
647 }
648 core_regsec (abfd) = (asection *) zalloc (sizeof (asection));
649 if (core_regsec (abfd) == NULL) {
650 loser2:
651 free ((void *)core_datasec (abfd));
652 goto loser1;
653 }
654 core_regsec2 (abfd) = (asection *) zalloc (sizeof (asection));
655 if (core_regsec2 (abfd) == NULL) {
656 free ((void *)core_regsec (abfd));
657 goto loser2;
658 }
659
660 core_stacksec (abfd)->name = ".stack";
661 core_datasec (abfd)->name = ".data";
662 core_regsec (abfd)->name = ".reg";
663 core_regsec2 (abfd)->name = ".reg2";
664
665 core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD;
666 core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD;
667 core_regsec (abfd)->flags = SEC_ALLOC;
668 core_regsec2 (abfd)->flags = SEC_ALLOC;
669
670 core_stacksec (abfd)->size = core->c_ssize;
671 core_datasec (abfd)->size = core->c_dsize;
672 core_regsec (abfd)->size = (sizeof core->c_regs);
673 /* Float regs take up end of struct, except c_ucode. */
674 core_regsec2 (abfd)->size = core_size - (sizeof core->c_ucode) -
675 (file_ptr)(((struct core *)0)->fp_stuff);
676
677 core_stacksec (abfd)->vma = (USRSTACK - core->c_ssize);
678 core_datasec (abfd)->vma = N_DATADDR(core->c_aouthdr);
679 core_regsec (abfd)->vma = -1;
680 core_regsec2 (abfd)->vma = -1;
681
682 core_stacksec (abfd)->filepos = core->c_len + core->c_dsize;
683 core_datasec (abfd)->filepos = core->c_len;
684 /* In file header: */
685 core_regsec (abfd)->filepos = (file_ptr)(&((struct core *)0)->c_regs);
686 core_regsec2 (abfd)->filepos = (file_ptr)(((struct core *)0)->fp_stuff);
687
688 /* Align to word at least */
689 core_stacksec (abfd)->alignment_power = 2;
690 core_datasec (abfd)->alignment_power = 2;
691 core_regsec (abfd)->alignment_power = 2;
692 core_regsec2 (abfd)->alignment_power = 2;
693
694 abfd->sections = core_stacksec (abfd);
695 core_stacksec (abfd)->next = core_datasec (abfd);
696 core_datasec (abfd)->next = core_regsec (abfd);
697 core_regsec (abfd)->next = core_regsec2 (abfd);
698
699 abfd->section_count = 4;
700
701 return abfd->xvec;
702 }
703
704 char *
705 sunos4_core_file_failing_command (abfd)
706 bfd *abfd;
707 {
708 return core_hdr (abfd)->c_cmdname;
709 }
710
711 int
712 sunos4_core_file_failing_signal (abfd)
713 bfd *abfd;
714 {
715 return core_hdr (abfd)->c_signo;
716 }
717
718 boolean
719 sunos4_core_file_matches_executable_p (core_bfd, exec_bfd)
720 bfd *core_bfd, *exec_bfd;
721 {
722 if (core_bfd->xvec != exec_bfd->xvec) {
723 bfd_error = system_call_error;
724 return false;
725 }
726
727 return (bcmp (&core_hdr (core_bfd), &exec_hdr (exec_bfd),
728 sizeof (struct exec)) == 0) ? true : false;
729 }
730
731 /* byte-swap core structure */
732 static void
733 swapcore (abfd, core)
734 bfd *abfd;
735 struct core *core;
736 {
737 if (bfd_header_twiddle_required(abfd)) {
738 core->c_magic = bfd_h_getlong (abfd, &core->c_magic);
739 core->c_len = bfd_h_getlong (abfd, &core->c_len);
740 /* regs */
741 swap_exec_header (abfd, &(core->c_aouthdr));
742 core->c_signo = bfd_h_getlong (abfd, &core->c_signo);
743 core->c_tsize = bfd_h_getlong (abfd, &core->c_tsize);
744 core->c_dsize = bfd_h_getlong (abfd, &core->c_dsize);
745 core->c_ssize = bfd_h_getlong (abfd, &core->c_ssize);
746 core->c_ucode = bfd_h_getlong (abfd, &core->c_ucode);
747 /* I don't understand how to swap an FP register */
748 }
749 }
750 \f
751 /** exec and core file sections */
752
753 boolean
754 sunos4_new_section_hook (abfd, newsect)
755 bfd *abfd;
756 asection *newsect;
757 {
758 /* align to double at least */
759 newsect->alignment_power = 3;
760
761 if (bfd_get_format (abfd) == bfd_object) {
762 if (obj_textsec(abfd) == NULL && !strcmp(newsect->name, ".text")) {
763 obj_textsec(abfd)= newsect;
764 return true;
765 }
766
767 if (obj_datasec(abfd) == NULL && !strcmp(newsect->name, ".data")) {
768 obj_datasec(abfd) = newsect;
769 return true;
770 }
771
772 if (obj_bsssec(abfd) == NULL && !strcmp(newsect->name, ".bss")) {
773 obj_bsssec(abfd) = newsect;
774 return true;
775 }
776 }
777
778 #if 0 /* FIXME -- this is temporary for steve */
779 bfd_error = invalid_operation;
780
781 return false;
782 #endif
783
784 /* We allow more than three sections internally */
785 return true;
786 }
787
788 boolean
789 sunos4_set_section_contents (abfd, section, location, offset, count)
790 bfd *abfd;
791 sec_ptr section;
792 unsigned char *location;
793 file_ptr offset;
794 int count;
795 {
796 if (abfd->output_has_begun == false)
797 { /* set by bfd.c handler */
798 if ((obj_textsec (abfd) == NULL) || (obj_datasec (abfd) == NULL)
799
800 /*||
801 (obj_textsec (abfd)->size == 0) || (obj_datasec (abfd)->size=
802 0)*/
803 )
804 {
805 bfd_error = invalid_operation;
806 return false;
807 }
808
809
810 #if 0
811 if (abfd->flags & D_PAGED)
812 {
813 obj_textsec (abfd)->filepos = sizeof(struct exec);
814 obj_datasec(abfd)->filepos = obj_textsec (abfd)->size;
815 }
816 else
817 #endif
818 {
819 obj_textsec (abfd)->filepos = sizeof(struct exec);
820 obj_datasec(abfd)->filepos = obj_textsec(abfd)->filepos + obj_textsec (abfd)->size;
821
822 }
823 }
824 /* regardless, once we know what we're doing, we might as well get going */
825
826 bfd_seek (abfd, section->filepos + offset, SEEK_SET);
827
828 if (count) {
829 return (bfd_write ((void *)location, 1, count, abfd) == count) ? true : false;
830 }
831 return false;
832 }
833 boolean
834 sunos4_get_section_contents (abfd, section, location, offset, count)
835 bfd *abfd;
836 sec_ptr section;
837 void *location;
838 file_ptr offset;
839 int count;
840 {
841 if (count) {
842 if (offset >= section->size) return false;
843
844 bfd_seek (abfd, section->filepos + offset, SEEK_SET);
845
846 return (bfd_read (location, 1, count, abfd) == count) ? true:false;
847 }
848 else return true;
849 }
850
851 \f
852 /* Classify stabs symbols */
853
854
855 #define sym_in_text_section(sym) \
856 (((sym)->n_type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_TEXT)
857
858 #define sym_in_data_section(sym) \
859 (((sym)->n_type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_DATA)
860
861 #define sym_in_bss_section(sym) \
862 (((sym)->n_type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_BSS)
863
864 /* Symbol is undefined if type is N_UNDF|N_EXT and if it has
865 zero in the "value" field. Nonzeroes there are fortrancommon
866 symbols. */
867 #define sym_is_undefined(sym) \
868 ((sym)->n_type == (N_UNDF | N_EXT) && (sym)->n_value == 0)
869
870 /* Symbol is a global definition if N_EXT is on and if it has
871 a nonzero type field. */
872 #define sym_is_global_defn(sym) \
873 (((sym)->n_type & N_EXT) && (sym)->n_type & N_TYPE)
874
875 /* Symbol is debugger info if any bits outside N_TYPE or N_EXT
876 are on. */
877 #define sym_is_debugger_info(sym) \
878 ((sym)->n_type & ~(N_EXT | N_TYPE))
879
880 #define sym_is_fortrancommon(sym) \
881 (((sym)->n_type == (N_EXT)) && (sym)->n_value != 0)
882
883 /* Symbol is absolute if it has N_ABS set */
884 #define sym_is_absolute(sym) \
885 (((sym)->n_type & N_TYPE)== N_ABS)
886
887
888 #define sym_is_indirect(sym) \
889 (((sym)->n_type & N_ABS)== N_ABS)
890
891 /* Only in their own functions for ease of debugging; when sym flags have
892 stabilised these should be inlined into their (single) caller */
893
894 static void
895 translate_from_native_sym_flags (sym_pointer, cache_ptr, abfd)
896 struct nlist *sym_pointer;
897 aout_symbol_type *cache_ptr;
898 bfd *abfd;
899 {
900 switch (cache_ptr->type & N_TYPE) {
901 case N_SETA:
902 case N_SETT:
903 case N_SETD:
904 case N_SETB:
905 {
906 asection *section = bfd_make_section(abfd,
907 cache_ptr->symbol.name);
908 arelent_chain *reloc = (arelent_chain *)malloc(sizeof(arelent_chain));
909
910 switch ( (cache_ptr->type & N_TYPE) ) {
911 case N_SETA:
912 reloc->relent.section = (asection *)NULL;
913 cache_ptr->symbol.section = (asection *)NULL;
914 break;
915 case N_SETT:
916 reloc->relent.section = (asection *)obj_textsec(abfd);
917 cache_ptr->symbol.value -= reloc->relent.section->vma;
918 break;
919 case N_SETD:
920 reloc->relent.section = (asection *)obj_datasec(abfd);
921 cache_ptr->symbol.value -= reloc->relent.section->vma;
922 break;
923 case N_SETB:
924 reloc->relent.section = (asection *)obj_bsssec(abfd);
925 cache_ptr->symbol.value -= reloc->relent.section->vma;
926 break;
927 }
928 cache_ptr->symbol.section = reloc->relent.section;
929 reloc->relent.addend = cache_ptr->symbol.value ;
930 /*
931 We modify the symbol to belong to a section depending upon the
932 name of the symbol - probably __CTOR__ or __DTOR__ but we don't
933 really care, and add to the size of the section to contain a
934 pointer to the symbol. Build a reloc entry to relocate to this
935 symbol attached to this section.
936 */
937
938
939 section->flags = SEC_CONSTRUCTOR;
940 section->reloc_count++;
941 section->alignment_power = 2;
942 reloc->relent.sym_ptr_ptr = (asymbol **)NULL;
943 reloc->next = section->constructor_chain;
944 section->constructor_chain = reloc;
945 reloc->relent.address = section->size;
946 section->size += sizeof(int *);
947
948 reloc->relent.howto = howto_table +CTOR_TABLE_RELOC_IDX;
949 cache_ptr->symbol.flags |= BSF_DEBUGGING ;
950 }
951 break;
952 default:
953
954 if (sym_is_debugger_info (sym_pointer)) {
955 cache_ptr->symbol.flags = BSF_DEBUGGING ;
956 /* Work out the section correct for this symbol */
957 switch (sym_pointer->n_type & N_TYPE)
958 {
959 case N_TEXT:
960 case N_FN:
961 cache_ptr->symbol.section = obj_textsec (abfd);
962 cache_ptr->symbol.value -= obj_textsec(abfd)->vma;
963 break;
964 case N_DATA:
965 cache_ptr->symbol.value -= obj_datasec(abfd)->vma;
966 cache_ptr->symbol.section = obj_datasec (abfd);
967 break;
968 case N_BSS :
969 cache_ptr->symbol.section = obj_bsssec (abfd);
970 cache_ptr->symbol.value -= obj_bsssec(abfd)->vma;
971 break;
972 case N_ABS:
973 default:
974 cache_ptr->symbol.section = 0;
975 break;
976 }
977 }
978 else {
979 if (sym_is_fortrancommon (sym_pointer))
980 {
981 cache_ptr->symbol.flags = BSF_FORT_COMM;
982 cache_ptr->symbol.section = (asection *)NULL;
983 }
984 else {
985 if (sym_is_undefined (sym_pointer)) {
986 cache_ptr->symbol.flags = BSF_UNDEFINED;
987 }
988 else if (sym_is_global_defn (sym_pointer)) {
989 cache_ptr->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
990 }
991
992 else if (sym_is_absolute (sym_pointer)) {
993 cache_ptr->symbol.flags = BSF_ABSOLUTE;
994 }
995 else {
996 cache_ptr->symbol.flags = BSF_LOCAL;
997 }
998
999 /* In a.out, the value of a symbol is always relative to the
1000 * start of the file, if this is a data symbol we'll subtract
1001 * the size of the text section to get the section relative
1002 * value. If this is a bss symbol (which would be strange)
1003 * we'll subtract the size of the previous two sections
1004 * to find the section relative address.
1005 */
1006
1007 if (sym_in_text_section (sym_pointer)) {
1008 cache_ptr->symbol.value -= obj_textsec(abfd)->vma;
1009 cache_ptr->symbol.section = obj_textsec (abfd);
1010 }
1011 else if (sym_in_data_section (sym_pointer)){
1012 cache_ptr->symbol.value -= obj_datasec(abfd)->vma;
1013 cache_ptr->symbol.section = obj_datasec (abfd);
1014 }
1015 else if (sym_in_bss_section(sym_pointer)) {
1016 cache_ptr->symbol.section = obj_bsssec (abfd);
1017 cache_ptr->symbol.value -= obj_bsssec(abfd)->vma;
1018 }
1019 else {
1020 cache_ptr->symbol.section = (asection *)NULL;
1021 cache_ptr->symbol.flags |= BSF_ABSOLUTE;
1022 }
1023 }
1024 }
1025 }
1026 }
1027 void
1028 translate_to_native_sym_flags (sym_pointer, cache_ptr_g, abfd)
1029 struct nlist *sym_pointer;
1030 generic_symbol_type *cache_ptr_g;
1031 bfd *abfd;
1032 {
1033 asymbol *cache_ptr = (asymbol *)cache_ptr_g;
1034
1035 /* FIXME check for wrigin bss */
1036 if (bfd_get_section(cache_ptr)) {
1037 if (bfd_get_output_section(cache_ptr) == obj_bsssec (abfd)) {
1038 sym_pointer->n_type |= N_BSS;
1039 }
1040 else if (bfd_get_output_section(cache_ptr) == obj_datasec (abfd)) {
1041 sym_pointer->n_type |= N_DATA;
1042 }
1043 else if (bfd_get_output_section(cache_ptr) == obj_textsec (abfd)) {
1044 sym_pointer->n_type |= N_TEXT;
1045 }
1046 else {
1047 BFD_ASSERT(0);
1048 }
1049 /* Turn the symbol from section relative to absolute again */
1050 sym_pointer->n_value +=
1051 cache_ptr->section->output_section->vma
1052 + cache_ptr->section->output_offset ;
1053 }
1054 else {
1055 sym_pointer->n_type |= N_ABS;
1056 }
1057
1058 if (cache_ptr->flags & (BSF_FORT_COMM | BSF_UNDEFINED)) {
1059 sym_pointer->n_type = (N_UNDF | N_EXT);
1060 return;
1061 }
1062
1063 if (cache_ptr->flags & BSF_ABSOLUTE) {
1064 sym_pointer->n_type |= N_ABS;
1065 }
1066
1067 if (cache_ptr->flags & (BSF_GLOBAL | BSF_EXPORT)) {
1068 sym_pointer->n_type |= N_EXT;
1069 }
1070 if (cache_ptr->flags & BSF_DEBUGGING) {
1071 sym_pointer->n_type = ((aout_symbol_type *)cache_ptr)->type;
1072 }
1073
1074 }
1075 \f
1076 /* Native-level interface to symbols. */
1077
1078 /* We read the symbols into a buffer, which is discarded when this
1079 function exits. We read the strings into a buffer large enough to
1080 hold them all plus all the cached symbol entries. */
1081
1082 asymbol *
1083 sunos4_make_empty_symbol (abfd)
1084 bfd *abfd;
1085 {
1086 aout_symbol_type *new =
1087 (aout_symbol_type *)zalloc (sizeof (aout_symbol_type));
1088 new->symbol.the_bfd = abfd;
1089
1090 return &new->symbol;
1091 }
1092
1093 boolean
1094 sunos4_slurp_symbol_table (abfd)
1095 bfd *abfd;
1096 {
1097 unsigned int symbol_count;
1098 size_t symbol_size;
1099 size_t string_size;
1100 struct nlist *syms;
1101 char *strings;
1102 aout_symbol_type *cached;
1103
1104 /* If there's no work to be done, don't do any */
1105 if (obj_aout_symbols (abfd) != (aout_symbol_type *)NULL) return true;
1106 symbol_size = exec_hdr(abfd)->a_syms;
1107 if (symbol_size == 0) {
1108 bfd_error = no_symbols;
1109 return false;
1110 }
1111
1112 bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET);
1113 if (bfd_read ((void *)&string_size, 4, 1, abfd) != 4)
1114 return false;
1115 string_size = bfd_h_getlong (abfd, (unsigned char *)&string_size);
1116
1117 symbol_count = symbol_size / sizeof (struct nlist);
1118
1119 /* Malloc (should alloca) space for native symbols, and
1120 malloc space for string table and symbol cache. */
1121
1122 syms = (struct nlist *) zalloc (symbol_size);
1123 if (syms == NULL) {
1124 bfd_error = no_memory;
1125 return false;
1126 }
1127
1128 cached = (aout_symbol_type *) zalloc ((size_t)(string_size + 1 +
1129 (symbol_count * sizeof (aout_symbol_type))));
1130 if (cached == NULL) {
1131 bfd_error = no_memory;
1132 free ((void *)syms);
1133 return false;
1134 }
1135
1136 strings = ((char *) cached) + (symbol_count * sizeof (aout_symbol_type));
1137
1138 bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET);
1139 if (bfd_read ((void *)syms, 1, symbol_size, abfd) != symbol_size) {
1140 bailout:
1141 free ((void *)cached);
1142 free ((void*)syms);
1143 return false;
1144 }
1145
1146 bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET);
1147 if (bfd_read ((void *)strings, 1, string_size, abfd) != string_size) {
1148 goto bailout;
1149 }
1150
1151 /* OK, now walk the new symtable, cacheing symbol properties */
1152 {
1153 register struct nlist *sym_pointer;
1154 register struct nlist *sym_end = syms + symbol_count;
1155 register aout_symbol_type *cache_ptr = cached;
1156
1157 if (bfd_header_twiddle_required (abfd) == true) {
1158 /* run through the table and byte swap if needed */
1159 for (sym_pointer = syms; sym_pointer < sym_end; sym_pointer++) {
1160 sym_pointer->n_un.n_strx =
1161 bfd_h_get_x (abfd, &sym_pointer->n_un.n_strx);
1162 sym_pointer->n_desc =
1163 bfd_h_get_x (abfd, &sym_pointer->n_desc);
1164 sym_pointer->n_value =
1165 bfd_h_get_x (abfd, &sym_pointer->n_value);
1166 sym_pointer->n_other = (char)
1167 bfd_h_get_x(abfd, &sym_pointer->n_other);
1168 sym_pointer->n_type = (char)
1169 bfd_h_get_x(abfd, &sym_pointer->n_type);
1170
1171 }
1172 }
1173 /* Run through table and copy values */
1174 for (sym_pointer = syms, cache_ptr = cached;
1175 sym_pointer < sym_end; sym_pointer++, cache_ptr++)
1176 {
1177 cache_ptr->symbol.the_bfd = abfd;
1178 if (sym_pointer->n_un.n_strx)
1179 cache_ptr->symbol.name = sym_pointer->n_un.n_strx + strings;
1180 else
1181 cache_ptr->symbol.name = (char *)NULL;
1182 cache_ptr->symbol.value = sym_pointer->n_value;
1183 cache_ptr->desc = sym_pointer->n_desc;
1184 cache_ptr->other = sym_pointer->n_other;
1185 cache_ptr->type = sym_pointer->n_type;
1186 cache_ptr->symbol.udata = 0;
1187 translate_from_native_sym_flags (sym_pointer, cache_ptr, abfd);
1188
1189 }
1190 }
1191
1192 obj_aout_symbols (abfd) = cached;
1193 bfd_get_symcount (abfd) = symbol_count;
1194 free ((void *)syms);
1195
1196 return true;
1197 }
1198
1199 \f
1200 void
1201 sunos4_write_syms (abfd)
1202 bfd *abfd;
1203 {
1204 unsigned int count ;
1205 asymbol **generic = bfd_get_outsymbols (abfd);
1206
1207 unsigned int stindex = sizeof(stindex); /* initial string length */
1208
1209 for (count = 0; count < bfd_get_symcount (abfd); count++) {
1210 asymbol *g = generic[count];
1211 struct nlist nsp;
1212
1213 if (g->name) {
1214 unsigned int length = strlen(g->name) +1;
1215 bfd_h_putlong (abfd, stindex, (unsigned char *)&nsp.n_un.n_strx);
1216 stindex += length;
1217 }
1218 else {
1219 bfd_h_putlong (abfd, 0, (unsigned char *)&nsp.n_un.n_strx);
1220 }
1221
1222 if (g->the_bfd->xvec->flavour == abfd->xvec->flavour)
1223 {
1224 nsp.n_desc = aout_symbol( g)->desc;
1225 nsp.n_other = aout_symbol(g)->other;
1226 nsp.n_type = aout_symbol(g)->type;
1227 }
1228 else
1229 {
1230 nsp.n_desc = 0;
1231 nsp.n_other = 0;
1232 nsp.n_type = 0;
1233 }
1234
1235
1236 nsp.n_value = g->value;
1237 translate_to_native_sym_flags (&nsp, (generic_symbol_type *)g, abfd);
1238
1239
1240 bfd_h_putshort (abfd, nsp.n_desc, (unsigned char *)&nsp.n_desc);
1241 bfd_h_putlong (abfd, nsp.n_value, (unsigned char *)&nsp.n_value);
1242 bfd_write((void *)&nsp,1, sizeof(nsp), abfd);
1243 }
1244
1245
1246 /* Now output the strings. Be sure to put string length into correct
1247 * byte ordering before writing it.
1248 */
1249 bfd_h_putlong (abfd, stindex, (unsigned char *)&stindex);
1250
1251 bfd_write((void *)&stindex, 1, sizeof(stindex), abfd);
1252
1253 generic = bfd_get_outsymbols(abfd);
1254 for (count = 0; count < bfd_get_symcount(abfd); count++)
1255 {
1256 asymbol *g = *(generic++);
1257
1258 if (g->name != (char *)NULL)
1259 {
1260 size_t length = strlen(g->name)+1;
1261 bfd_write((void *)g->name, 1, length, abfd);
1262 }
1263 if ((g->flags & BSF_FAKE)==0) {
1264 g->name = itos(count); /* smash the generic symbol */
1265 }
1266 }
1267 }
1268
1269 \f
1270 void
1271 sunos4_reclaim_symbol_table (abfd)
1272 bfd *abfd;
1273 {
1274 asection *section;
1275
1276 if (!bfd_get_symcount (abfd)) return;
1277
1278 for (section = abfd->sections;
1279 section != (asection *) NULL;
1280 section = section->next)
1281 if (section->relocation) {
1282 free ((void *)section->relocation);
1283 section->relocation = NULL;
1284 section->reloc_count = 0;
1285 }
1286
1287 bfd_get_symcount (abfd) = 0;
1288 free ((void *)obj_aout_symbols (abfd));
1289 obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
1290 }
1291 \f
1292 unsigned int
1293 sunos4_get_symtab_upper_bound (abfd)
1294 bfd *abfd;
1295 {
1296 if (!sunos4_slurp_symbol_table (abfd)) return 0;
1297
1298 return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
1299 }
1300
1301 unsigned int
1302 sunos4_get_symtab (abfd, location)
1303 bfd *abfd;
1304 asymbol **location;
1305 {
1306 unsigned int counter = 0;
1307 aout_symbol_type *symbase;
1308
1309 if (!sunos4_slurp_symbol_table (abfd)) return 0;
1310
1311 for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);)
1312 *(location++) = (asymbol *)( symbase++);
1313 *location++ =0;
1314 return bfd_get_symcount(abfd);
1315 }
1316
1317 \f
1318 /* Obsolete procedural interface; better to look at the cache directly */
1319
1320 /* User should have checked the file flags; perhaps we should return
1321 BFD_NO_MORE_SYMBOLS if there are none? */
1322
1323 int
1324 sunos4_get_symcount_upper_bound (abfd)
1325 bfd *abfd;
1326 {
1327 /* In case we're doing an output file or something...? */
1328 if (bfd_get_symcount (abfd)) return bfd_get_symcount (abfd);
1329
1330 return (exec_hdr (abfd)->a_syms) / (sizeof (struct nlist));
1331 }
1332
1333 symindex
1334 sunos4_get_first_symbol (ignore_abfd)
1335 bfd * ignore_abfd;
1336 {
1337 return 0;
1338 }
1339
1340 symindex
1341 sunos4_get_next_symbol (abfd, oidx)
1342 bfd *abfd;
1343 symindex oidx;
1344 {
1345 if (oidx == BFD_NO_MORE_SYMBOLS) return BFD_NO_MORE_SYMBOLS;
1346 return ++oidx >= bfd_get_symcount (abfd) ? BFD_NO_MORE_SYMBOLS : oidx;
1347 }
1348
1349 char *
1350 sunos4_symbol_name (abfd, idx)
1351 bfd *abfd;
1352 symindex idx;
1353 {
1354 return (obj_aout_symbols (abfd) + idx)->symbol.name;
1355 }
1356
1357 long
1358 sunos4_symbol_value (abfd, idx)
1359 bfd *abfd;
1360 symindex idx;
1361 {
1362 return (obj_aout_symbols (abfd) + idx)->symbol.value;
1363 }
1364
1365 symclass
1366 sunos4_classify_symbol (abfd, idx)
1367 bfd *abfd;
1368 symindex idx;
1369 {
1370 aout_symbol_type *sym = obj_aout_symbols (abfd) + idx;
1371
1372 if ((sym->symbol.flags & BSF_FORT_COMM) != 0) return bfd_symclass_fcommon;
1373 if ((sym->symbol.flags & BSF_GLOBAL) != 0) return bfd_symclass_global;
1374 if ((sym->symbol.flags & BSF_DEBUGGING) != 0) return bfd_symclass_debugger;
1375 if ((sym->symbol.flags & BSF_UNDEFINED) != 0) return bfd_symclass_undefined;
1376
1377 return bfd_symclass_unknown;
1378 }
1379
1380 boolean
1381 sunos4_symbol_hasclass (abfd, idx, class)
1382 bfd *abfd;
1383 symindex idx;
1384 symclass class;
1385 {
1386 aout_symbol_type *sym = obj_aout_symbols (abfd) + idx;
1387 switch (class) {
1388 case bfd_symclass_fcommon:
1389 return (sym->symbol.flags & BSF_FORT_COMM) ? true :false;
1390 case bfd_symclass_global:
1391 return (sym->symbol.flags & BSF_GLOBAL) ? true:false;
1392 case bfd_symclass_debugger:
1393 return (sym->symbol.flags & BSF_DEBUGGING) ? true:false;;
1394 case bfd_symclass_undefined:
1395 return (sym->symbol.flags & BSF_UNDEFINED) ? true:false;;
1396 default: return false;
1397 }
1398 }
1399 \f
1400 /** Some reloc hackery */
1401
1402
1403 boolean
1404 sunos4_slurp_reloc_table (abfd, asect, symbols)
1405 bfd *abfd;
1406 sec_ptr asect;
1407 asymbol **symbols;
1408 {
1409 unsigned int count;
1410 size_t reloc_size;
1411 struct reloc_info_extended *relocs;
1412 arelent *reloc_cache;
1413
1414 if (asect->relocation) return true;
1415
1416 if (asect->flags & SEC_CONSTRUCTOR) return true;
1417
1418 if (asect == obj_datasec (abfd)) {
1419 reloc_size = exec_hdr(abfd)->a_drsize;
1420 goto doit;
1421 }
1422
1423 if (asect == obj_textsec (abfd)) {
1424 reloc_size = exec_hdr(abfd)->a_trsize;
1425 goto doit;
1426 }
1427
1428 bfd_error = invalid_operation;
1429 return false;
1430
1431 doit:
1432 bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
1433 count = reloc_size / sizeof (struct reloc_info_extended);
1434
1435 relocs = (struct reloc_info_extended *) malloc (reloc_size);
1436 if (!relocs) {
1437 bfd_error = no_memory;
1438 return false;
1439 }
1440 reloc_cache = (arelent *) zalloc ((size_t)(count * sizeof (arelent)));
1441 if (reloc_cache == (arelent *)NULL) {
1442 free ((void *)relocs);
1443 bfd_error = no_memory;
1444 return false;
1445 }
1446
1447 if (bfd_read ((void*) relocs, 1, reloc_size, abfd) != reloc_size) {
1448 bfd_error = system_call_error;
1449 free (reloc_cache);
1450 free (relocs);
1451 return false;
1452 }
1453
1454 {
1455 register struct reloc_info_extended *rptr = relocs;
1456 unsigned int counter = 0;
1457 arelent *cache_ptr = reloc_cache;
1458
1459 for (; counter < count; counter++, rptr++, cache_ptr++) {
1460 /* john's old swap_reloc was a hell of a lot hairier... */
1461 /* FIXME, it needs to be!!! */
1462 rptr->r_address = bfd_h_getlong (abfd, &rptr->r_address);
1463 /* FIXME NOW!
1464 * The following can't be done because r_index is a bit field:
1465 *
1466 * rptr->r_index = bfd_h_getlong (abfd, &rptr->r_index);
1467 */
1468 rptr->r_addend = bfd_h_getlong (abfd, &rptr->r_addend);
1469 if (rptr->r_extern) {
1470 /* If this bit is set then the r_index is a index into the symbol table
1471 * if the bit is not set then r_index contains a section map.
1472 * We either fill in the sym entry with a pointer to the symbol,
1473 * or point to the correct section
1474 */
1475
1476 cache_ptr->sym_ptr_ptr = symbols + rptr->r_index;
1477 cache_ptr->addend = rptr->r_addend;
1478 cache_ptr->section = (asection *)NULL;
1479 }
1480 else
1481 {
1482 /* Remember that in a.out symbols are relative to the
1483 beginning of the file rather than sections ? (look in
1484 translate_from_native_sym_flags) The reloc entry addend
1485 has added to it the offset into the file of the data, so
1486 subtract the base to make the reloc section relative */
1487
1488 cache_ptr->sym_ptr_ptr = (asymbol **)NULL;
1489 switch (rptr->r_index) {
1490 case N_TEXT:
1491 case N_TEXT | N_EXT:
1492 cache_ptr->section = obj_textsec(abfd);
1493 cache_ptr->addend = rptr->r_addend - obj_textsec(abfd)->vma ;
1494 break;
1495 case N_DATA:
1496 case N_DATA | N_EXT:
1497 cache_ptr->section = obj_datasec(abfd);
1498 cache_ptr->addend = rptr->r_addend - obj_datasec(abfd)->vma ;
1499 break;
1500 case N_BSS:
1501 case N_BSS | N_EXT:
1502 cache_ptr->section = obj_bsssec(abfd);
1503 cache_ptr->addend = rptr->r_addend - obj_bsssec(abfd)->vma;
1504 break;
1505 case N_ABS:
1506 case N_ABS | N_EXT:
1507 BFD_ASSERT(1);
1508 break;
1509 default:
1510 BFD_ASSERT(1);
1511 break;
1512 }
1513
1514 }
1515
1516 cache_ptr->address = rptr->r_address;
1517 cache_ptr->howto = howto_table + (unsigned int)( rptr->r_type);
1518 }
1519 }
1520
1521 free (relocs);
1522 asect->relocation = reloc_cache;
1523 asect->reloc_count = count;
1524 return true;
1525 }
1526
1527 static boolean
1528 sunos4_squirt_out_relocs (abfd, section)
1529 bfd *abfd;
1530 asection *section;
1531 {
1532 arelent **generic;
1533
1534 unsigned int count = section->reloc_count;
1535 struct reloc_info_extended *native, *natptr;
1536 size_t natsize = count * sizeof (struct reloc_info_extended);
1537
1538 if (count == 0) return true;
1539 generic = section->orelocation;
1540 native = ((struct reloc_info_extended *) zalloc (natsize));
1541 if (!native) {
1542 bfd_error = no_memory;
1543 return false;
1544 }
1545
1546 for (natptr = native; count != 0; --count, ++natptr, ++generic)
1547 {
1548 arelent *g = *generic;
1549
1550 natptr->r_address = g->address;
1551 /* Find a type in the output format which matches the input howto -
1552 * at the moment we assume input format == output format FIXME!!
1553 */
1554 natptr->r_type = (enum reloc_type) g->howto->type;
1555 /* name clobbered by sunos4_write_syms to be symbol index*/
1556
1557 if (g->sym_ptr_ptr != (asymbol **)NULL)
1558 {
1559 if ((*(g->sym_ptr_ptr))->section) {
1560 /* replace the section offset into the addent */
1561 g->addend += (*(g->sym_ptr_ptr))->section->vma;
1562 }
1563
1564 natptr->r_index = stoi((*(g->sym_ptr_ptr))->name);
1565 BFD_ASSERT(natptr->r_index < 0xfff);
1566 natptr->r_extern = 1;
1567 natptr->r_addend = g->addend;
1568 }
1569 else {
1570 natptr->r_extern = 0;
1571 if (g->section == (asection *)NULL) {
1572 BFD_ASSERT(0);
1573 natptr->r_index = N_ABS | N_EXT;
1574 natptr->r_addend = g->addend;
1575 }
1576 else if(g->section->output_section == obj_textsec(abfd)) {
1577 natptr->r_index = N_TEXT | N_EXT;
1578 natptr->r_addend = g->addend + obj_textsec(abfd)->vma;
1579 }
1580 else if (g->section->output_section == obj_datasec(abfd)) {
1581 natptr->r_index = N_DATA | N_EXT;
1582 natptr->r_addend = g->addend + obj_datasec(abfd)->vma;
1583 }
1584 else if (g->section->output_section == obj_bsssec(abfd)) {
1585 natptr->r_index = N_BSS | N_EXT ;
1586 natptr->r_addend = g->addend + obj_bsssec(abfd)->vma;
1587
1588 }
1589 else {
1590 BFD_ASSERT(0);
1591 }
1592 }
1593
1594 if ( bfd_header_twiddle_required(abfd) ){
1595 bfd_h_putlong ( abfd, natptr->r_address, &natptr->r_address );
1596
1597 /* FIXME -- NOW!
1598 *
1599 * I came through here as part of my campaign to make compile-time
1600 * definition of host and target byte orders unnecessary. The new
1601 * code won't even compile because but r_index is a bit field.
1602 * But the simple-minded byte swap that was here before wasn't going
1603 * to work on a bit field anyway.
1604 *
1605 * The old code used to be #ifdef'd on
1606 * TARGET_BYTE_ORDER_BIG_P != HOST_BYTE_ORDER_BIG_P
1607 * Apparently, it was never tested in such a condition: the
1608 * macro invocations here (of swapoutlong) had the wrong number
1609 * of arguments and would never have compiled.
1610 * Chris
1611 bfd_h_putlong ( abfd, natptr->r_index, &natptr->r_index );
1612 */
1613
1614 bfd_h_putlong ( abfd, natptr->r_addend, &natptr->r_addend );
1615 }
1616
1617 }
1618
1619 if ( bfd_write ((void *) native, 1, natsize, abfd) != natsize) {
1620 free(native);
1621 return false;
1622 }
1623 free (native);
1624
1625 return true;
1626 }
1627
1628 /* This is stupid. This function should be a boolean predicate */
1629 unsigned int
1630 sunos4_canonicalize_reloc (abfd, section, relptr, symbols)
1631 bfd *abfd;
1632 sec_ptr section;
1633 arelent **relptr;
1634 asymbol **symbols;
1635 {
1636 arelent *tblptr = section->relocation;
1637 unsigned int count;
1638
1639 if (!(tblptr || sunos4_slurp_reloc_table (abfd, section, symbols)))
1640 return 0;
1641
1642 if (section->flags & SEC_CONSTRUCTOR) {
1643 arelent_chain *chain = section->constructor_chain;
1644 for (count = 0; count < section->reloc_count; count ++) {
1645 *relptr ++ = &chain->relent;
1646 chain = chain->next;
1647 }
1648 }
1649 else {
1650 tblptr = section->relocation;
1651 if (!tblptr) return 0;
1652
1653 for (count = 0; count++ < section->reloc_count;)
1654 {
1655 *relptr++ = tblptr++;
1656 }
1657 }
1658 *relptr = 0;
1659
1660 return section->reloc_count;
1661 }
1662
1663 unsigned int
1664 sunos4_get_reloc_upper_bound (abfd, asect)
1665 bfd *abfd;
1666 sec_ptr asect;
1667 {
1668 if (bfd_get_format (abfd) != bfd_object) {
1669 bfd_error = invalid_operation;
1670 return 0;
1671 }
1672 if (asect->flags & SEC_CONSTRUCTOR) {
1673 return (sizeof (arelent *) * (asect->reloc_count+1));
1674 }
1675
1676 if (asect == obj_datasec (abfd))
1677 return (sizeof (arelent *) *
1678 ((exec_hdr(abfd)->a_drsize / sizeof (struct reloc_info_extended))
1679 +1));
1680
1681 if (asect == obj_textsec (abfd))
1682 return (sizeof (arelent *) *
1683 ((exec_hdr(abfd)->a_trsize / sizeof (struct reloc_info_extended))
1684 +1));
1685
1686 bfd_error = invalid_operation;
1687 return 0;
1688 }
1689
1690 void
1691 sunos4_reclaim_reloc (ignore_abfd, section)
1692 bfd *ignore_abfd;
1693 sec_ptr section;
1694 {
1695 if (section->relocation) {
1696 free (section->relocation);
1697 section->relocation = NULL;
1698 section->reloc_count = 0;
1699 }
1700 }
1701 \f
1702
1703 alent *
1704 sunos4_get_lineno(ignore_abfd, ignore_symbol)
1705 bfd *ignore_abfd;
1706 generic_symbol_type *ignore_symbol;
1707 {
1708 return (alent *)NULL;
1709 }
1710
1711 void
1712 sunos4_print_symbol(ignore_abfd, file, symbol, how)
1713 bfd *ignore_abfd;
1714 FILE *file;
1715 asymbol *symbol;
1716 bfd_print_symbol_enum_type how;
1717 {
1718 switch (how) {
1719 case bfd_print_symbol_name_enum:
1720 fprintf(file,"%s", symbol->name);
1721 break;
1722 case bfd_print_symbol_type_enum:
1723 fprintf(file,"%4x %2x %2x",(unsigned)(aout_symbol(symbol)->desc & 0xffff),
1724 (unsigned)( aout_symbol(symbol)->other & 0xff),
1725 (unsigned)(aout_symbol(symbol)->type));
1726 break;
1727 case bfd_print_symbol_all_enum:
1728 {
1729 char *section_name = symbol->section == (asection *)NULL ?
1730 "*abs" : symbol->section->name;
1731
1732 bfd_print_symbol_vandf((void *)file,symbol);
1733
1734 fprintf(file," %-5s %04x %02x %02x %s",
1735 section_name,
1736 (unsigned)(aout_symbol(symbol)->desc & 0xffff),
1737 (unsigned)(aout_symbol(symbol)->other & 0xff),
1738 (unsigned)(aout_symbol(symbol)->type & 0xff),
1739 symbol->name);
1740 }
1741 break;
1742 }
1743 }
1744 /* Once we know all the stuff that could be consed, we know how to clean
1745 it up. So why don't we? */
1746
1747 boolean
1748 sunos4_close_and_cleanup (abfd)
1749 bfd *abfd;
1750 {
1751 if (!bfd_read_p (abfd))
1752 switch (abfd->format) {
1753 case bfd_archive:
1754 if (!_bfd_write_archive_contents (abfd)) return false; break;
1755 case bfd_object:
1756 if (!sunos4_write_object_contents (abfd)) return false; break;
1757 default: bfd_error = invalid_operation; return false;
1758 }
1759
1760 #define cleaner(ptr) if (abfd->ptr) free (abfd->ptr)
1761 cleaner (tdata);
1762
1763 if (abfd->my_archive)
1764 cleaner (filename);
1765
1766 #undef cleaner
1767 return true;
1768 }
1769
1770 /*
1771 provided a bfd, a section and an offset into the section, calculate
1772 and return the name of the source file and the line nearest to the
1773 wanted location.
1774 */
1775
1776 boolean
1777 sunos4_find_nearest_line(abfd,
1778 section,
1779 symbols,
1780 offset,
1781 filename_ptr,
1782 functionname_ptr,
1783 line_ptr)
1784 bfd *abfd;
1785 asection *section;
1786 asymbol **symbols;
1787 bfd_vma offset;
1788 char **filename_ptr;
1789 char **functionname_ptr;
1790 unsigned int *line_ptr;
1791 {
1792 /* Run down the file looking for the filename, function and linenumber */
1793 asymbol **p;
1794 static char buffer[100];
1795 bfd_vma high_line_vma = ~0;
1796 bfd_vma low_func_vma = 0;
1797 asymbol *func = 0;
1798 *filename_ptr = abfd->filename;
1799 *functionname_ptr = 0;
1800 *line_ptr = 0;
1801 if (symbols != (asymbol **)NULL) {
1802 for (p = symbols; *p; p++) {
1803 aout_symbol_type *q = (aout_symbol_type *)(*p);
1804 switch (q->type){
1805 case N_SO:
1806 *filename_ptr = q->symbol.name;
1807 if (obj_textsec(abfd) != section) {
1808 return true;
1809 }
1810 break;
1811 case N_SLINE:
1812
1813 case N_DSLINE:
1814 case N_BSLINE:
1815 /* We'll keep this if it resolves nearer than the one we have already */
1816 if (q->symbol.value >= offset &&
1817 q->symbol.value < high_line_vma) {
1818 *line_ptr = q->desc;
1819 high_line_vma = q->symbol.value;
1820 }
1821 break;
1822 case N_FUN:
1823 {
1824 /* We'll keep this if it is nearer than the one we have already */
1825 if (q->symbol.value >= low_func_vma &&
1826 q->symbol.value <= offset) {
1827 low_func_vma = q->symbol.value;
1828 func = (asymbol *)q;
1829 }
1830 if (*line_ptr && func) {
1831 char *function = func->name;
1832 char *p;
1833 strncpy(buffer, function, sizeof(buffer)-1);
1834 buffer[sizeof(buffer)-1] = 0;
1835 /* Have to remove : stuff */
1836 p = strchr(buffer,':');
1837 if (p != NULL) {*p = NULL; }
1838 *functionname_ptr = buffer;
1839 return true;
1840
1841 }
1842 }
1843 break;
1844 }
1845 }
1846 }
1847
1848 return true;
1849
1850 }
1851
1852 bfd_target aoutvec =
1853 {
1854 "a.out-generic-big", /* name */
1855 bfd_target_aout_flavour_enum,
1856 true, /* target byte order */
1857 true, /* target headers byte order */
1858 (HAS_RELOC | EXEC_P | /* object flags */
1859 HAS_LINENO | HAS_DEBUG |
1860 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
1861 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1862 0, /* valid reloc types */
1863 ' ', /* ar_pad_char */
1864 16, /* ar_max_namelen */
1865 sunos4_close_and_cleanup, /* _close_and_cleanup */
1866 sunos4_set_section_contents, /* bfd_set_section_contents */
1867 sunos4_get_section_contents, /* bfd_get_section_contents */
1868 sunos4_new_section_hook, /* new_section_hook */
1869 sunos4_core_file_failing_command, /* _core_file_failing_command */
1870 sunos4_core_file_failing_signal, /* _core_file_failing_signal */
1871 sunos4_core_file_matches_executable_p, /* _core_file_matches_ex...p */
1872
1873 bfd_slurp_bsd_armap, /* bfd_slurp_armap */
1874 bfd_true, /* bfd_slurp_extended_name_table */
1875 bfd_bsd_truncate_arname, /* bfd_truncate_arname */
1876
1877 sunos4_get_symtab_upper_bound, /* get_symtab_upper_bound */
1878 sunos4_get_symtab, /* canonicalize_symtab */
1879 sunos4_reclaim_symbol_table, /* bfd_reclaim_symbol_table */
1880 sunos4_get_reloc_upper_bound, /* get_reloc_upper_bound */
1881 sunos4_canonicalize_reloc, /* bfd_canonicalize_reloc */
1882 sunos4_reclaim_reloc, /* bfd_reclaim_reloc */
1883 sunos4_get_symcount_upper_bound, /* bfd_get_symcount_upper_bound */
1884 sunos4_get_first_symbol, /* bfd_get_first_symbol */
1885 sunos4_get_next_symbol, /* bfd_get_next_symbol */
1886 sunos4_classify_symbol, /* bfd_classify_symbol */
1887 sunos4_symbol_hasclass, /* bfd_symbol_hasclass */
1888 sunos4_symbol_name, /* bfd_symbol_name */
1889 sunos4_symbol_value, /* bfd_symbol_value */
1890
1891 _do_getblong, _do_putblong, _do_getbshort, _do_putbshort, /* data */
1892 _do_getblong, _do_putblong, _do_getbshort, _do_putbshort, /* hdrs */
1893
1894 {_bfd_dummy_target, sunos4_object_p, /* bfd_check_format */
1895 bfd_generic_archive_p, sunos4_core_file_p},
1896 {bfd_false, sunos4_mkobject, /* bfd_zxset_format */
1897 _bfd_generic_mkarchive, bfd_false},
1898 sunos4_make_empty_symbol,
1899 sunos4_print_symbol,
1900 sunos4_get_lineno,
1901 sunos4_set_arch_mach,
1902 bsd_write_armap,
1903 bfd_generic_openr_next_archived_file,
1904 sunos4_find_nearest_line, /* bfd_find_nearest_line */
1905 bfd_generic_stat_arch_elt /* bfd_stat_arch_elt */
1906 };
1907
This page took 0.069255 seconds and 5 git commands to generate.