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
|
/*
* 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 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <dlfcn.h>
#include <link.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <libintl.h>
#include <locale.h>
#include <conv.h>
#include <msg.h>
void
locale()
{
static int localeinit = 0;
/*
* Defer localization overhead until a localized message, is required.
* For successful specific (32-bit or 64-bit) commands, none of this
* overhead should be incurred.
*/
if (localeinit++)
return;
(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
}
const char *
_moe_msg(Msg mid)
{
return (gettext(MSG_ORIG(mid)));
}
/*
* Error messages from ld.so.1 are formatted as:
*
* ld.so.1: app-name: fatal: ....
*
* Here, we skip over these three components. Thus the message a little less
* hostile when displayed by moe(). Really, it would be nice to have some
* flexibility over what ld.so.1 displays.
*/
static char *
trim_msg(char *str)
{
char *ptr = str;
int cnt = 0;
/*
* Skip the first three components.
*/
while (*ptr) {
if (*ptr == ':') {
if (++cnt == 3)
break;
}
ptr++;
}
if (*ptr == '\0')
return (str);
else
return (ptr + 2);
}
#define ONLY32 1
#define ONLY64 2
static int
openlib(const char *prog, const char *name, int class, int silent, int verbose)
{
void *handle;
const char *modestr;
/*
* If the class of object is required, localize the prefix message.
*/
if (class) {
locale();
#if defined(_LP64)
modestr = MSG_INTL(MSG_PRE_64);
#else
modestr = MSG_INTL(MSG_PRE_32);
#endif
} else
modestr = MSG_ORIG(MSG_STR_EMPTY);
/*
* Open the optimal object, and determine its full name from the
* returned handle. Borrow the internal mode, RTLD_CONFGEN, from
* crle(1). This flag allows us to process incomplete objects, as
* would occur if the object couldn't find its dependencies or relocate
* itself.
*/
if ((handle = dlmopen(LM_ID_NEWLM, name,
(RTLD_FIRST | RTLD_CONFGEN | RTLD_LAZY))) == 0) {
if (verbose) {
(void) fprintf(stderr, MSG_ORIG(MSG_FMT_VERBOSE), prog,
modestr, trim_msg(dlerror()));
(void) fflush(stderr);
}
return (1);
}
if (silent == 0) {
Link_map *lmp;
if (dlinfo(handle, RTLD_DI_LINKMAP, &lmp) == -1) {
if (verbose) {
(void) fprintf(stderr,
MSG_ORIG(MSG_FMT_VERBOSE), prog, modestr,
trim_msg(dlerror()));
(void) fflush(stderr);
}
return (1);
}
if (verbose)
(void) printf(MSG_ORIG(MSG_FMT_VERBOSE), prog, modestr,
lmp->l_name);
else
(void) printf(MSG_ORIG(MSG_FMT_SIMPLE), modestr,
lmp->l_name);
(void) fflush(stdout);
}
(void) dlclose(handle);
return (0);
}
int
/* ARGSUSED2 */
main(int argc, char **argv, char **envp)
{
int var, verbose = 0, silent = 0, error = 0, mode = 0, class = 0;
char *prog;
if ((prog = strrchr(argv[0], '/')) == 0)
prog = argv[0];
else
prog++;
opterr = 0;
while ((var = getopt(argc, argv, MSG_ORIG(MSG_STR_OPTIONS))) != EOF) {
switch (var) {
case 'c':
class++;
break;
case '3':
#if !defined(_LP64)
if ((optarg[0] == '2') && (mode == 0))
mode = ONLY32;
else
#endif
error++;
break;
case '6':
if ((optarg[0] == '4') && (mode == 0))
mode = ONLY64;
else
error++;
break;
case 's':
if (verbose == 0)
silent++;
else
error++;
break;
case 'v':
if (silent == 0)
verbose++;
else
error++;
break;
case '?':
error++;
break;
default:
break;
}
}
if (error || ((argc - optind) == 0)) {
locale();
(void) fprintf(stderr, MSG_INTL(MSG_ARG_USAGE), prog);
return (1);
}
if (silent)
class = 0;
/*
* Process any 32-bit expansion.
*/
#if !defined(_LP64)
if (mode != ONLY64) {
#endif
if (openlib(prog, argv[optind], class, silent, verbose) != 0) {
if (mode)
error++;
}
#if !defined(_LP64)
}
#endif
if (mode == ONLY32)
return (error);
/*
* Re-exec ourselves to process any 64-bit expansion.
*/
#if !defined(__sparcv9) && !defined(__amd64)
(void) conv_check_native(argv, envp);
#endif
return (error);
}
|