Home | History | Annotate | Download | only in bfd
      1 /* BFD back-end for Intel 960 b.out binaries.
      2    Copyright (C) 1990-2014 Free Software Foundation, Inc.
      3    Written by Cygnus Support.
      4 
      5    This file is part of BFD, the Binary File Descriptor library.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20    MA 02110-1301, USA.  */
     21 
     22 #include "sysdep.h"
     23 #include "bfd.h"
     24 #include "libbfd.h"
     25 #include "bfdlink.h"
     26 #include "genlink.h"
     27 #include "bout.h"
     28 #include "libiberty.h"
     29 
     30 #include "aout/stab_gnu.h"
     31 #include "libaout.h"		/* BFD a.out internal data structures.  */
     32 
     33 #define ABS32CODE 0
     34 #define ABS32CODE_SHRUNK 1
     35 #define PCREL24 2
     36 #define CALLJ 3
     37 #define ABS32 4
     38 #define PCREL13 5
     39 #define ABS32_MAYBE_RELAXABLE 1
     40 #define ABS32_WAS_RELAXABLE 2
     41 
     42 #define ALIGNER 10
     43 #define ALIGNDONE 11
     44 
     45 static reloc_howto_type howto_reloc_callj =
     46   HOWTO (CALLJ, 0, 2, 24, TRUE, 0, complain_overflow_signed, 0,"callj", TRUE, 0x00ffffff, 0x00ffffff,FALSE);
     47 static  reloc_howto_type howto_reloc_abs32 =
     48   HOWTO (ABS32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"abs32", TRUE, 0xffffffff,0xffffffff,FALSE);
     49 static reloc_howto_type howto_reloc_pcrel24 =
     50   HOWTO (PCREL24, 0, 2, 24, TRUE, 0, complain_overflow_signed,0,"pcrel24", TRUE, 0x00ffffff,0x00ffffff,FALSE);
     51 static reloc_howto_type howto_reloc_pcrel13 =
     52   HOWTO (PCREL13, 0, 2, 13, TRUE, 0, complain_overflow_signed,0,"pcrel13", TRUE, 0x00001fff,0x00001fff,FALSE);
     53 static reloc_howto_type howto_reloc_abs32codeshrunk =
     54   HOWTO (ABS32CODE_SHRUNK, 0, 2, 24, TRUE, 0, complain_overflow_signed, 0,"callx->callj", TRUE, 0x00ffffff, 0x00ffffff,FALSE);
     55 static  reloc_howto_type howto_reloc_abs32code =
     56   HOWTO (ABS32CODE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"callx", TRUE, 0xffffffff,0xffffffff,FALSE);
     57 
     58 static reloc_howto_type howto_align_table[] =
     59 {
     60   HOWTO (ALIGNER, 0, 0x1, 0, FALSE, 0, complain_overflow_dont, 0, "align16", FALSE, 0, 0, FALSE),
     61   HOWTO (ALIGNER, 0, 0x3, 0, FALSE, 0, complain_overflow_dont, 0, "align32", FALSE, 0, 0, FALSE),
     62   HOWTO (ALIGNER, 0, 0x7, 0, FALSE, 0, complain_overflow_dont, 0, "align64", FALSE, 0, 0, FALSE),
     63   HOWTO (ALIGNER, 0, 0xf, 0, FALSE, 0, complain_overflow_dont, 0, "align128", FALSE, 0, 0, FALSE),
     64 };
     65 
     66 static reloc_howto_type howto_done_align_table[] =
     67 {
     68   HOWTO (ALIGNDONE, 0x1, 0x1, 0, FALSE, 0, complain_overflow_dont, 0, "donealign16", FALSE, 0, 0, FALSE),
     69   HOWTO (ALIGNDONE, 0x3, 0x3, 0, FALSE, 0, complain_overflow_dont, 0, "donealign32", FALSE, 0, 0, FALSE),
     70   HOWTO (ALIGNDONE, 0x7, 0x7, 0, FALSE, 0, complain_overflow_dont, 0, "donealign64", FALSE, 0, 0, FALSE),
     71   HOWTO (ALIGNDONE, 0xf, 0xf, 0, FALSE, 0, complain_overflow_dont, 0, "donealign128", FALSE, 0, 0, FALSE),
     72 };
     73 
     74 /* Swaps the information in an executable header taken from a raw byte
     75    stream memory image, into the internal exec_header structure.  */
     76 
     77 static void
     78 bout_swap_exec_header_in (bfd *abfd,
     79 			  struct external_exec *bytes,
     80 			  struct internal_exec *execp)
     81 {
     82   /* Now fill in fields in the execp, from the bytes in the raw data.  */
     83   execp->a_info      = H_GET_32 (abfd, bytes->e_info);
     84   execp->a_text      = GET_WORD (abfd, bytes->e_text);
     85   execp->a_data      = GET_WORD (abfd, bytes->e_data);
     86   execp->a_bss       = GET_WORD (abfd, bytes->e_bss);
     87   execp->a_syms      = GET_WORD (abfd, bytes->e_syms);
     88   execp->a_entry     = GET_WORD (abfd, bytes->e_entry);
     89   execp->a_trsize    = GET_WORD (abfd, bytes->e_trsize);
     90   execp->a_drsize    = GET_WORD (abfd, bytes->e_drsize);
     91   execp->a_tload     = GET_WORD (abfd, bytes->e_tload);
     92   execp->a_dload     = GET_WORD (abfd, bytes->e_dload);
     93   execp->a_talign    = bytes->e_talign[0];
     94   execp->a_dalign    = bytes->e_dalign[0];
     95   execp->a_balign    = bytes->e_balign[0];
     96   execp->a_relaxable = bytes->e_relaxable[0];
     97 }
     98 
     99 /* Swaps the information in an internal exec header structure into the
    100    supplied buffer ready for writing to disk.  */
    101 
    102 static void
    103 bout_swap_exec_header_out (bfd *abfd,
    104 			   struct internal_exec *execp,
    105 			   struct external_exec *bytes)
    106 {
    107   /* Now fill in fields in the raw data, from the fields in the exec struct.  */
    108   H_PUT_32 (abfd, execp->a_info  , bytes->e_info);
    109   PUT_WORD (abfd, execp->a_text  , bytes->e_text);
    110   PUT_WORD (abfd, execp->a_data  , bytes->e_data);
    111   PUT_WORD (abfd, execp->a_bss   , bytes->e_bss);
    112   PUT_WORD (abfd, execp->a_syms  , bytes->e_syms);
    113   PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
    114   PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
    115   PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
    116   PUT_WORD (abfd, execp->a_tload , bytes->e_tload);
    117   PUT_WORD (abfd, execp->a_dload , bytes->e_dload);
    118   bytes->e_talign[0]    = execp->a_talign;
    119   bytes->e_dalign[0]    = execp->a_dalign;
    120   bytes->e_balign[0]    = execp->a_balign;
    121   bytes->e_relaxable[0] = execp->a_relaxable;
    122 }
    123 
    124 /* Finish up the opening of a b.out file for reading.  Fill in all the
    125    fields that are not handled by common code.  */
    126 
    127 static const bfd_target *
    128 b_out_callback (bfd *abfd)
    129 {
    130   struct internal_exec *execp = exec_hdr (abfd);
    131   unsigned long bss_start;
    132 
    133   /* Architecture and machine type.  */
    134   bfd_set_arch_mach (abfd,
    135 		     bfd_arch_i960,     /* B.out only used on i960.  */
    136 		     bfd_mach_i960_core /* Default.  */
    137 		     );
    138 
    139   /* The positions of the string table and symbol table.  */
    140   obj_str_filepos (abfd) = N_STROFF (*execp);
    141   obj_sym_filepos (abfd) = N_SYMOFF (*execp);
    142 
    143   /* The alignments of the sections.  */
    144   obj_textsec (abfd)->alignment_power = execp->a_talign;
    145   obj_datasec (abfd)->alignment_power = execp->a_dalign;
    146   obj_bsssec  (abfd)->alignment_power = execp->a_balign;
    147 
    148   /* The starting addresses of the sections.  */
    149   obj_textsec (abfd)->vma = execp->a_tload;
    150   obj_datasec (abfd)->vma = execp->a_dload;
    151 
    152   obj_textsec (abfd)->lma = obj_textsec (abfd)->vma;
    153   obj_datasec (abfd)->lma = obj_datasec (abfd)->vma;
    154 
    155   /* And reload the sizes, since the aout module zaps them.  */
    156   obj_textsec (abfd)->size = execp->a_text;
    157 
    158   bss_start = execp->a_dload + execp->a_data; /* BSS = end of data section.  */
    159   obj_bsssec (abfd)->vma = align_power (bss_start, execp->a_balign);
    160 
    161   obj_bsssec (abfd)->lma = obj_bsssec (abfd)->vma;
    162 
    163   /* The file positions of the sections.  */
    164   obj_textsec (abfd)->filepos = N_TXTOFF (*execp);
    165   obj_datasec (abfd)->filepos = N_DATOFF (*execp);
    166 
    167   /* The file positions of the relocation info.  */
    168   obj_textsec (abfd)->rel_filepos = N_TROFF (*execp);
    169   obj_datasec (abfd)->rel_filepos =  N_DROFF (*execp);
    170 
    171   adata (abfd).page_size = 1;	/* Not applicable.  */
    172   adata (abfd).segment_size = 1; /* Not applicable.  */
    173   adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
    174 
    175   if (execp->a_relaxable)
    176    abfd->flags |= BFD_IS_RELAXABLE;
    177   return abfd->xvec;
    178 }
    179 
    180 static const bfd_target *
    181 b_out_object_p (bfd *abfd)
    182 {
    183   struct internal_exec anexec;
    184   struct external_exec exec_bytes;
    185   bfd_size_type amt = EXEC_BYTES_SIZE;
    186 
    187   if (bfd_bread ((void *) &exec_bytes, amt, abfd) != amt)
    188     {
    189       if (bfd_get_error () != bfd_error_system_call)
    190 	bfd_set_error (bfd_error_wrong_format);
    191       return 0;
    192     }
    193 
    194   anexec.a_info = H_GET_32 (abfd, exec_bytes.e_info);
    195 
    196   if (N_BADMAG (anexec))
    197     {
    198       bfd_set_error (bfd_error_wrong_format);
    199       return 0;
    200     }
    201 
    202   bout_swap_exec_header_in (abfd, &exec_bytes, &anexec);
    203   return aout_32_some_aout_object_p (abfd, &anexec, b_out_callback);
    204 }
    205 
    206 struct bout_data_struct
    207   {
    208     struct aoutdata a;
    209     struct internal_exec e;
    210   };
    211 
    212 static bfd_boolean
    213 b_out_mkobject (bfd *abfd)
    214 {
    215   struct bout_data_struct *rawptr;
    216   bfd_size_type amt = sizeof (struct bout_data_struct);
    217 
    218   rawptr = bfd_zalloc (abfd, amt);
    219   if (rawptr == NULL)
    220     return FALSE;
    221 
    222   abfd->tdata.bout_data = rawptr;
    223   exec_hdr (abfd) = &rawptr->e;
    224 
    225   obj_textsec (abfd) = NULL;
    226   obj_datasec (abfd) = NULL;
    227   obj_bsssec (abfd)  = NULL;
    228 
    229   return TRUE;
    230 }
    231 
    232 static int
    233 b_out_symbol_cmp (const void * a_ptr, const void * b_ptr)
    234 {
    235   struct aout_symbol ** a = (struct aout_symbol **) a_ptr;
    236   struct aout_symbol ** b = (struct aout_symbol **) b_ptr;
    237   asection *sec;
    238   bfd_vma av, bv;
    239 
    240   /* Primary key is address.  */
    241   sec = bfd_get_section (&(*a)->symbol);
    242   av = sec->output_section->vma + sec->output_offset + (*a)->symbol.value;
    243   sec = bfd_get_section (&(*b)->symbol);
    244   bv = sec->output_section->vma + sec->output_offset + (*b)->symbol.value;
    245 
    246   if (av < bv)
    247     return -1;
    248   if (av > bv)
    249     return 1;
    250 
    251   /* Secondary key puts CALLNAME syms last and BALNAME syms first,
    252      so that they have the best chance of being contiguous.  */
    253   if (IS_BALNAME ((*a)->other) || IS_CALLNAME ((*b)->other))
    254     return -1;
    255   if (IS_CALLNAME ((*a)->other) || IS_BALNAME ((*b)->other))
    256     return 1;
    257 
    258   return 0;
    259 }
    260 
    261 static bfd_boolean
    262 b_out_squirt_out_relocs (bfd *abfd, asection *section)
    263 {
    264   arelent **generic;
    265   int r_extern = 0;
    266   int r_idx;
    267   int incode_mask;
    268   int len_1;
    269   unsigned int count = section->reloc_count;
    270   struct relocation_info *native, *natptr;
    271   bfd_size_type natsize;
    272   int extern_mask, pcrel_mask, len_2, callj_mask;
    273 
    274   if (count == 0)
    275     return TRUE;
    276 
    277   generic = section->orelocation;
    278   natsize = (bfd_size_type) count * sizeof (struct relocation_info);
    279   native = bfd_malloc (natsize);
    280   if (!native && natsize != 0)
    281     return FALSE;
    282 
    283   if (bfd_header_big_endian (abfd))
    284     {
    285       /* Big-endian bit field allocation order.  */
    286       pcrel_mask  = 0x80;
    287       extern_mask = 0x10;
    288       len_2       = 0x40;
    289       len_1       = 0x20;
    290       callj_mask  = 0x02;
    291       incode_mask = 0x08;
    292     }
    293   else
    294     {
    295       /* Little-endian bit field allocation order.  */
    296       pcrel_mask  = 0x01;
    297       extern_mask = 0x08;
    298       len_2       = 0x04;
    299       len_1       = 0x02;
    300       callj_mask  = 0x40;
    301       incode_mask = 0x10;
    302     }
    303 
    304   for (natptr = native; count > 0; --count, ++natptr, ++generic)
    305     {
    306       arelent *g = *generic;
    307       unsigned char *raw = (unsigned char *) natptr;
    308       asymbol *sym = *(g->sym_ptr_ptr);
    309       asection *output_section = sym->section->output_section;
    310 
    311       H_PUT_32 (abfd, g->address, raw);
    312       /* Find a type in the output format which matches the input howto -
    313 	 at the moment we assume input format == output format FIXME!!  */
    314       r_idx = 0;
    315       /* FIXME:  Need callj stuff here, and to check the howto entries to
    316 	 be sure they are real for this architecture.  */
    317       if (g->howto== &howto_reloc_callj)
    318 	raw[7] = callj_mask + pcrel_mask + len_2;
    319       else if (g->howto == &howto_reloc_pcrel24)
    320 	raw[7] = pcrel_mask + len_2;
    321       else if (g->howto == &howto_reloc_pcrel13)
    322 	raw[7] = pcrel_mask + len_1;
    323       else if (g->howto == &howto_reloc_abs32code)
    324 	raw[7] = len_2 + incode_mask;
    325       else if (g->howto >= howto_align_table
    326 	       && g->howto <= (howto_align_table + ARRAY_SIZE (howto_align_table) - 1))
    327 	{
    328 	  /* symnum == -2; extern_mask not set, pcrel_mask set.  */
    329 	  r_idx = -2;
    330 	  r_extern = 0;
    331 	  raw[7] = (pcrel_mask
    332 		    | ((g->howto - howto_align_table) << 1));
    333 	}
    334       else
    335 	raw[7] = len_2;
    336 
    337       if (r_idx != 0)
    338 	/* Already mucked with r_extern, r_idx.  */;
    339       else if (bfd_is_com_section (output_section)
    340 	       || bfd_is_abs_section (output_section)
    341 	       || bfd_is_und_section (output_section))
    342 	{
    343 	  if (bfd_abs_section_ptr->symbol == sym)
    344 	    {
    345 	      /* Whoops, looked like an abs symbol, but is really an offset
    346 		 from the abs section.  */
    347 	      r_idx = 0;
    348 	      r_extern = 0;
    349 	    }
    350 	  else
    351 	    {
    352 	      /* Fill in symbol.  */
    353 	      r_extern = 1;
    354 	      r_idx = (*g->sym_ptr_ptr)->udata.i;
    355 	    }
    356 	}
    357       else
    358 	{
    359 	  /* Just an ordinary section.  */
    360 	  r_extern = 0;
    361 	  r_idx  = output_section->target_index;
    362 	}
    363 
    364       if (bfd_header_big_endian (abfd))
    365 	{
    366 	  raw[4] = (unsigned char) (r_idx >> 16);
    367 	  raw[5] = (unsigned char) (r_idx >>  8);
    368 	  raw[6] = (unsigned char) (r_idx      );
    369 	}
    370       else
    371 	{
    372 	  raw[6] = (unsigned char) (r_idx >> 16);
    373 	  raw[5] = (unsigned char) (r_idx>>   8);
    374 	  raw[4] = (unsigned char) (r_idx      );
    375 	}
    376 
    377       if (r_extern)
    378 	raw[7] |= extern_mask;
    379     }
    380 
    381   if (bfd_bwrite ((void *) native, natsize, abfd) != natsize)
    382     {
    383       free (native);
    384       return FALSE;
    385     }
    386 
    387   free (native);
    388 
    389   return TRUE;
    390 }
    391 
    392 static bfd_boolean
    393 b_out_write_object_contents (bfd *abfd)
    394 {
    395   struct external_exec swapped_hdr;
    396   bfd_size_type amt;
    397 
    398   if (! aout_32_make_sections (abfd))
    399     return FALSE;
    400 
    401   exec_hdr (abfd)->a_info = BMAGIC;
    402 
    403   exec_hdr (abfd)->a_text = obj_textsec (abfd)->size;
    404   exec_hdr (abfd)->a_data = obj_datasec (abfd)->size;
    405   exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->size;
    406   exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * 12;
    407   exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
    408   exec_hdr (abfd)->a_trsize = (obj_textsec (abfd)->reloc_count) * 8;
    409   exec_hdr (abfd)->a_drsize = (obj_datasec (abfd)->reloc_count) * 8;
    410 
    411   exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power;
    412   exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power;
    413   exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power;
    414 
    415   exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma;
    416   exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma;
    417 
    418   bout_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
    419 
    420   amt = EXEC_BYTES_SIZE;
    421   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
    422       || bfd_bwrite ((void *) &swapped_hdr, amt, abfd) != amt)
    423     return FALSE;
    424 
    425   /* Now write out reloc info, followed by syms and strings */
    426   if (bfd_get_symcount (abfd) != 0)
    427     {
    428       /* Make sure {CALL,BAL}NAME symbols remain adjacent on output
    429 	 by sorting.  This is complicated by the fact that stabs are
    430 	 also ordered.  Solve this by shifting all stabs to the end
    431 	 in order, then sorting the rest.  */
    432 
    433       asymbol **outsyms, **p, **q;
    434 
    435       outsyms = bfd_get_outsymbols (abfd);
    436       p = outsyms + bfd_get_symcount (abfd);
    437 
    438       for (q = p--; p >= outsyms; p--)
    439 	{
    440 	  if ((*p)->flags & BSF_DEBUGGING)
    441 	    {
    442 	      asymbol *t = *--q;
    443 	      *q = *p;
    444 	      *p = t;
    445 	    }
    446 	}
    447 
    448       if (q > outsyms)
    449 	qsort (outsyms, (size_t) (q - outsyms), sizeof (asymbol*),
    450 	       b_out_symbol_cmp);
    451 
    452       /* Back to your regularly scheduled program.  */
    453       if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (*exec_hdr (abfd))), SEEK_SET)
    454 	  != 0)
    455 	return FALSE;
    456 
    457       if (! aout_32_write_syms (abfd))
    458 	return FALSE;
    459 
    460       if (bfd_seek (abfd, (file_ptr) (N_TROFF (*exec_hdr (abfd))), SEEK_SET)
    461 	  != 0)
    462 	return FALSE;
    463 
    464       if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd)))
    465 	return FALSE;
    466       if (bfd_seek (abfd, (file_ptr) (N_DROFF (*exec_hdr (abfd))), SEEK_SET)
    467 	  != 0)
    468 	return FALSE;
    469 
    470       if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd)))
    471 	return FALSE;
    472     }
    473   return TRUE;
    474 }
    475 
    476 /* Some reloc hackery.  */
    478 
    479 #define CALLS	  0x66003800	/* Template for 'calls' instruction	*/
    480 #define BAL	  0x0b000000	/* Template for 'bal' instruction 	*/
    481 #define BAL_MASK  0x00ffffff
    482 #define BALX	  0x85f00000	/* Template for 'balx' instruction	*/
    483 #define BALX_MASK 0x0007ffff
    484 #define CALL      0x09000000
    485 #define PCREL13_MASK 0x1fff
    486 
    487 #define output_addr(sec) ((sec)->output_offset+(sec)->output_section->vma)
    488 
    489 static bfd_vma
    490 get_value (arelent *reloc,
    491 	   struct bfd_link_info *link_info,
    492 	   asection *input_section)
    493 {
    494   bfd_vma value;
    495   asymbol *symbol = *(reloc->sym_ptr_ptr);
    496 
    497   /* A symbol holds a pointer to a section, and an offset from the
    498      base of the section.  To relocate, we find where the section will
    499      live in the output and add that in.  */
    500   if (bfd_is_und_section (symbol->section))
    501     {
    502       struct bfd_link_hash_entry *h;
    503 
    504       /* The symbol is undefined in this BFD.  Look it up in the
    505 	 global linker hash table.  FIXME: This should be changed when
    506 	 we convert b.out to use a specific final_link function and
    507 	 change the interface to bfd_relax_section to not require the
    508 	 generic symbols.  */
    509       h = bfd_wrapped_link_hash_lookup (input_section->owner, link_info,
    510 					bfd_asymbol_name (symbol),
    511 					FALSE, FALSE, TRUE);
    512       if (h != (struct bfd_link_hash_entry *) NULL
    513 	  && (h->type == bfd_link_hash_defined
    514 	      || h->type == bfd_link_hash_defweak))
    515 	value = h->u.def.value + output_addr (h->u.def.section);
    516       else if (h != (struct bfd_link_hash_entry *) NULL
    517 	       && h->type == bfd_link_hash_common)
    518 	value = h->u.c.size;
    519       else
    520 	{
    521 	  if (! ((*link_info->callbacks->undefined_symbol)
    522 		 (link_info, bfd_asymbol_name (symbol),
    523 		  input_section->owner, input_section, reloc->address,
    524 		  TRUE)))
    525 	    abort ();
    526 	  value = 0;
    527 	}
    528     }
    529   else
    530     value = symbol->value + output_addr (symbol->section);
    531 
    532   /* Add the value contained in the relocation.  */
    533   value += reloc->addend;
    534 
    535   return value;
    536 }
    537 
    538 /* Magic to turn callx into calljx.  */
    539 
    540 static bfd_reloc_status_type
    541 calljx_callback (bfd *abfd,
    542 		 struct bfd_link_info *link_info,
    543 		 arelent *reloc_entry,
    544 		 void * src,
    545 		 void * dst,
    546 		 asection *input_section)
    547 {
    548   int word = bfd_get_32 (abfd, src);
    549   asymbol *symbol_in = *(reloc_entry->sym_ptr_ptr);
    550   aout_symbol_type *symbol = aout_symbol (symbol_in);
    551   bfd_vma value;
    552 
    553   value = get_value (reloc_entry, link_info, input_section);
    554 
    555   if (IS_CALLNAME (symbol->other))
    556     {
    557       aout_symbol_type *balsym = symbol+1;
    558       int inst = bfd_get_32 (abfd, (bfd_byte *) src-4);
    559 
    560       /* The next symbol should be an N_BALNAME.  */
    561       BFD_ASSERT (IS_BALNAME (balsym->other));
    562       inst &= BALX_MASK;
    563       inst |= BALX;
    564       bfd_put_32 (abfd, (bfd_vma) inst, (bfd_byte *) dst-4);
    565       symbol = balsym;
    566       value = (symbol->symbol.value
    567 	       + output_addr (symbol->symbol.section));
    568     }
    569 
    570   word += value + reloc_entry->addend;
    571 
    572   bfd_put_32 (abfd, (bfd_vma) word, dst);
    573   return bfd_reloc_ok;
    574 }
    575 
    576 /* Magic to turn call into callj.  */
    577 
    578 static bfd_reloc_status_type
    579 callj_callback (bfd *abfd,
    580 		struct bfd_link_info *link_info,
    581 		arelent *reloc_entry,
    582 		void * data,
    583 		unsigned int srcidx,
    584 		unsigned int dstidx,
    585 		asection *input_section,
    586 		bfd_boolean shrinking)
    587 {
    588   int word = bfd_get_32 (abfd, (bfd_byte *) data + srcidx);
    589   asymbol *symbol_in = *(reloc_entry->sym_ptr_ptr);
    590   aout_symbol_type *symbol = aout_symbol (symbol_in);
    591   bfd_vma value;
    592 
    593   value = get_value (reloc_entry, link_info, input_section);
    594 
    595   if (IS_OTHER (symbol->other))
    596     /* Call to a system procedure - replace code with system
    597        procedure number.  */
    598     word = CALLS | (symbol->other - 1);
    599 
    600   else if (IS_CALLNAME (symbol->other))
    601     {
    602       aout_symbol_type *balsym = symbol+1;
    603 
    604       /* The next symbol should be an N_BALNAME.  */
    605       BFD_ASSERT (IS_BALNAME (balsym->other));
    606 
    607       /* We are calling a leaf, so replace the call instruction with a
    608 	 bal.  */
    609       word = BAL | ((word
    610 		     + output_addr (balsym->symbol.section)
    611 		     + balsym->symbol.value + reloc_entry->addend
    612 		     - dstidx
    613 		     - output_addr (input_section))
    614 		    & BAL_MASK);
    615     }
    616   else if ((symbol->symbol.flags & BSF_SECTION_SYM) != 0)
    617     {
    618       /* A callj against a symbol in the same section is a fully
    619          resolved relative call.  We don't need to do anything here.
    620          If the symbol is not in the same section, I'm not sure what
    621          to do; fortunately, this case will probably never arise.  */
    622       BFD_ASSERT (! shrinking);
    623       BFD_ASSERT (symbol->symbol.section == input_section);
    624     }
    625   else
    626     word = CALL | (((word & BAL_MASK)
    627 		    + value
    628 		    + reloc_entry->addend
    629 		    - (shrinking ? dstidx : 0)
    630 		    - output_addr (input_section))
    631 		   & BAL_MASK);
    632 
    633   bfd_put_32 (abfd, (bfd_vma) word, (bfd_byte *) data + dstidx);
    634   return bfd_reloc_ok;
    635 }
    636 
    637 static reloc_howto_type *
    638 b_out_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    639 			     bfd_reloc_code_real_type code)
    640 {
    641   switch (code)
    642     {
    643     default:
    644       return 0;
    645     case BFD_RELOC_I960_CALLJ:
    646       return &howto_reloc_callj;
    647     case BFD_RELOC_32:
    648     case BFD_RELOC_CTOR:
    649       return &howto_reloc_abs32;
    650     case BFD_RELOC_24_PCREL:
    651       return &howto_reloc_pcrel24;
    652     }
    653 }
    654 
    655 static reloc_howto_type *
    656 b_out_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    657 			     const char *r_name)
    658 {
    659   if (strcasecmp (howto_reloc_callj.name, r_name) == 0)
    660     return &howto_reloc_callj;
    661   if (strcasecmp (howto_reloc_abs32.name, r_name) == 0)
    662     return &howto_reloc_abs32;
    663   if (strcasecmp (howto_reloc_pcrel24.name, r_name) == 0)
    664     return &howto_reloc_pcrel24;
    665 
    666   return NULL;
    667 }
    668 
    669 /* Allocate enough room for all the reloc entries, plus pointers to them all.  */
    670 
    671 static bfd_boolean
    672 b_out_slurp_reloc_table (bfd *abfd, sec_ptr asect, asymbol **symbols)
    673 {
    674   struct relocation_info *rptr;
    675   unsigned int counter;
    676   arelent *cache_ptr;
    677   int extern_mask, pcrel_mask, callj_mask, length_shift;
    678   int incode_mask;
    679   int size_mask;
    680   bfd_vma prev_addr = 0;
    681   unsigned int count;
    682   bfd_size_type reloc_size, amt;
    683   struct relocation_info *relocs;
    684   arelent *reloc_cache;
    685 
    686   if (asect->relocation)
    687     return TRUE;
    688 
    689   if (!aout_32_slurp_symbol_table (abfd))
    690     return FALSE;
    691 
    692   if (asect == obj_datasec (abfd))
    693     reloc_size = exec_hdr (abfd)->a_drsize;
    694   else if (asect == obj_textsec (abfd))
    695     reloc_size = exec_hdr (abfd)->a_trsize;
    696   else if (asect == obj_bsssec (abfd))
    697     reloc_size = 0;
    698   else
    699     {
    700       bfd_set_error (bfd_error_invalid_operation);
    701       return FALSE;
    702     }
    703 
    704   if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
    705     return FALSE;
    706   count = reloc_size / sizeof (struct relocation_info);
    707 
    708   relocs = bfd_malloc (reloc_size);
    709   if (!relocs && reloc_size != 0)
    710     return FALSE;
    711 
    712   amt = ((bfd_size_type) count + 1) * sizeof (arelent);
    713   reloc_cache = bfd_malloc (amt);
    714   if (!reloc_cache)
    715     {
    716       if (relocs != NULL)
    717 	free (relocs);
    718       return FALSE;
    719     }
    720 
    721   if (bfd_bread ((void *) relocs, reloc_size, abfd) != reloc_size)
    722     {
    723       free (reloc_cache);
    724       if (relocs != NULL)
    725 	free (relocs);
    726       return FALSE;
    727     }
    728 
    729   if (bfd_header_big_endian (abfd))
    730     {
    731       /* Big-endian bit field allocation order.  */
    732       pcrel_mask  = 0x80;
    733       extern_mask = 0x10;
    734       incode_mask = 0x08;
    735       callj_mask  = 0x02;
    736       size_mask =   0x20;
    737       length_shift = 5;
    738     }
    739   else
    740     {
    741       /* Little-endian bit field allocation order.  */
    742       pcrel_mask  = 0x01;
    743       extern_mask = 0x08;
    744       incode_mask = 0x10;
    745       callj_mask  = 0x40;
    746       size_mask   = 0x02;
    747       length_shift = 1;
    748     }
    749 
    750   for (rptr = relocs, cache_ptr = reloc_cache, counter = 0;
    751        counter < count;
    752        counter++, rptr++, cache_ptr++)
    753   {
    754     unsigned char *raw = (unsigned char *)rptr;
    755     unsigned int symnum;
    756 
    757     cache_ptr->address = H_GET_32 (abfd, raw + 0);
    758     cache_ptr->howto = 0;
    759 
    760     if (bfd_header_big_endian (abfd))
    761       symnum = (raw[4] << 16) | (raw[5] << 8) | raw[6];
    762     else
    763       symnum = (raw[6] << 16) | (raw[5] << 8) | raw[4];
    764 
    765     if (raw[7] & extern_mask)
    766       {
    767 	/* If this is set then the r_index is an index into the symbol table;
    768 	   if the bit is not set then r_index contains a section map.
    769 	   We either fill in the sym entry with a pointer to the symbol,
    770 	   or point to the correct section.  */
    771       cache_ptr->sym_ptr_ptr = symbols + symnum;
    772       cache_ptr->addend = 0;
    773       }
    774     else
    775       {
    776 	/* In a.out symbols are relative to the beginning of the
    777 	   file rather than sections ?
    778 	   (look in translate_from_native_sym_flags)
    779 	   The reloc entry addend has added to it the offset into the
    780 	   file of the data, so subtract the base to make the reloc
    781 	   section relative.  */
    782 	int s;
    783 
    784 	/* Sign-extend symnum from 24 bits to whatever host uses.  */
    785 	s = symnum;
    786 	if (s & (1 << 23))
    787 	  s |= (~0) << 24;
    788 
    789 	cache_ptr->sym_ptr_ptr = (asymbol **)NULL;
    790 	switch (s)
    791 	  {
    792 	  case N_TEXT:
    793 	  case N_TEXT | N_EXT:
    794 	    cache_ptr->sym_ptr_ptr = obj_textsec (abfd)->symbol_ptr_ptr;
    795 	    cache_ptr->addend = - obj_textsec (abfd)->vma;
    796 	    break;
    797 	  case N_DATA:
    798 	  case N_DATA | N_EXT:
    799 	    cache_ptr->sym_ptr_ptr = obj_datasec (abfd)->symbol_ptr_ptr;
    800 	    cache_ptr->addend = - obj_datasec (abfd)->vma;
    801 	    break;
    802 	  case N_BSS:
    803 	  case N_BSS | N_EXT:
    804 	    cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr;
    805 	    cache_ptr->addend =  - obj_bsssec (abfd)->vma;
    806 	    break;
    807 	  case N_ABS:
    808 	  case N_ABS | N_EXT:
    809 	    cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr;
    810 	    cache_ptr->addend = 0;
    811 	    break;
    812 	  case -2: /* .align */
    813 	    if (raw[7] & pcrel_mask)
    814 	      {
    815 		cache_ptr->howto = &howto_align_table[(raw[7] >> length_shift) & 3];
    816 		cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
    817 	      }
    818 	    else
    819 	      {
    820 		/* .org? */
    821 		abort ();
    822 	      }
    823 	    cache_ptr->addend = 0;
    824 	    break;
    825 	  default:
    826 	    BFD_ASSERT (FALSE);
    827 	    break;
    828 	  }
    829       }
    830 
    831     /* The i960 only has a few relocation types:
    832        abs 32-bit and pcrel 24bit.   except for callj's!  */
    833     if (cache_ptr->howto != 0)
    834       ;
    835     else if (raw[7] & callj_mask)
    836       {
    837 	cache_ptr->howto = &howto_reloc_callj;
    838       }
    839     else if ( raw[7] & pcrel_mask)
    840       {
    841 	if (raw[7] & size_mask)
    842 	  cache_ptr->howto = &howto_reloc_pcrel13;
    843 	else
    844 	  cache_ptr->howto = &howto_reloc_pcrel24;
    845       }
    846     else
    847       {
    848 	if (raw[7] & incode_mask)
    849 	  cache_ptr->howto = &howto_reloc_abs32code;
    850 	else
    851 	  cache_ptr->howto = &howto_reloc_abs32;
    852       }
    853 
    854     if (cache_ptr->address < prev_addr)
    855       {
    856 	/* Ouch! this reloc is out of order, insert into the right place.  */
    857 	arelent tmp;
    858 	arelent *cursor = cache_ptr-1;
    859 	bfd_vma stop = cache_ptr->address;
    860 
    861 	tmp  = *cache_ptr;
    862 	while (cursor->address > stop && cursor >= reloc_cache)
    863 	  {
    864 	    cursor[1] = cursor[0];
    865 	    cursor--;
    866 	  }
    867 
    868 	cursor[1] = tmp;
    869       }
    870     else
    871       prev_addr = cache_ptr->address;
    872   }
    873 
    874   if (relocs != NULL)
    875     free (relocs);
    876   asect->relocation = reloc_cache;
    877   asect->reloc_count = count;
    878 
    879   return TRUE;
    880 }
    881 
    882 /* This is stupid.  This function should be a boolean predicate.  */
    883 
    884 static long
    885 b_out_canonicalize_reloc (bfd *abfd,
    886 			  sec_ptr section,
    887 			  arelent **relptr,
    888 			  asymbol **symbols)
    889 {
    890   arelent *tblptr;
    891   unsigned int count;
    892 
    893   if ((section->flags & SEC_CONSTRUCTOR) != 0)
    894     {
    895       arelent_chain *chain = section->constructor_chain;
    896 
    897       for (count = 0; count < section->reloc_count; count++)
    898 	{
    899 	  *relptr++ = &chain->relent;
    900 	  chain = chain->next;
    901 	}
    902     }
    903   else
    904     {
    905       if (section->relocation == NULL
    906 	  && ! b_out_slurp_reloc_table (abfd, section, symbols))
    907 	return -1;
    908 
    909       tblptr = section->relocation;
    910       for (count = 0; count++ < section->reloc_count;)
    911 	*relptr++ = tblptr++;
    912     }
    913 
    914   *relptr = NULL;
    915 
    916   return section->reloc_count;
    917 }
    918 
    919 static long
    920 b_out_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
    921 {
    922   if (bfd_get_format (abfd) != bfd_object)
    923     {
    924       bfd_set_error (bfd_error_invalid_operation);
    925       return -1;
    926     }
    927 
    928   if (asect->flags & SEC_CONSTRUCTOR)
    929     return sizeof (arelent *) * (asect->reloc_count + 1);
    930 
    931   if (asect == obj_datasec (abfd))
    932     return (sizeof (arelent *) *
    933 	    ((exec_hdr (abfd)->a_drsize / sizeof (struct relocation_info))
    934 	     + 1));
    935 
    936   if (asect == obj_textsec (abfd))
    937     return (sizeof (arelent *) *
    938 	    ((exec_hdr (abfd)->a_trsize / sizeof (struct relocation_info))
    939 	     + 1));
    940 
    941   if (asect == obj_bsssec (abfd))
    942     return 0;
    943 
    944   bfd_set_error (bfd_error_invalid_operation);
    945   return -1;
    946 }
    947 
    948 
    949 static bfd_boolean
    951 b_out_set_section_contents (bfd *abfd,
    952 			    asection *section,
    953 			    const void * location,
    954 			    file_ptr offset,
    955 			    bfd_size_type count)
    956 {
    957   if (! abfd->output_has_begun)
    958     {
    959       /* Set by bfd.c handler.  */
    960       if (! aout_32_make_sections (abfd))
    961 	return FALSE;
    962 
    963       obj_textsec (abfd)->filepos = sizeof (struct external_exec);
    964       obj_datasec (abfd)->filepos = obj_textsec (abfd)->filepos
    965 	+  obj_textsec (abfd)->size;
    966     }
    967 
    968   /* Regardless, once we know what we're doing, we might as well get going.  */
    969   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
    970     return FALSE;
    971 
    972   if (count == 0)
    973     return TRUE;
    974 
    975   return bfd_bwrite ((void *) location, count, abfd) == count;
    976 }
    977 
    978 static bfd_boolean
    979 b_out_set_arch_mach (bfd *abfd,
    980 		     enum bfd_architecture arch,
    981 		     unsigned long machine)
    982 {
    983   bfd_default_set_arch_mach (abfd, arch, machine);
    984 
    985   if (arch == bfd_arch_unknown)	/* Unknown machine arch is OK.  */
    986     return TRUE;
    987 
    988   if (arch == bfd_arch_i960)	/* i960 default is OK.  */
    989     switch (machine)
    990       {
    991       case bfd_mach_i960_core:
    992       case bfd_mach_i960_kb_sb:
    993       case bfd_mach_i960_mc:
    994       case bfd_mach_i960_xa:
    995       case bfd_mach_i960_ca:
    996       case bfd_mach_i960_ka_sa:
    997       case bfd_mach_i960_jx:
    998       case bfd_mach_i960_hx:
    999       case 0:
   1000 	return TRUE;
   1001       default:
   1002 	return FALSE;
   1003       }
   1004 
   1005   return FALSE;
   1006 }
   1007 
   1008 static int
   1009 b_out_sizeof_headers (bfd *ignore_abfd ATTRIBUTE_UNUSED,
   1010 		      struct bfd_link_info *info ATTRIBUTE_UNUSED)
   1011 {
   1012   return sizeof (struct external_exec);
   1013 }
   1014 
   1015 static void
   1017 perform_slip (bfd *abfd,
   1018 	      unsigned int slip,
   1019 	      asection *input_section,
   1020 	      bfd_vma value)
   1021 {
   1022   asymbol **s;
   1023 
   1024   s = _bfd_generic_link_get_symbols (abfd);
   1025   BFD_ASSERT (s != (asymbol **) NULL);
   1026 
   1027   /* Find all symbols past this point, and make them know
   1028      what's happened.  */
   1029   while (*s)
   1030     {
   1031       asymbol *p = *s;
   1032 
   1033       if (p->section == input_section)
   1034 	{
   1035 	  /* This was pointing into this section, so mangle it.  */
   1036 	  if (p->value > value)
   1037 	    {
   1038 	      p->value -=slip;
   1039 
   1040 	      if (p->udata.p != NULL)
   1041 		{
   1042 		  struct generic_link_hash_entry *h;
   1043 
   1044 		  h = (struct generic_link_hash_entry *) p->udata.p;
   1045 		  BFD_ASSERT (h->root.type == bfd_link_hash_defined);
   1046 		  h->root.u.def.value -= slip;
   1047 		  BFD_ASSERT (h->root.u.def.value == p->value);
   1048 		}
   1049 	    }
   1050 	}
   1051       s++;
   1052     }
   1053 }
   1054 
   1055 /* This routine works out if the thing we want to get to can be
   1056    reached with a 24bit offset instead of a 32 bit one.
   1057    If it can, then it changes the amode.  */
   1058 
   1059 static int
   1060 abs32code (bfd *abfd,
   1061 	   asection *input_section,
   1062 	   arelent *r,
   1063 	   unsigned int shrink,
   1064 	   struct bfd_link_info *link_info)
   1065 {
   1066   bfd_vma value = get_value (r, link_info, input_section);
   1067   bfd_vma dot = output_addr (input_section) + r->address;
   1068   bfd_vma gap;
   1069 
   1070   /* See if the address we're looking at within 2^23 bytes of where
   1071      we are, if so then we can use a small branch rather than the
   1072      jump we were going to.  */
   1073   gap = value - (dot - shrink);
   1074 
   1075   if (-1 << 23 < (long)gap && (long)gap < 1 << 23)
   1076     {
   1077       /* Change the reloc type from 32bitcode possible 24, to 24bit
   1078 	 possible 32.  */
   1079       r->howto = &howto_reloc_abs32codeshrunk;
   1080       /* The place to relc moves back by four bytes.  */
   1081       r->address -=4;
   1082 
   1083       /* This will be four bytes smaller in the long run.  */
   1084       shrink += 4 ;
   1085       perform_slip (abfd, 4, input_section, r->address-shrink + 4);
   1086     }
   1087 
   1088   return shrink;
   1089 }
   1090 
   1091 static int
   1092 aligncode (bfd *abfd,
   1093 	   asection *input_section,
   1094 	   arelent *r,
   1095 	   unsigned int shrink)
   1096 {
   1097   bfd_vma dot = output_addr (input_section) + r->address;
   1098   bfd_vma old_end;
   1099   bfd_vma new_end;
   1100   unsigned int shrink_delta;
   1101   int size = r->howto->size;
   1102 
   1103   /* Reduce the size of the alignment so that it's still aligned but
   1104      smaller  - the current size is already the same size as or bigger
   1105      than the alignment required.  */
   1106 
   1107   /* Calculate the first byte following the padding before we optimize.  */
   1108   old_end = ((dot + size ) & ~size) + size+1;
   1109   /* Work out where the new end will be - remember that we're smaller
   1110      than we used to be.  */
   1111   new_end = ((dot - shrink + size) & ~size);
   1112 
   1113   shrink_delta = (old_end - new_end) - shrink;
   1114 
   1115   if (shrink_delta)
   1116     {
   1117       /* Change the reloc so that it knows how far to align to.  */
   1118       r->howto = howto_done_align_table + (r->howto - howto_align_table);
   1119 
   1120       /* Encode the stuff into the addend - for future use we need to
   1121 	 know how big the reloc used to be.  */
   1122       r->addend = old_end - dot + r->address;
   1123 
   1124       /* This will be N bytes smaller in the long run, adjust all the symbols.  */
   1125       perform_slip (abfd, shrink_delta, input_section, r->address - shrink);
   1126       shrink += shrink_delta;
   1127     }
   1128 
   1129   return shrink;
   1130 }
   1131 
   1132 static bfd_boolean
   1133 b_out_bfd_relax_section (bfd *abfd,
   1134 			 asection *i,
   1135 			 struct bfd_link_info *link_info,
   1136 			 bfd_boolean *again)
   1137 {
   1138   /* Get enough memory to hold the stuff.  */
   1139   bfd *input_bfd = i->owner;
   1140   asection *input_section = i;
   1141   unsigned int shrink = 0 ;
   1142   arelent **reloc_vector = NULL;
   1143   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
   1144 
   1145   if (link_info->relocatable)
   1146     (*link_info->callbacks->einfo)
   1147       (_("%P%F: --relax and -r may not be used together\n"));
   1148 
   1149   if (reloc_size < 0)
   1150     return FALSE;
   1151 
   1152   /* We only run this relaxation once.  It might work to run it
   1153      multiple times, but it hasn't been tested.  */
   1154   *again = FALSE;
   1155 
   1156   if (reloc_size)
   1157     {
   1158       long reloc_count;
   1159 
   1160       reloc_vector = bfd_malloc ((bfd_size_type) reloc_size);
   1161       if (reloc_vector == NULL && reloc_size != 0)
   1162 	goto error_return;
   1163 
   1164       /* Get the relocs and think about them.  */
   1165       reloc_count =
   1166 	bfd_canonicalize_reloc (input_bfd, input_section, reloc_vector,
   1167 				_bfd_generic_link_get_symbols (input_bfd));
   1168       if (reloc_count < 0)
   1169 	goto error_return;
   1170       if (reloc_count > 0)
   1171 	{
   1172 	  arelent **parent;
   1173 
   1174 	  for (parent = reloc_vector; *parent; parent++)
   1175 	    {
   1176 	      arelent *r = *parent;
   1177 
   1178 	      switch (r->howto->type)
   1179 		{
   1180 		case ALIGNER:
   1181 		  /* An alignment reloc.  */
   1182 		  shrink = aligncode (abfd, input_section, r, shrink);
   1183 		  break;
   1184 		case ABS32CODE:
   1185 		  /* A 32bit reloc in an addressing mode.  */
   1186 		  shrink = abs32code (input_bfd, input_section, r, shrink,
   1187 				      link_info);
   1188 		  break;
   1189 		case ABS32CODE_SHRUNK:
   1190 		  shrink += 4;
   1191 		  break;
   1192 		}
   1193 	    }
   1194 	}
   1195     }
   1196   input_section->size -= shrink;
   1197 
   1198   if (reloc_vector != NULL)
   1199     free (reloc_vector);
   1200   return TRUE;
   1201  error_return:
   1202   if (reloc_vector != NULL)
   1203     free (reloc_vector);
   1204   return FALSE;
   1205 }
   1206 
   1207 static bfd_byte *
   1208 b_out_bfd_get_relocated_section_contents (bfd *output_bfd,
   1209 					  struct bfd_link_info *link_info,
   1210 					  struct bfd_link_order *link_order,
   1211 					  bfd_byte *data,
   1212 					  bfd_boolean relocatable,
   1213 					  asymbol **symbols)
   1214 {
   1215   /* Get enough memory to hold the stuff.  */
   1216   bfd *input_bfd = link_order->u.indirect.section->owner;
   1217   asection *input_section = link_order->u.indirect.section;
   1218   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
   1219   arelent **reloc_vector = NULL;
   1220   long reloc_count;
   1221 
   1222   if (reloc_size < 0)
   1223     goto error_return;
   1224 
   1225   /* If producing relocatable output, don't bother to relax.  */
   1226   if (relocatable)
   1227     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
   1228 						       link_order,
   1229 						       data, relocatable,
   1230 						       symbols);
   1231 
   1232   reloc_vector = bfd_malloc ((bfd_size_type) reloc_size);
   1233   if (reloc_vector == NULL && reloc_size != 0)
   1234     goto error_return;
   1235 
   1236   /* Read in the section.  */
   1237   BFD_ASSERT (bfd_get_section_contents (input_bfd,
   1238 					input_section,
   1239 					data,
   1240 					(bfd_vma) 0,
   1241 					input_section->size));
   1242 
   1243   reloc_count = bfd_canonicalize_reloc (input_bfd,
   1244 					input_section,
   1245 					reloc_vector,
   1246 					symbols);
   1247   if (reloc_count < 0)
   1248     goto error_return;
   1249   if (reloc_count > 0)
   1250     {
   1251       arelent **parent = reloc_vector;
   1252       arelent *reloc ;
   1253       unsigned int dst_address = 0;
   1254       unsigned int src_address = 0;
   1255       unsigned int run;
   1256       unsigned int idx;
   1257 
   1258       /* Find how long a run we can do.  */
   1259       while (dst_address < link_order->size)
   1260 	{
   1261 	  reloc = *parent;
   1262 	  if (reloc)
   1263 	    {
   1264 	      /* Note that the relaxing didn't tie up the addresses in the
   1265 		 relocation, so we use the original address to work out the
   1266 		 run of non-relocated data.  */
   1267 	      BFD_ASSERT (reloc->address >= src_address);
   1268 	      run = reloc->address - src_address;
   1269 	      parent++;
   1270 	    }
   1271 	  else
   1272 	    run = link_order->size - dst_address;
   1273 
   1274 	  /* Copy the bytes.  */
   1275 	  for (idx = 0; idx < run; idx++)
   1276 	    data[dst_address++] = data[src_address++];
   1277 
   1278 	  /* Now do the relocation.  */
   1279 	  if (reloc)
   1280 	    {
   1281 	      switch (reloc->howto->type)
   1282 		{
   1283 		case ABS32CODE:
   1284 		  calljx_callback (input_bfd, link_info, reloc,
   1285 				   src_address + data, dst_address + data,
   1286 				   input_section);
   1287 		  src_address += 4;
   1288 		  dst_address += 4;
   1289 		  break;
   1290 		case ABS32:
   1291 		  bfd_put_32 (input_bfd,
   1292 			      (bfd_get_32 (input_bfd, data + src_address)
   1293 			       + get_value (reloc, link_info, input_section)),
   1294 			      data + dst_address);
   1295 		  src_address += 4;
   1296 		  dst_address += 4;
   1297 		  break;
   1298 		case CALLJ:
   1299 		  callj_callback (input_bfd, link_info, reloc, data,
   1300 				  src_address, dst_address, input_section,
   1301 				  FALSE);
   1302 		  src_address += 4;
   1303 		  dst_address += 4;
   1304 		  break;
   1305 		case ALIGNDONE:
   1306 		  BFD_ASSERT (reloc->addend >= src_address);
   1307 		  BFD_ASSERT ((bfd_vma) reloc->addend
   1308 			      <= input_section->size);
   1309 		  src_address = reloc->addend;
   1310 		  dst_address = ((dst_address + reloc->howto->size)
   1311 				 & ~reloc->howto->size);
   1312 		  break;
   1313 		case ABS32CODE_SHRUNK:
   1314 		  /* This used to be a callx, but we've found out that a
   1315 		     callj will reach, so do the right thing.  */
   1316 		  callj_callback (input_bfd, link_info, reloc, data,
   1317 				  src_address + 4, dst_address, input_section,
   1318 				  TRUE);
   1319 		  dst_address += 4;
   1320 		  src_address += 8;
   1321 		  break;
   1322 		case PCREL24:
   1323 		  {
   1324 		    long int word = bfd_get_32 (input_bfd,
   1325 						data + src_address);
   1326 		    bfd_vma value;
   1327 
   1328 		    value = get_value (reloc, link_info, input_section);
   1329 		    word = ((word & ~BAL_MASK)
   1330 			    | (((word & BAL_MASK)
   1331 				+ value
   1332 				- output_addr (input_section)
   1333 				+ reloc->addend)
   1334 			       & BAL_MASK));
   1335 
   1336 		    bfd_put_32 (input_bfd, (bfd_vma) word, data + dst_address);
   1337 		    dst_address += 4;
   1338 		    src_address += 4;
   1339 
   1340 		  }
   1341 		  break;
   1342 		case PCREL13:
   1343 		  {
   1344 		    long int word = bfd_get_32 (input_bfd,
   1345 						data + src_address);
   1346 		    bfd_vma value;
   1347 
   1348 		    value = get_value (reloc, link_info, input_section);
   1349 		    word = ((word & ~PCREL13_MASK)
   1350 			    | (((word & PCREL13_MASK)
   1351 				+ value
   1352 				+ reloc->addend
   1353 				- output_addr (input_section))
   1354 			       & PCREL13_MASK));
   1355 
   1356 		    bfd_put_32 (input_bfd, (bfd_vma) word, data + dst_address);
   1357 		    dst_address += 4;
   1358 		    src_address += 4;
   1359 		  }
   1360 		  break;
   1361 
   1362 		default:
   1363 		  abort ();
   1364 		}
   1365 	    }
   1366 	}
   1367     }
   1368   if (reloc_vector != NULL)
   1369     free (reloc_vector);
   1370   return data;
   1371  error_return:
   1372   if (reloc_vector != NULL)
   1373     free (reloc_vector);
   1374   return NULL;
   1375 }
   1376 
   1377 
   1379 /* Build the transfer vectors for Big and Little-Endian B.OUT files.  */
   1380 
   1381 #define aout_32_find_line                      _bfd_nosymbols_find_line
   1382 #define aout_32_bfd_make_debug_symbol          _bfd_nosymbols_bfd_make_debug_symbol
   1383 #define aout_32_close_and_cleanup              aout_32_bfd_free_cached_info
   1384 #define b_out_bfd_link_hash_table_create       _bfd_generic_link_hash_table_create
   1385 #define b_out_bfd_link_add_symbols             _bfd_generic_link_add_symbols
   1386 #define b_out_bfd_link_just_syms               _bfd_generic_link_just_syms
   1387 #define b_out_bfd_copy_link_hash_symbol_type \
   1388   _bfd_generic_copy_link_hash_symbol_type
   1389 #define b_out_bfd_final_link                   _bfd_generic_final_link
   1390 #define b_out_bfd_link_split_section           _bfd_generic_link_split_section
   1391 #define b_out_bfd_gc_sections                  bfd_generic_gc_sections
   1392 #define b_out_bfd_lookup_section_flags         bfd_generic_lookup_section_flags
   1393 #define b_out_bfd_merge_sections               bfd_generic_merge_sections
   1394 #define b_out_bfd_is_group_section             bfd_generic_is_group_section
   1395 #define b_out_bfd_discard_group                bfd_generic_discard_group
   1396 #define b_out_section_already_linked           _bfd_generic_section_already_linked
   1397 #define b_out_bfd_define_common_symbol         bfd_generic_define_common_symbol
   1398 #define aout_32_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
   1399 
   1400 extern const bfd_target bout_le_vec;
   1401 
   1402 const bfd_target bout_be_vec =
   1403 {
   1404   "b.out.big",			/* Name.  */
   1405   bfd_target_aout_flavour,
   1406   BFD_ENDIAN_LITTLE,		/* Data byte order.  */
   1407   BFD_ENDIAN_BIG,		/* Header byte order.  */
   1408   (HAS_RELOC | EXEC_P |		/* Object flags.  */
   1409    HAS_LINENO | HAS_DEBUG |
   1410    HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
   1411   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
   1412   '_',				/* Symbol leading char.  */
   1413   ' ',				/* AR_pad_char.  */
   1414   16,				/* AR_max_namelen.  */
   1415   0,				/* match priority.  */
   1416   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
   1417      bfd_getl32, bfd_getl_signed_32, bfd_putl32,
   1418      bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data.  */
   1419   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
   1420      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
   1421      bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers.  */
   1422  {_bfd_dummy_target, b_out_object_p, /* bfd_check_format.  */
   1423    bfd_generic_archive_p, _bfd_dummy_target},
   1424  {bfd_false, b_out_mkobject,	/* bfd_set_format.  */
   1425    _bfd_generic_mkarchive, bfd_false},
   1426  {bfd_false, b_out_write_object_contents, /* bfd_write_contents.  */
   1427    _bfd_write_archive_contents, bfd_false},
   1428 
   1429      BFD_JUMP_TABLE_GENERIC (aout_32),
   1430      BFD_JUMP_TABLE_COPY (_bfd_generic),
   1431      BFD_JUMP_TABLE_CORE (_bfd_nocore),
   1432      BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd),
   1433      BFD_JUMP_TABLE_SYMBOLS (aout_32),
   1434      BFD_JUMP_TABLE_RELOCS (b_out),
   1435      BFD_JUMP_TABLE_WRITE (b_out),
   1436      BFD_JUMP_TABLE_LINK (b_out),
   1437      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
   1438 
   1439   & bout_le_vec,
   1440 
   1441   NULL
   1442 };
   1443 
   1444 const bfd_target bout_le_vec =
   1445 {
   1446   "b.out.little",		/* Name.  */
   1447   bfd_target_aout_flavour,
   1448   BFD_ENDIAN_LITTLE,		/* Data byte order.  */
   1449   BFD_ENDIAN_LITTLE,		/* Header byte order.  */
   1450   (HAS_RELOC | EXEC_P |		/* Object flags.  */
   1451    HAS_LINENO | HAS_DEBUG |
   1452    HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
   1453   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
   1454   '_',				/* Symbol leading char.  */
   1455   ' ',				/* AR_pad_char.  */
   1456   16,				/* AR_max_namelen.  */
   1457   0,				/* match priority.  */
   1458   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
   1459     bfd_getl32, bfd_getl_signed_32, bfd_putl32,
   1460      bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data.  */
   1461   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
   1462      bfd_getl32, bfd_getl_signed_32, bfd_putl32,
   1463      bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Headers.  */
   1464 
   1465   {_bfd_dummy_target, b_out_object_p, /* bfd_check_format.  */
   1466      bfd_generic_archive_p, _bfd_dummy_target},
   1467   {bfd_false, b_out_mkobject,	/* bfd_set_format.  */
   1468      _bfd_generic_mkarchive, bfd_false},
   1469   {bfd_false, b_out_write_object_contents, /* bfd_write_contents.  */
   1470      _bfd_write_archive_contents, bfd_false},
   1471 
   1472      BFD_JUMP_TABLE_GENERIC (aout_32),
   1473      BFD_JUMP_TABLE_COPY (_bfd_generic),
   1474      BFD_JUMP_TABLE_CORE (_bfd_nocore),
   1475      BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd),
   1476      BFD_JUMP_TABLE_SYMBOLS (aout_32),
   1477      BFD_JUMP_TABLE_RELOCS (b_out),
   1478      BFD_JUMP_TABLE_WRITE (b_out),
   1479      BFD_JUMP_TABLE_LINK (b_out),
   1480      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
   1481 
   1482   & bout_be_vec,
   1483 
   1484   NULL
   1485 };
   1486