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