tools/elf4rom/libs/libelf-0.8.10/lib/32.fsize.c
changeset 34 92d87f2e53c2
equal deleted inserted replaced
33:1af5c1be89f8 34:92d87f2e53c2
       
     1 /*
       
     2 32.fsize.c - implementation of the elf{32,64}_fsize(3) functions.
       
     3 Copyright (C) 1995 - 2001 Michael Riepe
       
     4 
       
     5 This library is free software; you can redistribute it and/or
       
     6 modify it under the terms of the GNU Library General Public
       
     7 License as published by the Free Software Foundation; either
       
     8 version 2 of the License, or (at your option) any later version.
       
     9 
       
    10 This library is distributed in the hope that it will be useful,
       
    11 but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13 Library General Public License for more details.
       
    14 
       
    15 You should have received a copy of the GNU Library General Public
       
    16 License along with this library; if not, write to the Free Software
       
    17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18 */
       
    19 
       
    20 #include <private.h>
       
    21 #include <ext_types.h>
       
    22 
       
    23 #ifndef lint
       
    24 static const char rcsid[] = "@(#) $Id: 32.fsize.c,v 1.12 2005/05/21 15:39:19 michael Exp $";
       
    25 #endif /* lint */
       
    26 
       
    27 const size_t
       
    28 _elf_fmsize[2][EV_CURRENT - EV_NONE][ELF_T_NUM][2] = {
       
    29     /* ELFCLASS32 */
       
    30     {
       
    31 	/* version 1 */
       
    32 	{
       
    33 	    { sizeof(unsigned char),  sizeof(unsigned char)      },
       
    34 	    { sizeof(Elf32_Addr),     sizeof(__ext_Elf32_Addr)   },
       
    35 	    { sizeof(Elf32_Dyn),      sizeof(__ext_Elf32_Dyn)    },
       
    36 	    { sizeof(Elf32_Ehdr),     sizeof(__ext_Elf32_Ehdr)   },
       
    37 	    { sizeof(Elf32_Half),     sizeof(__ext_Elf32_Half)   },
       
    38 	    { sizeof(Elf32_Off),      sizeof(__ext_Elf32_Off)    },
       
    39 	    { sizeof(Elf32_Phdr),     sizeof(__ext_Elf32_Phdr)   },
       
    40 	    { sizeof(Elf32_Rela),     sizeof(__ext_Elf32_Rela)   },
       
    41 	    { sizeof(Elf32_Rel),      sizeof(__ext_Elf32_Rel)    },
       
    42 	    { sizeof(Elf32_Shdr),     sizeof(__ext_Elf32_Shdr)   },
       
    43 	    { sizeof(Elf32_Sword),    sizeof(__ext_Elf32_Sword)  },
       
    44 	    { sizeof(Elf32_Sym),      sizeof(__ext_Elf32_Sym)    },
       
    45 	    { sizeof(Elf32_Word),     sizeof(__ext_Elf32_Word)   },
       
    46 	    { 0, 0 },	/* there is no Elf32_Sxword */
       
    47 	    { 0, 0 },	/* there is no Elf32_Xword */
       
    48 	    /* XXX: check Solaris values */
       
    49 	    { 0, 0 },	/* Elf32_Verdef/Verdaux size varies */
       
    50 	    { 0, 0 },	/* Elf32_Verneed/Vernaux size varies */
       
    51 	},
       
    52     },
       
    53 #if __LIBELF64
       
    54     /* ELFCLASS64 */
       
    55     {
       
    56 	/* version 1 */
       
    57 	{
       
    58 	    { sizeof(unsigned char),  sizeof(unsigned char)      },
       
    59 	    { sizeof(Elf64_Addr),     sizeof(__ext_Elf64_Addr)   },
       
    60 	    { sizeof(Elf64_Dyn),      sizeof(__ext_Elf64_Dyn)    },
       
    61 	    { sizeof(Elf64_Ehdr),     sizeof(__ext_Elf64_Ehdr)   },
       
    62 	    { sizeof(Elf64_Half),     sizeof(__ext_Elf64_Half)   },
       
    63 	    { sizeof(Elf64_Off),      sizeof(__ext_Elf64_Off)    },
       
    64 	    { sizeof(Elf64_Phdr),     sizeof(__ext_Elf64_Phdr)   },
       
    65 	    { sizeof(Elf64_Rela),     sizeof(__ext_Elf64_Rela)   },
       
    66 	    { sizeof(Elf64_Rel),      sizeof(__ext_Elf64_Rel)    },
       
    67 	    { sizeof(Elf64_Shdr),     sizeof(__ext_Elf64_Shdr)   },
       
    68 	    { sizeof(Elf64_Sword),    sizeof(__ext_Elf64_Sword)  },
       
    69 	    { sizeof(Elf64_Sym),      sizeof(__ext_Elf64_Sym)    },
       
    70 	    { sizeof(Elf64_Word),     sizeof(__ext_Elf64_Word)   },
       
    71 	    { sizeof(Elf64_Sxword),   sizeof(__ext_Elf64_Sxword) },
       
    72 	    { sizeof(Elf64_Xword),    sizeof(__ext_Elf64_Xword)  },
       
    73 	    /* XXX: check Solaris values */
       
    74 	    { 0, 0 },	/* Elf64_Verdef/Verdaux size varies */
       
    75 	    { 0, 0 },	/* Elf64_Verneed/Vernaux size varies */
       
    76 	},
       
    77     },
       
    78 #endif /* __LIBELF64 */
       
    79 };
       
    80 
       
    81 static size_t
       
    82 _elf_fsize(unsigned cls, Elf_Type type, unsigned ver) {
       
    83     size_t n = 0;
       
    84 
       
    85     if (!valid_version(ver)) {
       
    86 	seterr(ERROR_UNKNOWN_VERSION);
       
    87     }
       
    88     else if (!valid_type(type)) {
       
    89 	seterr(ERROR_UNKNOWN_TYPE);
       
    90     }
       
    91     else if (!(n = _fsize(cls, ver, type))) {
       
    92 	seterr(ERROR_UNKNOWN_TYPE);
       
    93     }
       
    94     return n;
       
    95 }
       
    96 
       
    97 size_t
       
    98 elf32_fsize(Elf_Type type, size_t count, unsigned ver) {
       
    99     return count * _elf_fsize(ELFCLASS32, type, ver);
       
   100 }
       
   101 
       
   102 #if __LIBELF64
       
   103 
       
   104 size_t
       
   105 elf64_fsize(Elf_Type type, size_t count, unsigned ver) {
       
   106     return count * _elf_fsize(ELFCLASS64, type, ver);
       
   107 }
       
   108 
       
   109 size_t
       
   110 gelf_fsize(Elf *elf, Elf_Type type, size_t count, unsigned ver) {
       
   111     if (elf) {
       
   112 	if (elf->e_kind != ELF_K_ELF) {
       
   113 	    seterr(ERROR_NOTELF);
       
   114 	}
       
   115 	else if (valid_class(elf->e_class)) {
       
   116 	    return count * _elf_fsize(elf->e_class, type, ver);
       
   117 	}
       
   118 	else {
       
   119 	    seterr(ERROR_UNKNOWN_CLASS);
       
   120 	}
       
   121     }
       
   122     return 0;
       
   123 }
       
   124 
       
   125 /*
       
   126  * Extension: report memory size
       
   127  */
       
   128 size_t
       
   129 gelf_msize(Elf *elf, Elf_Type type, size_t count, unsigned ver) {
       
   130     size_t n;
       
   131 
       
   132     if (elf) {
       
   133 	if (elf->e_kind != ELF_K_ELF) {
       
   134 	    seterr(ERROR_NOTELF);
       
   135 	}
       
   136 	else if (!valid_class(elf->e_class)) {
       
   137 	    seterr(ERROR_UNKNOWN_CLASS);
       
   138 	}
       
   139 	else if (!valid_version(ver)) {
       
   140 	    seterr(ERROR_UNKNOWN_VERSION);
       
   141 	}
       
   142 	else if (!valid_type(type)) {
       
   143 	    seterr(ERROR_UNKNOWN_TYPE);
       
   144 	}
       
   145 	else if (!(n = _msize(elf->e_class, ver, type))) {
       
   146 	    seterr(ERROR_UNKNOWN_TYPE);
       
   147 	}
       
   148 	else {
       
   149 	    return count * n;
       
   150 	}
       
   151     }
       
   152     return 0;
       
   153 }
       
   154 
       
   155 #endif /* __LIBELF64 */