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