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