1dnl $NetBSD: libelf_msize.m4,v 1.5 2024/03/03 17:37:34 christos Exp $ 2/*- 3 * Copyright (c) 2006,2008-2011 Joseph Koshy 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#if HAVE_NBTOOL_CONFIG_H 29# include "nbtool_config.h" 30#endif 31 32#include <sys/cdefs.h> 33 34#include <assert.h> 35#include <libelf.h> 36#include <string.h> 37 38#include "_libelf.h" 39 40__RCSID("$NetBSD: libelf_msize.m4,v 1.5 2024/03/03 17:37:34 christos Exp $"); 41ELFTC_VCSID("Id: libelf_msize.m4 3977 2022-05-01 06:45:34Z jkoshy"); 42 43/* WARNING: GENERATED FROM __file__. */ 44 45struct msize { 46 size_t msz32; 47 size_t msz64; 48}; 49 50divert(-1) 51include(SRCDIR`/elf_types.m4') 52 53/* 54 * ELF types whose memory representations have a variable size. 55 */ 56define(BYTE_SIZE, 1) 57define(GNUHASH_SIZE, 1) 58define(NOTE_SIZE, 1) 59define(VDEF_SIZE, 1) 60define(VNEED_SIZE, 1) 61 62/* 63 * Unimplemented types. 64 */ 65define(MOVEP_SIZE, 0) 66define(SXWORD_SIZE32, 0) 67define(XWORD_SIZE32, 0) 68 69define(`DEFINE_ELF_MSIZE', 70 `ifdef($1`_SIZE', 71 `define($1_SIZE32,$1_SIZE) 72 define($1_SIZE64,$1_SIZE)', 73 `ifdef($1`_SIZE32',`', 74 `define($1_SIZE32,sizeof(Elf32_$2))') 75 ifdef($1`_SIZE64',`', 76 `define($1_SIZE64,sizeof(Elf64_$2))')')') 77define(`DEFINE_ELF_MSIZES', 78 `ifelse($#,1,`', 79 `DEFINE_ELF_MSIZE($1) 80 DEFINE_ELF_MSIZES(shift($@))')') 81 82DEFINE_ELF_MSIZES(ELF_TYPE_LIST) 83 84define(`MSIZE', 85 `[ELF_T_$1] = { .msz32 = $1_SIZE32, .msz64 = $1_SIZE64 }, 86') 87define(`MSIZES', 88 `ifelse($#,1,`', 89 `MSIZE($1) 90MSIZES(shift($@))')') 91 92divert(0) 93 94static struct msize msize[ELF_T_NUM] = { 95MSIZES(ELF_TYPE_LIST) 96}; 97 98/* 99 * Returns the memory size of the specified ELF type 't' of ELF 100 * class 'ec' and ELF version 'version'. 101 * 102 * If the specified combination of ELF type, class, and version is 103 * unsupported then a value of 0 will be returned and the appropriate 104 * library error code set. 105 */ 106size_t 107_libelf_msize(Elf_Type t, int elfclass, unsigned int version) 108{ 109 size_t sz; 110 111 assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64); 112 assert((signed) t >= ELF_T_FIRST && t <= ELF_T_LAST); 113 114 if (version != EV_CURRENT) { 115 LIBELF_SET_ERROR(VERSION, 0); 116 return (0); 117 } 118 119 sz = (elfclass == ELFCLASS32) ? msize[t].msz32 : msize[t].msz64; 120 121 if (sz == 0) { 122 LIBELF_SET_ERROR(UNIMPL, 0); 123 return (0); 124 } 125 126 return (sz); 127} 128