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