xref: /illumos-gate/usr/src/lib/libdtrace/common/dlink_init.c (revision 0ccfe5834c3b867c1b6c273cae02ec8922bc0fd2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright (c) 2012 by Delphix. All rights reserved.
29  */
30 
31 /*
32  * This is the basis for drti.o which dt_link.c links into the object file
33  * generated by dtrace(8) -G by default (note that -xlazyload disables this).
34  */
35 
36 #include <dlfcn.h>
37 #include <stdlib.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 
41 #include <dlink.h>
42 
43 static int gen;			/* DOF helper generation */
44 extern dof_hdr_t __SUNW_dof;	/* DOF defined in the .SUNW_dof section */
45 
46 #pragma init(dtrace_drti_init)
47 static void
48 dtrace_drti_init(void)
49 {
50 	Link_map *lmp;
51 	Lmid_t lmid;
52 
53 	dtrace_link_init();
54 
55 	if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
56 		dprintf(1, "couldn't discover module name or address\n");
57 		return;
58 	}
59 
60 	if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
61 		dprintf(1, "couldn't discover link map ID\n");
62 		return;
63 	}
64 
65 	dtrace_link_dof(&__SUNW_dof, lmid, lmp->l_name, lmp->l_addr);
66 }
67 
68 #pragma fini(dtrace_drti_fini)
69 static void
70 dtrace_drti_fini(void)
71 {
72 	int fd;
73 
74 	if ((fd = open64(devname, O_RDWR)) < 0) {
75 		dprintf(1, "failed to open helper device %s", devname);
76 		return;
77 	}
78 
79 	if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, gen)) == -1)
80 		dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
81 	else
82 		dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen);
83 
84 	(void) close(fd);
85 }
86