1164190Sjkoshy/*- 2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy 3164190Sjkoshy * All rights reserved. 4164190Sjkoshy * 5164190Sjkoshy * Redistribution and use in source and binary forms, with or without 6164190Sjkoshy * modification, are permitted provided that the following conditions 7164190Sjkoshy * are met: 8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright 9164190Sjkoshy * notice, this list of conditions and the following disclaimer. 10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright 11164190Sjkoshy * notice, this list of conditions and the following disclaimer in the 12164190Sjkoshy * documentation and/or other materials provided with the distribution. 13164190Sjkoshy * 14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17164190Sjkoshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24164190Sjkoshy * SUCH DAMAGE. 25164190Sjkoshy * 26164190Sjkoshy * $FreeBSD: releng/10.2/lib/libelf/libelf_fsize.m4 210340 2010-07-21 10:39:29Z kaiw $ 27164190Sjkoshy */ 28164190Sjkoshy 29164190Sjkoshy#include <libelf.h> 30165317Sjkoshy#include <osreldate.h> 31164190Sjkoshy 32164190Sjkoshy#include "_libelf.h" 33164190Sjkoshy 34164190Sjkoshy/* 35164190Sjkoshy * Create an array of file sizes from the elf_type definitions 36164190Sjkoshy */ 37164190Sjkoshy 38164190Sjkoshydivert(-1) 39164190Sjkoshyinclude(SRCDIR`/elf_types.m4') 40164190Sjkoshy 41164190Sjkoshy/* 42164190Sjkoshy * Translations from structure definitions to the size of their file 43164190Sjkoshy * representations. 44164190Sjkoshy */ 45164190Sjkoshy 46210335Skaiw/* `Basic' types. */ 47164190Sjkoshydefine(`BYTE_SIZE', 1) 48164190Sjkoshydefine(`IDENT_SIZE', `EI_NIDENT') 49164190Sjkoshy 50210335Skaiw/* Types that have variable length. */ 51210340Skaiwdefine(`GNUHASH_SIZE', 1) 52210340Skaiwdefine(`NOTE_SIZE', 1) 53210335Skaiw 54210335Skaiw/* Currently unimplemented types. */ 55164190Sjkoshydefine(`MOVEP_SIZE', 0) 56164190Sjkoshy 57210335Skaiw/* Overrides for 32 bit types that do not exist. */ 58164190Sjkoshydefine(`XWORD_SIZE32', 0) 59164190Sjkoshydefine(`SXWORD_SIZE32', 0) 60164190Sjkoshy 61164190Sjkoshy/* 62164190Sjkoshy * FSZ{32,64} define the sizes of 32 and 64 bit file structures respectively. 63164190Sjkoshy */ 64164190Sjkoshy 65164190Sjkoshydefine(`FSZ32',`_FSZ32($1_DEF)') 66164190Sjkoshydefine(`_FSZ32', 67164190Sjkoshy `ifelse($#,1,0, 68164190Sjkoshy `_BSZ32($1)+_FSZ32(shift($@))')') 69164190Sjkoshydefine(`_BSZ32',`$2_SIZE32') 70164190Sjkoshy 71164190Sjkoshydefine(`FSZ64',`_FSZ64($1_DEF)') 72164190Sjkoshydefine(`_FSZ64', 73164190Sjkoshy `ifelse($#,1,0, 74164190Sjkoshy `_BSZ64($1)+_FSZ64(shift($@))')') 75164190Sjkoshydefine(`_BSZ64',`$2_SIZE64') 76164190Sjkoshy 77164190Sjkoshy/* 78164190Sjkoshy * DEFINE_ELF_FSIZES(TYPE,NAME) 79164190Sjkoshy * 80164190Sjkoshy * Shorthand for defining for 32 and 64 versions 81164190Sjkoshy * of elf type TYPE. 82164190Sjkoshy * 83164190Sjkoshy * If TYPE`'_SIZE is defined, use its value for both 32 bit and 64 bit 84164190Sjkoshy * sizes. 85164190Sjkoshy * 86164190Sjkoshy * Otherwise, look for a explicit 32/64 bit size definition for TYPE, 87164190Sjkoshy * TYPE`'_SIZE32 or TYPE`'_SIZE64. If this definition is present, there 88164190Sjkoshy * is nothing further to do. 89164190Sjkoshy * 90164190Sjkoshy * Otherwise, if an Elf{32,64}_`'NAME structure definition is known, 91164190Sjkoshy * compute an expression that adds up the sizes of the structure's 92164190Sjkoshy * constituents. 93164190Sjkoshy * 94164190Sjkoshy * If such a structure definition is not known, treat TYPE as a primitive 95164190Sjkoshy * (i.e., integral) type and use sizeof(Elf{32,64}_`'NAME) to get its 96164190Sjkoshy * file representation size. 97164190Sjkoshy */ 98164190Sjkoshy 99164190Sjkoshydefine(`DEFINE_ELF_FSIZE', 100164190Sjkoshy `ifdef($1`_SIZE', 101164190Sjkoshy `define($1_SIZE32,$1_SIZE) 102164190Sjkoshy define($1_SIZE64,$1_SIZE)', 103164190Sjkoshy `ifdef($1`_SIZE32',`', 104164190Sjkoshy `ifdef(`Elf32_'$2`_DEF', 105164190Sjkoshy `define($1_SIZE32,FSZ32(Elf32_$2))', 106164190Sjkoshy `define($1_SIZE32,`sizeof(Elf32_'$2`)')')') 107164190Sjkoshy ifdef($1`_SIZE64',`', 108164190Sjkoshy `ifdef(`Elf64_'$2`_DEF', 109164190Sjkoshy `define($1_SIZE64,FSZ64(Elf64_$2))', 110164190Sjkoshy `define($1_SIZE64,`sizeof(Elf64_'$2`)')')')')') 111164190Sjkoshy 112164190Sjkoshydefine(`DEFINE_ELF_FSIZES', 113164190Sjkoshy `ifelse($#,1,`', 114164190Sjkoshy `DEFINE_ELF_FSIZE($1) 115164190Sjkoshy DEFINE_ELF_FSIZES(shift($@))')') 116164190Sjkoshy 117164190SjkoshyDEFINE_ELF_FSIZES(ELF_TYPE_LIST) 118164190SjkoshyDEFINE_ELF_FSIZE(`IDENT',`') # `IDENT' is a pseudo type 119164190Sjkoshy 120164190Sjkoshydefine(`FSIZE', 121165317Sjkoshy `#if __FreeBSD_version >= $3 122165317Sjkoshy [ELF_T_$1] = { .fsz32 = $1_SIZE32, .fsz64 = $1_SIZE64 }, 123165317Sjkoshy#endif') 124164190Sjkoshydefine(`FSIZES', 125164190Sjkoshy `ifelse($#,1,`', 126164190Sjkoshy `FSIZE($1) 127164190SjkoshyFSIZES(shift($@))')') 128164190Sjkoshy 129164190Sjkoshydivert(0) 130164190Sjkoshy 131164190Sjkoshystruct fsize { 132164190Sjkoshy size_t fsz32; 133164190Sjkoshy size_t fsz64; 134164190Sjkoshy}; 135164190Sjkoshy 136164190Sjkoshystatic struct fsize fsize[ELF_T_NUM] = { 137164190SjkoshyFSIZES(ELF_TYPE_LIST) 138164190Sjkoshy}; 139164190Sjkoshy 140164190Sjkoshysize_t 141164190Sjkoshy_libelf_fsize(Elf_Type t, int ec, unsigned int v, size_t c) 142164190Sjkoshy{ 143164190Sjkoshy size_t sz; 144164190Sjkoshy 145164190Sjkoshy sz = 0; 146164190Sjkoshy if (v != EV_CURRENT) 147164190Sjkoshy LIBELF_SET_ERROR(VERSION, 0); 148210340Skaiw else if ((int) t < ELF_T_FIRST || t > ELF_T_LAST) 149164190Sjkoshy LIBELF_SET_ERROR(ARGUMENT, 0); 150164190Sjkoshy else { 151164190Sjkoshy sz = ec == ELFCLASS64 ? fsize[t].fsz64 : fsize[t].fsz32; 152164190Sjkoshy if (sz == 0) 153164190Sjkoshy LIBELF_SET_ERROR(UNIMPL, 0); 154164190Sjkoshy } 155164190Sjkoshy 156164190Sjkoshy return (sz*c); 157164190Sjkoshy} 158164190Sjkoshy 159