xref: /titanic_52/usr/src/cmd/abi/spectrans/spec2trace/linkage.c (revision 2b24ab6b3865caeede9eeb9db6b83e1d89dcd1ea)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1997-1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include "parser.h"
34 #include "trace.h"
35 #include "util.h"
36 #include "db.h"
37 #include "symtab.h"
38 #include "io.h"
39 #include "printfuncs.h"
40 #include "errlog.h"
41 #include "parseproto.h"
42 
43 static void generate_interface_predeclaration(char *, ENTRY *);
44 static void generate_linkage_function(char *, char *);
45 
46 
47 /*
48  * generate_linkage -- make code for the linkage part of an individual
49  *	interface. Assumes Bodyfp.
50  */
51 void
52 generate_linkage(ENTRY *function)
53 {
54 	char	*library_name = db_get_current_library(),
55 		*function_name;
56 	char	composite_name[MAXLINE];
57 
58 	errlog(BEGIN, "generate_linkage() {");
59 
60 	function_name = name_of(function);
61 	(void) snprintf(composite_name, sizeof (composite_name),
62 		"%s_%s", library_name, function_name);
63 
64 	/* Print the predeclaration of the interceptor. */
65 	generate_interface_predeclaration(composite_name, function);
66 	/* Collect things we'll use more than once. */
67 
68 	/* Next the struct used to pass parameters. */
69 	(void) fprintf(Bodyfp, "static abisym_t __abi_%s_%s_sym;\n",
70 		library_name, function_name);
71 
72 	/* The linkage function, */
73 	generate_linkage_function(library_name, function_name);
74 
75 	(void) fputs("\n\n", Bodyfp);
76 	errlog(END, "}");
77 }
78 
79 
80 /*
81  *  generate_interface_predeclaration -- make things know so the compiler
82  *	won't kak.
83  */
84 static void
85 generate_interface_predeclaration(char *composite_name, ENTRY *function)
86 {
87 	decl_t *pp;
88 	char *p = symtab_get_prototype();
89 	char buf[BUFSIZ];
90 
91 	(void) fprintf(Bodyfp, "\n/* from \"%s\", line %d */\n",
92 		symtab_get_filename(), line_of(function));
93 	(void) fprintf(Bodyfp, "static ");
94 
95 	if (p[strlen(p)-1] != ';')
96 		(void) snprintf(buf, BUFSIZ, "%s;", strnormalize(p));
97 	else
98 		(void) snprintf(buf, BUFSIZ, "%s", strnormalize(p));
99 
100 	decl_Parse(buf, &pp);
101 	decl_AddArgNames(pp);
102 	symtab_set_prototype(decl_ToString(buf, DTS_DECL, pp, composite_name));
103 	(void) fprintf(Bodyfp, "%s;\n", symtab_get_prototype());
104 	decl_Destroy(pp);
105 }
106 
107 
108 
109 /*
110  * generate_linkage_function --  The linkage function itself.
111  */
112 static void
113 generate_linkage_function(char *lib, char *func)
114 {
115 	(void) fprintf(Bodyfp,
116 	    "void *__abi_%s_%s(void *real, int vflag) { \n", lib, func);
117 	(void) fprintf(Bodyfp, "    ABI_REAL(%s, %s) = real;\n", lib, func);
118 	(void) fprintf(Bodyfp, "    ABI_VFLAG(%s, %s) = vflag;\n", lib, func);
119 	(void) fprintf(Bodyfp,
120 	    "    return ((void *) %s_%s);\n}\n", lib, func);
121 }
122