base: Use system libelf instead of ext
[gem5.git] / ext / libelf / libelf_fsize.m4
1 /*-
2 * Copyright (c) 2006 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 * $FreeBSD: src/lib/libelf/libelf_fsize.m4,v 1.2 2006/12/18 05:40:01 jkoshy Exp $
27 */
28
29 #include "libelf.h"
30 #include "_libelf.h"
31
32 /*
33 * Create an array of file sizes from the elf_type definitions
34 */
35
36 divert(-1)
37 include(SRCDIR`/elf_types.m4')
38
39 /*
40 * Translations from structure definitions to the size of their file
41 * representations.
42 */
43
44 /* `Basic' types */
45 define(`BYTE_SIZE', 1)
46 define(`IDENT_SIZE', `EI_NIDENT')
47 define(`NOTE_SIZE', 1) /* Elf_Note structures have variable length. */
48
49 /* Currently unimplemented types */
50 define(`MOVEP_SIZE', 0)
51
52 /* Overrides for 32 bit types that do not exist */
53 define(`XWORD_SIZE32', 0)
54 define(`SXWORD_SIZE32', 0)
55
56 /*
57 * FSZ{32,64} define the sizes of 32 and 64 bit file structures respectively.
58 */
59
60 define(`FSZ32',`_FSZ32($1_DEF)')
61 define(`_FSZ32',
62 `ifelse($#,1,0,
63 `_BSZ32($1)+_FSZ32(shift($@))')')
64 define(`_BSZ32',`$2_SIZE32')
65
66 define(`FSZ64',`_FSZ64($1_DEF)')
67 define(`_FSZ64',
68 `ifelse($#,1,0,
69 `_BSZ64($1)+_FSZ64(shift($@))')')
70 define(`_BSZ64',`$2_SIZE64')
71
72 /*
73 * DEFINE_ELF_FSIZES(TYPE,NAME)
74 *
75 * Shorthand for defining for 32 and 64 versions
76 * of elf type TYPE.
77 *
78 * If TYPE`'_SIZE is defined, use its value for both 32 bit and 64 bit
79 * sizes.
80 *
81 * Otherwise, look for a explicit 32/64 bit size definition for TYPE,
82 * TYPE`'_SIZE32 or TYPE`'_SIZE64. If this definition is present, there
83 * is nothing further to do.
84 *
85 * Otherwise, if an Elf{32,64}_`'NAME structure definition is known,
86 * compute an expression that adds up the sizes of the structure's
87 * constituents.
88 *
89 * If such a structure definition is not known, treat TYPE as a primitive
90 * (i.e., integral) type and use sizeof(Elf{32,64}_`'NAME) to get its
91 * file representation size.
92 */
93
94 define(`DEFINE_ELF_FSIZE',
95 `ifdef($1`_SIZE',
96 `define($1_SIZE32,$1_SIZE)
97 define($1_SIZE64,$1_SIZE)',
98 `ifdef($1`_SIZE32',`',
99 `ifdef(`Elf32_'$2`_DEF',
100 `define($1_SIZE32,FSZ32(Elf32_$2))',
101 `define($1_SIZE32,`sizeof(Elf32_'$2`)')')')
102 ifdef($1`_SIZE64',`',
103 `ifdef(`Elf64_'$2`_DEF',
104 `define($1_SIZE64,FSZ64(Elf64_$2))',
105 `define($1_SIZE64,`sizeof(Elf64_'$2`)')')')')')
106
107 define(`DEFINE_ELF_FSIZES',
108 `ifelse($#,1,`',
109 `DEFINE_ELF_FSIZE($1)
110 DEFINE_ELF_FSIZES(shift($@))')')
111
112 DEFINE_ELF_FSIZES(ELF_TYPE_LIST)
113 DEFINE_ELF_FSIZE(`IDENT',`') # `IDENT' is a pseudo type
114
115 define(`FSIZE',
116 `[ELF_T_$1] = { .fsz32 = $1_SIZE32, .fsz64 = $1_SIZE64 },')
117 define(`FSIZES',
118 `ifelse($#,1,`',
119 `FSIZE($1)
120 FSIZES(shift($@))')')
121
122 divert(0)
123
124 struct fsize {
125 size_t fsz32;
126 size_t fsz64;
127 };
128
129 static struct fsize fsize[ELF_T_NUM] = {
130 FSIZES(ELF_TYPE_LIST)
131 };
132
133 size_t
134 _libelf_fsize(Elf_Type t, int ec, unsigned int v, size_t c)
135 {
136 size_t sz;
137
138 sz = 0;
139 if (v != EV_CURRENT)
140 LIBELF_SET_ERROR(VERSION, 0);
141 else if ((int) t < ELF_T_FIRST || t > ELF_T_LAST)
142 LIBELF_SET_ERROR(ARGUMENT, 0);
143 else {
144 sz = ec == ELFCLASS64 ? fsize[t].fsz64 : fsize[t].fsz32;
145 if (sz == 0)
146 LIBELF_SET_ERROR(UNIMPL, 0);
147 }
148
149 return (sz*c);
150 }
151