1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <strings.h>
#include <_machelf.h>
#include "_conv.h"
#include "globals_msg.h"
/*
* Map an integer into a descriptive string.
*
* entry:
* inv_buf - A buffer into which this routine can format
* a result string, if necessary.
* val - The value for which a string is desired.
* flags - CONV_FMT_* values to be passed to conv_invalid_val() if
* necessary. The caller is reponsible for having examined
* the CONV_FMT_ALT_* part of flags and passing the proper
* msg array.
* num_msg - # of Msg entries in msg.
* msg - Array of num_msg Msg items corresponding to the possible
* strings corresponding to val.
* local_sgs_msg - Message string table from module from which
* this function is called.
*
* exit:
* If val lies in the range [0-(num_msg-1)], then the string
* corresponding to it is returned. If val is outside the range,
* conv_invalid_val() is called to format an ASCII representation
* of it into inv_buf, and that is returned.
*/
/*ARGSUSED5*/
static const char *
map_msg2str(Conv_inv_buf_t *inv_buf, Conv_elfvalue_t val,
Conv_fmt_flags_t flags, size_t num_msg, const Msg *msg,
const char *local_sgs_msg)
{
if ((val < num_msg) && (msg[val] != 0))
return (MSG_ORIG_STRTAB(msg[val], local_sgs_msg));
/* If we get here, it's an unknown value */
return (conv_invalid_val(inv_buf, val, flags));
}
/*
* Map an integer into a descriptive string from a NULL terminated
* array of Val_desc or Val_desc2 descriptors.
*
* entry:
* inv_buf - A buffer into which this routine can format
* a result string, if necessary.
* osabi,mach (_conv_vd22str only) - The osab/mach under which
* val is to be interpreted. Items with a non-0 osabi or machine
* that do not match are quietly ignored.
* val - The value for which a string is desired.
* flags - CONV_FMT_* values to be passed to conv_invalid_val() if
* necessary. The caller is reponsible for having examined
* the CONV_FMT_ALT_* part of flags and passing the proper
* descriptor array.
* vdp - Pointer to NULL terminated array of Val_desc descriptors.
* local_sgs_msg - Message string table from module from which
* this function is called.
*
* exit:
* If val is found in the vdp array, and in the osabi version of
* this function if the osabi matches, then the string corresponding
* val is returned. If a string for val is not found, conv_invalid_val()
* is called to format an ASCII representation of it into inv_buf, and
* that is returned.
*/
/*ARGSUSED4*/
static const char *
map_vd2str(Conv_inv_buf_t *inv_buf, Conv_elfvalue_t val,
Conv_fmt_flags_t flags, const Val_desc *vdp, const char *local_sgs_msg)
{
for (; vdp->v_msg; vdp++) {
if (val == vdp->v_val)
return (MSG_ORIG_STRTAB(vdp->v_msg, local_sgs_msg));
}
/* If we get here, it's an unknown value */
return (conv_invalid_val(inv_buf, val, flags));
}
/*ARGSUSED6*/
static const char *
map_vd22str(Conv_inv_buf_t *inv_buf, uchar_t osabi, Half mach,
Conv_elfvalue_t val, Conv_fmt_flags_t flags, const Val_desc2 *vdp,
const char *local_sgs_msg)
{
for (; vdp->v_msg; vdp++) {
if (CONV_VD2_SKIP(osabi, mach, vdp))
continue;
if (val == vdp->v_val)
return (MSG_ORIG_STRTAB(vdp->v_msg, local_sgs_msg));
}
/* If we get here, it's an unknown value */
return (conv_invalid_val(inv_buf, val, flags));
}
/*
* Process an array of conv_ds_XXX_t structures and call the appropriate
* map functions for the format of the strings given.
*/
const char *
_conv_map_ds(uchar_t osabi, Half mach, Conv_elfvalue_t value,
const conv_ds_t **dsp, Conv_fmt_flags_t fmt_flags, Conv_inv_buf_t *inv_buf,
const char *local_sgs_msg)
{
const conv_ds_t *ds;
for (ds = *dsp; ds != NULL; ds = *(++dsp)) {
if ((value < ds->ds_baseval) || (value > ds->ds_topval))
continue;
switch (ds->ds_type) {
case CONV_DS_MSGARR:
return (map_msg2str(inv_buf, value - ds->ds_baseval,
fmt_flags, ds->ds_topval - ds->ds_baseval + 1,
/*LINTED*/
((conv_ds_msg_t *)ds)->ds_msg,
local_sgs_msg));
case CONV_DS_VD:
return (map_vd2str(inv_buf, value, fmt_flags,
/*LINTED*/
((conv_ds_vd_t *)ds)->ds_vd,
local_sgs_msg));
case CONV_DS_VD2:
return (map_vd22str(inv_buf, osabi, mach, value,
fmt_flags,
/*LINTED*/
((conv_ds_vd2_t *)ds)->ds_vd2,
local_sgs_msg));
}
}
return (conv_invalid_val(inv_buf, value, fmt_flags));
}
/*
* Iterate over every message string in a given array of Msg codes,
* calling a user supplied callback for each one.
*
* entry:
* basevalue - Value corresponding to the first Msg in the array.
* local_sgs_msg - Pointer to the __sgs_msg array for the
* libconv module making the call.
* num_msg - # of items in array referenced by msg
* msg - Array of Msg indexes for the strings to iterate over.
* The value corresponding to each element of msg must be:
* value[i] = basevalue + i
* func, uvalue - User supplied function to be called for each
* string in msg. uvalue is an arbitrary user supplied pointer
* to be passed to func.
* local_sgs_msg - Pointer to the __sgs_msg array for the
* libconv module making the call.
*
* exit:
* The callback function is called for every non-zero item in
* msg[]. If any callback returns CONV_ITER_DONE, execution stops
* with that item and the function returns immediately. Otherwise,
* it continues to the end of the array.
*
* The value from the last callback is returned.
*/
/*ARGSUSED5*/
static conv_iter_ret_t
_conv_iter_msgarr(uint32_t basevalue, const Msg *msg, size_t num_msg,
conv_iter_cb_t func, void *uvalue, const char *local_sgs_msg)
{
for (; num_msg-- > 0; basevalue++, msg++) {
if (*msg != 0)
if ((* func)(MSG_ORIG_STRTAB(*msg, local_sgs_msg),
basevalue, uvalue) == CONV_ITER_DONE)
return (CONV_ITER_DONE);
}
return (CONV_ITER_CONT);
}
/*
* Iterate over every message string in a given array of Val_desc or
* Val_desc2 descriptors, calling a user supplied callback for each one.
*
* entry:
* osabi,mach (_conv_iter_vd2 only) - The osabi/mach for which
* strings are desired. Strings with a non-0 osabi or machine
* that do not match are quietly ignored.
* vdp - Pointer to NULL terminated array of Val_desc descriptors.
* func, uvalue - User supplied function to be called for each
* string in msg. uvalue is an arbitrary user supplied pointer
* to be passed to func.
* local_sgs_msg - Pointer to the __sgs_msg array for the
* libconv module making the call.
*
* exit:
* The callback function is called for every descriptor referenced by
* vdp. In the case of the OSABI-version of this function, strings from
* the wrong osabi are not used. If any callback returns CONV_ITER_DONE,
* execution stops with that item and the function returns immediately.
* Otherwise, it continues to the end of the array.
*
* The value from the last callback is returned.
*/
/*ARGSUSED3*/
conv_iter_ret_t
_conv_iter_vd(const Val_desc *vdp, conv_iter_cb_t func, void *uvalue,
const char *local_sgs_msg)
{
for (; vdp->v_msg; vdp++) {
if ((* func)(MSG_ORIG_STRTAB(vdp->v_msg, local_sgs_msg),
vdp->v_val, uvalue) == CONV_ITER_DONE)
return (CONV_ITER_DONE);
}
return (CONV_ITER_CONT);
}
/*ARGSUSED5*/
conv_iter_ret_t
_conv_iter_vd2(conv_iter_osabi_t osabi, Half mach, const Val_desc2 *vdp,
conv_iter_cb_t func, void *uvalue, const char *local_sgs_msg)
{
for (; vdp->v_msg; vdp++) {
if (CONV_ITER_VD2_SKIP(osabi, mach, vdp))
continue;
if ((* func)(MSG_ORIG_STRTAB(vdp->v_msg, local_sgs_msg),
vdp->v_val, uvalue) == CONV_ITER_DONE)
return (CONV_ITER_DONE);
}
return (CONV_ITER_CONT);
}
/*
* Process an array of conv_ds_XXX_t structures and call the appropriate
* iteration functions for the format of the strings given.
*/
conv_iter_ret_t
_conv_iter_ds(conv_iter_osabi_t osabi, Half mach, const conv_ds_t **dsp,
conv_iter_cb_t func, void *uvalue, const char *local_sgs_msg)
{
const conv_ds_t *ds;
for (ds = *dsp; ds != NULL; ds = *(++dsp)) {
switch (ds->ds_type) {
case CONV_DS_MSGARR:
if (_conv_iter_msgarr(ds->ds_baseval,
/*LINTED*/
((conv_ds_msg_t *)ds)->ds_msg,
ds->ds_topval - ds->ds_baseval + 1, func, uvalue,
local_sgs_msg) == CONV_ITER_DONE)
return (CONV_ITER_DONE);
break;
case CONV_DS_VD:
/*LINTED*/
if (_conv_iter_vd(((conv_ds_vd_t *)ds)->ds_vd,
func, uvalue, local_sgs_msg) == CONV_ITER_DONE)
return (CONV_ITER_DONE);
break;
case CONV_DS_VD2:
if (_conv_iter_vd2(osabi, mach,
/*LINTED*/
((conv_ds_vd2_t *)ds)->ds_vd2,
func, uvalue, local_sgs_msg) == CONV_ITER_DONE)
return (CONV_ITER_DONE);
break;
}
}
return (CONV_ITER_CONT);
}
/*
* Initialize the uvalue block prior to use of an interation function
* employing conv_iter_strtol().
*
* entry:
* str - String to be matched to a value
* uvalue - Pointer to uninitialized uvalue block
*
* exit:
* Initializes the uvalue block for use. Returns True (1) if a non-empty
* string was supplied, and False (0).
*/
int
conv_iter_strtol_init(const char *str, conv_strtol_uvalue_t *uvalue)
{
const char *tail;
while (conv_strproc_isspace(*str))
str++;
uvalue->csl_str = str;
uvalue->csl_found = 0;
tail = str + strlen(str);
while ((tail > str) && conv_strproc_isspace(*(tail - 1)))
tail--;
uvalue->csl_strlen = tail - str;
return (uvalue->csl_strlen > 0);
}
/*
* conv_iter_strtol() is used with iteration functions to map a string
* to the value of its corresponding ELF constant.
*
* entry:
* str - String supplied by this iteration
* value - Value of ELF constant corresponding to str
* uvalue - Pointer to conv_strtol_uvalue_t block previously
* initialized by a call to conv_iter_strtol_init().
*/
conv_iter_ret_t
conv_iter_strtol(const char *str, uint32_t value, void *uvalue)
{
conv_strtol_uvalue_t *state = (conv_strtol_uvalue_t *)uvalue;
if ((strlen(str) == state->csl_strlen) &&
(strncasecmp(str, state->csl_str, state->csl_strlen) == 0)) {
state->csl_found = 1;
state->csl_value = value;
return (CONV_ITER_DONE); /* Found it. Stop now. */
}
return (CONV_ITER_CONT); /* Keep looking */
}
|