libelf_fsize.m4 revision 302408
191041Sobrien/*-
278828Sobrien * Copyright (c) 2006,2008-2011 Joseph Koshy
333965Sjdp * All rights reserved.
433965Sjdp *
533965Sjdp * Redistribution and use in source and binary forms, with or without
633965Sjdp * modification, are permitted provided that the following conditions
733965Sjdp * are met:
833965Sjdp * 1. Redistributions of source code must retain the above copyright
933965Sjdp *    notice, this list of conditions and the following disclaimer.
1033965Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1133965Sjdp *    notice, this list of conditions and the following disclaimer in the
1233965Sjdp *    documentation and/or other materials provided with the distribution.
1333965Sjdp *
1433965Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1533965Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1633965Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1733965Sjdp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1833965Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1933965Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2033965Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2133965Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2233965Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2333965Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2433965Sjdp * SUCH DAMAGE.
2533965Sjdp */
2633965Sjdp
2733965Sjdp#include <libelf.h>
2833965Sjdp
2933965Sjdp#include "_libelf.h"
3091041Sobrien
3138889SjdpELFTC_VCSID("$Id: libelf_fsize.m4 2225 2011-11-26 18:55:54Z jkoshy $");
3238889Sjdp
3391041Sobrien/* WARNING: GENERATED FROM __file__. */
3433965Sjdp
3560484Sobrien/*
3633965Sjdp * Create an array of file sizes from the elf_type definitions
3733965Sjdp */
3860484Sobrien
3960484Sobriendivert(-1)
4033965Sjdpinclude(SRCDIR`/elf_types.m4')
4177298Sobrien
4233965Sjdp/*
43130561Sobrien * Translations from structure definitions to the size of their file
4438889Sjdp * representations.
4577298Sobrien */
4633965Sjdp
4789857Sobrien/* `Basic' types. */
4860484Sobriendefine(`BYTE_SIZE',	1)
4960484Sobriendefine(`IDENT_SIZE',	`EI_NIDENT')
5089857Sobrien
51130561Sobrien/* Types that have variable length. */
5289857Sobriendefine(`GNUHASH_SIZE',	1)
5360484Sobriendefine(`NOTE_SIZE',	1)
5489857Sobriendefine(`VDEF_SIZE',	1)
5560484Sobriendefine(`VNEED_SIZE',	1)
5633965Sjdp
57130561Sobrien/* Currently unimplemented types. */
5860484Sobriendefine(`MOVEP_SIZE',	0)
5933965Sjdp
6033965Sjdp/* Overrides for 32 bit types that do not exist. */
61130561Sobriendefine(`XWORD_SIZE32',	0)
6260484Sobriendefine(`SXWORD_SIZE32',	0)
6333965Sjdp
6433965Sjdp/*
6533965Sjdp * FSZ{32,64} define the sizes of 32 and 64 bit file structures respectively.
6633965Sjdp */
6733965Sjdp
6833965Sjdpdefine(`FSZ32',`_FSZ32($1_DEF)')
6933965Sjdpdefine(`_FSZ32',
7033965Sjdp  `ifelse($#,1,0,
7133965Sjdp    `_BSZ32($1)+_FSZ32(shift($@))')')
7233965Sjdpdefine(`_BSZ32',`$2_SIZE32')
7333965Sjdp
7433965Sjdpdefine(`FSZ64',`_FSZ64($1_DEF)')
7533965Sjdpdefine(`_FSZ64',
7633965Sjdp  `ifelse($#,1,0,
7733965Sjdp    `_BSZ64($1)+_FSZ64(shift($@))')')
7833965Sjdpdefine(`_BSZ64',`$2_SIZE64')
7933965Sjdp
8033965Sjdp/*
8133965Sjdp * DEFINE_ELF_FSIZES(TYPE,NAME)
8233965Sjdp *
8333965Sjdp * Shorthand for defining  for 32 and 64 versions
8433965Sjdp * of elf type TYPE.
8533965Sjdp *
8633965Sjdp * If TYPE`'_SIZE is defined, use its value for both 32 bit and 64 bit
8733965Sjdp * sizes.
88 *
89 * Otherwise, look for a explicit 32/64 bit size definition for TYPE,
90 * TYPE`'_SIZE32 or TYPE`'_SIZE64. If this definition is present, there
91 * is nothing further to do.
92 *
93 * Otherwise, if an Elf{32,64}_`'NAME structure definition is known,
94 * compute an expression that adds up the sizes of the structure's
95 * constituents.
96 *
97 * If such a structure definition is not known, treat TYPE as a primitive
98 * (i.e., integral) type and use sizeof(Elf{32,64}_`'NAME) to get its
99 * file representation size.
100 */
101
102define(`DEFINE_ELF_FSIZE',
103  `ifdef($1`_SIZE',
104    `define($1_SIZE32,$1_SIZE)
105     define($1_SIZE64,$1_SIZE)',
106    `ifdef($1`_SIZE32',`',
107      `ifdef(`Elf32_'$2`_DEF',
108        `define($1_SIZE32,FSZ32(Elf32_$2))',
109        `define($1_SIZE32,`sizeof(Elf32_'$2`)')')')
110     ifdef($1`_SIZE64',`',
111      `ifdef(`Elf64_'$2`_DEF',
112        `define($1_SIZE64,FSZ64(Elf64_$2))',
113        `define($1_SIZE64,`sizeof(Elf64_'$2`)')')')')')
114
115define(`DEFINE_ELF_FSIZES',
116  `ifelse($#,1,`',
117    `DEFINE_ELF_FSIZE($1)
118     DEFINE_ELF_FSIZES(shift($@))')')
119
120DEFINE_ELF_FSIZES(ELF_TYPE_LIST)
121DEFINE_ELF_FSIZE(`IDENT',`')	# `IDENT' is a pseudo type
122
123define(`FSIZE',
124  `[ELF_T_$1] = { .fsz32 = $1_SIZE32, .fsz64 = $1_SIZE64 },
125')
126define(`FSIZES',
127  `ifelse($#,1,`',
128    `FSIZE($1)
129FSIZES(shift($@))')')
130
131divert(0)
132
133struct fsize {
134	size_t fsz32;
135	size_t fsz64;
136};
137
138static struct fsize fsize[ELF_T_NUM] = {
139FSIZES(ELF_TYPE_LIST)
140};
141
142size_t
143_libelf_fsize(Elf_Type t, int ec, unsigned int v, size_t c)
144{
145	size_t sz;
146
147	sz = 0;
148	if (v != EV_CURRENT)
149		LIBELF_SET_ERROR(VERSION, 0);
150	else if ((int) t < ELF_T_FIRST || t > ELF_T_LAST)
151		LIBELF_SET_ERROR(ARGUMENT, 0);
152	else {
153		sz = ec == ELFCLASS64 ? fsize[t].fsz64 : fsize[t].fsz32;
154		if (sz == 0)
155			LIBELF_SET_ERROR(UNIMPL, 0);
156	}
157
158	return (sz*c);
159}
160