summaryrefslogtreecommitdiff
path: root/libelf/lib/32.fsize.c
blob: 2845dd212b61bc404237b522a1c17cdf30ef69ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
32.fsize.c - implementation of the elf{32,64}_fsize(3) functions.
Copyright (C) 1995 - 2001 Michael Riepe <michael@stud.uni-hannover.de>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <private.h>
#include <ext_types.h>

#ifndef lint
static const char rcsid[] = "@(#) Id: 32.fsize.c,v 1.9 2001/10/05 19:05:25 michael Exp ";
#endif /* lint */

/*@-initsize@*/
const size_t
_elf_fmsize[2][EV_CURRENT - EV_NONE][ELF_T_NUM][2] = {
    /* ELFCLASS32 */
    {
	/* version 1 */
	{
	    { sizeof(unsigned char),  sizeof(unsigned char)      },
	    { sizeof(Elf32_Addr),     sizeof(__ext_Elf32_Addr)   },
	    { sizeof(Elf32_Dyn),      sizeof(__ext_Elf32_Dyn)    },
	    { sizeof(Elf32_Ehdr),     sizeof(__ext_Elf32_Ehdr)   },
	    { sizeof(Elf32_Half),     sizeof(__ext_Elf32_Half)   },
	    { sizeof(Elf32_Off),      sizeof(__ext_Elf32_Off)    },
	    { sizeof(Elf32_Phdr),     sizeof(__ext_Elf32_Phdr)   },
	    { sizeof(Elf32_Rela),     sizeof(__ext_Elf32_Rela)   },
	    { sizeof(Elf32_Rel),      sizeof(__ext_Elf32_Rel)    },
	    { sizeof(Elf32_Shdr),     sizeof(__ext_Elf32_Shdr)   },
	    { sizeof(Elf32_Sword),    sizeof(__ext_Elf32_Sword)  },
	    { sizeof(Elf32_Sym),      sizeof(__ext_Elf32_Sym)    },
	    { sizeof(Elf32_Word),     sizeof(__ext_Elf32_Word)   },
	    { 0, 0 },	/* there is no Elf32_Sxword */
	    { 0, 0 },	/* there is no Elf32_Xword */
	    /* XXX: check Solaris values */
	    { 0, 0 },	/* Elf32_Verdef/Verdaux size varies */
	    { 0, 0 },	/* Elf32_Verneed/Vernaux size varies */
	},
    },
#if __LIBELF64
    /* ELFCLASS64 */
    {
	/* version 1 */
	{
	    { sizeof(unsigned char),  sizeof(unsigned char)      },
	    { sizeof(Elf64_Addr),     sizeof(__ext_Elf64_Addr)   },
	    { sizeof(Elf64_Dyn),      sizeof(__ext_Elf64_Dyn)    },
	    { sizeof(Elf64_Ehdr),     sizeof(__ext_Elf64_Ehdr)   },
	    { sizeof(Elf64_Half),     sizeof(__ext_Elf64_Half)   },
	    { sizeof(Elf64_Off),      sizeof(__ext_Elf64_Off)    },
	    { sizeof(Elf64_Phdr),     sizeof(__ext_Elf64_Phdr)   },
	    { sizeof(Elf64_Rela),     sizeof(__ext_Elf64_Rela)   },
	    { sizeof(Elf64_Rel),      sizeof(__ext_Elf64_Rel)    },
	    { sizeof(Elf64_Shdr),     sizeof(__ext_Elf64_Shdr)   },
	    { sizeof(Elf64_Sword),    sizeof(__ext_Elf64_Sword)  },
	    { sizeof(Elf64_Sym),      sizeof(__ext_Elf64_Sym)    },
	    { sizeof(Elf64_Word),     sizeof(__ext_Elf64_Word)   },
	    { sizeof(Elf64_Sxword),   sizeof(__ext_Elf64_Sxword) },
	    { sizeof(Elf64_Xword),    sizeof(__ext_Elf64_Xword)  },
	    /* XXX: check Solaris values */
	    { 0, 0 },	/* Elf64_Verdef/Verdaux size varies */
	    { 0, 0 },	/* Elf64_Verneed/Vernaux size varies */
	},
    },
#endif /* __LIBELF64 */
};
/*@=initsize@*/

static size_t
_elf_fsize(unsigned cls, Elf_Type type, unsigned ver)
	/*@globals _elf_errno @*/
	/*@modifies _elf_errno @*/
{
    size_t n = 0;

    if (!valid_version(ver)) {
	seterr(ERROR_UNKNOWN_VERSION);
    }
    else if (!valid_type(type)) {
	seterr(ERROR_UNKNOWN_TYPE);
    }
/*@-boundsread@*/
    else if (!(n = _fsize(cls, ver, type))) {
	seterr(ERROR_UNKNOWN_TYPE);
    }
/*@=boundsread@*/
    return n;
}

size_t
elf32_fsize(Elf_Type type, size_t count, unsigned ver) {
    return count * _elf_fsize(ELFCLASS32, type, ver);
}

#if __LIBELF64

size_t
elf64_fsize(Elf_Type type, size_t count, unsigned ver) {
    return count * _elf_fsize(ELFCLASS64, type, ver);
}

size_t
gelf_fsize(Elf *elf, Elf_Type type, size_t count, unsigned ver) {
    if (elf) {
	if (elf->e_kind != ELF_K_ELF) {
	    seterr(ERROR_NOTELF);
	}
	else if (valid_class(elf->e_class)) {
	    return count * _elf_fsize(elf->e_class, type, ver);
	}
	else {
	    seterr(ERROR_UNKNOWN_CLASS);
	}
    }
    return 0;
}

/*
 * Extension: report memory size
 */
size_t
gelf_msize(Elf *elf, Elf_Type type, size_t count, unsigned ver) {
    size_t n;

    if (elf) {
	if (elf->e_kind != ELF_K_ELF) {
	    seterr(ERROR_NOTELF);
	}
	else if (!valid_class(elf->e_class)) {
	    seterr(ERROR_UNKNOWN_CLASS);
	}
	else if (!valid_version(ver)) {
	    seterr(ERROR_UNKNOWN_VERSION);
	}
	else if (!valid_type(type)) {
	    seterr(ERROR_UNKNOWN_TYPE);
	}
/*@-boundsread@*/
	else if (!(n = _msize(elf->e_class, ver, type))) {
	    seterr(ERROR_UNKNOWN_TYPE);
	}
/*@=boundsread@*/
	else {
	    return count * n;
	}
    }
    return 0;
}

#endif /* __LIBELF64 */