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