3 # Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
4 # Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5 # Copyright (c) 2015-2018 The strace developers.
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # 3. The name of the author may not be used to endorse or promote products
17 # derived from this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 function array_get(array_idx, array_member, \
33 array_return = array[array_idx, array_member]
34 if ("" == array_return) {
35 printf("%s: index [%s] without %s\n",
36 FILENAME, array_idx, array_member) > "/dev/stderr"
41 function norm_idx(idx)
43 return sprintf("%016s", idx)
45 function array_seq(array_idx)
47 if ((array_idx, "seq") in array)
48 return array[array_idx, "seq"]
50 array[array_idx, "seq"] = index_seq
53 function enter(array_idx,
56 if (array_idx in called) {
57 printf("%s: index loop detected:", FILENAME) > "/dev/stderr"
59 printf(" %s", item) > "/dev/stderr"
60 print "" > "/dev/stderr"
65 function leave(array_idx, to_return)
67 delete called[array_idx]
70 function update_upper_bound(idx, val, \
73 count = array[idx, "count"]
76 array[idx, "count"] = count * val
77 array[idx, "upper_bound"] = array[idx, "upper_bound"] "[" val "]"
79 function what_is(what_idx, \
80 item, loc_diff, location, prev_location, prev_returned_size, \
81 special, to_return, type_idx, enc, i)
84 special = array_get(what_idx, "special")
85 if (special == "base_type") {
86 enc = array_get(what_idx, "encoding")
87 if (enc == 5) { # signed
89 8 * array_get(what_idx, "byte_size"))
90 } else if (enc == 7) { # unsigned
92 8 * array_get(what_idx, "byte_size"))
93 } else { # float, signed/unsigned char
94 printf("%s ", array_get(what_idx, "name"))
96 returned_size = array_get(what_idx, "byte_size")
97 } else if (special == "enumeration_type") {
98 returned_size = array_get(what_idx, "byte_size")
99 printf("uint%s_t ", 8 * returned_size)
100 } else if (special == "pointer_type") {
101 printf("mpers_ptr_t ")
102 returned_size = array_get(what_idx, "byte_size")
103 } else if (special == "array_type") {
104 type_idx = array_get(what_idx, "type")
106 to_return = array[what_idx, "upper_bound"]
109 returned_size = array[what_idx, "count"] * returned_size
110 return leave(what_idx, to_return)
111 } else if (special == "structure_type") {
116 prev_returned_size = 0
117 for (i = 1; i <= parents_cnt; i += 1) {
118 if (array_parents[aparents_keys[i]] == what_idx) {
119 location = array_get(aparents_keys[i], "location")
120 loc_diff = location - prev_location - \
123 printf("unsigned char mpers_%s_%s[%s];\n",
124 "filler", array_seq(aparents_keys[i]), loc_diff)
126 prev_location = location
127 returned = what_is(aparents_keys[i])
128 prev_returned_size = returned_size
129 printf("%s%s;\n", array[aparents_keys[i], "name"], returned)
132 returned_size = array_get(what_idx, "byte_size")
133 loc_diff = returned_size - prev_location - prev_returned_size
135 printf("unsigned char mpers_%s_%s[%s];\n",
136 "end_filler", array_seq(item), loc_diff)
138 printf("} ATTRIBUTE_PACKED ")
139 } else if (special == "union_type") {
141 for (i = 1; i <= parents_cnt; i += 1) {
142 if (array_parents[aparents_keys[i]] == what_idx) {
143 returned = what_is(aparents_keys[i])
144 printf("%s%s;\n", array[aparents_keys[i], "name"], returned)
148 returned_size = array_get(what_idx, "byte_size")
149 } else if (special == "typedef") {
150 type_idx = array_get(what_idx, "type")
151 return leave(what_idx, what_is(type_idx))
152 } else if (special == "member") {
153 type_idx = array_get(what_idx, "type")
154 return leave(what_idx, what_is(type_idx))
156 type_idx = array_get(what_idx, "type")
159 return leave(what_idx, "")
162 match(ARCH_FLAG, /[[:digit:]]+/, temparray)
163 default_pointer_size = temparray[0] / 8
164 print "#include <stdint.h>"
167 match($0, /([[:alnum:]]+)><([[:alnum:]]+)/, matches)
169 idx = norm_idx(matches[2])
170 array[idx, "idx"] = idx
173 /^DW_AT_data_member_location/ {
174 if (!match($0, /\(DW_OP_plus_uconst:[[:space:]]+([[:digit:]]+)\)/, temparray))
175 match($0, /([[:digit:]]+)/, temparray)
176 array[idx, "location"] = temparray[1]
179 match($0, /:[[:space:]]+([[:alpha:]_][[:alnum:]_[:space:]]*)/, \
182 array[idx, "name"] = temparray[1]
185 match($0, /[[:digit:]]+/, temparray)
186 array[idx, "byte_size"] = temparray[0]
189 match($0, /[[:digit:]]+/, temparray)
190 array[idx, "encoding"] = temparray[0]
193 match($0, /:[[:space:]]+<0x([[:xdigit:]]*)>$/, temparray)
194 array[idx, "type"] = norm_idx(temparray[1])
196 /^DW_AT_upper_bound/ {
197 match($0, /[[:digit:]]+/, temparray)
198 update_upper_bound(parent[level - 1], temparray[0] + 1)
201 match($0, /[[:digit:]]+/, temparray)
202 update_upper_bound(parent[level - 1], temparray[0])
204 /^Abbrev Number:[^(]+\(DW_TAG_/ {
205 if (match($0, /typedef|union_type|structure_type|pointer_type\
206 |enumeration_type|array_type|base_type|member/, temparray)) {
207 array_special[idx] = temparray[0]
208 array[idx, "special"] = temparray[0]
209 if ("pointer_type" == temparray[0])
210 array[idx, "byte_size"] = default_pointer_size
211 if (level > 1 && "member" == temparray[0])
212 array_parents[idx] = parent[level-1]
216 parents_cnt = asorti(array_parents, aparents_keys)
218 for (item in array_special) {
219 if (array[item, "special"] == "pointer_type") {
221 "uint" 8 * array_get(item, "byte_size") "_t"
222 print "#ifndef mpers_ptr_t_is_" mpers_ptr_t
223 print "typedef " mpers_ptr_t " mpers_ptr_t;"
224 print "#define mpers_ptr_t_is_" mpers_ptr_t
229 for (item in array_names) {
230 if (array[item, "name"] == VAR_NAME) {
231 type = array_get(item, "type")
234 name = array_get(type, "name")
235 print ARCH_FLAG "_" name ";"
236 print "#define MPERS_" \
237 ARCH_FLAG "_" name " " \