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