Please consider a donation to the Higher Intellect project. See https://preterhuman.net/donate.php or the Donate to Higher Intellect page for more info.

Showcore.shar

From Higher Intellect Vintage Wiki
Jump to navigation Jump to search
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	showcore.c
#	Makefile
# This archive created: Wed Jul 14 11:26:50 1999
export PATH; PATH=/bin:$PATH
if test -f 'showcore.c'
then
	echo shar: will not over-write existing file "'showcore.c'"
else
sed 's/^X//' << \SHAR_EOF > 'showcore.c'
X/*
X	showcore.c
X
X	This prints interesting parts of an SGI core file to show what
X	information is present.
X
X	Usage:
X		showcore  -i -v   corefile ...
X
X	-i: show all idesc entries, even those not marked valid.
X	-I: show internals of idesc entries (only of valid ones).
X
X	davea 6/92   Silicon Graphics
X
X	$Revision: 1.18 $
X*/
X#include <stdio.h>
X#include <sys/types.h>
X#include <core.out.h>
X#include <malloc.h>
X#include <string.h>
X#include <elf_abi.h>
X#include <elf_mips.h>
X#ifndef I_THREADDATA
X/* If not in our core.out.h, define now.
X** Is part of IRIX6.5 core.out.h
X*/
Xstruct core_thread_data {
X        __uint64_t      thrd_offset;    /* offset to secondary thread data */
X        uint_t          nthreads;       /* number of secondary threads */
X        uint_t          desc_offset[CORE_NIDESC]; /* desc offsets within thrd data */
X        uint_t          prda_offset;    /* prda offset within thrd data */
X        uint_t          prda_len;       /* prda length */
X};
X
X#define I_THREADDATA    5               /* data for secondary (non-faulting)
X                                         * threads
X                                         */
X
X#endif
X
X#ifndef VPARTIAL
X#define VPARTIAL        0x2             
X		/* 1st 4096 bytes of text regions,
X                * to get elf program headers */
X#endif
X
X#define P printf
X#define F fflush(stdout)
X#define RR(buf,loc,siz)  ((fseek(fin,loc,0)<0) ? -1 : \
X		((fread(buf,siz,1,fin)!=1)?-2:0))
X#define RN(buf,siz)  ((fread(buf,siz,1,fin) != 1) ? -2 : 0)
X#define CURLOC     ( ftell(fin) )
X#define SEEKTO(i)  (fseek(fin,i,SEEK_SET))
X
X#define NGPREGS 32
Xtypedef unsigned long long ULONG64;
Xtypedef long long LONG64;
X
Xenum whichabi_e {noabi,o32,n32,n64};
X
Xstatic void dofile(void);
Xstatic int do_idesc(int i,enum whichabi_e thisabi);
Xstatic int do_vmap(int i);
Xstatic int do_vmap64(int i);
Xstatic void print_i_internals(int i, struct idesc *ip,enum whichabi_e thisabi);
Xstatic struct coreout corehdr;
Xchar *filename;
Xstatic int all_idesc;
Xstatic int show_i_internals;
Xstatic void elf_print_progheaders32(unsigned long offset,unsigned long entsize,
X	unsigned long count,int vmapindex);
Xstatic void elf_print_aout32( unsigned long offset, unsigned long len,
X	int vmapindex);
Xstatic void elf_print_aout64( unsigned long long offset, 
X	unsigned long  long len,int vmapindex);
Xstatic void elf_print_progheaders64(ULONG64 offset,
X	ULONG64 entsize,ULONG64 count, int vmapindex);
X
Xstatic int vmapphdrs_show;
X
X
X
Xstatic char  *idesc_names[] =  {
X	"I_GPREGS",
X	"I_FPREGS",
X	"I_SPECREGS",
X	"I_SIGHANDLER",
X	"I_EXDATA",
X	"I_THREADDATA",
X};
X
X
Xstatic void pr_gpregs(int, struct idesc *,enum whichabi_e thisabi);
Xstatic void pr_fpregs(int, struct idesc *,enum whichabi_e thisabi);
Xstatic void pr_specregs(int, struct idesc *,enum whichabi_e thisabi);
Xstatic void pr_sighandler(int, struct idesc *,enum whichabi_e thisabi);
Xstatic void pr_exdata(int, struct idesc *,enum whichabi_e thisabi);
Xstatic void pr_thread_data(int, struct idesc *,enum whichabi_e thisabi);
Xstatic void (*idesc_func[])(int, struct idesc *,enum whichabi_e thisabi) = {
X	pr_gpregs,
X	pr_fpregs,
X	pr_specregs,
X	pr_sighandler,
X	pr_exdata,
X	pr_thread_data
X};
Xstatic char * vmap_names[] = {
X	"0 (unused)",
X	"VTEXT",
X	"VDATA",
X	"VSTACK",
X	"VSHMEM",
X	"VLIBTEXT",
X	"VLIBDATA",
X	"VGRAPHICS",
X	"VMAPFILE",
X};
X#define NUMBER_OF_ELEMENTS(x)  (sizeof(x)/sizeof(x[0]))
X
X
XFILE *fin;
Xlong  total_file_length;
X
Xint
Xmain(int argc,char **argv)
X{
X	int i;
X
X	if( argc == 1) {
X		printf("Usage: showcore -i -p -I corefilename ....\n");
X		printf("-i print even idesc entries not marked IVALID\n");
X		printf("-p print VPARTIAL program header data\n");
X		printf("-I show some VDUMPED section internals\n");
X	} else {
X		argv++;
X		for(i =1; i<argc; i++,argv++) {
X			int res;
X			if(strcmp(argv[0],"-i") == 0) {
X				++all_idesc;
X				continue;
X			}
X			if(strcmp(argv[0],"-p") == 0) {
X				++vmapphdrs_show;
X				continue;
X			}
X			if(strcmp(argv[0],"-I") == 0) {
X				++show_i_internals;
X				continue;
X			}
X			fin = fopen(argv[0],"r");
X			if(fin == NULL) {
X				printf("No such file as %s\n",argv[0]);
X				continue;
X			}
X			filename = argv[0];
X			res = fseek(fin,0,SEEK_END);
X			if(res < 0) {
X			  printf("Unable to seek to end of %s\n",filename);
X			  continue;
X			}
X			total_file_length = ftell(fin);
X			if(total_file_length < 0) {
X			  printf("Unable to ftell get end pos of %s\n",filename);
X			  continue;
X			}
X			res = fseek(fin,0,SEEK_SET);
X			if(res < 0) {
X			  printf("Unable to seek back to beginning of %s\n",filename);
X			  continue;
X			}
X			
X			
X			dofile();
X			fclose(fin);
X		}
X	}
X	return 0;
X}
X
X
X
Xstatic void
Xdofile(void)
X{
X	int res;
X	int i;
X	int usevmap64 = 0;
X	enum whichabi_e thisabi = o32;
X	char lcname[CORE_NAMESIZE +2];
X	char lcargs[CORE_ARGSIZE + 2];
X
X	res = RR(&corehdr,0,sizeof(corehdr));
X	if(res) {
X		P("could not read whole ocre header of %s\n",filename);
X		return;
X	}
X	P("File: %s\n",filename);
X	P("  magic %#x %s version %d\n",
X	   corehdr.c_magic,(corehdr.c_magic == CORE_MAGIC)? "CORE_MAGIC" : 
X		corehdr.c_magic == CORE_MAGICN32? "CORE_MAGICN32":
X		corehdr.c_magic == CORE_MAGIC64? "CORE_MAGIC64": "NOT A CORE FILE!!!",
X	   corehdr.c_version);
X        thisabi = o32;
X	if(corehdr.c_magic == CORE_MAGIC64) {
X	  usevmap64 = 1;
X	  thisabi = n64;
X	} else if(corehdr.c_magic == CORE_MAGICN32) {
X	  thisabi = n32;
X	}
X	P("  vmap offset:  %#x  number of vmaps %d\n",
X	   corehdr.c_vmapoffset,
X		corehdr.c_nvmap);
X	strncpy(lcname,corehdr.c_name,sizeof(corehdr.c_name));
X	lcname[CORE_NAMESIZE] = 0;
X	strncpy(lcargs,corehdr.c_args,sizeof(corehdr.c_args));
X	lcname[CORE_ARGSIZE] = 0;
X	P("  process name: %s\n",lcname);
X	P("  process args: %s\n",lcargs);
X	P("  cause (signal causing dump): %d\n",
X		corehdr.c_sigcause);
X	F;
X	for(i = 0; i < CORE_NIDESC; i++) {
X		if(do_idesc(i,thisabi))
X			return;
X	}
X	for(i = 0; i < corehdr.c_nvmap; i++) {
X	   if(usevmap64) {
X		if(do_vmap64(i))
X			return;
X	   } else {
X		if(do_vmap(i))
X			return;
X	   }
X	}
X	F;
X}
X/*
X	Print an idesc.
X	Return non-zero in case of error/problem.
X*/
Xstatic int
Xdo_idesc(int i, enum whichabi_e thisa)
X{
X	struct idesc *ip;
X	ip = &corehdr.c_idesc[i];
X	if( (ip->i_flags & IVALID) == 0 && all_idesc == 0)
X		return 0; /* no print non-valid entries */
X	P("  idesc (info descriptor) %d",i);
X	if( i < NUMBER_OF_ELEMENTS(idesc_names)) {
X		P(" (%s)",idesc_names[i]);
X	}
X	P("\n");
X	F;
X	P("    offset %#x  length (bytes) %d (%#x) flags %#x",
X		ip->i_offset,
X		ip->i_len,
X		ip->i_len,
X		ip->i_flags);
X	P(" %s",ip->i_flags& IVALID? "IVALID" :"");
X	P("\n");
X	if(ip->i_flags& IVALID) {
X		long endpt =ip->i_offset + ip->i_len; 
X		if(endpt > total_file_length) {
X		  printf("    IDSEC RUNS OFF END OF COREFILE: end file 0x%lx (%ld) end idesc 0x%lx (%ld)\n",
X			(long)total_file_length,
X			(long)total_file_length,
X			(long)endpt,
X			(long)endpt);
X		}
X	}
X	if((ip->i_flags & IVALID)  && show_i_internals) {
X		print_i_internals(i,ip,thisa);
X	}
X	F;
X	return 0;
X}
X
X/*
X	Print a vmap.
X	Return non-zero in case of error/problem.
X*/
Xstatic int
Xdo_vmap(int i)
X{
X	long vmapoff;
X	struct vmap lvmap;
X	struct vmap *vp;
X
X	vmapoff = corehdr.c_vmapoffset + i * sizeof(struct vmap);
X
X	if(RR(&lvmap,vmapoff,sizeof(lvmap)) < 0) {
X	  P("Could not read vmap %d at offset %#lx\n",
X			i,vmapoff);
X	  return 1;
X	} 
X	vp = &lvmap;
X	P("  Vmap %d at offset %#lx\n",
X		i,vmapoff);
X	P("     Address %#x  length(bytes) %d (%#x)  data at %#x\n",
X		vp->v_vaddr,
X		vp->v_len,
X		vp->v_len,
X		vp->v_offset);
X
X	P("     flags %#x %s %s",
X		(int)vp->v_flags,
X		(((int)vp->v_flags) & VDUMPED) ? "VDUMPED" : "",
X		(((int)vp->v_flags) & VPARTIAL) ? "VPARTIAL" : ""
X			);
X	P("     type %d %s",
X		(int)vp->v_type,
X			(vp->v_type < NUMBER_OF_ELEMENTS( vmap_names))?
X				vmap_names[(int)vp->v_type] : "");
X
X	P("\n");
X	if(((int)vp->v_flags) & VDUMPED) {
X	  long endpt = vp->v_len + vp->v_offset;
X	  if(endpt > total_file_length) {
X	     printf("     VMAP RUNS OFF FILE endpt 0x%lx (%ld) file length 0x%lx (%ld)\n",    
X			(long)endpt,
X			(long)endpt,
X			(long)total_file_length,
X			(long)total_file_length);
X	  }
X	}
X	F;
X	if(vmapphdrs_show &&((int)vp->v_flags) & VPARTIAL){
X	    elf_print_aout64(vp->v_offset,vp->v_len,i);
X	}
X	return 0;
X}
X
X/*
X	Print a vmap.
X	Return non-zero in case of error/problem.
X*/
Xstatic int
Xdo_vmap64(int i)
X{
X	long vmapoff;
X	struct vmap64 lvmap;
X	struct vmap64 *vp;
X
X	vmapoff = corehdr.c_vmapoffset + i * sizeof(struct vmap64);
X
X	if(RR(&lvmap,vmapoff,sizeof(lvmap)) < 0) {
X	  P("Could not read vmap %d at offset %#lx\n",
X			i,vmapoff);
X	  return 1;
X	} 
X	vp = &lvmap;
X	P("  Vmap %d at offset %#lx\n",
X		i,vmapoff);
X	P("     Address 0x%llx  length(bytes) %lld (0x%llx)  data at 0x%llx\n",
X		vp->v_vaddr,
X		(long long)vp->v_len,
X		(unsigned long long)vp->v_len,
X		vp->v_offset);
X
X	P("     flags %#x %s %s",
X		(int)vp->v_flags,
X		(((int)vp->v_flags) & VDUMPED) ? "VDUMPED" : "",
X		(((int)vp->v_flags) & VPARTIAL) ? "VPARTIAL" : ""
X			);
X
X	
X		
X	P("     type %d %s",
X		(int)vp->v_type,
X			(vp->v_type < NUMBER_OF_ELEMENTS( vmap_names))?
X				vmap_names[(int)vp->v_type] : "");
X
X	P("\n");
X        if(((int)vp->v_flags) & VDUMPED) {
X          long long endpt = vp->v_len + vp->v_offset;
X          if(endpt > total_file_length) {
X             printf("     VMAP RUNS OFF FILE endpt 0x%llx (%lld) file length 0x%llx (%lld)\n",
X                        (long long)endpt,
X                        (long long)endpt,
X                        (long long)total_file_length,
X                        (long long)total_file_length);
X          }
X        }
X
X	F;
X	if(vmapphdrs_show &&((int)vp->v_flags) & VPARTIAL){
X	    elf_print_aout32(vp->v_offset,vp->v_len,i);
X	}
X
X
X	return 0;
X}
X
X
X/*
X	Figure out what kind this is and then print the internals,
X	if we know how to.
X*/
Xstatic void
Xprint_i_internals(int i, struct idesc *ip,enum whichabi_e thisa)
X{
X	int elt_count = sizeof(idesc_func)/sizeof(int *);
X
X	if(i >= elt_count) {
X	  P("      No function for IDESC %d: not valid core file?\n",i);
X	  return;
X	}
X
X	if( SEEKTO(ip->i_offset) < 0) {
X		P("      Could not seek to offset %#x in idesc %d\n",
X		ip->i_offset,i);
X		return;
X	}
X	(*idesc_func[i])(i,ip,thisa);
X}
X
X/*ARGSUSED*/
X#define REGS_ON_ONE_LINE 2
Xstatic void pr_gpregs(int i, struct idesc *ip,enum whichabi_e thisabi)
X{
X	int k;
X	static unsigned long long gp_reg[NGPREGS];
X	static unsigned gp_reg32[NGPREGS];
X	if(thisabi == o32) {
X	   if(ip->i_len != sizeof(gp_reg32)) {
X	   	P("     Expected %ld bytes for gp regs not %ld!\n",
X			(long)sizeof(gp_reg32),(long)ip->i_len);
X		return;
X	   }
X	  if(RN(gp_reg,sizeof(gp_reg32)) < 0) {
X		P("     Could not read %ld bytes of gp regs\n",
X			(long)sizeof(gp_reg32));
X		return;
X	  }
X	  for(k = 0; k < NGPREGS; ++k) {
X		gp_reg[k] = gp_reg32[k];
X	  }
X 	} else {
X	   if(ip->i_len != sizeof(gp_reg)) {
X	   	P("     Expected %ld bytes for gp regs not %lld!\n",
X			(long)sizeof(gp_reg),(long long)ip->i_len);
X		return;
X	   }
X	  if(RN(gp_reg,sizeof(gp_reg)) < 0) {
X		P("     Could not read %ld bytes of gp regs\n",
X			(long)sizeof(gp_reg));
X		return;
X	  }
X	}
X	for(k = 0; k < NGPREGS; k++) {
X		if(k%REGS_ON_ONE_LINE == 0)
X			P("    gp reg %2d: ",k);
X		P( " 0x%08llx",gp_reg[k]);
X		if(k%REGS_ON_ONE_LINE == (REGS_ON_ONE_LINE -1))
X			P("\n");
X	}
X}
X/*ARGSUSED*/
Xstatic void pr_fpregs(int i, struct idesc *ip,enum whichabi_e thisabi)
X{
X	/* not implemented */
X}
X#define SPECCNT 7
X/*ARGSUSED*/
Xstatic void pr_specregs(int i, struct idesc *ip,enum whichabi_e thisabi)
X{
X/* special purpose control registers
X   * int EPC, CAUSE, BADVADDR, MDHI, MDLO
X   * int fpcsr, fpeir
X   */
X   	unsigned specregs32[SPECCNT];
X   	unsigned long long specregs[SPECCNT];
X	int k;
X
X	if(thisabi == o32) {
X	  if(ip->i_len != sizeof(specregs32)) {
X		P("     Expected %ld bytes for spec regs regs not %ld!\n",
X			(long)sizeof(specregs32),(long)ip->i_len);
X		return;
X	  }
X	  if(RN(specregs,sizeof(specregs32)) < 0) {
X		P("     Could not read %ld bytes of special regs\n",
X			(long)sizeof(specregs32));
X		return;
X	  }
X	  for(k = 0; k < NGPREGS; ++k) {
X                specregs[k] = specregs32[k];
X          }
X
X	} else {
X	  if(ip->i_len != sizeof(specregs)) {
X		P("     Expected %ld bytes for spec regs regs not %ld!\n",
X			 (long)sizeof(specregs),(long)ip->i_len);
X		return;
X	  }
X	  if(RN(specregs,sizeof(specregs)) < 0) {
X		P("     Could not read %ld bytes of special regs\n",
X			(long)sizeof(specregs));
X		return;
X	  }
X	}
X   
X	P("      EPC:   0x%08llx",specregs[0]);
X	P("   CAUSE: 0x%08llx",specregs[1]);
X	P("   BADVADDR: 0x%08llx",specregs[2]);
X	P("\n");
X	P("      MDHI:  0x%08llx",specregs[3]);
X	P("   MDLO:  0x%08llx",specregs[4]);
X	P("\n");
X	P("      fpcsr: 0x%08llx",specregs[5]);
X	P("   fpeir: 0x%08llx",specregs[6]);
X	P("\n");
X}
X/*ARGSUSED*/
Xstatic void pr_sighandler(int i, struct idesc *ip,enum whichabi_e thisabi)
X{
X}
X/*
X	Read and print tsize, dsize, bsize;
X*/
X/*ARGSUSED*/
Xstatic void pr_exdata(int i, struct idesc *ip,enum whichabi_e thisabi)
X{
X	static unsigned locarr32[3];
X	static unsigned long long locarr[3];
X	int k;
X
X	if(thisabi == o32) {
X	  if(ip->i_len != sizeof(locarr32)) {
X		P("     Expected %ld bytes for tsize, dsize,bsize, not %ld!\n",
X			(long)sizeof(locarr32),(long)ip->i_len);
X		return;
X	  }
X	  if(RN(locarr,sizeof(locarr32)) < 0) {
X		P("     Could not read %ld bytes of tsize, dsize, bsize\n",
X			(long)sizeof(locarr32));
X		return;
X	  }
X	  for(k = 0; k < 3; ++k ) {
X		locarr[k] = locarr32[k];
X	  }
X	} else {
X	  if(ip->i_len != sizeof(locarr)) {
X		P("     Expected %ld bytes for tsize, dsize,bsize, not %ld!\n",
X			(long)sizeof(locarr),(long)ip->i_len);
X		return;
X	  }
X	  if(RN(locarr,sizeof(locarr)) < 0) {
X		P("     Could not read %ld bytes of tsize, dsize, bsize\n",
X			(long)sizeof(locarr));
X		return;
X	  }
X	}
X	P("       tsize 0x%lx dsize 0x%lx bsize 0x%lx\n",
X		(unsigned long)locarr[0],
X		(unsigned long)locarr[1],
X		(unsigned long)locarr[2]);
X}
X/*ARGSUSED*/
Xstatic void pr_thread_data(int i, struct idesc *ip,enum whichabi_e thisabi)
X{
X	static struct core_thread_data th_data;
X        int k;
X
X        if(ip->i_len != sizeof(th_data)) {
X                P("     Expected %d bytes for core thread data %d!\n",
X                        (int)sizeof(th_data),(int)ip->i_len);
X                return;
X        }
X        if(RN(&th_data,sizeof(th_data)) < 0) {
X                P("     Could not read %d bytes of core thread data\n",
X                        (int)sizeof(th_data));
X                return;
X        }
X        P("  0x%llx: file offset to secondary thread data\n",
X			(unsigned long long)th_data.thrd_offset);
X        P("    %6u: number of secondary_threads\n",
X				(unsigned)th_data.nthreads);
X	P("    %6u: prda offset within thread data\n",
X			(unsigned)th_data.prda_offset); 
X	P("    %6u: prda length within thread data\n",
X			(unsigned)th_data.prda_len); 
X	for( k = 0;  k <CORE_NIDESC ; ++k) {
X	  P("    [%2d] 0x%04x desc. offset within thread data\n",
X		(int)k,
X		(unsigned)th_data.desc_offset[k]);
X	}
X}
X
X
X
X
X
Xstatic void
Xelf_print_aout32( unsigned long offset, unsigned long len,int vmapindex)
X{
X	int i;
X	int res;
X	unsigned char c;
X	Elf32_Ehdr ehdr;
X
X	SEEKTO(offset);
X	res = RR(&ehdr,offset,sizeof(ehdr));
X	if(res) {
X		P("could not read whole ELF file header"
X		"  offset 0x%lx vmap index %d \n",
X		offset,vmapindex);
X		return;
X	}
X	c = ehdr.e_ident[EI_CLASS];
X	if (c == ELFCLASS64) {
X		elf_print_aout64(offset,len,vmapindex);
X		return;
X	}
X
X	P("Elf object file header\n");
X	P("\tident bytes: ");
X	for(i = 0; i < EI_NIDENT; i++) {
X		c =  ehdr.e_ident[i];
X		P(" %02x",c);
X	}
X	P("\n");
X	c = ehdr.e_ident[EI_CLASS];
X	P("\t\tident[class] %#x   %s\n",c,(c == ELFCLASSNONE)? "ELFCLASSNONE":
X			(c == ELFCLASS32) ? "ELFCLASS32" :
X			(c == ELFCLASS64) ? "ELFCLASS64" :
X			"unknown ");
X	c = ehdr.e_ident[EI_DATA];
X	P("\t\tident[data] %#x   %s\n",c,(c == ELFDATANONE)? "ELFDATANONE":
X			(c == ELFDATA2MSB)? "ELFDATA2MSB":
X			(c == ELFDATA2LSB) ? "ELFDATA2LSB":
X			"unknown");
X	c = ehdr.e_ident[EI_VERSION];
X	P("\t\tident[version] %#x   %s\n",c,(c == EV_CURRENT)? "EV_CURRENT":
X			"unknown");
X	i = ehdr.e_type;
X	P("\ttype %#x	%s\n",i,(i == ET_NONE)? "ET_NONE No file type":
X		(i == ET_REL)? "ET_REL Relocatable file":
X		(i == ET_EXEC)? "ET_EXEC Executable file":
X		(i == ET_DYN)? "ET_DYN Shared object file":
X		(i == ET_CORE) ? "ET_CORE Core file":
X			"unknown");
X	P("\tmachine %#x %s\n",ehdr.e_machine,
X			(ehdr.e_machine == EM_NONE) ? "EM_NONE Invalid machine":
X			(ehdr.e_machine == EM_MIPS) ? "EM_MIPS Mips cpu":
X			"unknown");
X	P("\tversion %#x %s\n",ehdr.e_version,
X			(ehdr.e_version == EV_NONE) ? "EV_NONE Invalid version":
X			(ehdr.e_version == EV_CURRENT) ? "EV_CURRENT current version":
X			"unknown");
X	P("\tEntry %#x   prog hdr off: %#x   sec hdr off %#x\n",
X		ehdr.e_entry,ehdr.e_phoff,ehdr.e_shoff);
X	P("\tFlags %#x",ehdr.e_flags);
X	if(ehdr.e_flags& EF_MIPS_NOREORDER){
X		P(", .noreorder present(flag EF_MIPS_NOREORDER)");
X	}
X	if(ehdr.e_flags& EF_MIPS_PIC){
X		P(", EF_MIPS_PIC");
X	}
X	if(ehdr.e_flags& EF_MIPS_CPIC){
X		P(", EF_MIPS_CPIC(calls pic)");
X	}
X#ifdef EF_MIPS_XGOT
X        if(ehdr.e_flags& EF_MIPS_XGOT){
X                P(", EF_MIPS_XGOT");
X        }
X#endif
X        if(ehdr.e_flags& EF_MIPS_UGEN_RESERVED){
X                P(", EF_MIPS_UGEN_RESERVED");
X        }
X	P("\n");
X	if(ehdr.e_flags& EF_MIPS_ARCH){
X		int c;
X		c=(ehdr.e_flags& EF_MIPS_ARCH);
X		P("\t    flag mips-arch 0x%x",c);
X		P("  %s", (c == EF_MIPS_ARCH_2) ? "EF_MIPS_ARCH_2" :
X			(c == EF_MIPS_ARCH_3) ? "EF_MIPS_ARCH_3":
X			(c == EF_MIPS_ARCH_4) ? "EF_MIPS_ARCH_4":
X			(c == 0) ? "mips_1" :
X			"unknownarch");
X		P("\n");
X	}
X	P("\tEhdrsize %3d  Proghdrsize %3d  Sechdrsize %3d\n",
X		ehdr.e_ehsize,ehdr.e_phentsize,ehdr.e_shentsize);
X	P("\t              P-hdrcount  %3d  S-hdrcount %3d\n",
X		ehdr.e_phnum,ehdr.e_shnum);
X	if(ehdr.e_shstrndx == SHN_UNDEF) {
X	  P("\tSection strings are not present e_shstrndx ==SHN_UNDEF\n");
X	} else {
X	  P("\tSection strings are in section %d\n",ehdr.e_shstrndx);
X	}
X
X	if(ehdr.e_shstrndx > ehdr.e_shnum) {
X		P("String section index is wrong: %ld vs only %ld sections. Consider it 0\n",
X			(long)(ehdr.e_shstrndx),(long)(ehdr.e_shnum));
X		ehdr.e_shstrndx = 0;
X	}
X	if((len + offset)  < (ehdr.e_phoff + ehdr.e_phentsize*ehdr.e_phnum)){
X		P("Program headers for vmap %d did not all fit in VPARTIAL "
X		  "page\n",vmapindex);
X	}
X	elf_print_progheaders32(ehdr.e_phoff + offset,ehdr.e_phentsize,ehdr.e_phnum,vmapindex);
X}
X
X
X/*ARGSUSED*/
Xstatic void 
Xelf_print_progheaders32(unsigned long offset,unsigned long entsize,unsigned long count,int vmapindex)
X{
X	int i;
X	Elf32_Phdr *pph;
X	Elf32_Phdr *orig_pph;
X
X	if(count == 0) {
X		P("No program headers\n");
X		return;
X	}
X	if(entsize < sizeof(Elf32_Phdr)) {
X		P("Elf Program header too small? %ld vs %ld\n",
X			entsize,(unsigned long)sizeof(Elf32_Phdr));
X	}
X	i =SEEKTO(offset);
X	if(i) {
X		P("Seek to %ld to read program headers failed\n",offset);
X		return;
X	}
X	pph = (Elf32_Phdr *)malloc(count * entsize);
X	if(pph == 0) {
X		P("malloc to %ld bytes of program header space failed\n",count *entsize);
X		return;
X	}
X	orig_pph = pph;
X	i = RN(pph,count*entsize);
X	if(i) {
X		P("Read  %ld bytes program headers failed\n",count*entsize);
X		return;
X	}
X#define PHTYPE(x) (\
X	((x) == PT_NULL) ? "PT_NULL": \
X	((x) == PT_LOAD) ? "PT_LOAD": \
X	((x) == PT_DYNAMIC) ? "PT_DYNAMIC": \
X	((x) == PT_INTERP) ? "PT_INTERP": \
X	((x) == PT_NOTE) ? "PT_NOTE": \
X	((x) == PT_SHLIB) ? "PT_SHLIB": \
X	((x) == PT_PHDR) ? "PT_PHDR": \
X	((x) == PT_MIPS_REGINFO) ? "PT_MIPS_REGINFO": \
X	((x) == PT_MIPS_OPTIONS) ? "PT_MIPS_OPTIONS": \
X	((x) == PT_MIPS_RTPROC) ? "PT_MIPS_RTPROC (run time proc tbl)":  \
X		/* run time proc table*/ \
X	"unknowntype")
X
X	P("Program header count %ld\n",count);
X	for( i = 0; i < count; 
X		++i,  pph = (Elf32_Phdr *) ((char *)pph + entsize)) {
X	  P("Program header %d",i);
X	  P("  type %s (%lx)",PHTYPE(pph->p_type),(unsigned long)pph->p_type);
X	  P(", offset %#lx %ld",(long)pph->p_offset,(long)pph->p_offset);
X	  P("\n");
X	  P("\tvaddr %#lx %ld",(long)pph->p_vaddr,(long)pph->p_vaddr);
X	  P(", paddr %#lx %ld",(long)pph->p_paddr,(long)pph->p_paddr);
X	  P("\n");
X	  P("\tfilesz %#lx %ld",(long)pph->p_filesz,(long)pph->p_filesz);
X	  P("\n");
X	  P("\tmemsz %#lx %ld",(long)pph->p_memsz,(long)pph->p_memsz);
X	  P(", flags %#lx",(long)pph->p_flags);
X	  if(pph->p_flags & PF_X) {
X		P(" PF_X");
X	  }
X	  if(pph->p_flags & PF_W) {
X		P(" PF_W");
X	  }
X	  if(pph->p_flags & PF_R) {
X		P(" PF_R");
X	  }
X	  P(", align %#lx %ld",(long)pph->p_align,(long)pph->p_align);
X	  P("\n");
X/*
X	  if(pph->p_type == PT_INTERP) {
X		elf_print_interp(pph->p_offset,pph->p_filesz);
X	  }
X*/
X	}
X	free(orig_pph);
X}
X
X
Xstatic void
Xelf_print_aout64( unsigned long long offset, unsigned long  long len,int vmapindex)
X{
X	int i;
X	int res;
X	unsigned char c;
X	 Elf64_Ehdr ehdr;
X
X
X	P("Elf 64-bit object file header\n");
X	SEEKTO(0);
X	res = RR(&ehdr,offset,sizeof(ehdr));
X	if(res) {
X		P("could not read whole ELF file header of vmap %d\n",vmapindex);
X		return;
X	}
X	c = ehdr.e_ident[EI_CLASS];
X	if (c == ELFCLASS32) {
X		elf_print_aout32(offset,len,vmapindex);
X		return;
X	}
X	P("\tident bytes: ");
X	for(i = 0; i < EI_NIDENT; i++) {
X		c =  ehdr.e_ident[i];
X		P(" %02x",c);
X	}
X	P("\n");
X	c = ehdr.e_ident[EI_CLASS];
X	P("\t\tident[class] 0x%x   %s\n",(int)c,
X			(c == ELFCLASSNONE)? "ELFCLASSNONE":
X			(c == ELFCLASS32) ? "ELFCLASS32" :
X			(c == ELFCLASS64) ? "ELFCLASS64" :
X			"unknown ");
X	c = ehdr.e_ident[EI_DATA];
X	P("\t\tident[data] 0x%x   %s\n",(int)c,
X			(c == ELFDATANONE)? "ELFDATANONE":
X			(c == ELFDATA2MSB)? "ELFDATA2MSB":
X			(c == ELFDATA2LSB) ? "ELFDATA2LSB":
X			"unknown");
X	c = ehdr.e_ident[EI_VERSION];
X	P("\t\tident[version] 0x%x   %s\n",(int)c,
X			(c == EV_CURRENT)? "EV_CURRENT":
X			"unknown");
X	i = ehdr.e_type;
X	P("\ttype 0x%x	%s\n",(int)i,(i == ET_NONE)? "ET_NONE No file type":
X		(i == ET_REL)? "ET_REL Relocatable file":
X		(i == ET_EXEC)? "ET_EXEC Executable file":
X		(i == ET_DYN)? "ET_DYN Shared object file":
X		(i == ET_CORE) ? "ET_CORE Core file":
X			"unknown");
X	P("\tmachine 0x%x %s\n",(int)ehdr.e_machine,
X			(ehdr.e_machine == EM_NONE) ? "EM_NONE Invalid machine":
X			(ehdr.e_machine == EM_MIPS) ? "EM_MIPS Mips cpu":
X			"unknown");
X	P("\tversion 0x%x %s\n",(int)ehdr.e_version,
X			(ehdr.e_version == EV_NONE) ? "EV_NONE Invalid version":
X			(ehdr.e_version == EV_CURRENT) ? "EV_CURRENT current version":
X			"unknown");
X	P("\tEntry 0x%llx   prog hdr off: 0x%llx   sec hdr off 0x%llx\n",
X		(ULONG64)ehdr.e_entry,(ULONG64)ehdr.e_phoff,(ULONG64)ehdr.e_shoff);
X	P("\tFlags 0x%llx",(ULONG64)ehdr.e_flags);
X	if(ehdr.e_flags& EF_MIPS_NOREORDER){
X		P(", .noreorder present(flag EF_MIPS_NOREORDER)");
X	}
X	if(ehdr.e_flags& EF_MIPS_PIC){
X		P(", EF_MIPS_PIC");
X	}
X	if(ehdr.e_flags& EF_MIPS_CPIC){
X		P(", EF_MIPS_CPIC(calls pic)");
X	}
X#ifdef EF_MIPS_XGOT
X	if(ehdr.e_flags& EF_MIPS_XGOT){
X		P(", EF_MIPS_XGOT");
X	}
X#endif
X	if(ehdr.e_flags& EF_MIPS_UGEN_RESERVED){
X		P(", EF_MIPS_UGEN_RESERVED");
X	}
X	P("\n");
X	if(ehdr.e_flags& EF_MIPS_ARCH){
X		int c;
X		c=(ehdr.e_flags& EF_MIPS_ARCH);
X		P("\t    flag mips-arch 0x%x",c);
X		P("  %s", (c == EF_MIPS_ARCH_2) ? "EF_MIPS_ARCH_2" :
X			(c == EF_MIPS_ARCH_3) ? "EF_MIPS_ARCH_3":
X			(c == EF_MIPS_ARCH_4) ? "EF_MIPS_ARCH_4":
X			(c == 0) ? "mips_1" :
X			"unknownarch");
X		P("\n");
X	}
X	P("\tEhdrsize %3d  Proghdrsize %3d  Sechdrsize %3d\n",
X		(int)ehdr.e_ehsize,(int)ehdr.e_phentsize,(int)ehdr.e_shentsize);
X	P("\t              P-hdrcount  %3d  S-hdrcount %3d\n",
X		(int)ehdr.e_phnum,(int)ehdr.e_shnum);
X	if(ehdr.e_shstrndx == SHN_UNDEF) {
X	  P("\tSection strings are not present e_shstrndx ==SHN_UNDEF\n");
X	} else {
X	  P("\tSection strings are in section %lld\n",(LONG64)ehdr.e_shstrndx);
X	}
X
X	if(ehdr.e_shstrndx > ehdr.e_shnum) {
X		P("String section index is wrong: %lld vs only %lld sections. Consider it 0\n",
X			(LONG64)(ehdr.e_shstrndx),(LONG64)(ehdr.e_shnum));
X		ehdr.e_shstrndx = 0;
X	}
X	if((len + offset)  < (ehdr.e_phoff + ehdr.e_phentsize*ehdr.e_phnum)){
X		P("Program headers for vmap %d did not all fit in VPARTIAL "
X			"page\n",vmapindex);
X	}
X	elf_print_progheaders64(ehdr.e_phoff + offset,ehdr.e_phentsize,ehdr.e_phnum,vmapindex);
X}
X
X
X
X/*ARGSUSED*/
Xstatic void 
Xelf_print_progheaders64(ULONG64 offset,ULONG64 entsize,ULONG64 count, int vmapindex)
X{
X	int i;
X	Elf64_Phdr *pph;
X	Elf64_Phdr *orig_pph;
X
X
X
X	if(count == 0) {
X		P("No program headers\n");
X		return;
X	}
X	if(entsize < sizeof(Elf64_Phdr)) {
X		P("Elf Program header too small? %lld vs %lld\n",
X			(LONG64)entsize,(LONG64)sizeof(Elf64_Phdr));
X	}
X	i =SEEKTO(offset);
X	if(i) {
X		P("Seek to %lld to read program headers failed\n",
X			(LONG64)offset);
X		return;
X	}
X	pph = (Elf64_Phdr *)malloc(count * entsize);
X	if(pph == 0) {
X		P("malloc to %lld bytes of program header space failed\n",
X			(LONG64)count *  (LONG64)entsize);
X		return;
X	}
X	orig_pph = pph;
X	i = RN(pph,count*entsize);
X	if(i) {
X		P("Read  %lld bytes program headers failed\n",count*entsize);
X		return;
X	}
X#define PHTYPE(x) (\
X	((x) == PT_NULL) ? "PT_NULL": \
X	((x) == PT_LOAD) ? "PT_LOAD": \
X	((x) == PT_DYNAMIC) ? "PT_DYNAMIC": \
X	((x) == PT_INTERP) ? "PT_INTERP": \
X	((x) == PT_NOTE) ? "PT_NOTE": \
X	((x) == PT_SHLIB) ? "PT_SHLIB": \
X	((x) == PT_PHDR) ? "PT_PHDR": \
X	((x) == PT_MIPS_REGINFO) ? "PT_MIPS_REGINFO": \
X	((x) == PT_MIPS_OPTIONS) ? "PT_MIPS_OPTIONS": \
X	((x) == PT_MIPS_RTPROC) ? "PT_MIPS_RTPROC (run time proc tbl)":  \
X		/* run time proc table*/ \
X	"unknowntype")
X
X	P("Program header count %lld\n",(LONG64)count);
X	for( i = 0; i < count; 
X		++i,  pph = (Elf64_Phdr *) ((char *)pph + entsize)) {
X	  P("Program header %lld",(LONG64)i);
X	  P("  type %s (%llx)",PHTYPE(pph->p_type),(ULONG64)pph->p_type);
X	  P(", offset 0x%llx %lld",(LONG64)pph->p_offset,(LONG64)pph->p_offset);
X	  P("\n");
X	  P("\tvaddr 0x%llx %lld",(LONG64)pph->p_vaddr,(LONG64)pph->p_vaddr);
X	  P(", paddr 0x%llx %lld",(LONG64)pph->p_paddr,(LONG64)pph->p_paddr);
X	  P("\n");
X	  P("\tfilesz 0x%llx %lld",(LONG64)pph->p_filesz,(LONG64)pph->p_filesz);
X	  P("\n");
X	  P("\tmemsz 0x%llx %lld",(LONG64)pph->p_memsz,(LONG64)pph->p_memsz);
X	  P(", flags %#lx",(long)pph->p_flags);
X	  if(pph->p_flags & PF_X) {
X		P(" PF_X");
X	  }
X	  if(pph->p_flags & PF_W) {
X		P(" PF_W");
X	  }
X	  if(pph->p_flags & PF_R) {
X		P(" PF_R");
X	  }
X	  P(", align 0x%llx %lld",(LONG64)pph->p_align,(LONG64)pph->p_align);
X	  P("\n");
X/*
X	  if(pph->p_type == PT_INTERP) {
X		elf_print_interp(pph->p_offset,pph->p_filesz);
X	  }
X*/
X	}
X	free(orig_pph);
X}
X
X
SHAR_EOF
fi # end of overwriting check
if test -f 'Makefile'
then
	echo shar: will not over-write existing file "'Makefile'"
else
sed 's/^X//' << \SHAR_EOF > 'Makefile'
XSHELL = /bin/sh
X
X# for core files > 2GB big, 
X# (possible with -64 apps)
X# change the -n32 below to -64 and build a -64 showcore.
XCFLAGS = -n32 -g -fullwarn
X
Xall: showcore
X
Xshowcore:  showcore.o
X	$(CC) $(CFLAGS) -g showcore.o -o showcore
X
Xshar:
X	shar -p X showcore.c Makefile  >/d2/public/showcore.shar
Xclobber:
X	rm *.o core showcore
X
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0