xref: /illumos-gate/usr/src/cmd/abi/spectrans/spec2trace/linkage.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 #include <stdio.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include "parser.h"
31 #include "trace.h"
32 #include "util.h"
33 #include "db.h"
34 #include "symtab.h"
35 #include "io.h"
36 #include "printfuncs.h"
37 #include "errlog.h"
38 #include "parseproto.h"
39 
40 static void generate_interface_predeclaration(char *, ENTRY *);
41 static void generate_linkage_function(char *, char *);
42 
43 
44 /*
45  * generate_linkage -- make code for the linkage part of an individual
46  *	interface. Assumes Bodyfp.
47  */
48 void
generate_linkage(ENTRY * function)49 generate_linkage(ENTRY *function)
50 {
51 	char	*library_name = db_get_current_library(),
52 		*function_name;
53 	char	composite_name[MAXLINE];
54 
55 	errlog(BEGIN, "generate_linkage() {");
56 
57 	function_name = name_of(function);
58 	(void) snprintf(composite_name, sizeof (composite_name),
59 		"%s_%s", library_name, function_name);
60 
61 	/* Print the predeclaration of the interceptor. */
62 	generate_interface_predeclaration(composite_name, function);
63 	/* Collect things we'll use more than once. */
64 
65 	/* Next the struct used to pass parameters. */
66 	(void) fprintf(Bodyfp, "static abisym_t __abi_%s_%s_sym;\n",
67 		library_name, function_name);
68 
69 	/* The linkage function, */
70 	generate_linkage_function(library_name, function_name);
71 
72 	(void) fputs("\n\n", Bodyfp);
73 	errlog(END, "}");
74 }
75 
76 
77 /*
78  *  generate_interface_predeclaration -- make things know so the compiler
79  *	won't kak.
80  */
81 static void
generate_interface_predeclaration(char * composite_name,ENTRY * function)82 generate_interface_predeclaration(char *composite_name, ENTRY *function)
83 {
84 	decl_t *pp;
85 	char *p = symtab_get_prototype();
86 	char buf[BUFSIZ];
87 
88 	(void) fprintf(Bodyfp, "\n/* from \"%s\", line %d */\n",
89 		symtab_get_filename(), line_of(function));
90 	(void) fprintf(Bodyfp, "static ");
91 
92 	if (p[strlen(p)-1] != ';')
93 		(void) snprintf(buf, BUFSIZ, "%s;", strnormalize(p));
94 	else
95 		(void) snprintf(buf, BUFSIZ, "%s", strnormalize(p));
96 
97 	decl_Parse(buf, &pp);
98 	decl_AddArgNames(pp);
99 	symtab_set_prototype(decl_ToString(buf, DTS_DECL, pp, composite_name));
100 	(void) fprintf(Bodyfp, "%s;\n", symtab_get_prototype());
101 	decl_Destroy(pp);
102 }
103 
104 
105 
106 /*
107  * generate_linkage_function --  The linkage function itself.
108  */
109 static void
generate_linkage_function(char * lib,char * func)110 generate_linkage_function(char *lib, char *func)
111 {
112 	(void) fprintf(Bodyfp,
113 	    "void *__abi_%s_%s(void *real, int vflag) { \n", lib, func);
114 	(void) fprintf(Bodyfp, "    ABI_REAL(%s, %s) = real;\n", lib, func);
115 	(void) fprintf(Bodyfp, "    ABI_VFLAG(%s, %s) = vflag;\n", lib, func);
116 	(void) fprintf(Bodyfp,
117 	    "    return ((void *) %s_%s);\n}\n", lib, func);
118 }
119