1 /* BFD back-end for VERSAdos-E objects.
2    Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3    2006, 2007, 2009, 2010 Free Software Foundation, Inc.
4    Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
5 
6    Versados is a Motorola trademark.
7 
8    This file is part of BFD, the Binary File Descriptor library.
9 
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23    MA 02110-1301, USA.  */
24 
25 /*
26    SUBSECTION
27    VERSAdos-E relocatable object file format
28 
29    DESCRIPTION
30 
31    This module supports reading of VERSAdos relocatable
32    object files.
33 
34    A VERSAdos file looks like contains
35 
36    o Identification Record
37    o External Symbol Definition Record
38    o Object Text Record
39    o End Record.  */
40 
41 #include "sysdep.h"
42 #include "bfd.h"
43 #include "libbfd.h"
44 #include "libiberty.h"
45 
46 
47 #define VHEADER '1'
48 #define VESTDEF '2'
49 #define VOTR '3'
50 #define VEND '4'
51 
52 #define ES_BASE 17		/* First symbol has esdid 17.  */
53 
54 /* Per file target dependent information.  */
55 
56 /* One for each section.  */
57 struct esdid
58 {
59   asection *section;		/* Ptr to bfd version.  */
60   unsigned char *contents;	/* Used to build image.  */
61   int pc;
62   int relocs;			/* Reloc count, valid end of pass 1.  */
63   int donerel;			/* Have relocs been translated.  */
64 };
65 
66 typedef struct versados_data_struct
67 {
68   int es_done;			/* Count of symbol index, starts at ES_BASE.  */
69   asymbol *symbols;		/* Pointer to local symbols.  */
70   char *strings;		/* Strings of all the above.  */
71   int stringlen;		/* Len of string table (valid end of pass1).  */
72   int nsecsyms;			/* Number of sections.  */
73 
74   int ndefs;			/* Number of exported symbols (they dont get esdids).  */
75   int nrefs;			/* Number of imported symbols  (valid end of pass1).  */
76 
77   int ref_idx;			/* Current processed value of the above.  */
78   int def_idx;
79 
80   int pass_2_done;
81 
82   struct esdid e[16];		/* Per section info.  */
83   int alert;			/* To see if we're trampling.  */
84   asymbol *rest[256 - 16];	/* Per symbol info.  */
85 }
86 tdata_type;
87 
88 #define VDATA(abfd)       (abfd->tdata.versados_data)
89 #define EDATA(abfd, n)    (abfd->tdata.versados_data->e[n])
90 #define RDATA(abfd, n)    (abfd->tdata.versados_data->rest[n])
91 
92 struct ext_otr
93 {
94   unsigned char size;
95   char type;
96   unsigned char map[4];
97   unsigned char esdid;
98   unsigned char data[200];
99 };
100 
101 struct ext_vheader
102 {
103   unsigned char size;
104   char type;			/* Record type.  */
105   char name[10];		/* Module name.  */
106   char rev;			/* Module rev number.  */
107   char lang;
108   char vol[4];
109   char user[2];
110   char cat[8];
111   char fname[8];
112   char ext[2];
113   char time[3];
114   char date[3];
115   char rest[211];
116 };
117 
118 struct ext_esd
119 {
120   unsigned char size;
121   char type;
122   unsigned char esd_entries[1];
123 };
124 
125 #define ESD_ABS 	  0
126 #define ESD_COMMON 	  1
127 #define ESD_STD_REL_SEC   2
128 #define ESD_SHRT_REL_SEC  3
129 #define ESD_XDEF_IN_SEC   4
130 #define ESD_XDEF_IN_ABS   5
131 #define ESD_XREF_SEC	  6
132 #define ESD_XREF_SYM      7
133 
134 union ext_any
135 {
136   unsigned char size;
137   struct ext_vheader header;
138   struct ext_esd esd;
139   struct ext_otr otr;
140 };
141 
142 /* Initialize by filling in the hex conversion array.  */
143 
144 /* Set up the tdata information.  */
145 
146 static bfd_boolean
versados_mkobject(bfd * abfd)147 versados_mkobject (bfd *abfd)
148 {
149   if (abfd->tdata.versados_data == NULL)
150     {
151       bfd_size_type amt = sizeof (tdata_type);
152       tdata_type *tdata = bfd_alloc (abfd, amt);
153 
154       if (tdata == NULL)
155 	return FALSE;
156       abfd->tdata.versados_data = tdata;
157       tdata->symbols = NULL;
158       VDATA (abfd)->alert = 0x12345678;
159     }
160 
161   bfd_default_set_arch_mach (abfd, bfd_arch_m68k, 0);
162   return TRUE;
163 }
164 
165 /* Report a problem in an S record file.  FIXME: This probably should
166    not call fprintf, but we really do need some mechanism for printing
167    error messages.  */
168 
169 static asymbol *
versados_new_symbol(bfd * abfd,int snum,const char * name,bfd_vma val,asection * sec)170 versados_new_symbol (bfd *abfd,
171 		     int snum,
172 		     const char *name,
173 		     bfd_vma val,
174 		     asection *sec)
175 {
176   asymbol *n = VDATA (abfd)->symbols + snum;
177   n->name = name;
178   n->value = val;
179   n->section = sec;
180   n->the_bfd = abfd;
181   n->flags = 0;
182   return n;
183 }
184 
185 static int
get_record(bfd * abfd,union ext_any * ptr)186 get_record (bfd *abfd, union ext_any *ptr)
187 {
188   if (bfd_bread (&ptr->size, (bfd_size_type) 1, abfd) != 1
189       || (bfd_bread ((char *) ptr + 1, (bfd_size_type) ptr->size, abfd)
190 	  != ptr->size))
191     return 0;
192   return 1;
193 }
194 
195 static int
get_4(unsigned char ** pp)196 get_4 (unsigned char **pp)
197 {
198   unsigned char *p = *pp;
199 
200   *pp += 4;
201   return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
202 }
203 
204 static void
get_10(unsigned char ** pp,char * name)205 get_10 (unsigned char **pp, char *name)
206 {
207   char *p = (char *) *pp;
208   int len = 10;
209 
210   *pp += len;
211   while (*p != ' ' && len)
212     {
213       *name++ = *p++;
214       len--;
215     }
216   *name = 0;
217 }
218 
219 static char *
new_symbol_string(bfd * abfd,const char * name)220 new_symbol_string (bfd *abfd, const char *name)
221 {
222   char *n = VDATA (abfd)->strings;
223 
224   strcpy (VDATA (abfd)->strings, name);
225   VDATA (abfd)->strings += strlen (VDATA (abfd)->strings) + 1;
226   return n;
227 }
228 
229 static void
process_esd(bfd * abfd,struct ext_esd * esd,int pass)230 process_esd (bfd *abfd, struct ext_esd *esd, int pass)
231 {
232   /* Read through the ext def for the est entries.  */
233   int togo = esd->size - 2;
234   bfd_vma size;
235   bfd_vma start;
236   asection *sec;
237   char name[11];
238   unsigned char *ptr = esd->esd_entries;
239   unsigned char *end = ptr + togo;
240 
241   while (ptr < end)
242     {
243       int scn = *ptr & 0xf;
244       int typ = (*ptr >> 4) & 0xf;
245 
246       /* Declare this section.  */
247       sprintf (name, "%d", scn);
248       sec = bfd_make_section_old_way (abfd, strdup (name));
249       sec->target_index = scn;
250       EDATA (abfd, scn).section = sec;
251       ptr++;
252 
253       switch (typ)
254 	{
255 	default:
256 	  abort ();
257 	case ESD_XREF_SEC:
258 	case ESD_XREF_SYM:
259 	  {
260 	    int snum = VDATA (abfd)->ref_idx++;
261 	    get_10 (&ptr, name);
262 	    if (pass == 1)
263 	      VDATA (abfd)->stringlen += strlen (name) + 1;
264 	    else
265 	      {
266 		int esidx;
267 		asymbol *s;
268 		char *n = new_symbol_string (abfd, name);
269 
270 		s = versados_new_symbol (abfd, snum, n, (bfd_vma) 0,
271 					 bfd_und_section_ptr);
272 		esidx = VDATA (abfd)->es_done++;
273 		RDATA (abfd, esidx - ES_BASE) = s;
274 	      }
275 	  }
276 	  break;
277 
278 	case ESD_ABS:
279 	  size = get_4 (&ptr);
280 	  (void) size;
281 	  start = get_4 (&ptr);
282 	  (void) start;
283 	  break;
284 	case ESD_STD_REL_SEC:
285 	case ESD_SHRT_REL_SEC:
286 	  sec->size = get_4 (&ptr);
287 	  sec->flags |= SEC_ALLOC;
288 	  break;
289 	case ESD_XDEF_IN_ABS:
290 	  sec = (asection *) & bfd_abs_section;
291 	case ESD_XDEF_IN_SEC:
292 	  {
293 	    int snum = VDATA (abfd)->def_idx++;
294 	    bfd_vma val;
295 
296 	    get_10 (&ptr, name);
297 	    val = get_4 (&ptr);
298 	    if (pass == 1)
299 	      /* Just remember the symbol.  */
300 	      VDATA (abfd)->stringlen += strlen (name) + 1;
301 	    else
302 	      {
303 		asymbol *s;
304 		char *n = new_symbol_string (abfd, name);
305 
306 		s = versados_new_symbol (abfd, snum + VDATA (abfd)->nrefs, n,
307 					 val, sec);
308 		s->flags |= BSF_GLOBAL;
309 	      }
310 	  }
311 	  break;
312 	}
313     }
314 }
315 
316 #define R_RELWORD     1
317 #define R_RELLONG     2
318 #define R_RELWORD_NEG 3
319 #define R_RELLONG_NEG 4
320 
321 reloc_howto_type versados_howto_table[] =
322 {
323   HOWTO (R_RELWORD, 0, 1, 16, FALSE,
324 	 0, complain_overflow_dont, 0,
325 	 "+v16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
326   HOWTO (R_RELLONG, 0, 2, 32, FALSE,
327 	 0, complain_overflow_dont, 0,
328 	 "+v32", TRUE, 0xffffffff, 0xffffffff, FALSE),
329 
330   HOWTO (R_RELWORD_NEG, 0, -1, 16, FALSE,
331 	 0, complain_overflow_dont, 0,
332 	 "-v16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
333   HOWTO (R_RELLONG_NEG, 0, -2, 32, FALSE,
334 	 0, complain_overflow_dont, 0,
335 	 "-v32", TRUE, 0xffffffff, 0xffffffff, FALSE),
336 };
337 
338 static int
get_offset(int len,unsigned char * ptr)339 get_offset (int len, unsigned char *ptr)
340 {
341   int val = 0;
342 
343   if (len)
344     {
345       int i;
346 
347       val = *ptr++;
348       if (val & 0x80)
349 	val |= ~0xff;
350       for (i = 1; i < len; i++)
351 	val = (val << 8) | *ptr++;
352     }
353 
354   return val;
355 }
356 
357 static void
process_otr(bfd * abfd,struct ext_otr * otr,int pass)358 process_otr (bfd *abfd, struct ext_otr *otr, int pass)
359 {
360   unsigned long shift;
361   unsigned char *srcp = otr->data;
362   unsigned char *endp = (unsigned char *) otr + otr->size;
363   unsigned int bits = (otr->map[0] << 24)
364   | (otr->map[1] << 16)
365   | (otr->map[2] << 8)
366   | (otr->map[3] << 0);
367 
368   struct esdid *esdid = &EDATA (abfd, otr->esdid - 1);
369   unsigned char *contents = esdid->contents;
370   int need_contents = 0;
371   unsigned int dst_idx = esdid->pc;
372 
373   for (shift = ((unsigned long) 1 << 31); shift && srcp < endp; shift >>= 1)
374     {
375       if (bits & shift)
376 	{
377 	  int flag = *srcp++;
378 	  int esdids = (flag >> 5) & 0x7;
379 	  int sizeinwords = ((flag >> 3) & 1) ? 2 : 1;
380 	  int offsetlen = flag & 0x7;
381 	  int j;
382 
383 	  if (esdids == 0)
384 	    {
385 	      /* A zero esdid means the new pc is the offset given.  */
386 	      dst_idx += get_offset (offsetlen, srcp);
387 	      srcp += offsetlen;
388 	    }
389 	  else
390 	    {
391 	      int val = get_offset (offsetlen, srcp + esdids);
392 
393 	      if (pass == 1)
394 		need_contents = 1;
395 	      else
396 		for (j = 0; j < sizeinwords * 2; j++)
397 		  {
398 		    contents[dst_idx + (sizeinwords * 2) - j - 1] = val;
399 		    val >>= 8;
400 		  }
401 
402 	      for (j = 0; j < esdids; j++)
403 		{
404 		  int id = *srcp++;
405 
406 		  if (id)
407 		    {
408 		      int rn = EDATA (abfd, otr->esdid - 1).relocs++;
409 
410 		      if (pass == 1)
411 			{
412 			  /* This is the first pass over the data,
413 			     just remember that we need a reloc.  */
414 			}
415 		      else
416 			{
417 			  arelent *n =
418 			  EDATA (abfd, otr->esdid - 1).section->relocation + rn;
419 			  n->address = dst_idx;
420 
421 			  n->sym_ptr_ptr = (asymbol **) (size_t) id;
422 			  n->addend = 0;
423 			  n->howto = versados_howto_table + ((j & 1) * 2) + (sizeinwords - 1);
424 			}
425 		    }
426 		}
427 	      srcp += offsetlen;
428 	      dst_idx += sizeinwords * 2;
429 	    }
430 	}
431       else
432 	{
433 	  need_contents = 1;
434 	  if (dst_idx < esdid->section->size)
435 	    if (pass == 2)
436 	      {
437 		/* Absolute code, comes in 16 bit lumps.  */
438 		contents[dst_idx] = srcp[0];
439 		contents[dst_idx + 1] = srcp[1];
440 	      }
441 	  dst_idx += 2;
442 	  srcp += 2;
443 	}
444     }
445   EDATA (abfd, otr->esdid - 1).pc = dst_idx;
446 
447   if (!contents && need_contents)
448     {
449       bfd_size_type size = esdid->section->size;
450       esdid->contents = bfd_alloc (abfd, size);
451     }
452 }
453 
454 static bfd_boolean
versados_scan(bfd * abfd)455 versados_scan (bfd *abfd)
456 {
457   int loop = 1;
458   int i;
459   int j;
460   int nsecs = 0;
461   bfd_size_type amt;
462 
463   VDATA (abfd)->stringlen = 0;
464   VDATA (abfd)->nrefs = 0;
465   VDATA (abfd)->ndefs = 0;
466   VDATA (abfd)->ref_idx = 0;
467   VDATA (abfd)->def_idx = 0;
468   VDATA (abfd)->pass_2_done = 0;
469 
470   while (loop)
471     {
472       union ext_any any;
473 
474       if (!get_record (abfd, &any))
475 	return TRUE;
476       switch (any.header.type)
477 	{
478 	case VHEADER:
479 	  break;
480 	case VEND:
481 	  loop = 0;
482 	  break;
483 	case VESTDEF:
484 	  process_esd (abfd, &any.esd, 1);
485 	  break;
486 	case VOTR:
487 	  process_otr (abfd, &any.otr, 1);
488 	  break;
489 	}
490     }
491 
492   /* Now allocate space for the relocs and sections.  */
493   VDATA (abfd)->nrefs = VDATA (abfd)->ref_idx;
494   VDATA (abfd)->ndefs = VDATA (abfd)->def_idx;
495   VDATA (abfd)->ref_idx = 0;
496   VDATA (abfd)->def_idx = 0;
497 
498   abfd->symcount = VDATA (abfd)->nrefs + VDATA (abfd)->ndefs;
499 
500   for (i = 0; i < 16; i++)
501     {
502       struct esdid *esdid = &EDATA (abfd, i);
503 
504       if (esdid->section)
505 	{
506 	  amt = (bfd_size_type) esdid->relocs * sizeof (arelent);
507 	  esdid->section->relocation = bfd_alloc (abfd, amt);
508 
509 	  esdid->pc = 0;
510 
511 	  if (esdid->contents)
512 	    esdid->section->flags |= SEC_HAS_CONTENTS | SEC_LOAD;
513 
514 	  esdid->section->reloc_count = esdid->relocs;
515 	  if (esdid->relocs)
516 	    esdid->section->flags |= SEC_RELOC;
517 
518 	  esdid->relocs = 0;
519 
520 	  /* Add an entry into the symbol table for it.  */
521 	  nsecs++;
522 	  VDATA (abfd)->stringlen += strlen (esdid->section->name) + 1;
523 	}
524     }
525 
526   abfd->symcount += nsecs;
527 
528   amt = abfd->symcount;
529   amt *= sizeof (asymbol);
530   VDATA (abfd)->symbols = bfd_alloc (abfd, amt);
531 
532   amt = VDATA (abfd)->stringlen;
533   VDATA (abfd)->strings = bfd_alloc (abfd, amt);
534 
535   if ((VDATA (abfd)->symbols == NULL && abfd->symcount > 0)
536       || (VDATA (abfd)->strings == NULL && VDATA (abfd)->stringlen > 0))
537     return FALSE;
538 
539   /* Actually fill in the section symbols,
540      we stick them at the end of the table.  */
541   for (j = VDATA (abfd)->nrefs + VDATA (abfd)->ndefs, i = 0; i < 16; i++)
542     {
543       struct esdid *esdid = &EDATA (abfd, i);
544       asection *sec = esdid->section;
545 
546       if (sec)
547 	{
548 	  asymbol *s = VDATA (abfd)->symbols + j;
549 	  s->name = new_symbol_string (abfd, sec->name);
550 	  s->section = sec;
551 	  s->flags = BSF_LOCAL;
552 	  s->value = 0;
553 	  s->the_bfd = abfd;
554 	  j++;
555 	}
556     }
557 
558   if (abfd->symcount)
559     abfd->flags |= HAS_SYMS;
560 
561   /* Set this to nsecs - since we've already planted the section
562      symbols.  */
563   VDATA (abfd)->nsecsyms = nsecs;
564 
565   VDATA (abfd)->ref_idx = 0;
566 
567   return 1;
568 }
569 
570 /* Check whether an existing file is a versados  file.  */
571 
572 static const bfd_target *
versados_object_p(bfd * abfd)573 versados_object_p (bfd *abfd)
574 {
575   struct ext_vheader ext;
576   unsigned char len;
577   tdata_type *tdata_save;
578 
579   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
580     return NULL;
581 
582   if (bfd_bread (&len, (bfd_size_type) 1, abfd) != 1)
583     {
584       if (bfd_get_error () != bfd_error_system_call)
585 	bfd_set_error (bfd_error_wrong_format);
586       return NULL;
587     }
588 
589   if (bfd_bread (&ext.type, (bfd_size_type) len, abfd) != len)
590     {
591       if (bfd_get_error () != bfd_error_system_call)
592 	bfd_set_error (bfd_error_wrong_format);
593       return NULL;
594     }
595 
596   /* We guess that the language field will never be larger than 10.
597      In sample files, it is always either 0 or 1.  Checking for this
598      prevents confusion with Intel Hex files.  */
599   if (ext.type != VHEADER
600       || ext.lang > 10)
601     {
602       bfd_set_error (bfd_error_wrong_format);
603       return NULL;
604     }
605 
606   /* OK, looks like a record, build the tdata and read in.  */
607   tdata_save = abfd->tdata.versados_data;
608   if (!versados_mkobject (abfd) || !versados_scan (abfd))
609     {
610       abfd->tdata.versados_data = tdata_save;
611       return NULL;
612     }
613 
614   return abfd->xvec;
615 }
616 
617 static bfd_boolean
versados_pass_2(bfd * abfd)618 versados_pass_2 (bfd *abfd)
619 {
620   union ext_any any;
621 
622   if (VDATA (abfd)->pass_2_done)
623     return 1;
624 
625   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
626     return 0;
627 
628   VDATA (abfd)->es_done = ES_BASE;
629 
630   /* Read records till we get to where we want to be.  */
631   while (1)
632     {
633       get_record (abfd, &any);
634       switch (any.header.type)
635 	{
636 	case VEND:
637 	  VDATA (abfd)->pass_2_done = 1;
638 	  return 1;
639 	case VESTDEF:
640 	  process_esd (abfd, &any.esd, 2);
641 	  break;
642 	case VOTR:
643 	  process_otr (abfd, &any.otr, 2);
644 	  break;
645 	}
646     }
647 }
648 
649 static bfd_boolean
versados_get_section_contents(bfd * abfd,asection * section,void * location,file_ptr offset,bfd_size_type count)650 versados_get_section_contents (bfd *abfd,
651 			       asection *section,
652 			       void * location,
653 			       file_ptr offset,
654 			       bfd_size_type count)
655 {
656   if (!versados_pass_2 (abfd))
657     return FALSE;
658 
659   memcpy (location,
660 	  EDATA (abfd, section->target_index).contents + offset,
661 	  (size_t) count);
662 
663   return TRUE;
664 }
665 
666 #define versados_get_section_contents_in_window \
667   _bfd_generic_get_section_contents_in_window
668 
669 static bfd_boolean
versados_set_section_contents(bfd * abfd ATTRIBUTE_UNUSED,sec_ptr section ATTRIBUTE_UNUSED,const void * location ATTRIBUTE_UNUSED,file_ptr offset ATTRIBUTE_UNUSED,bfd_size_type bytes_to_do ATTRIBUTE_UNUSED)670 versados_set_section_contents (bfd *abfd ATTRIBUTE_UNUSED,
671 			       sec_ptr section ATTRIBUTE_UNUSED,
672 			       const void * location ATTRIBUTE_UNUSED,
673 			       file_ptr offset ATTRIBUTE_UNUSED,
674 			       bfd_size_type bytes_to_do ATTRIBUTE_UNUSED)
675 {
676   return FALSE;
677 }
678 
679 static int
versados_sizeof_headers(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info ATTRIBUTE_UNUSED)680 versados_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
681 			 struct bfd_link_info *info ATTRIBUTE_UNUSED)
682 {
683   return 0;
684 }
685 
686 /* Return the amount of memory needed to read the symbol table.  */
687 
688 static long
versados_get_symtab_upper_bound(bfd * abfd)689 versados_get_symtab_upper_bound (bfd *abfd)
690 {
691   return (bfd_get_symcount (abfd) + 1) * sizeof (asymbol *);
692 }
693 
694 /* Return the symbol table.  */
695 
696 static long
versados_canonicalize_symtab(bfd * abfd,asymbol ** alocation)697 versados_canonicalize_symtab (bfd *abfd, asymbol **alocation)
698 {
699   unsigned int symcount = bfd_get_symcount (abfd);
700   unsigned int i;
701   asymbol *s;
702 
703   versados_pass_2 (abfd);
704 
705   for (i = 0, s = VDATA (abfd)->symbols;
706        i < symcount;
707        s++, i++)
708     *alocation++ = s;
709 
710   *alocation = NULL;
711 
712   return symcount;
713 }
714 
715 static void
versados_get_symbol_info(bfd * abfd ATTRIBUTE_UNUSED,asymbol * symbol,symbol_info * ret)716 versados_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
717 			  asymbol *symbol,
718 			  symbol_info *ret)
719 {
720   bfd_symbol_info (symbol, ret);
721 }
722 
723 static void
versados_print_symbol(bfd * abfd,void * afile,asymbol * symbol,bfd_print_symbol_type how)724 versados_print_symbol (bfd *abfd,
725 		       void * afile,
726 		       asymbol *symbol,
727 		       bfd_print_symbol_type how)
728 {
729   FILE *file = (FILE *) afile;
730 
731   switch (how)
732     {
733     case bfd_print_symbol_name:
734       fprintf (file, "%s", symbol->name);
735       break;
736     default:
737       bfd_print_symbol_vandf (abfd, (void *) file, symbol);
738       fprintf (file, " %-5s %s",
739 	       symbol->section->name,
740 	       symbol->name);
741     }
742 }
743 
744 static long
versados_get_reloc_upper_bound(bfd * abfd ATTRIBUTE_UNUSED,sec_ptr asect)745 versados_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
746 				sec_ptr asect)
747 {
748   return (asect->reloc_count + 1) * sizeof (arelent *);
749 }
750 
751 static long
versados_canonicalize_reloc(bfd * abfd,sec_ptr section,arelent ** relptr,asymbol ** symbols)752 versados_canonicalize_reloc (bfd *abfd,
753 			     sec_ptr section,
754 			     arelent **relptr,
755 			     asymbol **symbols)
756 {
757   unsigned int count;
758   arelent *src;
759 
760   versados_pass_2 (abfd);
761   src = section->relocation;
762   if (!EDATA (abfd, section->target_index).donerel)
763     {
764       EDATA (abfd, section->target_index).donerel = 1;
765       /* Translate from indexes to symptr ptrs.  */
766       for (count = 0; count < section->reloc_count; count++)
767 	{
768 	  int esdid = (int) (size_t) src[count].sym_ptr_ptr;
769 
770 	  if (esdid == 0)
771 	    src[count].sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
772 	  else if (esdid < ES_BASE)
773 	    {
774 	      /* Section relative thing.  */
775 	      struct esdid *e = &EDATA (abfd, esdid - 1);
776 
777 	      src[count].sym_ptr_ptr = e->section->symbol_ptr_ptr;
778 	    }
779 	  else
780 	    src[count].sym_ptr_ptr = symbols + esdid - ES_BASE;
781 	}
782     }
783 
784   for (count = 0; count < section->reloc_count; count++)
785     *relptr++ = src++;
786 
787   *relptr = 0;
788   return section->reloc_count;
789 }
790 
791 #define	versados_close_and_cleanup                    _bfd_generic_close_and_cleanup
792 #define versados_bfd_free_cached_info                 _bfd_generic_bfd_free_cached_info
793 #define versados_new_section_hook                     _bfd_generic_new_section_hook
794 #define versados_bfd_is_target_special_symbol   ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
795 #define versados_bfd_is_local_label_name              bfd_generic_is_local_label_name
796 #define versados_get_lineno                           _bfd_nosymbols_get_lineno
797 #define versados_find_nearest_line                    _bfd_nosymbols_find_nearest_line
798 #define versados_find_inliner_info                    _bfd_nosymbols_find_inliner_info
799 #define versados_make_empty_symbol                    _bfd_generic_make_empty_symbol
800 #define versados_bfd_make_debug_symbol                _bfd_nosymbols_bfd_make_debug_symbol
801 #define versados_read_minisymbols                     _bfd_generic_read_minisymbols
802 #define versados_minisymbol_to_symbol                 _bfd_generic_minisymbol_to_symbol
803 #define versados_bfd_reloc_type_lookup                _bfd_norelocs_bfd_reloc_type_lookup
804 #define versados_bfd_reloc_name_lookup          _bfd_norelocs_bfd_reloc_name_lookup
805 #define versados_set_arch_mach                        bfd_default_set_arch_mach
806 #define versados_bfd_get_relocated_section_contents   bfd_generic_get_relocated_section_contents
807 #define versados_bfd_relax_section                    bfd_generic_relax_section
808 #define versados_bfd_gc_sections                      bfd_generic_gc_sections
809 #define versados_bfd_merge_sections                   bfd_generic_merge_sections
810 #define versados_bfd_is_group_section                 bfd_generic_is_group_section
811 #define versados_bfd_discard_group                    bfd_generic_discard_group
812 #define versados_section_already_linked               _bfd_generic_section_already_linked
813 #define versados_bfd_define_common_symbol             bfd_generic_define_common_symbol
814 #define versados_bfd_link_hash_table_create           _bfd_generic_link_hash_table_create
815 #define versados_bfd_link_hash_table_free             _bfd_generic_link_hash_table_free
816 #define versados_bfd_link_add_symbols                 _bfd_generic_link_add_symbols
817 #define versados_bfd_link_just_syms                   _bfd_generic_link_just_syms
818 #define versados_bfd_copy_link_hash_symbol_type \
819   _bfd_generic_copy_link_hash_symbol_type
820 #define versados_bfd_final_link                       _bfd_generic_final_link
821 #define versados_bfd_link_split_section               _bfd_generic_link_split_section
822 
823 const bfd_target versados_vec =
824 {
825   "versados",			/* Name.  */
826   bfd_target_versados_flavour,
827   BFD_ENDIAN_BIG,		/* Target byte order.  */
828   BFD_ENDIAN_BIG,		/* Target headers byte order.  */
829   (HAS_RELOC | EXEC_P |		/* Object flags.  */
830    HAS_LINENO | HAS_DEBUG |
831    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
832   (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
833    | SEC_ALLOC | SEC_LOAD | SEC_RELOC),		/* Section flags.  */
834   0,				/* Leading underscore.  */
835   ' ',				/* AR_pad_char.  */
836   16,				/* AR_max_namelen.  */
837   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
838   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
839   bfd_getb16, bfd_getb_signed_16, bfd_putb16,	/* Data.  */
840   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
841   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
842   bfd_getb16, bfd_getb_signed_16, bfd_putb16,	/* Headers.  */
843 
844   {
845     _bfd_dummy_target,
846     versados_object_p,		/* bfd_check_format.  */
847     _bfd_dummy_target,
848     _bfd_dummy_target,
849   },
850   {
851     bfd_false,
852     versados_mkobject,
853     _bfd_generic_mkarchive,
854     bfd_false,
855   },
856   {				/* bfd_write_contents.  */
857     bfd_false,
858     bfd_false,
859     _bfd_write_archive_contents,
860     bfd_false,
861   },
862 
863   BFD_JUMP_TABLE_GENERIC (versados),
864   BFD_JUMP_TABLE_COPY (_bfd_generic),
865   BFD_JUMP_TABLE_CORE (_bfd_nocore),
866   BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
867   BFD_JUMP_TABLE_SYMBOLS (versados),
868   BFD_JUMP_TABLE_RELOCS (versados),
869   BFD_JUMP_TABLE_WRITE (versados),
870   BFD_JUMP_TABLE_LINK (versados),
871   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
872 
873   NULL,
874 
875   NULL
876 };
877