xref: /minix3/external/bsd/elftoolchain/dist/libelf/libelf_fsize.m4 (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc/*	$NetBSD: libelf_fsize.m4,v 1.2 2014/03/09 16:58:04 christos Exp $	*/
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc/*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2006,2008-2011 Joseph Koshy
5*0a6a1f1dSLionel Sambuc * All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc * are met:
10*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc *    notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc *    notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc *    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc *
16*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*0a6a1f1dSLionel Sambuc * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*0a6a1f1dSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*0a6a1f1dSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*0a6a1f1dSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*0a6a1f1dSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*0a6a1f1dSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0a6a1f1dSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0a6a1f1dSLionel Sambuc * SUCH DAMAGE.
27*0a6a1f1dSLionel Sambuc */
28*0a6a1f1dSLionel Sambuc
29*0a6a1f1dSLionel Sambuc#include <libelf.h>
30*0a6a1f1dSLionel Sambuc
31*0a6a1f1dSLionel Sambuc#include "_libelf.h"
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc__RCSID("$NetBSD: libelf_fsize.m4,v 1.2 2014/03/09 16:58:04 christos Exp $");
34*0a6a1f1dSLionel SambucELFTC_VCSID("Id: libelf_fsize.m4 2225 2011-11-26 18:55:54Z jkoshy ");
35*0a6a1f1dSLionel Sambuc
36*0a6a1f1dSLionel Sambuc/* WARNING: GENERATED FROM __file__. */
37*0a6a1f1dSLionel Sambuc
38*0a6a1f1dSLionel Sambuc/*
39*0a6a1f1dSLionel Sambuc * Create an array of file sizes from the elf_type definitions
40*0a6a1f1dSLionel Sambuc */
41*0a6a1f1dSLionel Sambuc
42*0a6a1f1dSLionel Sambucdivert(-1)
43*0a6a1f1dSLionel Sambucinclude(SRCDIR`/elf_types.m4')
44*0a6a1f1dSLionel Sambuc
45*0a6a1f1dSLionel Sambuc/*
46*0a6a1f1dSLionel Sambuc * Translations from structure definitions to the size of their file
47*0a6a1f1dSLionel Sambuc * representations.
48*0a6a1f1dSLionel Sambuc */
49*0a6a1f1dSLionel Sambuc
50*0a6a1f1dSLionel Sambuc/* `Basic' types. */
51*0a6a1f1dSLionel Sambucdefine(`BYTE_SIZE',	1)
52*0a6a1f1dSLionel Sambucdefine(`IDENT_SIZE',	`EI_NIDENT')
53*0a6a1f1dSLionel Sambuc
54*0a6a1f1dSLionel Sambuc/* Types that have variable length. */
55*0a6a1f1dSLionel Sambucdefine(`GNUHASH_SIZE',	1)
56*0a6a1f1dSLionel Sambucdefine(`NOTE_SIZE',	1)
57*0a6a1f1dSLionel Sambucdefine(`VDEF_SIZE',	1)
58*0a6a1f1dSLionel Sambucdefine(`VNEED_SIZE',	1)
59*0a6a1f1dSLionel Sambuc
60*0a6a1f1dSLionel Sambuc/* Currently unimplemented types. */
61*0a6a1f1dSLionel Sambucdefine(`MOVEP_SIZE',	0)
62*0a6a1f1dSLionel Sambuc
63*0a6a1f1dSLionel Sambuc/* Overrides for 32 bit types that do not exist. */
64*0a6a1f1dSLionel Sambucdefine(`XWORD_SIZE32',	0)
65*0a6a1f1dSLionel Sambucdefine(`SXWORD_SIZE32',	0)
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuc/*
68*0a6a1f1dSLionel Sambuc * FSZ{32,64} define the sizes of 32 and 64 bit file structures respectively.
69*0a6a1f1dSLionel Sambuc */
70*0a6a1f1dSLionel Sambuc
71*0a6a1f1dSLionel Sambucdefine(`FSZ32',`_FSZ32($1_DEF)')
72*0a6a1f1dSLionel Sambucdefine(`_FSZ32',
73*0a6a1f1dSLionel Sambuc  `ifelse($#,1,0,
74*0a6a1f1dSLionel Sambuc    `_BSZ32($1)+_FSZ32(shift($@))')')
75*0a6a1f1dSLionel Sambucdefine(`_BSZ32',`$2_SIZE32')
76*0a6a1f1dSLionel Sambuc
77*0a6a1f1dSLionel Sambucdefine(`FSZ64',`_FSZ64($1_DEF)')
78*0a6a1f1dSLionel Sambucdefine(`_FSZ64',
79*0a6a1f1dSLionel Sambuc  `ifelse($#,1,0,
80*0a6a1f1dSLionel Sambuc    `_BSZ64($1)+_FSZ64(shift($@))')')
81*0a6a1f1dSLionel Sambucdefine(`_BSZ64',`$2_SIZE64')
82*0a6a1f1dSLionel Sambuc
83*0a6a1f1dSLionel Sambuc/*
84*0a6a1f1dSLionel Sambuc * DEFINE_ELF_FSIZES(TYPE,NAME)
85*0a6a1f1dSLionel Sambuc *
86*0a6a1f1dSLionel Sambuc * Shorthand for defining  for 32 and 64 versions
87*0a6a1f1dSLionel Sambuc * of elf type TYPE.
88*0a6a1f1dSLionel Sambuc *
89*0a6a1f1dSLionel Sambuc * If TYPE`'_SIZE is defined, use its value for both 32 bit and 64 bit
90*0a6a1f1dSLionel Sambuc * sizes.
91*0a6a1f1dSLionel Sambuc *
92*0a6a1f1dSLionel Sambuc * Otherwise, look for a explicit 32/64 bit size definition for TYPE,
93*0a6a1f1dSLionel Sambuc * TYPE`'_SIZE32 or TYPE`'_SIZE64. If this definition is present, there
94*0a6a1f1dSLionel Sambuc * is nothing further to do.
95*0a6a1f1dSLionel Sambuc *
96*0a6a1f1dSLionel Sambuc * Otherwise, if an Elf{32,64}_`'NAME structure definition is known,
97*0a6a1f1dSLionel Sambuc * compute an expression that adds up the sizes of the structure's
98*0a6a1f1dSLionel Sambuc * constituents.
99*0a6a1f1dSLionel Sambuc *
100*0a6a1f1dSLionel Sambuc * If such a structure definition is not known, treat TYPE as a primitive
101*0a6a1f1dSLionel Sambuc * (i.e., integral) type and use sizeof(Elf{32,64}_`'NAME) to get its
102*0a6a1f1dSLionel Sambuc * file representation size.
103*0a6a1f1dSLionel Sambuc */
104*0a6a1f1dSLionel Sambuc
105*0a6a1f1dSLionel Sambucdefine(`DEFINE_ELF_FSIZE',
106*0a6a1f1dSLionel Sambuc  `ifdef($1`_SIZE',
107*0a6a1f1dSLionel Sambuc    `define($1_SIZE32,$1_SIZE)
108*0a6a1f1dSLionel Sambuc     define($1_SIZE64,$1_SIZE)',
109*0a6a1f1dSLionel Sambuc    `ifdef($1`_SIZE32',`',
110*0a6a1f1dSLionel Sambuc      `ifdef(`Elf32_'$2`_DEF',
111*0a6a1f1dSLionel Sambuc        `define($1_SIZE32,FSZ32(Elf32_$2))',
112*0a6a1f1dSLionel Sambuc        `define($1_SIZE32,`sizeof(Elf32_'$2`)')')')
113*0a6a1f1dSLionel Sambuc     ifdef($1`_SIZE64',`',
114*0a6a1f1dSLionel Sambuc      `ifdef(`Elf64_'$2`_DEF',
115*0a6a1f1dSLionel Sambuc        `define($1_SIZE64,FSZ64(Elf64_$2))',
116*0a6a1f1dSLionel Sambuc        `define($1_SIZE64,`sizeof(Elf64_'$2`)')')')')')
117*0a6a1f1dSLionel Sambuc
118*0a6a1f1dSLionel Sambucdefine(`DEFINE_ELF_FSIZES',
119*0a6a1f1dSLionel Sambuc  `ifelse($#,1,`',
120*0a6a1f1dSLionel Sambuc    `DEFINE_ELF_FSIZE($1)
121*0a6a1f1dSLionel Sambuc     DEFINE_ELF_FSIZES(shift($@))')')
122*0a6a1f1dSLionel Sambuc
123*0a6a1f1dSLionel SambucDEFINE_ELF_FSIZES(ELF_TYPE_LIST)
124*0a6a1f1dSLionel SambucDEFINE_ELF_FSIZE(`IDENT',`')	# `IDENT' is a pseudo type
125*0a6a1f1dSLionel Sambuc
126*0a6a1f1dSLionel Sambucdefine(`FSIZE',
127*0a6a1f1dSLionel Sambuc  `[ELF_T_$1] = { .fsz32 = $1_SIZE32, .fsz64 = $1_SIZE64 },
128*0a6a1f1dSLionel Sambuc')
129*0a6a1f1dSLionel Sambucdefine(`FSIZES',
130*0a6a1f1dSLionel Sambuc  `ifelse($#,1,`',
131*0a6a1f1dSLionel Sambuc    `FSIZE($1)
132*0a6a1f1dSLionel SambucFSIZES(shift($@))')')
133*0a6a1f1dSLionel Sambuc
134*0a6a1f1dSLionel Sambucdivert(0)
135*0a6a1f1dSLionel Sambuc
136*0a6a1f1dSLionel Sambucstruct fsize {
137*0a6a1f1dSLionel Sambuc	size_t fsz32;
138*0a6a1f1dSLionel Sambuc	size_t fsz64;
139*0a6a1f1dSLionel Sambuc};
140*0a6a1f1dSLionel Sambuc
141*0a6a1f1dSLionel Sambucstatic struct fsize fsize[ELF_T_NUM] = {
142*0a6a1f1dSLionel SambucFSIZES(ELF_TYPE_LIST)
143*0a6a1f1dSLionel Sambuc};
144*0a6a1f1dSLionel Sambuc
145*0a6a1f1dSLionel Sambucsize_t
146*0a6a1f1dSLionel Sambuc_libelf_fsize(Elf_Type t, int ec, unsigned int v, size_t c)
147*0a6a1f1dSLionel Sambuc{
148*0a6a1f1dSLionel Sambuc	size_t sz;
149*0a6a1f1dSLionel Sambuc
150*0a6a1f1dSLionel Sambuc	sz = 0;
151*0a6a1f1dSLionel Sambuc	if (v != EV_CURRENT)
152*0a6a1f1dSLionel Sambuc		LIBELF_SET_ERROR(VERSION, 0);
153*0a6a1f1dSLionel Sambuc	else if ((int) t < ELF_T_FIRST || t > ELF_T_LAST)
154*0a6a1f1dSLionel Sambuc		LIBELF_SET_ERROR(ARGUMENT, 0);
155*0a6a1f1dSLionel Sambuc	else {
156*0a6a1f1dSLionel Sambuc		sz = ec == ELFCLASS64 ? fsize[t].fsz64 : fsize[t].fsz32;
157*0a6a1f1dSLionel Sambuc		if (sz == 0)
158*0a6a1f1dSLionel Sambuc			LIBELF_SET_ERROR(UNIMPL, 0);
159*0a6a1f1dSLionel Sambuc	}
160*0a6a1f1dSLionel Sambuc
161*0a6a1f1dSLionel Sambuc	return (sz*c);
162*0a6a1f1dSLionel Sambuc}
163