file_formats.h 7.17 KB
/*
 * Copyright (C) 1996-1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * 
 * This file is part of the SimOS distribution. 
 * See LICENSE file for terms of the license. 
 *
 */

/*****************************************************************
 * Structures needed to load and interpret an SGI kernel.
 *
 * $Author: blythe $
 * $Date: 2003/03/05 08:05:57 $
 *****************************************************************/

#ifndef _FILE_FORMATS_H
#define _FILE_FORMATS_H

#ifdef sgi
#define __ELF_H__   /* Prevent the system's elf.h from being included */
#define __LIBELF_H__
#define __SYS_ELFTYPES_H__
#endif

#include "simtypes.h"


typedef struct filehdr {
   uint16  f_magic;        /* magic number */
   uint16  f_nscns;        /* number of sections */
   int32   f_timdat;       /* time & date stamp */
   int32   f_symptr;       /* file pointer to symbolic header */
   int32   f_nsyms;        /* sizeof(symbolic hdr) */
   uint16  f_opthdr;       /* sizeof(optional hdr) */
   uint16  f_flags;        /* flags */
} FILHDR;

typedef struct scnhdr {
   char    s_name[8];      /* section name */
   int32   s_paddr;        /* physical address, aliased s_nlib */
   int32   s_vaddr;        /* virtual address */
   int32   s_size;         /* section size */
   int32   s_scnptr;       /* file ptr to raw data for section */
   int32   s_relptr;       /* file ptr to relocation */
   int32   s_lnnoptr;      /* file ptr to gp histogram */
   uint16  s_nreloc;       /* number of relocation entries */
   uint16  s_nlnno;        /* number of gp histogram entries */
   int32   s_flags;        /* flags field */
   int32   s_type;	   /* type field */
} SCNHDR;

typedef struct aouthdr {
   int16   magic;          /* see above                            */
   int16   vstamp;         /* version stamp                        */
   int32   tsize;          /* text size in bytes, padded to DW bdry*/
   int32   dsize;          /* initialized data "  "                */
   int32   bsize;          /* uninitialized data "   "             */
   int32   entry;          /* entry pt.                            */
   int32   text_start;     /* base of text used for this file      */
   int32   data_start;     /* base of data used for this file      */
   int32   bss_start;      /* base of bss used for this file       */
   int32   gprmask;        /* general purpose register mask        */
   int32   cprmask[4];     /* co-processor register masks          */
   int32   gp_value;       /* the gp value used for this object    */
} AOUTHDR;

/*      ELF header
 */
typedef __uint32_t      Elf32_Addr;
typedef unsigned short  Elf32_Half;
typedef __uint32_t      Elf32_Off;
typedef __int32_t       Elf32_Sword;
typedef __uint32_t      Elf32_Word;

typedef unsigned char   Elf32_Byte;     
typedef unsigned short  Elf32_Section;  

/* 64 bit data types */
typedef __uint64_t      Elf64_Addr;
typedef unsigned short  Elf64_Half;
typedef __uint64_t      Elf64_Off;
typedef __int32_t       Elf64_Sword;
typedef __int64_t       Elf64_Sxword;
typedef __uint32_t      Elf64_Word;
typedef __uint64_t      Elf64_Xword;
typedef unsigned char   Elf64_Byte;     /* unsigned tiny integer */
typedef unsigned short  Elf64_Section;  /* section index (unsigned) */

#define EI_NIDENT       16

typedef struct {
        unsigned char   e_ident[EI_NIDENT];     /* ident bytes */
        Elf32_Half      e_type;                 /* file type */
        Elf32_Half      e_machine;              /* target machine */
        Elf32_Word      e_version;              /* file version */
        Elf32_Addr      e_entry;                /* start address */
        Elf32_Off       e_phoff;                /* phdr file offset */
        Elf32_Off       e_shoff;                /* shdr file offset */
        Elf32_Word      e_flags;                /* file flags */
        Elf32_Half      e_ehsize;               /* sizeof ehdr */
        Elf32_Half      e_phentsize;            /* sizeof phdr */
        Elf32_Half      e_phnum;                /* number phdrs */
        Elf32_Half      e_shentsize;            /* sizeof shdr */
        Elf32_Half      e_shnum;                /* number shdrs */
        Elf32_Half      e_shstrndx;             /* shdr string index */
} Elf32_Ehdr;

typedef struct {
	unsigned char	e_ident[EI_NIDENT];	/* ident bytes */
	Elf64_Half	e_type;			/* file type */
	Elf64_Half	e_machine;		/* target machine */
	Elf64_Word	e_version;		/* file version */
	Elf64_Addr	e_entry;		/* start address */
	Elf64_Off	e_phoff;		/* phdr file offset */
	Elf64_Off	e_shoff;		/* shdr file offset */
	Elf64_Word	e_flags;		/* file flags */
	Elf64_Half	e_ehsize;		/* sizeof ehdr */
	Elf64_Half	e_phentsize;		/* sizeof phdr */
	Elf64_Half	e_phnum;		/* number phdrs */
	Elf64_Half	e_shentsize;		/* sizeof shdr */
	Elf64_Half	e_shnum;		/* number shdrs */
	Elf64_Half	e_shstrndx;		/* shdr string index */
} Elf64_Ehdr;


#define EI_MAG0         0               /* e_ident[] indexes */
#define EI_MAG1         1
#define EI_MAG2         2
#define EI_MAG3         3
#define EI_CLASS        4
#define EI_DATA         5
#define EI_VERSION      6
#define EI_PAD          7

#define ELFMAG0         0x7f            /* EI_MAG */
#define ELFMAG1         'E'
#define ELFMAG2         'L'
#define ELFMAG3         'F'
#define ELFMAG          "\177ELF"
#define SELFMAG         4

#define ELFCLASSNONE    0               /* EI_CLASS */
#define ELFCLASS32      1
#define ELFCLASS64      2
#define ELFCLASSNUM     3


typedef struct {
        Elf32_Word      sh_name;        /* section name */
        Elf32_Word      sh_type;        /* SHT_... */
        Elf32_Word      sh_flags;       /* SHF_... */
        Elf32_Addr      sh_addr;        /* virtual address */
        Elf32_Off       sh_offset;      /* file offset */
        Elf32_Word      sh_size;        /* section size */
        Elf32_Word      sh_link;        /* misc info */
        Elf32_Word      sh_info;        /* misc info */
        Elf32_Word      sh_addralign;   /* memory alignment */
        Elf32_Word      sh_entsize;     /* entry size if table */
} Elf32_Shdr;

typedef struct {
	Elf64_Word	sh_name;	/* section name */
	Elf64_Word	sh_type;	/* SHT_... */
	Elf64_Xword	sh_flags;	/* SHF_... */
	Elf64_Addr	sh_addr;	/* virtual address */
	Elf64_Off	sh_offset;	/* file offset */
	Elf64_Xword	sh_size;	/* section size */
	Elf64_Word	sh_link;	/* misc info */
	Elf64_Word	sh_info;	/* misc info */
	Elf64_Xword	sh_addralign;	/* memory alignment */
	Elf64_Xword	sh_entsize;	/* entry size if table */
} Elf64_Shdr;

#define OMAGIC  0407
/* Taken from /usr/include/filehdr.h */
#define  MIPSEBMAGIC    0x0160
#define  MIPSEBMAGIC_2  0x0163
#define  MIPSEBMAGIC_3  0x0140

#define IS_MIPSEBMAGIC(x) ((x) == MIPSEBMAGIC || (x) == MIPSEBMAGIC_2 || (x) == MIPSEBMAGIC_3)

#define ISCOFF(x) IS_MIPSEBMAGIC(x)

#define ELFMAG0         0x7f            /* EI_MAG */
#define ELFMAG1         'E'
#define ELFMAG2         'L'
#define ELFMAG3         'F'
#define ELFMAG          "\177ELF"
#define SELFMAG         4

#define IS_ELF(ehdr)    ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
                        (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
                        (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
                        (ehdr).e_ident[EI_MAG3] == ELFMAG3)


#endif /* _FILE_FORMATS_H */