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 (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#pragma ident	"%Z%%M%	%I%	%E% SMI"
27
28/*
29 * Workarounds for stabs generation bugs in the compiler and general needed
30 * fixups.
31 */
32
33#if HAVE_NBTOOL_CONFIG_H
34# include "nbtool_config.h"
35#endif
36
37#include <stdio.h>
38#include <strings.h>
39
40#include "ctf_headers.h"
41#include "ctftools.h"
42#include "hash.h"
43#include "memory.h"
44
45/*
46 * Due to 4432619, the 6.1 compiler will sometimes incorrectly generate pointer
47 * stabs.  Given a struct foo, and a corresponding typedef struct foo foo_t.
48 * In some cases, when faced with a pointer to a foo_t, the compiler will
49 * sometimes generate a stab that describes a pointer to a struct foo.
50 * Regardless of correctness, this breaks merges, as it occurs inconsistently
51 * by file.  The following two routines know how to recognize and repair foo_t *
52 * and foo_t ** bugs in a specific set of cases.  There is no general way to
53 * solve this problem without a fix to the compiler.  In general, cases should
54 * only be added to these routines to fix merging problems in genunix.
55 */
56static void
57fix_ptrptr_to_struct(tdata_t *td)
58{
59	const char *strs[2] = { "as", "fdbuffer" };
60	const char *mems[2] = { "a_objectdir", "fd_shadow" };
61	const char *acts[2] = { "vnode", "page" };
62	const char *tgts[2] = { "vnode_t", "page_t" };
63	tdesc_t *str;
64	tdesc_t *act, *tgt;
65	tdesc_t *p1, *p2;
66	mlist_t *ml;
67	int i;
68
69	for (i = 0; i < (int) (sizeof (strs) / sizeof (strs[0])); i++) {
70		if (!(str = lookupname(strs[i])) || str->t_type != STRUCT)
71			continue;
72
73		for (ml = str->t_members; ml; ml = ml->ml_next) {
74			if (streq(ml->ml_name, mems[i]))
75				break;
76		}
77		if (!ml)
78			continue;
79
80		if (ml->ml_type->t_type != POINTER || ml->ml_type->t_name ||
81		    ml->ml_type->t_tdesc->t_type != POINTER ||
82		    ml->ml_type->t_tdesc->t_name)
83			continue;
84
85		act = ml->ml_type->t_tdesc->t_tdesc;
86		if (act->t_type != STRUCT || !streq(act->t_name, acts[i]))
87			continue;
88
89		if (!(tgt = lookupname(tgts[i])) || tgt->t_type != TYPEDEF)
90			continue;
91
92		/* We have an instance of the bug */
93		p2 = xcalloc(sizeof (*p2));
94		p2->t_type = POINTER;
95		p2->t_id = td->td_nextid++;
96		p2->t_tdesc = tgt;
97
98		p1 = xcalloc(sizeof (*p1));
99		p1->t_type = POINTER;
100		p1->t_id = td->td_nextid++;
101		p1->t_tdesc = p2;
102
103		ml->ml_type = p1;
104
105		debug(3, "Fixed %s->%s => ptrptr struct %s bug\n",
106		    strs[i], mems[i], acts[i]);
107	}
108}
109
110static void
111fix_ptr_to_struct(tdata_t *td)
112{
113	const char *strs[2] = { "vmem", "id_space" };
114	const char *mems[2] = { NULL, "is_vmem" };
115	tdesc_t *ptr = NULL;
116	tdesc_t *str, *vmt;
117	mlist_t *ml;
118	int i;
119
120	if ((vmt = lookupname("vmem_t")) == NULL || vmt->t_type != TYPEDEF)
121		return;
122
123	for (i = 0; i < (int) (sizeof (strs) / sizeof (strs[0])); i++) {
124		if (!(str = lookupname(strs[i])) || str->t_type != STRUCT)
125			continue;
126
127		for (ml = str->t_members; ml; ml = ml->ml_next) {
128			if (mems[i] && !streq(ml->ml_name, mems[i]))
129				continue;
130
131			if (ml->ml_type->t_type != POINTER ||
132			    ml->ml_type->t_name ||
133			    (ml->ml_type->t_tdesc->t_type != STRUCT &&
134			    ml->ml_type->t_tdesc->t_type != FORWARD) ||
135			    !streq(ml->ml_type->t_tdesc->t_name, "vmem"))
136				continue;
137
138			debug(3, "Fixed %s->%s => ptr struct vmem bug\n",
139			    strs[i], ml->ml_name);
140
141			if (!ptr) {
142				ptr = xcalloc(sizeof (*ptr));
143				ptr->t_type = POINTER;
144				ptr->t_id = td->td_nextid++;
145				ptr->t_tdesc = vmt;
146			}
147
148			ml->ml_type = ptr;
149		}
150	}
151}
152
153/*
154 * Fix stabs generation bugs.  These routines must be run before the
155 * post-conversion merge
156 */
157void
158cvt_fixstabs(tdata_t *td)
159{
160	fix_ptrptr_to_struct(td);
161	fix_ptr_to_struct(td);
162}
163
164struct match {
165	tdesc_t *m_ret;
166	const char *m_name;
167};
168
169static int
170matching_iidesc(void *arg1, void *arg2)
171{
172	iidesc_t *iidesc = arg1;
173	struct match *match = arg2;
174	if (!streq(iidesc->ii_name, match->m_name))
175		return (0);
176
177	if (iidesc->ii_type != II_TYPE && iidesc->ii_type != II_SOU)
178		return (0);
179
180	match->m_ret = iidesc->ii_dtype;
181	return (-1);
182}
183
184static tdesc_t *
185lookup_tdesc(tdata_t *td, char const *name)
186{
187	struct match match = { NULL, name };
188	iter_iidescs_by_name(td, name, matching_iidesc, &match);
189	return (match.m_ret);
190}
191
192/*
193 * The cpu structure grows, with the addition of a machcpu member, if
194 * _MACHDEP is defined.  This means that, for example, the cpu structure
195 * in unix is different from the cpu structure in genunix.  As one might
196 * expect, this causes merges to fail.  Since everyone indirectly contains
197 * a pointer to a CPU structure, the failed merges can cause massive amounts
198 * of duplication.  In the case of unix uniquifying against genunix, upwards
199 * of 50% of the structures were unmerged due to this problem.  We fix this
200 * by adding a cpu_m member.  If machcpu hasn't been defined in our module,
201 * we make a forward node for it.
202 */
203static void
204fix_small_cpu_struct(tdata_t *td, size_t ptrsize)
205{
206	tdesc_t *cput, *cpu;
207	tdesc_t *machcpu;
208	mlist_t *ml, *lml;
209	mlist_t *cpum;
210	int foundcpucyc = 0;
211
212	/*
213	 * We're going to take the circuitous route finding the cpu structure,
214	 * because we want to make sure that we find the right one.  It would
215	 * be nice if we could verify the header name too.  DWARF might not
216	 * have the cpu_t, so we let this pass.
217	 */
218	if ((cput = lookup_tdesc(td, "cpu_t")) != NULL) {
219		if (cput->t_type != TYPEDEF)
220			return;
221		cpu = cput->t_tdesc;
222	} else {
223		cpu = lookup_tdesc(td, "cpu");
224	}
225
226	if (cpu == NULL)
227		return;
228
229	if (!streq(cpu->t_name, "cpu") || cpu->t_type != STRUCT)
230		return;
231
232	for (ml = cpu->t_members, lml = NULL; ml;
233	    lml = ml, ml = ml->ml_next) {
234		if (strcmp(ml->ml_name, "cpu_cyclic") == 0)
235			foundcpucyc = 1;
236	}
237
238	if (foundcpucyc == 0 || lml == NULL ||
239	    strcmp(lml->ml_name, "cpu_m") == 0)
240		return;
241
242	/*
243	 * We need to derive the right offset for the fake cpu_m member.  To do
244	 * that, we require a special unused member to be the last member
245	 * before the 'cpu_m', that we encode knowledge of here.  ABI alignment
246	 * on all platforms is such that we only need to add a pointer-size
247	 * number of bits to get the right offset for cpu_m.  This would most
248	 * likely break if gcc's -malign-double were ever used, but that option
249	 * breaks the ABI anyway.
250	 */
251	if (!streq(lml->ml_name, "cpu_m_pad") &&
252	    getenv("CTFCONVERT_PERMISSIVE") == NULL) {
253		terminate("last cpu_t member before cpu_m is %s; "
254		    "it must be cpu_m_pad.\n", lml->ml_name);
255	}
256
257	if ((machcpu = lookup_tdesc(td, "machcpu")) == NULL) {
258		machcpu = xcalloc(sizeof (*machcpu));
259		machcpu->t_name = xstrdup("machcpu");
260		machcpu->t_id = td->td_nextid++;
261		machcpu->t_type = FORWARD;
262	} else if (machcpu->t_type != STRUCT) {
263		return;
264	}
265
266	debug(3, "Adding cpu_m machcpu %s to cpu struct\n",
267	    (machcpu->t_type == FORWARD ? "forward" : "struct"));
268
269	cpum = xmalloc(sizeof (*cpum));
270	cpum->ml_offset = lml->ml_offset + (ptrsize * NBBY);
271	cpum->ml_size = 0;
272	cpum->ml_name = xstrdup("cpu_m");
273	cpum->ml_type = machcpu;
274	cpum->ml_next = NULL;
275
276	lml->ml_next = cpum;
277}
278
279void
280cvt_fixups(tdata_t *td, size_t ptrsize)
281{
282	fix_small_cpu_struct(td, ptrsize);
283}
284