* Makefile.am (ALL_MACHINES): Add cpu-cris.lo.
[platform/upstream/binutils.git] / bfd / aout-cris.c
1 /* BFD backend for CRIS a.out binaries.
2    Copyright (C) 2000 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 BYTES_IN_WORD 4
27 #define N_TXTOFF(x) 32
28 #define ENTRY_CAN_BE_ZERO
29 #define TEXT_START_ADDR 0
30
31 /* If you change this to 4, you can not link to an address N*4+2.  */
32 #define SEGMENT_SIZE 2
33
34 /* For some reason, if the a.out file has Z_MAGIC, then
35    adata(abfd).exec_bytes_size is not used, but rather
36    adata(abfd).zmagic_disk_block_size, even though the exec_header is
37    *not* included in the text segment.  A simple workaround is to
38    #define ZMAGIC_DISK_BLOCK_SIZE, which is used if defined; otherwise
39    TARGET_PAGE_SIZE is used. */
40 #define ZMAGIC_DISK_BLOCK_SIZE N_TXTOFF (0)
41
42 /* It seems odd at first to set a page-size this low, but gives greater
43    freedom in where things can be linked.  The drawback is that you have
44    to set alignment and padding in linker scripts.  */
45 #define TARGET_PAGE_SIZE SEGMENT_SIZE
46 #define TARGETNAME "a.out-cris"
47
48 /* N_SHARED_LIB gets this reasonable default as of 1999-07-12, but we
49    have to work with 2.9.1.  Note that N_SHARED_LIB is used in a
50    SUN-specific context, not applicable to CRIS.  */
51 #define N_SHARED_LIB(x) 0
52
53 /* The definition here seems not used; just provided as a convention.  */
54 #define DEFAULT_ARCH bfd_arch_cris
55
56 /* ??? Spacing might be essential for the parameters in this macro.
57    Do Not Change.  */
58 #define MY(OP) CAT (cris_aout_,OP)
59 #define NAME(x,y) CAT3(cris_aout,_32_,y)
60
61 #include "bfd.h"
62
63 /* Version 1 of the header.  */
64 #define MY_exec_hdr_flags 1
65
66 #define MY_write_object_contents MY(write_object_contents)
67 static boolean MY(write_object_contents) PARAMS ((bfd *));
68
69 /* Forward this, so we can use a pointer to it in PARAMS.  */
70 struct reloc_ext_external;
71
72 #define MY_swap_ext_reloc_out MY(swap_ext_reloc_out)
73 static void MY(swap_ext_reloc_out) PARAMS ((bfd *, arelent *,
74                                             struct reloc_ext_external *));
75
76 #define MY_swap_ext_reloc_in MY(swap_ext_reloc_in)
77 static void MY(swap_ext_reloc_in) PARAMS ((bfd *, struct
78                                            reloc_ext_external *,
79                                            arelent *, asymbol **,
80                                            bfd_size_type));
81
82 #define MY_set_sizes MY(set_sizes)
83 static boolean MY(set_sizes) PARAMS ((bfd *));
84
85 /* To set back reloc_size to ext, we make MY(set_sizes) be called
86    through this construct.  Note that MY_set_arch_mach is only called
87    through SET_ARCH_MACH.  The default bfd_default_set_arch_mach will
88    not call set_sizes.  */
89
90 #define MY_set_arch_mach NAME (aout, set_arch_mach)
91 #define SET_ARCH_MACH(BFD, EXEC) \
92  MY_set_arch_mach (BFD, DEFAULT_ARCH, N_MACHTYPE (EXEC))
93
94 /* These macros describe the binary layout of the reloc information we
95    use in a file.  */
96 #define RELOC_EXT_BITS_EXTERN_LITTLE 0x80
97 #define RELOC_EXT_BITS_TYPE_LITTLE 3
98 #define RELOC_EXT_BITS_TYPE_SH_LITTLE 0
99
100 #ifndef MY_get_section_contents
101 #define MY_get_section_contents aout_32_get_section_contents
102 #endif
103
104 /* Include generic functions (some are overridden above).  */
105 #include "aout32.c"
106 #include "aout-target.h"
107
108
109 /* We need our own version to set header flags.  */
110
111 static boolean
112 MY(write_object_contents) (abfd)
113      bfd *abfd;
114 {
115   struct external_exec exec_bytes;
116   struct internal_exec *execp = exec_hdr (abfd);
117
118   /* We set the reloc type to RELOC_EXT_SIZE, although setting it at all
119      seems unnecessary when inspecting as and ld behavior (not an
120      exhaustive inspection).  The default write_object_contents
121      definition sets RELOC_EXT_SIZE, so we follow suite and set it too.  */
122   obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
123
124   /* Setting N_SET_MACHTYPE and using N_SET_FLAGS is not performed by
125      the default definition.  */
126   if (bfd_get_arch(abfd) == bfd_arch_cris)
127     N_SET_MACHTYPE(*execp, M_CRIS);
128
129   N_SET_FLAGS (*execp, aout_backend_info (abfd)->exec_hdr_flags);
130
131   WRITE_HEADERS (abfd, execp);
132
133   return true;
134 }
135
136
137 /* We need our own for these reasons:
138    - Assert that a normal 8, 16 or 32 reloc is output. 
139    - Fix what seems to be a weak-bug (perhaps there for valid reasons).  */ 
140
141 static void
142 MY(swap_ext_reloc_out) (abfd, g, natptr)
143      bfd *abfd;
144      arelent *g;
145      struct reloc_ext_external *natptr;
146 {
147   int r_index;
148   int r_extern;
149   unsigned int r_type;
150   unsigned int r_addend;
151   asymbol *sym = *(g->sym_ptr_ptr);
152   asection *output_section = sym->section->output_section;
153
154   PUT_WORD (abfd, g->address, natptr->r_address);
155
156   r_type = (unsigned int) g->howto->type;
157
158   r_addend = g->addend;
159   if ((sym->flags & BSF_SECTION_SYM) != 0)
160     r_addend += (*(g->sym_ptr_ptr))->section->output_section->vma;
161
162   /* If this relocation is relative to a symbol then set the
163      r_index to the symbols index, and the r_extern bit.
164
165      Absolute symbols can come in in two ways, either as an offset
166      from the abs section, or as a symbol which has an abs value.
167      check for that here.  */
168
169   if (bfd_is_abs_section (bfd_get_section (sym)))
170     {
171       r_extern = 0;
172       r_index = N_ABS;
173     }
174   else if ((sym->flags & BSF_SECTION_SYM) == 0)
175     {
176       if (bfd_is_und_section (bfd_get_section (sym))
177           /* Remember to check for weak symbols; they count as global.  */
178           || (sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
179         r_extern = 1;
180       else
181         r_extern = 0;
182       r_index = (*(g->sym_ptr_ptr))->KEEPIT;
183     }
184   else
185     {
186       /* Just an ordinary section.  */
187       r_extern = 0;
188       r_index = output_section->target_index;
189     }
190
191   /* The relocation type is the same as the canonical ones, but only
192      the first 3 are used: RELOC_8, RELOC_16, RELOC_32.
193      We may change this later, but assert this for the moment.  */
194   if (r_type > 2)
195     {
196       (*_bfd_error_handler) (_("%s: Invalid relocation type exported: %d"),
197                              bfd_get_filename (abfd), r_type);
198     
199       bfd_set_error (bfd_error_wrong_format);
200     }
201
202   /* Now the fun stuff.  */
203   natptr->r_index[2] = r_index >> 16;
204   natptr->r_index[1] = r_index >> 8;
205   natptr->r_index[0] = r_index;
206   natptr->r_type[0] =
207      (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
208       | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
209
210   PUT_WORD (abfd, r_addend, natptr->r_addend);
211 }
212
213
214 /* We need our own to assert that a normal 8, 16 or 32 reloc is input.  */
215
216 static void
217 MY(swap_ext_reloc_in) (abfd, bytes, cache_ptr, symbols, symcount)
218      bfd *abfd;
219      struct reloc_ext_external *bytes;
220      arelent *cache_ptr;
221      asymbol **symbols;
222      bfd_size_type symcount;
223 {
224   unsigned int r_index;
225   int r_extern;
226   unsigned int r_type;
227   struct aoutdata *su = &(abfd->tdata.aout_data->a);
228
229   cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
230
231   /* Now the fun stuff.  */
232   r_index =  (bytes->r_index[2] << 16)
233     | (bytes->r_index[1] << 8)
234     |  bytes->r_index[0];
235   r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
236   r_type = ((bytes->r_type[0]) >> RELOC_EXT_BITS_TYPE_SH_LITTLE)
237     & RELOC_EXT_BITS_TYPE_LITTLE;
238
239   if (r_type > 2)
240     {
241       (*_bfd_error_handler) (_("%s: Invalid relocation type imported: %d"),
242                              bfd_get_filename (abfd), r_type);
243     
244       bfd_set_error(bfd_error_wrong_format);
245     }
246     
247   cache_ptr->howto =  howto_table_ext + r_type;
248
249   if (r_extern && r_index > symcount)
250     {
251       (*_bfd_error_handler)
252         (_("%s: Bad relocation record imported: %d"),
253          bfd_get_filename (abfd), r_index);
254     
255       bfd_set_error (bfd_error_wrong_format);
256
257       /* We continue, so we can catch further errors.  */
258       r_extern = 0;
259       r_index = N_ABS;
260     }
261
262   /* Magically uses r_extern, symbols etc.  Ugly, but it's what's in the
263      default.  */
264   MOVE_ADDRESS (GET_SWORD (abfd, bytes->r_addend));
265 }
266
267
268 /* We use the same as the default, except that we also set
269    "obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;", to avoid changing
270    NAME (aout, set_arch_mach) in aoutx.  */
271
272 static boolean
273 MY(set_sizes) (abfd)
274      bfd *abfd;
275 {
276   /* Just as the default in aout-target.h (with some #ifdefs folded)...  */
277
278   adata(abfd).page_size = TARGET_PAGE_SIZE;
279   adata(abfd).segment_size = SEGMENT_SIZE;
280   adata(abfd).zmagic_disk_block_size = ZMAGIC_DISK_BLOCK_SIZE;
281   adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE;
282
283   /* ... except for that we have the extended reloc.  The alternative
284      would be to add a check on bfd_arch_cris in NAME (aout,
285      set_arch_mach) in aoutx.h, but I don't want to do that since
286      target-specific things should not be added there.  */
287
288   obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
289   
290   return true;
291 }
292
293 /*
294  * Local variables:
295  * eval: (c-set-style "gnu")
296  * indent-tabs-mode: t
297  * End:
298  */