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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 2018, Joyent, Inc.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <proc_service.h>
#include <link.h>
#include <rtld_db.h>
#include <rtld.h>
#include <_rtld_db.h>
#include <msg.h>
#include <sys/param.h>
/*
* Mutex to protect global data
*/
mutex_t glob_mutex = DEFAULTMUTEX;
int rtld_db_version = RD_VERSION1;
int rtld_db_logging = 0;
char rtld_db_helper_path[MAXPATHLEN];
void
rd_log(const int on_off)
{
(void) mutex_lock(&glob_mutex);
rtld_db_logging = on_off;
(void) mutex_unlock(&glob_mutex);
LOG(ps_plog(MSG_ORIG(MSG_DB_LOGENABLE)));
}
/*
* Versioning Notes.
*
* The following have been added as the versions of librtld_db
* have grown:
*
* RD_VERSION1:
* o baseline version
*
* RD_VERSION2:
* o added support for the use of the AT_SUN_LDBASE auxvector
* to find the initialial debugging (r_debug) structures
* in ld.so.1
* o added the rl_dynamic field to rd_loadobj_t
* o added the RD_FLG_MEM_OBJECT to be used with the
* rl_dynamic->rl_flags field.
*
* RD_VERSION3:
* o added the following fields/flags to the rd_plt_info_t
* type:
* pi_baddr - bound address of PLT (if bound)
* pi_flags - flag field
* RD_FLG_PI_PLTBOUND (flag for pi_flags)
* if set - the PLT is bound and pi_baddr
* is filled in with the destination of the PLT.
*
* RD_VERSION4:
* o added the following field to the rd_loadobj_t structure:
* rl_tlsmodid - module ID for TLS references
*/
rd_err_e
rd_init(int version)
{
if ((version < RD_VERSION1) ||
(version > RD_VERSION))
return (RD_NOCAPAB);
rtld_db_version = version;
LOG(ps_plog(MSG_ORIG(MSG_DB_RDINIT), rtld_db_version));
return (RD_OK);
}
rd_err_e
rd_ctl(int cmd, void *arg)
{
if (cmd != RD_CTL_SET_HELPPATH || arg == NULL ||
strlen((char *)arg) >= MAXPATHLEN)
return (RD_ERR);
(void) strcpy(rtld_db_helper_path, (char *)arg);
return (RD_OK);
}
rd_err_e
rd_get_dyns(rd_agent_t *rap, psaddr_t addr, void **dynpp, size_t *dynpp_sz)
{
if (rap->rd_helper.rh_ops != NULL)
return (rap->rd_helper.rh_ops->rho_get_dyns(
rap->rd_helper.rh_data, addr, dynpp, dynpp_sz));
#ifdef _LP64
if (rap->rd_dmodel == PR_MODEL_LP64)
return (_rd_get_dyns64(rap,
addr, (Elf64_Dyn **)dynpp, dynpp_sz));
else
#endif
return (_rd_get_dyns32(rap,
addr, (Dyn **)dynpp, dynpp_sz));
}
rd_err_e
rd_reset(struct rd_agent *rap)
{
rd_err_e err;
RDAGLOCK(rap);
rap->rd_flags = 0;
#ifdef _LP64
/*
* Determine if client is 32-bit or 64-bit.
*/
if (ps_pdmodel(rap->rd_psp, &rap->rd_dmodel) != PS_OK) {
LOG(ps_plog(MSG_ORIG(MSG_DB_DMLOOKFAIL)));
RDAGUNLOCK(rap);
return (RD_DBERR);
}
if (rap->rd_dmodel == PR_MODEL_LP64)
err = _rd_reset64(rap);
else
#endif
err = _rd_reset32(rap);
RDAGUNLOCK(rap);
return (err);
}
rd_agent_t *
rd_new(struct ps_prochandle *php)
{
rd_agent_t *rap;
LOG(ps_plog(MSG_ORIG(MSG_DB_RDNEW), php));
if ((rap = (rd_agent_t *)calloc(1, sizeof (rd_agent_t))) == NULL)
return (0);
rap->rd_psp = php;
(void) mutex_init(&rap->rd_mutex, USYNC_THREAD, 0);
if (rd_reset(rap) != RD_OK) {
if (rap->rd_helper.rh_dlhandle != NULL) {
rap->rd_helper.rh_ops->rho_fini(rap->rd_helper.rh_data);
(void) dlclose(rap->rd_helper.rh_dlhandle);
}
free(rap);
LOG(ps_plog(MSG_ORIG(MSG_DB_RESETFAIL)));
return ((rd_agent_t *)0);
}
return (rap);
}
void
rd_delete(rd_agent_t *rap)
{
LOG(ps_plog(MSG_ORIG(MSG_DB_RDDELETE), rap));
if (rap->rd_helper.rh_dlhandle != NULL) {
rap->rd_helper.rh_ops->rho_fini(rap->rd_helper.rh_data);
(void) dlclose(rap->rd_helper.rh_dlhandle);
}
free(rap);
}
rd_err_e
rd_loadobj_iter(rd_agent_t *rap, rl_iter_f *cb, void *client_data)
{
rd_err_e err;
RDAGLOCK(rap);
#ifdef _LP64
if (rap->rd_dmodel == PR_MODEL_LP64)
err = _rd_loadobj_iter64(rap, cb, client_data);
else
#endif
err = _rd_loadobj_iter32(rap, cb, client_data);
RDAGUNLOCK(rap);
return (err);
}
rd_err_e
rd_plt_resolution(rd_agent_t *rap, psaddr_t pc, lwpid_t lwpid,
psaddr_t pltbase, rd_plt_info_t *rpi)
{
rd_err_e err;
RDAGLOCK(rap);
#ifdef _LP64
if (rap->rd_dmodel == PR_MODEL_LP64)
err = plt64_resolution(rap, pc, lwpid, pltbase,
rpi);
else
#endif
err = plt32_resolution(rap, pc, lwpid, pltbase,
rpi);
RDAGUNLOCK(rap);
return (err);
}
rd_err_e
rd_event_addr(rd_agent_t *rap, rd_event_e num, rd_notify_t *np)
{
rd_err_e rc = RD_OK;
RDAGLOCK(rap);
switch (num) {
case RD_NONE:
break;
case RD_PREINIT:
np->type = RD_NOTIFY_BPT;
np->u.bptaddr = rap->rd_preinit;
break;
case RD_POSTINIT:
np->type = RD_NOTIFY_BPT;
np->u.bptaddr = rap->rd_postinit;
break;
case RD_DLACTIVITY:
np->type = RD_NOTIFY_BPT;
np->u.bptaddr = rap->rd_dlact;
break;
default:
LOG(ps_plog(MSG_ORIG(MSG_DB_UNEXPEVENT), num));
rc = RD_ERR;
break;
}
if (rc == RD_OK) {
LOG(ps_plog(MSG_ORIG(MSG_DB_RDEVENTADDR), num,
EC_ADDR(np->u.bptaddr)));
}
RDAGUNLOCK(rap);
return (rc);
}
/* ARGSUSED 0 */
rd_err_e
rd_event_enable(rd_agent_t *rap, int onoff)
{
rd_err_e err;
RDAGLOCK(rap);
#ifdef _LP64
if (rap->rd_dmodel == PR_MODEL_LP64)
err = _rd_event_enable64(rap, onoff);
else
#endif
err = _rd_event_enable32(rap, onoff);
RDAGUNLOCK(rap);
return (err);
}
rd_err_e
rd_event_getmsg(rd_agent_t *rap, rd_event_msg_t *emsg)
{
rd_err_e err;
RDAGLOCK(rap);
#ifdef _LP64
if (rap->rd_dmodel == PR_MODEL_LP64)
err = _rd_event_getmsg64(rap, emsg);
else
#endif
err = _rd_event_getmsg32(rap, emsg);
RDAGUNLOCK(rap);
return (err);
}
rd_err_e
rd_binder_exit_addr(struct rd_agent *rap, const char *bname, psaddr_t *beaddr)
{
ps_sym_t sym;
if (rap->rd_tbinder) {
*beaddr = rap->rd_tbinder;
return (RD_OK);
}
if (ps_pglobal_sym(rap->rd_psp, PS_OBJ_LDSO, bname, &sym) != PS_OK) {
LOG(ps_plog(MSG_ORIG(MSG_DB_UNFNDSYM),
bname));
return (RD_ERR);
}
rap->rd_tbinder = *beaddr = sym.st_value + sym.st_size - M_BIND_ADJ;
return (RD_OK);
}
rd_err_e
rd_objpad_enable(struct rd_agent *rap, size_t padsize)
{
rd_err_e err;
RDAGLOCK(rap);
#ifdef _LP64
if (rap->rd_dmodel == PR_MODEL_LP64)
err = _rd_objpad_enable64(rap, padsize);
else
#endif
err = _rd_objpad_enable32(rap, padsize);
RDAGUNLOCK(rap);
return (err);
}
char *
rd_errstr(rd_err_e rderr)
{
/*
* Convert an 'rd_err_e' to a string
*/
switch (rderr) {
case RD_OK:
return ((char *)MSG_ORIG(MSG_ER_OK));
case RD_ERR:
return ((char *)MSG_ORIG(MSG_ER_ERR));
case RD_DBERR:
return ((char *)MSG_ORIG(MSG_ER_DBERR));
case RD_NOCAPAB:
return ((char *)MSG_ORIG(MSG_ER_NOCAPAB));
case RD_NODYNAM:
return ((char *)MSG_ORIG(MSG_ER_NODYNAM));
case RD_NOBASE:
return ((char *)MSG_ORIG(MSG_ER_NOBASE));
case RD_NOMAPS:
return ((char *)MSG_ORIG(MSG_ER_NOMAPS));
default:
return ((char *)MSG_ORIG(MSG_ER_DEFAULT));
}
}
|