• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Return the size of an object file type.
2    Copyright (C) 1998-2010, 2015 Red Hat, Inc.
3    This file is part of elfutils.
4    Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5 
6    This file is free software; you can redistribute it and/or modify
7    it under the terms of either
8 
9      * the GNU Lesser General Public License as published by the Free
10        Software Foundation; either version 3 of the License, or (at
11        your option) any later version
12 
13    or
14 
15      * the GNU General Public License as published by the Free
16        Software Foundation; either version 2 of the License, or (at
17        your option) any later version
18 
19    or both in parallel, as here.
20 
21    elfutils is distributed in the hope that it will be useful, but
22    WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24    General Public License for more details.
25 
26    You should have received copies of the GNU General Public License and
27    the GNU Lesser General Public License along with this program.  If
28    not, see <http://www.gnu.org/licenses/>.  */
29 
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33 
34 #include <gelf.h>
35 #include <stddef.h>
36 
37 #include "libelfP.h"
38 
39 
40 /* These are the sizes for all the known types.  */
41 const size_t __libelf_type_sizes[EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] =
42 {
43   /* We have no entry for EV_NONE since we have to set an error.  */
44   [EV_CURRENT - 1] = {
45     [ELFCLASS32 - 1] = {
46 #define TYPE_SIZES(LIBELFBITS) \
47       [ELF_T_ADDR]	= ELFW2(LIBELFBITS, FSZ_ADDR),			      \
48       [ELF_T_OFF]	= ELFW2(LIBELFBITS, FSZ_OFF),			      \
49       [ELF_T_BYTE]	= 1,						      \
50       [ELF_T_HALF]	= ELFW2(LIBELFBITS, FSZ_HALF),			      \
51       [ELF_T_WORD]	= ELFW2(LIBELFBITS, FSZ_WORD),			      \
52       [ELF_T_SWORD]	= ELFW2(LIBELFBITS, FSZ_SWORD),			      \
53       [ELF_T_XWORD]	= ELFW2(LIBELFBITS, FSZ_XWORD),			      \
54       [ELF_T_SXWORD]	= ELFW2(LIBELFBITS, FSZ_SXWORD),		      \
55       [ELF_T_EHDR]	= sizeof (ElfW2(LIBELFBITS, Ext_Ehdr)),		      \
56       [ELF_T_SHDR]	= sizeof (ElfW2(LIBELFBITS, Ext_Shdr)),		      \
57       [ELF_T_SYM]	= sizeof (ElfW2(LIBELFBITS, Ext_Sym)),		      \
58       [ELF_T_REL]	= sizeof (ElfW2(LIBELFBITS, Ext_Rel)),		      \
59       [ELF_T_RELA]	= sizeof (ElfW2(LIBELFBITS, Ext_Rela)),		      \
60       [ELF_T_PHDR]	= sizeof (ElfW2(LIBELFBITS, Ext_Phdr)),		      \
61       [ELF_T_DYN]	= sizeof (ElfW2(LIBELFBITS, Ext_Dyn)),		      \
62       [ELF_T_VDEF]	= sizeof (ElfW2(LIBELFBITS, Ext_Verdef)),	      \
63       [ELF_T_VDAUX]	= sizeof (ElfW2(LIBELFBITS, Ext_Verdaux)),	      \
64       [ELF_T_VNEED]	= sizeof (ElfW2(LIBELFBITS, Ext_Verneed)),	      \
65       [ELF_T_VNAUX]	= sizeof (ElfW2(LIBELFBITS, Ext_Vernaux)),	      \
66       [ELF_T_NHDR]	= sizeof (ElfW2(LIBELFBITS, Ext_Nhdr)),		      \
67       [ELF_T_SYMINFO]	= sizeof (ElfW2(LIBELFBITS, Ext_Syminfo)),	      \
68       [ELF_T_MOVE]	= sizeof (ElfW2(LIBELFBITS, Ext_Move)),		      \
69       [ELF_T_LIB]	= sizeof (ElfW2(LIBELFBITS, Ext_Lib)),		      \
70       [ELF_T_AUXV]	= sizeof (ElfW2(LIBELFBITS, Ext_auxv_t)),	      \
71       [ELF_T_CHDR]	= sizeof (ElfW2(LIBELFBITS, Ext_Chdr)),		      \
72       [ELF_T_GNUHASH]	= ELFW2(LIBELFBITS, FSZ_WORD)
73       TYPE_SIZES (32)
74     },
75     [ELFCLASS64 - 1] = {
76       TYPE_SIZES (64)
77     }
78   }
79 };
80 
81 
82 size_t
gelf_fsize(Elf * elf,Elf_Type type,size_t count,unsigned int version)83 gelf_fsize (Elf *elf, Elf_Type type, size_t count, unsigned int version)
84 {
85   /* We do not have differences between file and memory sizes.  Better
86      not since otherwise `mmap' would not work.  */
87   if (elf == NULL)
88     return 0;
89 
90   if (version == EV_NONE || version >= EV_NUM)
91     {
92       __libelf_seterrno (ELF_E_UNKNOWN_VERSION);
93       return 0;
94     }
95 
96   if (type >= ELF_T_NUM)
97     {
98       __libelf_seterrno (ELF_E_UNKNOWN_TYPE);
99       return 0;
100     }
101 
102 #if EV_NUM != 2
103   return count * __libelf_type_sizes[version - 1][elf->class - 1][type];
104 #else
105   return count * __libelf_type_sizes[0][elf->class - 1][type];
106 #endif
107 }
108 INTDEF(gelf_fsize)
109