freebsd.h revision 33973
1/* BFD back-end definitions used by all FreeBSD a.out targets.
2   Copyright (C) 1990, 1991, 1992, 1996 Free Software Foundation, Inc.
3
4This file is part of BFD, the Binary File Descriptor library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19*/
20
21/* FreeBSD QMAGIC files have the header in the text. */
22#define	N_HEADER_IN_TEXT(x)	1
23#define MY_text_includes_header 1
24
25#define TEXT_START_ADDR		(TARGET_PAGE_SIZE + 0x20)
26
27/*
28 * FreeBSD uses a weird mix of byte orderings for its a_info field.
29 * Its assembler emits NetBSD style object files, with a big-endian
30 * a_info.  Its linker seems to accept either byte ordering, but
31 * emits a little-endian a_info.
32 *
33 * Here, we accept either byte ordering, but always produce
34 * little-endian.
35 *
36 * FIXME - Probably we should always produce the _native_ byte
37 * ordering.  I.e., it should be in the architecture-specific
38 * file, not here.  But in reality, there is almost zero chance
39 * that FreeBSD will ever use a.out in a new port.
40 */
41
42#define N_MACHTYPE(exec) \
43	((enum machine_type) \
44	 ((freebsd_swap_magic(&(exec).a_info) >> 16) & 0x3ff))
45#define N_FLAGS(exec) \
46	((enum machine_type) \
47	 ((freebsd_swap_magic(&(exec).a_info) >> 26) & 0x3f))
48
49#define N_SET_INFO(exec, magic, type, flags) \
50	((exec).a_info = ((magic) & 0xffff) \
51	 | (((int)(type) & 0x3ff) << 16) \
52	 | (((flags) & 0x3f) << 26))
53#define N_SET_MACHTYPE(exec, machtype) \
54	((exec).a_info = \
55         ((exec).a_info & 0xfb00ffff) | ((((int)(machtype))&0x3ff) << 16))
56#define N_SET_FLAGS(exec, flags) \
57	((exec).a_info = \
58	 ((exec).a_info & 0x03ffffff) | ((flags & 0x03f) << 26))
59
60#include "bfd.h"
61#include "sysdep.h"
62#include "libbfd.h"
63#include "libaout.h"
64
65#define SWAP_MAGIC(ext)			(freebsd_swap_magic(ext))
66
67#define MY_bfd_final_link		freebsd_bfd_final_link
68#define MY_write_object_contents	freebsd_write_object_contents
69
70static boolean freebsd_bfd_final_link PARAMS ((bfd *, struct bfd_link_info *));
71static long freebsd_swap_magic PARAMS ((void *ext));
72static boolean freebsd_write_object_contents PARAMS ((bfd *abfd));
73
74#include "aout-target.h"
75
76static boolean
77freebsd_bfd_final_link(abfd, info)
78  bfd *abfd;
79  struct bfd_link_info *info;
80{
81  obj_aout_subformat (abfd) = q_magic_format;
82  return NAME(aout,final_link) (abfd, info, MY_final_link_callback);
83}
84
85/* Swap a magic number.  We accept either endian, whichever looks valid. */
86
87static long
88freebsd_swap_magic(ext)
89  void *ext;
90{
91  long linfo = bfd_getl32(ext);
92  long binfo = bfd_getb32(ext);
93  int lmagic = linfo & 0xffff;
94  int bmagic = binfo & 0xffff;
95  int lmagic_ok = lmagic == OMAGIC || lmagic == NMAGIC ||
96    lmagic == ZMAGIC || lmagic == QMAGIC;
97  int bmagic_ok = bmagic == OMAGIC || bmagic == NMAGIC ||
98    bmagic == ZMAGIC || bmagic == QMAGIC;
99
100  return bmagic_ok && !lmagic_ok ? binfo : linfo;
101}
102
103/* Write an object file.
104   Section contents have already been written.  We write the
105   file header, symbols, and relocation.  */
106
107static boolean
108freebsd_write_object_contents(abfd)
109     bfd *abfd;
110{
111  struct external_exec exec_bytes;
112  struct internal_exec *execp = exec_hdr (abfd);
113
114#if CHOOSE_RELOC_SIZE
115  CHOOSE_RELOC_SIZE(abfd);
116#else
117  obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
118#endif
119
120  /* Magic number, maestro, please!  */
121  switch (bfd_get_arch(abfd)) {
122  case bfd_arch_m68k:
123    if (strcmp (abfd->xvec->name, "a.out-m68k4k-netbsd") == 0)
124      N_SET_MACHTYPE(*execp, M_68K4K_NETBSD);
125    else
126      N_SET_MACHTYPE(*execp, M_68K_NETBSD);
127    break;
128  case bfd_arch_sparc:
129    N_SET_MACHTYPE(*execp, M_SPARC_NETBSD);
130    break;
131  case bfd_arch_i386:
132    N_SET_MACHTYPE(*execp, M_386_NETBSD);
133    break;
134  case bfd_arch_ns32k:
135    N_SET_MACHTYPE(*execp, M_532_NETBSD);
136    break;
137  default:
138    N_SET_MACHTYPE(*execp, M_UNKNOWN);
139    break;
140  }
141
142  WRITE_HEADERS(abfd, execp);
143
144  return true;
145}
146