1*cdcc71c0SAli Bahrami /*
2*cdcc71c0SAli Bahrami * CDDL HEADER START
3*cdcc71c0SAli Bahrami *
4*cdcc71c0SAli Bahrami * The contents of this file are subject to the terms of the
5*cdcc71c0SAli Bahrami * Common Development and Distribution License (the "License").
6*cdcc71c0SAli Bahrami * You may not use this file except in compliance with the License.
7*cdcc71c0SAli Bahrami *
8*cdcc71c0SAli Bahrami * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*cdcc71c0SAli Bahrami * or http://www.opensolaris.org/os/licensing.
10*cdcc71c0SAli Bahrami * See the License for the specific language governing permissions
11*cdcc71c0SAli Bahrami * and limitations under the License.
12*cdcc71c0SAli Bahrami *
13*cdcc71c0SAli Bahrami * When distributing Covered Code, include this CDDL HEADER in each
14*cdcc71c0SAli Bahrami * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*cdcc71c0SAli Bahrami * If applicable, add the following below this CDDL HEADER, with the
16*cdcc71c0SAli Bahrami * fields enclosed by brackets "[]" replaced with your own identifying
17*cdcc71c0SAli Bahrami * information: Portions Copyright [yyyy] [name of copyright owner]
18*cdcc71c0SAli Bahrami *
19*cdcc71c0SAli Bahrami * CDDL HEADER END
20*cdcc71c0SAli Bahrami */
21*cdcc71c0SAli Bahrami
22*cdcc71c0SAli Bahrami /*
23*cdcc71c0SAli Bahrami * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24*cdcc71c0SAli Bahrami * Use is subject to license terms.
25*cdcc71c0SAli Bahrami */
26*cdcc71c0SAli Bahrami
27*cdcc71c0SAli Bahrami #include <unistd.h>
28*cdcc71c0SAli Bahrami #include <stdio.h>
29*cdcc71c0SAli Bahrami #include <stdarg.h>
30*cdcc71c0SAli Bahrami #include "msg.h"
31*cdcc71c0SAli Bahrami #include "_libld.h"
32*cdcc71c0SAli Bahrami
33*cdcc71c0SAli Bahrami /*
34*cdcc71c0SAli Bahrami * GNU ld --wrap support, also known as -z wrap.
35*cdcc71c0SAli Bahrami *
36*cdcc71c0SAli Bahrami * We maintain an AVL tree of wrapped symbol names. Every undefined
37*cdcc71c0SAli Bahrami * symbol is tested against this tree, and those that match have
38*cdcc71c0SAli Bahrami * their names modified to produce the wrapping effect:
39*cdcc71c0SAli Bahrami *
40*cdcc71c0SAli Bahrami * - An undefined reference to XXX is converted to __wrap_XXX
41*cdcc71c0SAli Bahrami * - An undefined reference to __real_XXX is converted to XXX
42*cdcc71c0SAli Bahrami *
43*cdcc71c0SAli Bahrami * This operation has a cost, but that is mitigated by two factors:
44*cdcc71c0SAli Bahrami *
45*cdcc71c0SAli Bahrami * - This is a test feature, not used for production code, so somewhat
46*cdcc71c0SAli Bahrami * longer link times are tolerable.
47*cdcc71c0SAli Bahrami * - The cost of this feature is only paid when it is used. Otherwise,
48*cdcc71c0SAli Bahrami * the sole overhead is the cost of testing the NULL AVL tree pointer
49*cdcc71c0SAli Bahrami * during symbol processing.
50*cdcc71c0SAli Bahrami */
51*cdcc71c0SAli Bahrami
52*cdcc71c0SAli Bahrami
53*cdcc71c0SAli Bahrami /*
54*cdcc71c0SAli Bahrami * AVL comparison function for WrapSymNode items.
55*cdcc71c0SAli Bahrami *
56*cdcc71c0SAli Bahrami * entry:
57*cdcc71c0SAli Bahrami * n1, n2 - pointers to nodes to be compared
58*cdcc71c0SAli Bahrami *
59*cdcc71c0SAli Bahrami * exit:
60*cdcc71c0SAli Bahrami * Returns -1 if (n1 < n2), 0 if they are equal, and 1 if (n1 > n2)
61*cdcc71c0SAli Bahrami */
62*cdcc71c0SAli Bahrami static int
wrap_cmp(const void * n1,const void * n2)63*cdcc71c0SAli Bahrami wrap_cmp(const void *n1, const void *n2)
64*cdcc71c0SAli Bahrami {
65*cdcc71c0SAli Bahrami int rc;
66*cdcc71c0SAli Bahrami
67*cdcc71c0SAli Bahrami rc = strcmp(((WrapSymNode *)n1)->wsn_name,
68*cdcc71c0SAli Bahrami ((WrapSymNode *)n2)->wsn_name);
69*cdcc71c0SAli Bahrami
70*cdcc71c0SAli Bahrami if (rc > 0)
71*cdcc71c0SAli Bahrami return (1);
72*cdcc71c0SAli Bahrami if (rc < 0)
73*cdcc71c0SAli Bahrami return (-1);
74*cdcc71c0SAli Bahrami return (0);
75*cdcc71c0SAli Bahrami }
76*cdcc71c0SAli Bahrami
77*cdcc71c0SAli Bahrami /*
78*cdcc71c0SAli Bahrami * Enter a -z wrap symbol into the ofl_wrap AVL tree
79*cdcc71c0SAli Bahrami *
80*cdcc71c0SAli Bahrami * entry:
81*cdcc71c0SAli Bahrami * ofl - Output file descriptor
82*cdcc71c0SAli Bahrami * name - Name of symbol to be entered. Caller must ensure that
83*cdcc71c0SAli Bahrami * memory used to hold name remains available for the life
84*cdcc71c0SAli Bahrami * of the link-edit process.
85*cdcc71c0SAli Bahrami *
86*cdcc71c0SAli Bahrami * exit:
87*cdcc71c0SAli Bahrami * On success, updates ofl->wrap_cache with a pointer to the
88*cdcc71c0SAli Bahrami * resulting WrapSymNode, and returns that pointer. On failure,
89*cdcc71c0SAli Bahrami * returns NULL.
90*cdcc71c0SAli Bahrami */
91*cdcc71c0SAli Bahrami WrapSymNode *
ld_wrap_enter(Ofl_desc * ofl,const char * name)92*cdcc71c0SAli Bahrami ld_wrap_enter(Ofl_desc *ofl, const char *name)
93*cdcc71c0SAli Bahrami {
94*cdcc71c0SAli Bahrami WrapSymNode *wsnp, wsn;
95*cdcc71c0SAli Bahrami avl_index_t where;
96*cdcc71c0SAli Bahrami size_t name_len, wrapname_len;
97*cdcc71c0SAli Bahrami char *tmpname;
98*cdcc71c0SAli Bahrami
99*cdcc71c0SAli Bahrami /* If this is the first wrap symbol, create the AVL tree */
100*cdcc71c0SAli Bahrami if (ofl->ofl_wrap == NULL) {
101*cdcc71c0SAli Bahrami ofl->ofl_wrap = libld_calloc(1, sizeof (*ofl->ofl_wrap));
102*cdcc71c0SAli Bahrami if (ofl->ofl_wrap == NULL)
103*cdcc71c0SAli Bahrami return (NULL);
104*cdcc71c0SAli Bahrami avl_create(ofl->ofl_wrap, wrap_cmp, sizeof (WrapSymNode),
105*cdcc71c0SAli Bahrami SGSOFFSETOF(WrapSymNode, wsn_avlnode));
106*cdcc71c0SAli Bahrami }
107*cdcc71c0SAli Bahrami
108*cdcc71c0SAli Bahrami /* Have we already entered this one? */
109*cdcc71c0SAli Bahrami wsn.wsn_name = name;
110*cdcc71c0SAli Bahrami if ((wsnp = avl_find(ofl->ofl_wrap, &wsn, &where)) != NULL)
111*cdcc71c0SAli Bahrami return (wsnp);
112*cdcc71c0SAli Bahrami
113*cdcc71c0SAli Bahrami /*
114*cdcc71c0SAli Bahrami * Allocate a new node, along with room for the wrapped name.
115*cdcc71c0SAli Bahrami * Since strings have byte alignment, we can allocate it immediately
116*cdcc71c0SAli Bahrami * following the AVL node without the need for alignment padding.
117*cdcc71c0SAli Bahrami */
118*cdcc71c0SAli Bahrami name_len = strlen(wsn.wsn_name);
119*cdcc71c0SAli Bahrami wrapname_len = MSG_STR_UU_WRAP_U_SIZE + name_len + 1;
120*cdcc71c0SAli Bahrami if ((wsnp = libld_calloc(1, sizeof (*wsnp) + wrapname_len)) == NULL)
121*cdcc71c0SAli Bahrami return (NULL);
122*cdcc71c0SAli Bahrami wsnp->wsn_name = name;
123*cdcc71c0SAli Bahrami
124*cdcc71c0SAli Bahrami wsnp->wsn_wrapname = tmpname = (char *)(wsnp + 1);
125*cdcc71c0SAli Bahrami (void) snprintf(tmpname, wrapname_len, MSG_ORIG(MSG_FMT_STRCAT),
126*cdcc71c0SAli Bahrami MSG_ORIG(MSG_STR_UU_WRAP_U), name);
127*cdcc71c0SAli Bahrami
128*cdcc71c0SAli Bahrami /* Insert the new node */
129*cdcc71c0SAli Bahrami avl_insert(ofl->ofl_wrap, wsnp, where);
130*cdcc71c0SAli Bahrami return (wsnp);
131*cdcc71c0SAli Bahrami }
132