1/* BFD backend for CRIS a.out binaries.
2   Copyright 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3   Contributed by Axis Communications AB.
4   Written by Hans-Peter Nilsson.
5
6   This file is part of BFD, the Binary File Descriptor library.
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
21
22/* See info in the file PORTING for documentation of these macros and
23   functions.  Beware; some of the information there is outdated.  */
24
25#define N_HEADER_IN_TEXT(x) 0
26#define N_TXTOFF(x)         32
27#define ENTRY_CAN_BE_ZERO
28#define TEXT_START_ADDR     0
29
30/* Without reading symbols to get the text start symbol, there is no way
31   to know where the text segment starts in an a.out file.  Defaulting to
32   anything as constant as TEXT_START_ADDR is bad.  But we can guess from
33   the entry point, which is usually within the first 64k of the text
34   segment.  We also assume here that the text segment is 64k-aligned.
35   FIXME: It is also wrong to assume that data and bss follow immediately
36   after text, but with those, we don't have any choice besides reading
37   symbol info, and luckily there's no pressing need for correctness for
38   those vma:s at this time.  */
39#define N_TXTADDR(x) ((x).a_entry & ~(bfd_vma) 0xffff)
40
41/* If you change this to 4, you can not link to an address N*4+2.  */
42#define SEGMENT_SIZE 2
43
44/* For some reason, if the a.out file has Z_MAGIC, then
45   adata(abfd).exec_bytes_size is not used, but rather
46   adata(abfd).zmagic_disk_block_size, even though the exec_header is
47   *not* included in the text segment.  A simple workaround is to
48   #define ZMAGIC_DISK_BLOCK_SIZE, which is used if defined; otherwise
49   TARGET_PAGE_SIZE is used.  */
50#define ZMAGIC_DISK_BLOCK_SIZE     N_TXTOFF (0)
51
52/* It seems odd at first to set a page-size this low, but gives greater
53   freedom in where things can be linked.  The drawback is that you have
54   to set alignment and padding in linker scripts.  */
55#define TARGET_PAGE_SIZE SEGMENT_SIZE
56#define TARGETNAME "a.out-cris"
57
58/* N_SHARED_LIB gets this reasonable default as of 1999-07-12, but we
59   have to work with 2.9.1.  Note that N_SHARED_LIB is used in a
60   SUN-specific context, not applicable to CRIS.  */
61#define N_SHARED_LIB(x) 0
62
63/* The definition here seems not used; just provided as a convention.  */
64#define DEFAULT_ARCH bfd_arch_cris
65
66/* Do not "beautify" the CONCAT* macro args.  Traditional C will not
67   remove whitespace added here, and thus will fail to concatenate
68   the tokens.  */
69#define MY(OP) CONCAT2 (cris_aout_,OP)
70#define NAME(x, y) CONCAT3 (cris_aout,_32_,y)
71
72#include "bfd.h"
73
74/* Version 1 of the header.  */
75#define MY_exec_hdr_flags 1
76
77#define MY_write_object_contents MY (write_object_contents)
78static bfd_boolean MY (write_object_contents) (bfd *);
79
80/* Forward this, so we can use a pointer to it in PARAMS.  */
81struct reloc_ext_external;
82
83#define MY_swap_ext_reloc_out MY (swap_ext_reloc_out)
84static void MY (swap_ext_reloc_out) (bfd *, arelent *, struct reloc_ext_external *);
85
86#define MY_swap_ext_reloc_in MY (swap_ext_reloc_in)
87static void MY (swap_ext_reloc_in) (bfd *, struct reloc_ext_external *,
88				    arelent *, asymbol **, bfd_size_type);
89
90#define MY_set_sizes MY (set_sizes)
91static bfd_boolean MY (set_sizes) (bfd *);
92
93/* To set back reloc_size to ext, we make MY (set_sizes) be called
94   through this construct.  Note that MY_set_arch_mach is only called
95   through SET_ARCH_MACH.  The default bfd_default_set_arch_mach will
96   not call set_sizes.  */
97
98#define MY_set_arch_mach NAME (aout, set_arch_mach)
99#define SET_ARCH_MACH(BFD, EXEC) \
100 MY_set_arch_mach (BFD, DEFAULT_ARCH, N_MACHTYPE (EXEC))
101
102/* These macros describe the binary layout of the reloc information we
103   use in a file.  */
104#define RELOC_EXT_BITS_EXTERN_LITTLE  0x80
105#define RELOC_EXT_BITS_TYPE_LITTLE    3
106#define RELOC_EXT_BITS_TYPE_SH_LITTLE 0
107
108#ifndef MY_get_section_contents
109#define MY_get_section_contents aout_32_get_section_contents
110#endif
111
112#define MACHTYPE_OK(mtype) ((mtype) == M_CRIS)
113
114/* Include generic functions (some are overridden above).  */
115#include "aout32.c"
116#include "aout-target.h"
117
118/* We need our own version to set header flags.  */
119
120static bfd_boolean
121MY (write_object_contents) (bfd *abfd)
122{
123  struct external_exec exec_bytes;
124  struct internal_exec *execp = exec_hdr (abfd);
125
126  /* We set the reloc type to RELOC_EXT_SIZE, although setting it at all
127     seems unnecessary when inspecting as and ld behavior (not an
128     exhaustive inspection).  The default write_object_contents
129     definition sets RELOC_EXT_SIZE, so we follow suite and set it too.  */
130  obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
131
132  /* Setting N_SET_MACHTYPE and using N_SET_FLAGS is not performed by
133     the default definition.  */
134  if (bfd_get_arch (abfd) == bfd_arch_cris)
135    N_SET_MACHTYPE (*execp, M_CRIS);
136
137  N_SET_FLAGS (*execp, aout_backend_info (abfd)->exec_hdr_flags);
138
139  WRITE_HEADERS (abfd, execp);
140
141  return TRUE;
142}
143
144/* We need our own for these reasons:
145   - Assert that a normal 8, 16 or 32 reloc is output.
146   - Fix what seems to be a weak-bug (perhaps there for valid reasons).  */
147
148static void
149MY (swap_ext_reloc_out) (bfd *abfd,
150			 arelent *g,
151			 struct reloc_ext_external *natptr)
152{
153  int r_index;
154  int r_extern;
155  unsigned int r_type;
156  bfd_vma r_addend;
157  asymbol *sym = *(g->sym_ptr_ptr);
158  asection *output_section = sym->section->output_section;
159
160  PUT_WORD (abfd, g->address, natptr->r_address);
161
162  r_type = (unsigned int) g->howto->type;
163
164  r_addend = g->addend;
165  if ((sym->flags & BSF_SECTION_SYM) != 0)
166    r_addend += (*(g->sym_ptr_ptr))->section->output_section->vma;
167
168  /* If this relocation is relative to a symbol then set the
169     r_index to the symbols index, and the r_extern bit.
170
171     Absolute symbols can come in in two ways, either as an offset
172     from the abs section, or as a symbol which has an abs value.
173     check for that here.  */
174
175  if (bfd_is_abs_section (bfd_get_section (sym)))
176    {
177      r_extern = 0;
178      r_index = N_ABS;
179    }
180  else if ((sym->flags & BSF_SECTION_SYM) == 0)
181    {
182      if (bfd_is_und_section (bfd_get_section (sym))
183	  /* Remember to check for weak symbols; they count as global.  */
184	  || (sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
185	r_extern = 1;
186      else
187	r_extern = 0;
188      r_index = (*(g->sym_ptr_ptr))->KEEPIT;
189    }
190  else
191    {
192      /* Just an ordinary section.  */
193      r_extern = 0;
194      r_index = output_section->target_index;
195    }
196
197  /* The relocation type is the same as the canonical ones, but only
198     the first 3 are used: RELOC_8, RELOC_16, RELOC_32.
199     We may change this later, but assert this for the moment.  */
200  if (r_type > 2)
201    {
202      (*_bfd_error_handler) (_("%s: Invalid relocation type exported: %d"),
203			     bfd_get_filename (abfd), r_type);
204
205      bfd_set_error (bfd_error_wrong_format);
206    }
207
208  /* Now the fun stuff.  */
209  natptr->r_index[2] = r_index >> 16;
210  natptr->r_index[1] = r_index >> 8;
211  natptr->r_index[0] = r_index;
212  natptr->r_type[0] =
213     (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
214      | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
215
216  PUT_WORD (abfd, r_addend, natptr->r_addend);
217}
218
219/* We need our own to assert that a normal 8, 16 or 32 reloc is input.  */
220
221static void
222MY (swap_ext_reloc_in) (bfd *abfd,
223			struct reloc_ext_external *bytes,
224			arelent *cache_ptr,
225			asymbol **symbols,
226			bfd_size_type symcount)
227{
228  unsigned int r_index;
229  int r_extern;
230  unsigned int r_type;
231  struct aoutdata *su = &(abfd->tdata.aout_data->a);
232
233  cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
234
235  /* Now the fun stuff.  */
236  r_index =  (bytes->r_index[2] << 16)
237    | (bytes->r_index[1] << 8)
238    |  bytes->r_index[0];
239  r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
240  r_type = ((bytes->r_type[0]) >> RELOC_EXT_BITS_TYPE_SH_LITTLE)
241    & RELOC_EXT_BITS_TYPE_LITTLE;
242
243  if (r_type > 2)
244    {
245      (*_bfd_error_handler) (_("%B: Invalid relocation type imported: %d"),
246			     abfd, r_type);
247
248      bfd_set_error (bfd_error_wrong_format);
249    }
250
251  cache_ptr->howto =  howto_table_ext + r_type;
252
253  if (r_extern && r_index > symcount)
254    {
255      (*_bfd_error_handler)
256        (_("%B: Bad relocation record imported: %d"), abfd, r_index);
257
258      bfd_set_error (bfd_error_wrong_format);
259
260      /* We continue, so we can catch further errors.  */
261      r_extern = 0;
262      r_index = N_ABS;
263    }
264
265  /* Magically uses r_extern, symbols etc.  Ugly, but it's what's in the
266     default.  */
267  MOVE_ADDRESS (GET_SWORD (abfd, bytes->r_addend));
268}
269
270/* We use the same as the default, except that we also set
271   "obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;", to avoid changing
272   NAME (aout, set_arch_mach) in aoutx.  */
273
274static bfd_boolean
275MY (set_sizes) (bfd *abfd)
276{
277  /* Just as the default in aout-target.h (with some #ifdefs folded)...  */
278
279  adata (abfd).page_size = TARGET_PAGE_SIZE;
280  adata (abfd).segment_size = SEGMENT_SIZE;
281  adata (abfd).zmagic_disk_block_size = ZMAGIC_DISK_BLOCK_SIZE;
282  adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
283
284  /* ... except for that we have the extended reloc.  The alternative
285     would be to add a check on bfd_arch_cris in NAME (aout,
286     set_arch_mach) in aoutx.h, but I don't want to do that since
287     target-specific things should not be added there.  */
288
289  obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
290
291  return TRUE;
292}
293