1 /* $NetBSD: libelf_fsize.m4,v 1.2 2009/12/19 07:31:04 thorpej Exp $ */
4 * Copyright (c) 2006 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/lib/libelf/libelf_fsize.m4,v 1.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $
36 * Create an array of file sizes from the elf_type definitions
40 include(SRCDIR`/elf_types.m4')
43 * Translations from structure definitions to the size of their file
48 define(`BYTE_SIZE', 1)
49 define(`IDENT_SIZE', `EI_NIDENT')
50 define(`NOTE_SIZE', 1) /* Elf_Note structures have variable length. */
52 /* Currently unimplemented types */
53 define(`MOVEP_SIZE', 0)
55 /* Overrides for 32 bit types that do not exist */
56 define(`XWORD_SIZE32', 0)
57 define(`SXWORD_SIZE32', 0)
60 * FSZ{32,64} define the sizes of 32 and 64 bit file structures respectively.
63 define(`FSZ32',`_FSZ32($1_DEF)')
66 `_BSZ32($1)+_FSZ32(shift($@))')')
67 define(`_BSZ32',`$2_SIZE32')
69 define(`FSZ64',`_FSZ64($1_DEF)')
72 `_BSZ64($1)+_FSZ64(shift($@))')')
73 define(`_BSZ64',`$2_SIZE64')
76 * DEFINE_ELF_FSIZES(TYPE,NAME)
78 * Shorthand for defining for 32 and 64 versions
81 * If TYPE`'_SIZE is defined, use its value for both 32 bit and 64 bit
84 * Otherwise, look for a explicit 32/64 bit size definition for TYPE,
85 * TYPE`'_SIZE32 or TYPE`'_SIZE64. If this definition is present, there
86 * is nothing further to do.
88 * Otherwise, if an Elf{32,64}_`'NAME structure definition is known,
89 * compute an expression that adds up the sizes of the structure's
92 * If such a structure definition is not known, treat TYPE as a primitive
93 * (i.e., integral) type and use sizeof(Elf{32,64}_`'NAME) to get its
94 * file representation size.
97 define(`DEFINE_ELF_FSIZE',
99 `define($1_SIZE32,$1_SIZE)
100 define($1_SIZE64,$1_SIZE)',
101 `ifdef($1`_SIZE32',`',
102 `ifdef(`Elf32_'$2`_DEF',
103 `define($1_SIZE32,FSZ32(Elf32_$2))',
104 `define($1_SIZE32,`sizeof(Elf32_'$2`)')')')
105 ifdef($1`_SIZE64',`',
106 `ifdef(`Elf64_'$2`_DEF',
107 `define($1_SIZE64,FSZ64(Elf64_$2))',
108 `define($1_SIZE64,`sizeof(Elf64_'$2`)')')')')')
110 define(`DEFINE_ELF_FSIZES',
112 `DEFINE_ELF_FSIZE($1)
113 DEFINE_ELF_FSIZES(shift($@))')')
115 DEFINE_ELF_FSIZES(ELF_TYPE_LIST)
116 DEFINE_ELF_FSIZE(`IDENT',`') # `IDENT' is a pseudo type
120 [ELF_T_$1] = { .fsz32 = $1_SIZE32, .fsz64 = $1_SIZE64 },
125 FSIZES(shift($@))')')
134 static struct fsize fsize[ELF_T_NUM] = {
135 FSIZES(ELF_TYPE_LIST)
139 _libelf_fsize(Elf_Type t, int ec, unsigned int v, size_t c)
145 LIBELF_SET_ERROR(VERSION, 0);
146 else if ((int) t < ELF_T_FIRST || t > ELF_T_LAST)
147 LIBELF_SET_ERROR(ARGUMENT, 0);
149 sz = ec == ELFCLASS64 ? fsize[t].fsz64 : fsize[t].fsz32;
151 LIBELF_SET_ERROR(UNIMPL, 0);