Back to index

cell-binutils  2.17cvs20070401
aout-cris.c
Go to the documentation of this file.
00001 /* BFD backend for CRIS a.out binaries.
00002    Copyright 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
00003    Contributed by Axis Communications AB.
00004    Written by Hans-Peter Nilsson.
00005 
00006    This file is part of BFD, the Binary File Descriptor library.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 /* See info in the file PORTING for documentation of these macros and
00023    functions.  Beware; some of the information there is outdated.  */
00024 
00025 #define N_HEADER_IN_TEXT(x) 0
00026 #define N_TXTOFF(x)         32
00027 #define ENTRY_CAN_BE_ZERO
00028 #define TEXT_START_ADDR     0
00029 
00030 /* Without reading symbols to get the text start symbol, there is no way
00031    to know where the text segment starts in an a.out file.  Defaulting to
00032    anything as constant as TEXT_START_ADDR is bad.  But we can guess from
00033    the entry point, which is usually within the first 64k of the text
00034    segment.  We also assume here that the text segment is 64k-aligned.
00035    FIXME: It is also wrong to assume that data and bss follow immediately
00036    after text, but with those, we don't have any choice besides reading
00037    symbol info, and luckily there's no pressing need for correctness for
00038    those vma:s at this time.  */
00039 #define N_TXTADDR(x) ((x).a_entry & ~(bfd_vma) 0xffff)
00040 
00041 /* If you change this to 4, you can not link to an address N*4+2.  */
00042 #define SEGMENT_SIZE 2
00043 
00044 /* For some reason, if the a.out file has Z_MAGIC, then
00045    adata(abfd).exec_bytes_size is not used, but rather
00046    adata(abfd).zmagic_disk_block_size, even though the exec_header is
00047    *not* included in the text segment.  A simple workaround is to
00048    #define ZMAGIC_DISK_BLOCK_SIZE, which is used if defined; otherwise
00049    TARGET_PAGE_SIZE is used.  */
00050 #define ZMAGIC_DISK_BLOCK_SIZE     N_TXTOFF (0)
00051 
00052 /* It seems odd at first to set a page-size this low, but gives greater
00053    freedom in where things can be linked.  The drawback is that you have
00054    to set alignment and padding in linker scripts.  */
00055 #define TARGET_PAGE_SIZE SEGMENT_SIZE
00056 #define TARGETNAME "a.out-cris"
00057 
00058 /* N_SHARED_LIB gets this reasonable default as of 1999-07-12, but we
00059    have to work with 2.9.1.  Note that N_SHARED_LIB is used in a
00060    SUN-specific context, not applicable to CRIS.  */
00061 #define N_SHARED_LIB(x) 0
00062 
00063 /* The definition here seems not used; just provided as a convention.  */
00064 #define DEFAULT_ARCH bfd_arch_cris
00065 
00066 /* Do not "beautify" the CONCAT* macro args.  Traditional C will not
00067    remove whitespace added here, and thus will fail to concatenate
00068    the tokens.  */
00069 #define MY(OP) CONCAT2 (cris_aout_,OP)
00070 #define NAME(x, y) CONCAT3 (cris_aout,_32_,y)
00071 
00072 #include "bfd.h"
00073 
00074 /* Version 1 of the header.  */
00075 #define MY_exec_hdr_flags 1
00076 
00077 #define MY_write_object_contents MY (write_object_contents)
00078 static bfd_boolean MY (write_object_contents) (bfd *);
00079 
00080 /* Forward this, so we can use a pointer to it in PARAMS.  */
00081 struct reloc_ext_external;
00082 
00083 #define MY_swap_ext_reloc_out MY (swap_ext_reloc_out)
00084 static void MY (swap_ext_reloc_out) (bfd *, arelent *, struct reloc_ext_external *);
00085 
00086 #define MY_swap_ext_reloc_in MY (swap_ext_reloc_in)
00087 static void MY (swap_ext_reloc_in) (bfd *, struct reloc_ext_external *,
00088                                 arelent *, asymbol **, bfd_size_type);
00089 
00090 #define MY_set_sizes MY (set_sizes)
00091 static bfd_boolean MY (set_sizes) (bfd *);
00092 
00093 /* To set back reloc_size to ext, we make MY (set_sizes) be called
00094    through this construct.  Note that MY_set_arch_mach is only called
00095    through SET_ARCH_MACH.  The default bfd_default_set_arch_mach will
00096    not call set_sizes.  */
00097 
00098 #define MY_set_arch_mach NAME (aout, set_arch_mach)
00099 #define SET_ARCH_MACH(BFD, EXEC) \
00100  MY_set_arch_mach (BFD, DEFAULT_ARCH, N_MACHTYPE (EXEC))
00101 
00102 /* These macros describe the binary layout of the reloc information we
00103    use in a file.  */
00104 #define RELOC_EXT_BITS_EXTERN_LITTLE  0x80
00105 #define RELOC_EXT_BITS_TYPE_LITTLE    3
00106 #define RELOC_EXT_BITS_TYPE_SH_LITTLE 0
00107 
00108 #ifndef MY_get_section_contents
00109 #define MY_get_section_contents aout_32_get_section_contents
00110 #endif
00111 
00112 #define MACHTYPE_OK(mtype) ((mtype) == M_CRIS)
00113 
00114 /* Include generic functions (some are overridden above).  */
00115 #include "aout32.c"
00116 #include "aout-target.h"
00117 
00118 /* We need our own version to set header flags.  */
00119 
00120 static bfd_boolean
00121 MY (write_object_contents) (bfd *abfd)
00122 {
00123   struct external_exec exec_bytes;
00124   struct internal_exec *execp = exec_hdr (abfd);
00125 
00126   /* We set the reloc type to RELOC_EXT_SIZE, although setting it at all
00127      seems unnecessary when inspecting as and ld behavior (not an
00128      exhaustive inspection).  The default write_object_contents
00129      definition sets RELOC_EXT_SIZE, so we follow suite and set it too.  */
00130   obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
00131 
00132   /* Setting N_SET_MACHTYPE and using N_SET_FLAGS is not performed by
00133      the default definition.  */
00134   if (bfd_get_arch (abfd) == bfd_arch_cris)
00135     N_SET_MACHTYPE (*execp, M_CRIS);
00136 
00137   N_SET_FLAGS (*execp, aout_backend_info (abfd)->exec_hdr_flags);
00138 
00139   WRITE_HEADERS (abfd, execp);
00140 
00141   return TRUE;
00142 }
00143 
00144 /* We need our own for these reasons:
00145    - Assert that a normal 8, 16 or 32 reloc is output.
00146    - Fix what seems to be a weak-bug (perhaps there for valid reasons).  */
00147 
00148 static void
00149 MY (swap_ext_reloc_out) (bfd *abfd,
00150                       arelent *g,
00151                       struct reloc_ext_external *natptr)
00152 {
00153   int r_index;
00154   int r_extern;
00155   unsigned int r_type;
00156   bfd_vma r_addend;
00157   asymbol *sym = *(g->sym_ptr_ptr);
00158   asection *output_section = sym->section->output_section;
00159 
00160   PUT_WORD (abfd, g->address, natptr->r_address);
00161 
00162   r_type = (unsigned int) g->howto->type;
00163 
00164   r_addend = g->addend;
00165   if ((sym->flags & BSF_SECTION_SYM) != 0)
00166     r_addend += (*(g->sym_ptr_ptr))->section->output_section->vma;
00167 
00168   /* If this relocation is relative to a symbol then set the
00169      r_index to the symbols index, and the r_extern bit.
00170 
00171      Absolute symbols can come in in two ways, either as an offset
00172      from the abs section, or as a symbol which has an abs value.
00173      check for that here.  */
00174 
00175   if (bfd_is_abs_section (bfd_get_section (sym)))
00176     {
00177       r_extern = 0;
00178       r_index = N_ABS;
00179     }
00180   else if ((sym->flags & BSF_SECTION_SYM) == 0)
00181     {
00182       if (bfd_is_und_section (bfd_get_section (sym))
00183          /* Remember to check for weak symbols; they count as global.  */
00184          || (sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
00185        r_extern = 1;
00186       else
00187        r_extern = 0;
00188       r_index = (*(g->sym_ptr_ptr))->KEEPIT;
00189     }
00190   else
00191     {
00192       /* Just an ordinary section.  */
00193       r_extern = 0;
00194       r_index = output_section->target_index;
00195     }
00196 
00197   /* The relocation type is the same as the canonical ones, but only
00198      the first 3 are used: RELOC_8, RELOC_16, RELOC_32.
00199      We may change this later, but assert this for the moment.  */
00200   if (r_type > 2)
00201     {
00202       (*_bfd_error_handler) (_("%s: Invalid relocation type exported: %d"),
00203                           bfd_get_filename (abfd), r_type);
00204 
00205       bfd_set_error (bfd_error_wrong_format);
00206     }
00207 
00208   /* Now the fun stuff.  */
00209   natptr->r_index[2] = r_index >> 16;
00210   natptr->r_index[1] = r_index >> 8;
00211   natptr->r_index[0] = r_index;
00212   natptr->r_type[0] =
00213      (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
00214       | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
00215 
00216   PUT_WORD (abfd, r_addend, natptr->r_addend);
00217 }
00218 
00219 /* We need our own to assert that a normal 8, 16 or 32 reloc is input.  */
00220 
00221 static void
00222 MY (swap_ext_reloc_in) (bfd *abfd,
00223                      struct reloc_ext_external *bytes,
00224                      arelent *cache_ptr,
00225                      asymbol **symbols,
00226                      bfd_size_type symcount)
00227 {
00228   unsigned int r_index;
00229   int r_extern;
00230   unsigned int r_type;
00231   struct aoutdata *su = &(abfd->tdata.aout_data->a);
00232 
00233   cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
00234 
00235   /* Now the fun stuff.  */
00236   r_index =  (bytes->r_index[2] << 16)
00237     | (bytes->r_index[1] << 8)
00238     |  bytes->r_index[0];
00239   r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
00240   r_type = ((bytes->r_type[0]) >> RELOC_EXT_BITS_TYPE_SH_LITTLE)
00241     & RELOC_EXT_BITS_TYPE_LITTLE;
00242 
00243   if (r_type > 2)
00244     {
00245       (*_bfd_error_handler) (_("%B: Invalid relocation type imported: %d"),
00246                           abfd, r_type);
00247 
00248       bfd_set_error (bfd_error_wrong_format);
00249     }
00250 
00251   cache_ptr->howto =  howto_table_ext + r_type;
00252 
00253   if (r_extern && r_index > symcount)
00254     {
00255       (*_bfd_error_handler)
00256         (_("%B: Bad relocation record imported: %d"), abfd, r_index);
00257 
00258       bfd_set_error (bfd_error_wrong_format);
00259 
00260       /* We continue, so we can catch further errors.  */
00261       r_extern = 0;
00262       r_index = N_ABS;
00263     }
00264 
00265   /* Magically uses r_extern, symbols etc.  Ugly, but it's what's in the
00266      default.  */
00267   MOVE_ADDRESS (GET_SWORD (abfd, bytes->r_addend));
00268 }
00269 
00270 /* We use the same as the default, except that we also set
00271    "obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;", to avoid changing
00272    NAME (aout, set_arch_mach) in aoutx.  */
00273 
00274 static bfd_boolean
00275 MY (set_sizes) (bfd *abfd)
00276 {
00277   /* Just as the default in aout-target.h (with some #ifdefs folded)...  */
00278 
00279   adata (abfd).page_size = TARGET_PAGE_SIZE;
00280   adata (abfd).segment_size = SEGMENT_SIZE;
00281   adata (abfd).zmagic_disk_block_size = ZMAGIC_DISK_BLOCK_SIZE;
00282   adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
00283 
00284   /* ... except for that we have the extended reloc.  The alternative
00285      would be to add a check on bfd_arch_cris in NAME (aout,
00286      set_arch_mach) in aoutx.h, but I don't want to do that since
00287      target-specific things should not be added there.  */
00288 
00289   obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
00290 
00291   return TRUE;
00292 }