1/* $NetBSD: libelf_fsize.m4,v 1.2 2014/03/09 16:58:04 christos Exp $ */ 2 3/*- 4 * Copyright (c) 2006,2008-2011 Joseph Koshy 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 */ 28 29#include <libelf.h> 30 31#include "_libelf.h" 32 33__RCSID("$NetBSD: libelf_fsize.m4,v 1.2 2014/03/09 16:58:04 christos Exp $"); 34ELFTC_VCSID("Id: libelf_fsize.m4 2225 2011-11-26 18:55:54Z jkoshy "); 35 36/* WARNING: GENERATED FROM __file__. */ 37 38/* 39 * Create an array of file sizes from the elf_type definitions 40 */ 41 42divert(-1) 43include(SRCDIR`/elf_types.m4') 44 45/* 46 * Translations from structure definitions to the size of their file 47 * representations. 48 */ 49 50/* `Basic' types. */ 51define(`BYTE_SIZE', 1) 52define(`IDENT_SIZE', `EI_NIDENT') 53 54/* Types that have variable length. */ 55define(`GNUHASH_SIZE', 1) 56define(`NOTE_SIZE', 1) 57define(`VDEF_SIZE', 1) 58define(`VNEED_SIZE', 1) 59 60/* Currently unimplemented types. */ 61define(`MOVEP_SIZE', 0) 62 63/* Overrides for 32 bit types that do not exist. */ 64define(`XWORD_SIZE32', 0) 65define(`SXWORD_SIZE32', 0) 66 67/* 68 * FSZ{32,64} define the sizes of 32 and 64 bit file structures respectively. 69 */ 70 71define(`FSZ32',`_FSZ32($1_DEF)') 72define(`_FSZ32', 73 `ifelse($#,1,0, 74 `_BSZ32($1)+_FSZ32(shift($@))')') 75define(`_BSZ32',`$2_SIZE32') 76 77define(`FSZ64',`_FSZ64($1_DEF)') 78define(`_FSZ64', 79 `ifelse($#,1,0, 80 `_BSZ64($1)+_FSZ64(shift($@))')') 81define(`_BSZ64',`$2_SIZE64') 82 83/* 84 * DEFINE_ELF_FSIZES(TYPE,NAME) 85 * 86 * Shorthand for defining for 32 and 64 versions 87 * of elf type TYPE. 88 * 89 * If TYPE`'_SIZE is defined, use its value for both 32 bit and 64 bit 90 * sizes. 91 * 92 * Otherwise, look for a explicit 32/64 bit size definition for TYPE, 93 * TYPE`'_SIZE32 or TYPE`'_SIZE64. If this definition is present, there 94 * is nothing further to do. 95 * 96 * Otherwise, if an Elf{32,64}_`'NAME structure definition is known, 97 * compute an expression that adds up the sizes of the structure's 98 * constituents. 99 * 100 * If such a structure definition is not known, treat TYPE as a primitive 101 * (i.e., integral) type and use sizeof(Elf{32,64}_`'NAME) to get its 102 * file representation size. 103 */ 104 105define(`DEFINE_ELF_FSIZE', 106 `ifdef($1`_SIZE', 107 `define($1_SIZE32,$1_SIZE) 108 define($1_SIZE64,$1_SIZE)', 109 `ifdef($1`_SIZE32',`', 110 `ifdef(`Elf32_'$2`_DEF', 111 `define($1_SIZE32,FSZ32(Elf32_$2))', 112 `define($1_SIZE32,`sizeof(Elf32_'$2`)')')') 113 ifdef($1`_SIZE64',`', 114 `ifdef(`Elf64_'$2`_DEF', 115 `define($1_SIZE64,FSZ64(Elf64_$2))', 116 `define($1_SIZE64,`sizeof(Elf64_'$2`)')')')')') 117 118define(`DEFINE_ELF_FSIZES', 119 `ifelse($#,1,`', 120 `DEFINE_ELF_FSIZE($1) 121 DEFINE_ELF_FSIZES(shift($@))')') 122 123DEFINE_ELF_FSIZES(ELF_TYPE_LIST) 124DEFINE_ELF_FSIZE(`IDENT',`') # `IDENT' is a pseudo type 125 126define(`FSIZE', 127 `[ELF_T_$1] = { .fsz32 = $1_SIZE32, .fsz64 = $1_SIZE64 }, 128') 129define(`FSIZES', 130 `ifelse($#,1,`', 131 `FSIZE($1) 132FSIZES(shift($@))')') 133 134divert(0) 135 136struct fsize { 137 size_t fsz32; 138 size_t fsz64; 139}; 140 141static struct fsize fsize[ELF_T_NUM] = { 142FSIZES(ELF_TYPE_LIST) 143}; 144 145size_t 146_libelf_fsize(Elf_Type t, int ec, unsigned int v, size_t c) 147{ 148 size_t sz; 149 150 sz = 0; 151 if (v != EV_CURRENT) 152 LIBELF_SET_ERROR(VERSION, 0); 153 else if ((int) t < ELF_T_FIRST || t > ELF_T_LAST) 154 LIBELF_SET_ERROR(ARGUMENT, 0); 155 else { 156 sz = ec == ELFCLASS64 ? fsize[t].fsz64 : fsize[t].fsz32; 157 if (sz == 0) 158 LIBELF_SET_ERROR(UNIMPL, 0); 159 } 160 161 return (sz*c); 162} 163