Branch data Line data Source code
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[ELFCLASSNUM - 1][ELF_T_NUM] =
42 : : {
43 : : [ELFCLASS32 - 1] = {
44 : : #define TYPE_SIZES(LIBELFBITS) \
45 : : [ELF_T_ADDR] = ELFW2(LIBELFBITS, FSZ_ADDR), \
46 : : [ELF_T_OFF] = ELFW2(LIBELFBITS, FSZ_OFF), \
47 : : [ELF_T_BYTE] = 1, \
48 : : [ELF_T_HALF] = ELFW2(LIBELFBITS, FSZ_HALF), \
49 : : [ELF_T_WORD] = ELFW2(LIBELFBITS, FSZ_WORD), \
50 : : [ELF_T_SWORD] = ELFW2(LIBELFBITS, FSZ_SWORD), \
51 : : [ELF_T_XWORD] = ELFW2(LIBELFBITS, FSZ_XWORD), \
52 : : [ELF_T_SXWORD] = ELFW2(LIBELFBITS, FSZ_SXWORD), \
53 : : [ELF_T_EHDR] = sizeof (ElfW2(LIBELFBITS, Ext_Ehdr)), \
54 : : [ELF_T_SHDR] = sizeof (ElfW2(LIBELFBITS, Ext_Shdr)), \
55 : : [ELF_T_SYM] = sizeof (ElfW2(LIBELFBITS, Ext_Sym)), \
56 : : [ELF_T_REL] = sizeof (ElfW2(LIBELFBITS, Ext_Rel)), \
57 : : [ELF_T_RELA] = sizeof (ElfW2(LIBELFBITS, Ext_Rela)), \
58 : : [ELF_T_PHDR] = sizeof (ElfW2(LIBELFBITS, Ext_Phdr)), \
59 : : [ELF_T_DYN] = sizeof (ElfW2(LIBELFBITS, Ext_Dyn)), \
60 : : [ELF_T_VDEF] = sizeof (ElfW2(LIBELFBITS, Ext_Verdef)), \
61 : : [ELF_T_VDAUX] = sizeof (ElfW2(LIBELFBITS, Ext_Verdaux)), \
62 : : [ELF_T_VNEED] = sizeof (ElfW2(LIBELFBITS, Ext_Verneed)), \
63 : : [ELF_T_VNAUX] = sizeof (ElfW2(LIBELFBITS, Ext_Vernaux)), \
64 : : [ELF_T_NHDR] = sizeof (ElfW2(LIBELFBITS, Ext_Nhdr)), \
65 : : /* Note the header size is the same, but padding is different. */ \
66 : : [ELF_T_NHDR8] = 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 : : [ELF_T_RELR] = ELFW2(LIBELFBITS, FSZ_RELR)
74 : : TYPE_SIZES (32)
75 : : },
76 : : [ELFCLASS64 - 1] = {
77 : : TYPE_SIZES (64)
78 : : }
79 : : };
80 : :
81 : :
82 : : size_t
83 : 358964 : 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 [ - + ]: 358964 : if (elf == NULL)
88 : : return 0;
89 : :
90 [ - + ]: 358964 : if (version != EV_CURRENT)
91 : : {
92 : 0 : __libelf_seterrno (ELF_E_UNKNOWN_VERSION);
93 : 0 : return 0;
94 : : }
95 : :
96 [ - + ]: 358964 : if (type >= ELF_T_NUM)
97 : : {
98 : 0 : __libelf_seterrno (ELF_E_UNKNOWN_TYPE);
99 : 0 : return 0;
100 : : }
101 : :
102 : 358964 : return count * __libelf_type_sizes[elf->class - 1][type];
103 : : }
104 : : INTDEF(gelf_fsize)
|