gonzui


Format: Advanced Search

t2ex/t2ex_source/t2ex/load/src/elf.hbare sourcepermlink (0.01 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T2EX Software Package
    4:  *
    5:  *    Copyright 2012 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2012/12/12.
   10:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/04.
   11:  *
   12:  *----------------------------------------------------------------------
   13:  */
   14: /*
   15:  * This software package is available for use, modification, 
   16:  * and redistribution in accordance with the terms of the attached 
   17:  * T-License 2.x.
   18:  * If you want to redistribute the source code, you need to attach 
   19:  * the T-License 2.x document.
   20:  * There's no obligation to publish the content, and no obligation 
   21:  * to disclose it to the TRON Forum if you have modified the 
   22:  * software package.
   23:  * You can also distribute the modified source code. In this case, 
   24:  * please register the modification to T-Kernel traceability service.
   25:  * People can know the history of modifications by the service, 
   26:  * and can be sure that the version you have inherited some 
   27:  * modification of a particular version or not.
   28:  *
   29:  *    http://trace.tron.org/tk/?lang=en
   30:  *    http://trace.tron.org/tk/?lang=ja
   31:  *
   32:  * As per the provisions of the T-License 2.x, TRON Forum ensures that 
   33:  * the portion of the software that is copyrighted by Ken Sakamura or 
   34:  * the TRON Forum does not infringe the copyrights of a third party.
   35:  * However, it does not make any warranty other than this.
   36:  * DISCLAIMER: TRON Forum and Ken Sakamura shall not be held
   37:  * responsible for any consequences or damages caused directly or
   38:  * indirectly by the use of this software package.
   39:  *
   40:  * The source codes in bsd_source.tar.gz in this software package are 
   41:  * derived from NetBSD or OpenBSD and not covered under T-License 2.x.
   42:  * They need to be changed or redistributed according to the 
   43:  * representation of each source header.
   44:  */
   45: 
   46: /*
   47:  *      elf.h
   48:  *
   49:  *       T2EX: program load functions
   50:  *       ELF (Executable and Linking Format) definitions
   51:  */
   52: 
   53: #ifndef _T2EX_LOAD_ELF_
   54: #define _T2EX_LOAD_ELF_
   55: 
   56: #include <basic.h>
   57: 
   58: /* ELF header */
   59: #define EI_NIDENT       16
   60: typedef struct {
   61:         UB     e_ident[EI_NIDENT]; /* ELF identifier */
   62:         UH     e_type;                     /* object file type */
   63:         UH     e_machine;          /* machine architecture */
   64:         UW     e_version;          /* file format & version */
   65:         VP     e_entry;            /* entry point address */
   66:         UW     e_phoff;            /* program header offset */
   67:         UW     e_shoff;            /* section header offset */
   68:         UW     e_flags;            /* processor-specific flags (EF_xxx) */
   69:         UH     e_ehsize;           /* ELF header size */
   70:         UH     e_phentsize;                /* program header entry size */
   71:         UH     e_phnum;            /* the number of program header entries */
   72:         UH     e_shentsize;                /* section header entry size */
   73:         UH     e_shnum;            /* the number of section header entries */
   74:         UH     e_shstrndx;         /* section header string index */
   75: } Elf32_Ehdr;
   76: 
   77: /* e_ident[] indices */
   78: #define EI_MAG0         0      /* magic number */
   79: #define EI_MAG1         1
   80: #define EI_MAG2         2
   81: #define EI_MAG3         3
   82: #define EI_CLASS        4      /* file class */
   83: #define EI_DATA         5      /* data encoding */
   84: #define EI_VERSION      6    /* ELF version */
   85: #define EI_PAD          7       /* reserved (0) */
   86: 
   87: /* EI_MAG */
   88: #define ELFMAG0         0x7f
   89: #define ELFMAG1         'E'
   90: #define ELFMAG2         'L'
   91: #define ELFMAG3         'F'
   92: 
   93: /* EI_CLASS */
   94: #define ELFCLASSNONE    0  /* invalid */
   95: #define ELFCLASS32      1    /* 32-bit object */
   96: #define ELFCLASS64      2    /* 64-bit object */
   97: 
   98: /* EI_DATA */
   99: #define ELFDATANONE     0   /* invalid */
  100: #define ELFDATA2LSB     1   /* two's complement, little endian */
  101: #define ELFDATA2MSB     2   /* two's complement, big endian */
  102: 
  103: /* e_type */
  104: #define ET_NONE         0      /* unknown */
  105: #define ET_REL          1       /* relocatable format */
  106: #define ET_EXEC         2      /* executable format */
  107: #define ET_DYN          3       /* shared object format */
  108: #define ET_CORE         4      /* core format */
  109: #define ET_LOPROC       0xff00        /* CPU-specific */
  110: #define ET_HIPROC       0xffff
  111: 
  112: /* e_machine */
  113: #define EM_NONE         0      /* unknown */
  114: #define EM_M32          1       /* AT&T WE 32100 */
  115: #define EM_SPARC        2      /* Sun SPARC */
  116: #define EM_386          3       /* Intel 80386 */
  117: #define EM_68K          4       /* Motorola 68000 */
  118: #define EM_88K          5       /* Motorola 88000 */
  119: #define EM_486          6       /* Intel 80486 */
  120: #define EM_860          7       /* Intel i860 */
  121: #define EM_MIPS         8      /* MIPS R3000 */
  122: #define EM_PPC          20      /* PowerPC */
  123: #define EM_V810         0x24   /* NEC V810 */
  124: #define EM_ARM          40      /* ARM */
  125: #define EM_SH           42       /* Hitachi SH */
  126: #define EM_NIOS2        0x71   /* Nios2 */
  127: 
  128: /* e_version, EI_VERSION */
  129: #define EV_NONE         0      /* unknown */
  130: #define EV_CURRENT      1    /* current version */
  131: 
  132: 
  133: /* Section header */
  134: typedef struct {
  135:         UW     sh_name;    /* section name index */
  136:         UW     sh_type;    /* section type (SHT_xxx) */
  137:         UW     sh_flags;   /* section flags (SHF_xxx) */
  138:         VP     sh_addr;    /* section address */
  139:         UW     sh_offset;  /* section offset */
  140:         UW     sh_size;    /* section size */
  141:         UW     sh_link;    /* section header table index link */
  142:         UW     sh_info;    /* extra information */
  143:         UW     sh_addralign;       /* alignment */
  144:         UW     sh_entsize; /* entry size */
  145: } Elf32_Shdr;
  146: 
  147: /* sh_name: special section indices */
  148: #define SHN_UNDEF       0     /* undefined */
  149: #define SHN_LORESERVE   0xff00    /* lower bound of the reserved indices range */
  150: #define SHN_ABS         0xfff1 /* absolute values */
  151: #define SHN_COMMON      0xfff2       /* common symbols */
  152: #define SHN_HIRESERVE   0xffff    /* upper bound of the reserved indices range */
  153: #define SHN_LOPROC      0xff00       /* CPU-specific */
  154: #define SHN_HIPROC      0xff1f
  155: 
  156: /* sh_type */
  157: #define SHT_NULL        0      /* invalid section */
  158: #define SHT_PROGBITS    1  /* program */
  159: #define SHT_SYMTAB      2    /* symbol table */
  160: #define SHT_STRTAB      3    /* string table */
  161: #define SHT_RELA        4      /* relocation information, w/ explicit addends */
  162: #define SHT_HASH        5      /* symbol hash table */
  163: #define SHT_DYNAMIC     6   /* dynamic-link information */
  164: #define SHT_NOTE        7      /* note */
  165: #define SHT_NOBITS      8    /* empty section */
  166: #define SHT_REL         9      /* relocation information, w/o explicit addends */
  167: #define SHT_SHLIB       10    /* (reserved) */
  168: #define SHT_DYNSYM      11   /* symbol table for dynamic links */
  169: #define SHT_LOUSER      0x80000000   /* application-specific */
  170: #define SHT_HIUSER      0xffffffff
  171: #define SHT_LOPROC      0x70000000   /* CPU-specific */
  172: #define SHT_HIPROC      0x7fffffff
  173: #define SHT_MIPS_REGINFO 0x70000006     /* MIPS-specific */
  174: 
  175: /* sh_flags */
  176: #define SHF_WRITE       0x1   /* writable during execution */
  177: #define SHF_ALLOC       0x2   /* occupied during execution */
  178: #define SHF_EXECINSTR   0x4       /* contains executable instructions */
  179: #define SHF_MASKPROC    0xf0000000 /* CPU-specific */
  180: 
  181: 
  182: /* Symbol table */
  183: typedef struct {
  184:         UW     st_name;    /* symbol name index */
  185:         VP     st_value;   /* value */
  186:         UW     st_size;    /* size */
  187:         UB     st_info;    /* symbol type & binding attributes */
  188:         UB     st_other;   /* reserved (0) */
  189:         UH     st_shndx;   /* section index */
  190: } Elf32_Sym;
  191: 
  192: #define STN_UNDEF       0     /* undefined */
  193: 
  194: /* st_info */
  195: #define ELF32_ST_BIND(info)             ((UB)(info) >> 4)
  196: #define ELF32_ST_TYPE(info)             ((UB)(info) & 0xf)
  197: #define ELF32_ST_INFO(bind, type)       (UB)(((bind) << 4) + ((type) & 0xf))
  198: 
  199: /* st_info : bind */
  200: #define STB_LOCAL       0     /* local symbol */
  201: #define STB_GLOBAL      1    /* global symbol */
  202: #define STB_WEAK        2      /* weak global symbol */
  203: #define STB_LOPROC      13   /* CPU-specific */
  204: #define STB_HIPROC      15
  205: 
  206: /* st_info : type */
  207: #define STT_NOTYPE      0    /* undefined */
  208: #define STT_OBJECT      1    /* data */
  209: #define STT_FUNC        2      /* function */
  210: #define STT_SECTION     3   /* section */
  211: #define STT_FILE        4      /* file name */
  212: #define STT_LOPROC      13   /* CPU-specific */
  213: #define STT_HIPROC      15
  214: 
  215: /* st_other */
  216: #define STO_MIPS16      0xf0 /* MIPS16 */
  217: 
  218: 
  219: /* Relocation */
  220: typedef struct {
  221:         VP     r_offset;   /* relocation target offset */
  222:         UW     r_info;             /* symbol number & relocation type */
  223: } Elf32_Rel;
  224: 
  225: typedef struct {
  226:         VP     r_offset;   /* relocation target offset */
  227:         UW     r_info;             /* symbol number & relocation type */
  228:         W      r_addend;    /* addend */
  229: } Elf32_Rela;
  230: 
  231: /* r_info */
  232: #define ELF32_R_SYM(info)       ((UW)(info)>>8)
  233: #define ELF32_R_TYPE(info)      ((UB)(info))
  234: #define ELF32_R_INFO(sym, type) (UW)(((sym) << 8) + (UB)(type))
  235: 
  236: /* i386 Relocation Type */
  237: #define R_386_NONE      0
  238: #define R_386_32        1
  239: #define R_386_PC32      2
  240: #define R_386_GOT32     3
  241: #define R_386_PLT32     4
  242: #define R_386_COPY      5
  243: #define R_386_GLOB_DAT  6
  244: #define R_386_JMP_SLOT  7
  245: #define R_386_RELATIVE  8
  246: #define R_386_GOTOFF    9
  247: #define R_386_GOTPC     10
  248: 
  249: /* SH Relocation Type */
  250: #define R_SH_NONE       0     /* No relocation */
  251: #define R_SH_DIR32      1    /* 32 bit absolute relocation */
  252: #define R_SH_REL32      2    /* 32 bit PC relative relocation */
  253: #define R_SH_DIR8WPN    3  /* 8 bit PC relative branch divided by 2 */
  254: #define R_SH_IND12W     4   /* 12 bit PC relative branch divided by 2 */
  255: #define R_SH_DIR8WPL    5  /* 8 bit unsigned PC relative divided by 4 */
  256: #define R_SH_DIR8WPZ    6  /* 8 bit unsigned PC relative divided by 2 */
  257: #define R_SH_DIR8BP     7   /* 8 bit GBR relative */
  258: #define R_SH_DIR8W      8    /* 8 bit GBR relative divided by 2 */
  259: #define R_SH_DIR8L      9    /* 8 bit GBR relative divided by 4 */
  260: #define R_SH_GOT32      160
  261: #define R_SH_PLT32      161
  262: #define R_SH_COPY       162
  263: #define R_SH_GLOB_DAT   163
  264: #define R_SH_JMP_SLOT   164
  265: #define R_SH_RELATIVE   165
  266: 
  267: /* ARM Relocation Type */
  268: #define R_ARM_NONE      0
  269: #define R_ARM_PC24      1
  270: #define R_ARM_ABS32     2
  271: #define R_ARM_REL32     3
  272: #define R_ARM_THM_PC22  10
  273: #define R_ARM_COPY      20   /* Copy symbol at runtime.  */
  274: #define R_ARM_GLOB_DAT  21       /* Create GOT entry.  */
  275: #define R_ARM_JUMP_SLOT 22      /* Create PLT entry.  */
  276: #define R_ARM_RELATIVE  23       /* Adjust by program base.  */
  277: #define R_ARM_GOTOFF    24 /* 32 bit offset to GOT.  */
  278: #define R_ARM_GOTPC     25  /* 32 bit PC relative offset to GOT.  */
  279: #define R_ARM_GOT32     26  /* 32 bit GOT entry.  */
  280: #define R_ARM_PLT32     27  /* 32 bit PLT address.  */
  281: 
  282: /* MIPS Relocation Type */
  283: #define R_MIPS_NONE     0
  284: #define R_MIPS_16       1
  285: #define R_MIPS_32       2
  286: #define R_MIPS_REL32    3
  287: #define R_MIPS_26       4
  288: #define R_MIPS_HI16     5
  289: #define R_MIPS_LO16     6
  290: #define R_MIPS_GPREL16  7
  291: #define R_MIPS_LITERAL  8
  292: #define R_MIPS16_26     100
  293: #define R_MIPS16_GPREL  101
  294: 
  295: /* PowerPC Relocations Type */
  296: #define R_PPC_NONE              0
  297: #define R_PPC_ADDR32            1
  298: #define R_PPC_ADDR24            2
  299: #define R_PPC_ADDR16            3
  300: #define R_PPC_ADDR16_LO         4
  301: #define R_PPC_ADDR16_HI         5
  302: #define R_PPC_ADDR16_HA         6
  303: #define R_PPC_ADDR14            7
  304: #define R_PPC_ADDR14_BRTAKEN    8
  305: #define R_PPC_ADDR14_BRNTAKEN   9
  306: #define R_PPC_REL24             10
  307: #define R_PPC_REL14             11
  308: #define R_PPC_REL14_BRTAKEN     12
  309: #define R_PPC_REL14_BRNTAKEN    13
  310: #define R_PPC_GOT16             14
  311: #define R_PPC_GOT16_LO          15
  312: #define R_PPC_GOT16_HI          16
  313: #define R_PPC_GOT16_HA          17
  314: #define R_PPC_PLTREL24          18
  315: #define R_PPC_COPY              19
  316: #define R_PPC_GLOB_DAT          20
  317: #define R_PPC_JMP_SLOT          21
  318: #define R_PPC_RELATIVE          22
  319: #define R_PPC_LOCAL24PC         23
  320: #define R_PPC_UADDR32           24
  321: #define R_PPC_UADDR16           25
  322: #define R_PPC_REL32             26
  323: #define R_PPC_PLT32             27
  324: #define R_PPC_PLTREL32          28
  325: #define R_PPC_PLT16_LO          29
  326: #define R_PPC_PLT16_HI          30
  327: #define R_PPC_PLT16_HA          31
  328: #define R_PPC_SDAREL16          32
  329: #define R_PPC_SECTOFF           33
  330: #define R_PPC_SECTOFF_LO        34
  331: #define R_PPC_SECTOFF_HI        35
  332: #define R_PPC_SECTOFF_HA        36
  333: #define R_PPC_ADDR30            37
  334: 
  335: 
  336: /* Program header */
  337: typedef struct {
  338:         UW     p_type;             /* segment type */
  339:         UW     p_offset;   /* segment offset */
  340:         VP     p_vaddr;    /* virtual address */
  341:         VP     p_paddr;    /* physical address */
  342:         UW     p_filesz;   /* segment size in file image */
  343:         UW     p_memsz;    /* segment size on memory */
  344:         UW     p_flags;    /* flags (PF_xxx) */
  345:         UW     p_align;    /* alignment */
  346: } Elf32_Phdr;
  347: 
  348: /* p_type */
  349: #define PT_NULL         0      /* invalid segment */
  350: #define PT_LOAD         1      /* loadable segment */
  351: #define PT_DYNAMIC      2    /* dynamic link information */
  352: #define PT_INTERP       3     /* interpreter path name */
  353: #define PT_NOTE         4      /* auxiliary information */
  354: #define PT_SHLIB        5      /* (reserved) */
  355: #define PT_PHDR         6      /* program header */
  356: #define PT_LOPROC       0x70000000    /* CPU-specific */
  357: #define PT_HIPROC       0x7fffffff
  358: 
  359: /* p_flags */
  360: #define PF_R            0x4       /* Read */
  361: #define PF_W            0x2       /* Write */
  362: #define PF_X            0x1       /* Execute */
  363: #define PF_MASKPROC     0xf0000000  /* CPU-specific */
  364: 
  365: 
  366: /* Dynamic structure */
  367: typedef struct {
  368:         W      d_tag;               /* type */
  369:         union {
  370:                 UW    d_val;     /* value */
  371:                 VP    d_ptr;     /* virtual address */
  372:         } d_un;
  373: } Elf32_Dyn;
  374: 
  375: IMPORT  Elf32_Dyn        _DYNAMIC[];
  376: 
  377: /* d_tag  I:ignored V:d_val P:d_ptr */
  378: #define DT_NULL         0      /* I: _DYNAMIC array terminal mark */
  379: #define DT_NEEDED       1     /* V: needed library */
  380: #define DT_PLTRELSZ     2   /* V: PLT size */
  381: #define DT_PLTGOT       3     /* P: PLT and/or GOT */
  382: #define DT_HASH         4      /* P: symbol hash table */
  383: #define DT_STRTAB       5     /* P: string table */
  384: #define DT_SYMTAB       6     /* P: symbol table */
  385: #define DT_RELA         7      /* P: relocation table (Elf32_Rela) */
  386: #define DT_RELASZ       8     /* V: total size of DT_RELA relocation table */
  387: #define DT_RELAENT      9    /* V: size of DT_RELA entry */
  388: #define DT_STRSZ        10     /* V: total size of string table */
  389: #define DT_SYMENT       11    /* V: size of symbol table entry */
  390: #define DT_INIT         12     /* P: initialization function address */
  391: #define DT_FINI         13     /* P: termination function address */
  392: #define DT_SONAME       14    /* V: shared object name */
  393: #define DT_RPATH        15     /* V: library search path */
  394: #define DT_SYMBOLIC     16  /* I: symbol resolution algorithm switch */
  395: #define DT_REL          17      /* P: relocation table (Elf32_Rel) */
  396: #define DT_RELSZ        18     /* V: total size of DT_REL relocation table */
  397: #define DT_RELENT       19    /* V: size of DT_REL entry */
  398: #define DT_PLTREL       20    /* V: PLT type (REL/RELA) */
  399: #define DT_DEBUG        21     /* P: debug */
  400: #define DT_TEXTREL      22   /* I: text relocation */
  401: #define DT_JMPREL       23    /* P: PLTREL relocation entries for lazy binding */
  402: #define DT_LOPROC       0x70000000    /* CPU-specific */
  403: #define DT_HIPROC       0x7fffffff
  404: 
  405: /* For MIPS */
  406: #define DT_MIPS_LOCAL_GOTNO     0x7000000a  /* Number of local GOT entries */
  407: #define DT_MIPS_SYMTABNO        0x70000011  /* Number of DYNSYM entries */
  408: #define DT_MIPS_GOTSYM          0x70000013  /* First GOT entry */
  409: 
  410: /* Global offset table */
  411: IMPORT  VP       _GLOBAL_OFFSET_TABLE_[];
  412: 
  413: /* Hashing function */
  414: Inline UW Elf_hash( const UB *name )
  415: {
  416:         UW     h = 0, g;
  417: 
  418:         while ( *name != '\0' ) {
  419:                 h = (h << 4) + *name++;
  420:                 g = h & 0xf0000000;
  421:                 h = (h ^ (g >> 24)) & ~g;
  422:         }
  423: 
  424:         return h;
  425: }
  426: 
  427: /* Register Information */
  428: typedef struct {
  429:         UW     ri_gprmask; /* generic registers used */
  430:         UW     ri_cprmask[4];      /* coprocessor registers used */
  431:         W      ri_gp_value; /* gp register value */
  432: } Elf32_RegInfo;
  433: 
  434: #endif /* _T2EX_LOAD_ELF_ */