xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/irix-core.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* BFD back-end for Irix core files.
2*e992f068Schristos    Copyright (C) 1993-2022 Free Software Foundation, Inc.
375fd0b74Schristos    Written by Stu Grossman, Cygnus Support.
475fd0b74Schristos    Converted to back-end form by Ian Lance Taylor, Cygnus Support
575fd0b74Schristos 
675fd0b74Schristos    This file is part of BFD, the Binary File Descriptor library.
775fd0b74Schristos 
875fd0b74Schristos    This program is free software; you can redistribute it and/or modify
975fd0b74Schristos    it under the terms of the GNU General Public License as published by
1075fd0b74Schristos    the Free Software Foundation; either version 3 of the License, or
1175fd0b74Schristos    (at your option) any later version.
1275fd0b74Schristos 
1375fd0b74Schristos    This program is distributed in the hope that it will be useful,
1475fd0b74Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1575fd0b74Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1675fd0b74Schristos    GNU General Public License for more details.
1775fd0b74Schristos 
1875fd0b74Schristos    You should have received a copy of the GNU General Public License
1975fd0b74Schristos    along with this program; if not, write to the Free Software
2075fd0b74Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2175fd0b74Schristos    MA 02110-1301, USA.  */
2275fd0b74Schristos 
2375fd0b74Schristos 
2475fd0b74Schristos /* This file can only be compiled on systems which use Irix style core
2575fd0b74Schristos    files (namely, Irix 4 and Irix 5, so far).  */
2675fd0b74Schristos 
2775fd0b74Schristos #include "sysdep.h"
2875fd0b74Schristos #include "bfd.h"
2975fd0b74Schristos #include "libbfd.h"
3075fd0b74Schristos 
3175fd0b74Schristos #ifdef IRIX_CORE
3275fd0b74Schristos 
3375fd0b74Schristos #include <core.out.h>
3475fd0b74Schristos 
3575fd0b74Schristos struct sgi_core_struct
3675fd0b74Schristos {
3775fd0b74Schristos   int sig;
3875fd0b74Schristos   char cmd[CORE_NAMESIZE];
3975fd0b74Schristos };
4075fd0b74Schristos 
4175fd0b74Schristos #define core_hdr(bfd) ((bfd)->tdata.sgi_core_data)
4275fd0b74Schristos #define core_signal(bfd) (core_hdr(bfd)->sig)
4375fd0b74Schristos #define core_command(bfd) (core_hdr(bfd)->cmd)
4475fd0b74Schristos 
4575fd0b74Schristos #define irix_core_core_file_matches_executable_p generic_core_file_matches_executable_p
4675fd0b74Schristos #define irix_core_core_file_pid _bfd_nocore_core_file_pid
4775fd0b74Schristos 
4875fd0b74Schristos static asection *make_bfd_asection
4975fd0b74Schristos   (bfd *, const char *, flagword, bfd_size_type, bfd_vma, file_ptr);
5075fd0b74Schristos 
5175fd0b74Schristos /* Helper function for irix_core_core_file_p:
5275fd0b74Schristos    32-bit and 64-bit versions.  */
5375fd0b74Schristos 
5475fd0b74Schristos #ifdef CORE_MAGIC64
5575fd0b74Schristos static int
do_sections64(bfd * abfd,struct coreout * coreout)5675fd0b74Schristos do_sections64 (bfd *abfd, struct coreout *coreout)
5775fd0b74Schristos {
5875fd0b74Schristos   struct vmap64 vmap;
5975fd0b74Schristos   char *secname;
6075fd0b74Schristos   int i, val;
6175fd0b74Schristos 
6275fd0b74Schristos   for (i = 0; i < coreout->c_nvmap; i++)
6375fd0b74Schristos     {
6475fd0b74Schristos       val = bfd_bread (&vmap, (bfd_size_type) sizeof vmap, abfd);
6575fd0b74Schristos       if (val != sizeof vmap)
6675fd0b74Schristos 	break;
6775fd0b74Schristos 
6875fd0b74Schristos       switch (vmap.v_type)
6975fd0b74Schristos 	{
7075fd0b74Schristos 	case VDATA:
7175fd0b74Schristos 	  secname = ".data";
7275fd0b74Schristos 	  break;
7375fd0b74Schristos 	case VSTACK:
7475fd0b74Schristos 	  secname = ".stack";
7575fd0b74Schristos 	  break;
7675fd0b74Schristos #ifdef VMAPFILE
7775fd0b74Schristos 	case VMAPFILE:
7875fd0b74Schristos 	  secname = ".mapfile";
7975fd0b74Schristos 	  break;
8075fd0b74Schristos #endif
8175fd0b74Schristos 	default:
8275fd0b74Schristos 	  continue;
8375fd0b74Schristos 	}
8475fd0b74Schristos 
8575fd0b74Schristos       /* A file offset of zero means that the
8675fd0b74Schristos 	 section is not contained in the corefile.  */
8775fd0b74Schristos       if (vmap.v_offset == 0)
8875fd0b74Schristos 	continue;
8975fd0b74Schristos 
9075fd0b74Schristos       if (!make_bfd_asection (abfd, secname,
9175fd0b74Schristos 			      SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
9275fd0b74Schristos 			      vmap.v_len, vmap.v_vaddr, vmap.v_offset))
9375fd0b74Schristos 	/* Fail.  */
9475fd0b74Schristos 	return 0;
9575fd0b74Schristos     }
9675fd0b74Schristos 
9775fd0b74Schristos   return 1;
9875fd0b74Schristos }
9975fd0b74Schristos #endif
10075fd0b74Schristos 
10175fd0b74Schristos /* 32-bit version.  */
10275fd0b74Schristos 
10375fd0b74Schristos static int
do_sections(bfd * abfd,struct coreout * coreout)10475fd0b74Schristos do_sections (bfd *abfd, struct coreout *coreout)
10575fd0b74Schristos {
10675fd0b74Schristos   struct vmap vmap;
10775fd0b74Schristos   char *secname;
10875fd0b74Schristos   int i, val;
10975fd0b74Schristos 
11075fd0b74Schristos   for (i = 0; i < coreout->c_nvmap; i++)
11175fd0b74Schristos     {
11275fd0b74Schristos       val = bfd_bread (&vmap, (bfd_size_type) sizeof vmap, abfd);
11375fd0b74Schristos       if (val != sizeof vmap)
11475fd0b74Schristos 	break;
11575fd0b74Schristos 
11675fd0b74Schristos       switch (vmap.v_type)
11775fd0b74Schristos 	{
11875fd0b74Schristos 	case VDATA:
11975fd0b74Schristos 	  secname = ".data";
12075fd0b74Schristos 	  break;
12175fd0b74Schristos 	case VSTACK:
12275fd0b74Schristos 	  secname = ".stack";
12375fd0b74Schristos 	  break;
12475fd0b74Schristos #ifdef VMAPFILE
12575fd0b74Schristos 	case VMAPFILE:
12675fd0b74Schristos 	  secname = ".mapfile";
12775fd0b74Schristos 	  break;
12875fd0b74Schristos #endif
12975fd0b74Schristos 	default:
13075fd0b74Schristos 	  continue;
13175fd0b74Schristos 	}
13275fd0b74Schristos 
13375fd0b74Schristos       /* A file offset of zero means that the
13475fd0b74Schristos 	 section is not contained in the corefile.  */
13575fd0b74Schristos       if (vmap.v_offset == 0)
13675fd0b74Schristos 	continue;
13775fd0b74Schristos 
13875fd0b74Schristos       if (!make_bfd_asection (abfd, secname,
13975fd0b74Schristos 			      SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
14075fd0b74Schristos 			      vmap.v_len, vmap.v_vaddr, vmap.v_offset))
14175fd0b74Schristos 	/* Fail.  */
14275fd0b74Schristos 	return 0;
14375fd0b74Schristos     }
14475fd0b74Schristos   return 1;
14575fd0b74Schristos }
14675fd0b74Schristos 
14775fd0b74Schristos static asection *
make_bfd_asection(bfd * abfd,const char * name,flagword flags,bfd_size_type size,bfd_vma vma,file_ptr filepos)14875fd0b74Schristos make_bfd_asection (bfd *abfd,
14975fd0b74Schristos 		   const char *name,
15075fd0b74Schristos 		   flagword flags,
15175fd0b74Schristos 		   bfd_size_type size,
15275fd0b74Schristos 		   bfd_vma vma,
15375fd0b74Schristos 		   file_ptr filepos)
15475fd0b74Schristos {
15575fd0b74Schristos   asection *asect;
15675fd0b74Schristos 
15775fd0b74Schristos   asect = bfd_make_section_anyway_with_flags (abfd, name, flags);
15875fd0b74Schristos   if (!asect)
15975fd0b74Schristos     return NULL;
16075fd0b74Schristos 
16175fd0b74Schristos   asect->size = size;
16275fd0b74Schristos   asect->vma = vma;
16375fd0b74Schristos   asect->filepos = filepos;
16475fd0b74Schristos   asect->alignment_power = 4;
16575fd0b74Schristos 
16675fd0b74Schristos   return asect;
16775fd0b74Schristos }
16875fd0b74Schristos 
169*e992f068Schristos static bfd_cleanup
irix_core_core_file_p(bfd * abfd)17075fd0b74Schristos irix_core_core_file_p (bfd *abfd)
17175fd0b74Schristos {
17275fd0b74Schristos   int val;
17375fd0b74Schristos   struct coreout coreout;
17475fd0b74Schristos   struct idesc *idg, *idf, *ids;
175*e992f068Schristos   size_t amt;
17675fd0b74Schristos 
17775fd0b74Schristos   val = bfd_bread (&coreout, (bfd_size_type) sizeof coreout, abfd);
17875fd0b74Schristos   if (val != sizeof coreout)
17975fd0b74Schristos     {
18075fd0b74Schristos       if (bfd_get_error () != bfd_error_system_call)
18175fd0b74Schristos 	bfd_set_error (bfd_error_wrong_format);
18275fd0b74Schristos       return 0;
18375fd0b74Schristos     }
18475fd0b74Schristos 
18575fd0b74Schristos   if (coreout.c_version != CORE_VERSION1)
18675fd0b74Schristos     return 0;
18775fd0b74Schristos 
18875fd0b74Schristos   /* Have we got a corefile?  */
18975fd0b74Schristos   switch (coreout.c_magic)
19075fd0b74Schristos     {
19175fd0b74Schristos     case CORE_MAGIC:	break;
19275fd0b74Schristos #ifdef CORE_MAGIC64
19375fd0b74Schristos     case CORE_MAGIC64:	break;
19475fd0b74Schristos #endif
19575fd0b74Schristos #ifdef CORE_MAGICN32
19675fd0b74Schristos     case CORE_MAGICN32:	break;
19775fd0b74Schristos #endif
19875fd0b74Schristos     default:		return 0;	/* Un-identifiable or not corefile.  */
19975fd0b74Schristos     }
20075fd0b74Schristos 
20175fd0b74Schristos   amt = sizeof (struct sgi_core_struct);
20275fd0b74Schristos   core_hdr (abfd) = (struct sgi_core_struct *) bfd_zalloc (abfd, amt);
20375fd0b74Schristos   if (!core_hdr (abfd))
20475fd0b74Schristos     return NULL;
20575fd0b74Schristos 
20675fd0b74Schristos   strncpy (core_command (abfd), coreout.c_name, CORE_NAMESIZE);
20775fd0b74Schristos   core_signal (abfd) = coreout.c_sigcause;
20875fd0b74Schristos 
20975fd0b74Schristos   if (bfd_seek (abfd, coreout.c_vmapoffset, SEEK_SET) != 0)
21075fd0b74Schristos     goto fail;
21175fd0b74Schristos 
21275fd0b74Schristos   /* Process corefile sections.  */
21375fd0b74Schristos #ifdef CORE_MAGIC64
21475fd0b74Schristos   if (coreout.c_magic == (int) CORE_MAGIC64)
21575fd0b74Schristos     {
21675fd0b74Schristos       if (! do_sections64 (abfd, & coreout))
21775fd0b74Schristos 	goto fail;
21875fd0b74Schristos     }
21975fd0b74Schristos   else
22075fd0b74Schristos #endif
22175fd0b74Schristos     if (! do_sections (abfd, & coreout))
22275fd0b74Schristos       goto fail;
22375fd0b74Schristos 
22475fd0b74Schristos   /* Make sure that the regs are contiguous within the core file.  */
22575fd0b74Schristos 
22675fd0b74Schristos   idg = &coreout.c_idesc[I_GPREGS];
22775fd0b74Schristos   idf = &coreout.c_idesc[I_FPREGS];
22875fd0b74Schristos   ids = &coreout.c_idesc[I_SPECREGS];
22975fd0b74Schristos 
23075fd0b74Schristos   if (idg->i_offset + idg->i_len != idf->i_offset
23175fd0b74Schristos       || idf->i_offset + idf->i_len != ids->i_offset)
23275fd0b74Schristos     goto fail;			/* Can't deal with non-contig regs */
23375fd0b74Schristos 
23475fd0b74Schristos   if (bfd_seek (abfd, idg->i_offset, SEEK_SET) != 0)
23575fd0b74Schristos     goto fail;
23675fd0b74Schristos 
23775fd0b74Schristos   if (!make_bfd_asection (abfd, ".reg",
23875fd0b74Schristos 			  SEC_HAS_CONTENTS,
23975fd0b74Schristos 			  idg->i_len + idf->i_len + ids->i_len,
24075fd0b74Schristos 			  0,
24175fd0b74Schristos 			  idg->i_offset))
24275fd0b74Schristos     goto fail;
24375fd0b74Schristos 
24475fd0b74Schristos   /* OK, we believe you.  You're a core file (sure, sure).  */
24575fd0b74Schristos   bfd_default_set_arch_mach (abfd, bfd_arch_mips, 0);
24675fd0b74Schristos 
247*e992f068Schristos   return _bfd_no_cleanup;
24875fd0b74Schristos 
24975fd0b74Schristos  fail:
25075fd0b74Schristos   bfd_release (abfd, core_hdr (abfd));
25175fd0b74Schristos   core_hdr (abfd) = NULL;
25275fd0b74Schristos   bfd_section_list_clear (abfd);
25375fd0b74Schristos   return NULL;
25475fd0b74Schristos }
25575fd0b74Schristos 
25675fd0b74Schristos static char *
irix_core_core_file_failing_command(bfd * abfd)25775fd0b74Schristos irix_core_core_file_failing_command (bfd *abfd)
25875fd0b74Schristos {
25975fd0b74Schristos   return core_command (abfd);
26075fd0b74Schristos }
26175fd0b74Schristos 
26275fd0b74Schristos static int
irix_core_core_file_failing_signal(bfd * abfd)26375fd0b74Schristos irix_core_core_file_failing_signal (bfd *abfd)
26475fd0b74Schristos {
26575fd0b74Schristos   return core_signal (abfd);
26675fd0b74Schristos }
26775fd0b74Schristos 
26875fd0b74Schristos /* If somebody calls any byte-swapping routines, shoot them.  */
26975fd0b74Schristos static void
swap_abort(void)27075fd0b74Schristos swap_abort(void)
27175fd0b74Schristos {
27275fd0b74Schristos   abort(); /* This way doesn't require any declaration for ANSI to fuck up */
27375fd0b74Schristos }
27475fd0b74Schristos 
27575fd0b74Schristos #define	NO_GET ((bfd_vma (*) (const void *)) swap_abort)
27675fd0b74Schristos #define	NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
27775fd0b74Schristos #define	NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
278*e992f068Schristos #define	NO_GET64 ((uint64_t (*) (const void *)) swap_abort)
279*e992f068Schristos #define	NO_PUT64 ((void (*) (uint64_t, void *)) swap_abort)
280*e992f068Schristos #define	NO_GETS64 ((int64_t (*) (const void *)) swap_abort)
28175fd0b74Schristos 
28275fd0b74Schristos const bfd_target core_irix_vec =
28375fd0b74Schristos   {
28475fd0b74Schristos     "irix-core",
28575fd0b74Schristos     bfd_target_unknown_flavour,
28675fd0b74Schristos     BFD_ENDIAN_BIG,		/* target byte order */
28775fd0b74Schristos     BFD_ENDIAN_BIG,		/* target headers byte order */
28875fd0b74Schristos     (HAS_RELOC | EXEC_P |	/* object flags */
28975fd0b74Schristos      HAS_LINENO | HAS_DEBUG |
29075fd0b74Schristos      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
29175fd0b74Schristos     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
29275fd0b74Schristos     0,							   /* symbol prefix */
29375fd0b74Schristos     ' ',						   /* ar_pad_char */
29475fd0b74Schristos     16,							   /* ar_max_namelen */
29575fd0b74Schristos     0,							   /* match_priority */
296*e992f068Schristos     TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */
29775fd0b74Schristos     NO_GET64, NO_GETS64, NO_PUT64,	/* 64 bit data */
29875fd0b74Schristos     NO_GET, NO_GETS, NO_PUT,		/* 32 bit data */
29975fd0b74Schristos     NO_GET, NO_GETS, NO_PUT,		/* 16 bit data */
30075fd0b74Schristos     NO_GET64, NO_GETS64, NO_PUT64,	/* 64 bit hdrs */
30175fd0b74Schristos     NO_GET, NO_GETS, NO_PUT,		/* 32 bit hdrs */
30275fd0b74Schristos     NO_GET, NO_GETS, NO_PUT,		/* 16 bit hdrs */
30375fd0b74Schristos 
30475fd0b74Schristos     {				/* bfd_check_format */
30575fd0b74Schristos       _bfd_dummy_target,		/* unknown format */
30675fd0b74Schristos       _bfd_dummy_target,		/* object file */
30775fd0b74Schristos       _bfd_dummy_target,		/* archive */
30875fd0b74Schristos       irix_core_core_file_p		/* a core file */
30975fd0b74Schristos     },
31075fd0b74Schristos     {				/* bfd_set_format */
311ede78133Schristos       _bfd_bool_bfd_false_error,
312ede78133Schristos       _bfd_bool_bfd_false_error,
313ede78133Schristos       _bfd_bool_bfd_false_error,
314ede78133Schristos       _bfd_bool_bfd_false_error
31575fd0b74Schristos     },
31675fd0b74Schristos     {				/* bfd_write_contents */
317ede78133Schristos       _bfd_bool_bfd_false_error,
318ede78133Schristos       _bfd_bool_bfd_false_error,
319ede78133Schristos       _bfd_bool_bfd_false_error,
320ede78133Schristos       _bfd_bool_bfd_false_error
32175fd0b74Schristos     },
32275fd0b74Schristos 
32375fd0b74Schristos     BFD_JUMP_TABLE_GENERIC (_bfd_generic),
32475fd0b74Schristos     BFD_JUMP_TABLE_COPY (_bfd_generic),
32575fd0b74Schristos     BFD_JUMP_TABLE_CORE (irix_core),
32675fd0b74Schristos     BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
32775fd0b74Schristos     BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
32875fd0b74Schristos     BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
32975fd0b74Schristos     BFD_JUMP_TABLE_WRITE (_bfd_generic),
33075fd0b74Schristos     BFD_JUMP_TABLE_LINK (_bfd_nolink),
33175fd0b74Schristos     BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
33275fd0b74Schristos 
33375fd0b74Schristos     NULL,
33475fd0b74Schristos 
33575fd0b74Schristos     NULL			/* backend_data */
33675fd0b74Schristos   };
33775fd0b74Schristos 
33875fd0b74Schristos #endif /* IRIX_CORE */
339