summaryrefslogtreecommitdiff
path: root/usr/src/cmd/abi/spectrans/spec2trace/linkage.c
blob: 4d0f4c69a7f75d32436e510fe4cd5a40e14956fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1997-1999 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include "parser.h"
#include "trace.h"
#include "util.h"
#include "db.h"
#include "symtab.h"
#include "io.h"
#include "printfuncs.h"
#include "errlog.h"
#include "parseproto.h"

static void generate_interface_predeclaration(char *, ENTRY *);
static void generate_linkage_function(char *, char *);


/*
 * generate_linkage -- make code for the linkage part of an individual
 *	interface. Assumes Bodyfp.
 */
void
generate_linkage(ENTRY *function)
{
	char	*library_name = db_get_current_library(),
		*function_name;
	char	composite_name[MAXLINE];

	errlog(BEGIN, "generate_linkage() {");

	function_name = name_of(function);
	(void) snprintf(composite_name, sizeof (composite_name),
		"%s_%s", library_name, function_name);

	/* Print the predeclaration of the interceptor. */
	generate_interface_predeclaration(composite_name, function);
	/* Collect things we'll use more than once. */

	/* Next the struct used to pass parameters. */
	(void) fprintf(Bodyfp, "static abisym_t __abi_%s_%s_sym;\n",
		library_name, function_name);

	/* The linkage function, */
	generate_linkage_function(library_name, function_name);

	(void) fputs("\n\n", Bodyfp);
	errlog(END, "}");
}


/*
 *  generate_interface_predeclaration -- make things know so the compiler
 *	won't kak.
 */
static void
generate_interface_predeclaration(char *composite_name, ENTRY *function)
{
	decl_t *pp;
	char *p = symtab_get_prototype();
	char buf[BUFSIZ];

	(void) fprintf(Bodyfp, "\n/* from \"%s\", line %d */\n",
		symtab_get_filename(), line_of(function));
	(void) fprintf(Bodyfp, "static ");

	if (p[strlen(p)-1] != ';')
		(void) snprintf(buf, BUFSIZ, "%s;", strnormalize(p));
	else
		(void) snprintf(buf, BUFSIZ, "%s", strnormalize(p));

	decl_Parse(buf, &pp);
	decl_AddArgNames(pp);
	symtab_set_prototype(decl_ToString(buf, DTS_DECL, pp, composite_name));
	(void) fprintf(Bodyfp, "%s;\n", symtab_get_prototype());
	decl_Destroy(pp);
}



/*
 * generate_linkage_function --  The linkage function itself.
 */
static void
generate_linkage_function(char *lib, char *func)
{
	(void) fprintf(Bodyfp,
	    "void *__abi_%s_%s(void *real, int vflag) { \n", lib, func);
	(void) fprintf(Bodyfp, "    ABI_REAL(%s, %s) = real;\n", lib, func);
	(void) fprintf(Bodyfp, "    ABI_VFLAG(%s, %s) = vflag;\n", lib, func);
	(void) fprintf(Bodyfp,
	    "    return ((void *) %s_%s);\n}\n", lib, func);
}