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