1/*- 2 * Copyright (c) 2006,2008-2011 Joseph Koshy 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <libelf.h> 28 29#include "_libelf.h" 30 31ELFTC_VCSID("$Id: libelf_fsize.m4 2225 2011-11-26 18:55:54Z jkoshy $"); 32 33/* WARNING: GENERATED FROM __file__. */ 34 35/* 36 * Create an array of file sizes from the elf_type definitions 37 */ 38 39divert(-1) 40include(SRCDIR`/elf_types.m4') 41 42/* 43 * Translations from structure definitions to the size of their file 44 * representations. 45 */ 46 47/* `Basic' types. */ 48define(`BYTE_SIZE', 1) 49define(`IDENT_SIZE', `EI_NIDENT') 50 51/* Types that have variable length. */ 52define(`GNUHASH_SIZE', 1) 53define(`NOTE_SIZE', 1) 54define(`VDEF_SIZE', 1) 55define(`VNEED_SIZE', 1) 56 57/* Currently unimplemented types. */ 58define(`MOVEP_SIZE', 0) 59 60/* Overrides for 32 bit types that do not exist. */ 61define(`XWORD_SIZE32', 0) 62define(`SXWORD_SIZE32', 0) 63 64/* 65 * FSZ{32,64} define the sizes of 32 and 64 bit file structures respectively. 66 */ 67 68define(`FSZ32',`_FSZ32($1_DEF)') 69define(`_FSZ32', 70 `ifelse($#,1,0, 71 `_BSZ32($1)+_FSZ32(shift($@))')') 72define(`_BSZ32',`$2_SIZE32') 73 74define(`FSZ64',`_FSZ64($1_DEF)') 75define(`_FSZ64', 76 `ifelse($#,1,0, 77 `_BSZ64($1)+_FSZ64(shift($@))')') 78define(`_BSZ64',`$2_SIZE64') 79 80/* 81 * DEFINE_ELF_FSIZES(TYPE,NAME) 82 * 83 * Shorthand for defining for 32 and 64 versions 84 * of elf type TYPE. 85 * 86 * If TYPE`'_SIZE is defined, use its value for both 32 bit and 64 bit 87 * 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