11849Swollman/* 21849Swollman * CDDL HEADER START 31849Swollman * 41849Swollman * The contents of this file are subject to the terms of the 51849Swollman * Common Development and Distribution License (the "License"). 61849Swollman * You may not use this file except in compliance with the License. 71849Swollman * 81849Swollman * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 91849Swollman * or http://www.opensolaris.org/os/licensing. 101849Swollman * See the License for the specific language governing permissions 111849Swollman * and limitations under the License. 121849Swollman * 131849Swollman * When distributing Covered Code, include this CDDL HEADER in each 141849Swollman * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 151849Swollman * If applicable, add the following below this CDDL HEADER, with the 161849Swollman * fields enclosed by brackets "[]" replaced with your own identifying 171849Swollman * information: Portions Copyright [yyyy] [name of copyright owner] 181849Swollman * 191849Swollman * CDDL HEADER END 201849Swollman */ 211849Swollman/* 221849Swollman * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 231849Swollman * Use is subject to license terms. 241849Swollman */ 251849Swollman 261849Swollman#pragma ident "%Z%%M% %I% %E% SMI" 271849Swollman 281849Swollman/* 291849Swollman * Workarounds for stabs generation bugs in the compiler and general needed 301849Swollman * fixups. 311849Swollman */ 321849Swollman 3392999Sobrien#include <stdio.h> 3492999Sobrien#include <strings.h> 3592999Sobrien 3692999Sobrien#include "ctf_headers.h" 3792999Sobrien#include "ctftools.h" 381849Swollman#include "hash.h" 391849Swollman#include "memory.h" 401849Swollman 411849Swollman/* 421849Swollman * Due to 4432619, the 6.1 compiler will sometimes incorrectly generate pointer 431849Swollman * stabs. Given a struct foo, and a corresponding typedef struct foo foo_t. 441849Swollman * In some cases, when faced with a pointer to a foo_t, the compiler will 451849Swollman * sometimes generate a stab that describes a pointer to a struct foo. 461849Swollman * Regardless of correctness, this breaks merges, as it occurs inconsistently 471849Swollman * by file. The following two routines know how to recognize and repair foo_t * 481849Swollman * and foo_t ** bugs in a specific set of cases. There is no general way to 491849Swollman * solve this problem without a fix to the compiler. In general, cases should 501849Swollman * only be added to these routines to fix merging problems in genunix. 5156345Sjasone */ 52114309Speterstatic void 53114309Speterfix_ptrptr_to_struct(tdata_t *td) 54114829Speter{ 55114309Speter const char *strs[2] = { "as", "fdbuffer" }; 56115745Speter const char *mems[2] = { "a_objectdir", "fd_shadow" }; 57227023Skib const char *acts[2] = { "vnode", "page" }; 58287480Skib const char *tgts[2] = { "vnode_t", "page_t" }; 59114309Speter tdesc_t *str; 60114309Speter tdesc_t *act, *tgt; 61114309Speter tdesc_t *p1, *p2; 62115745Speter mlist_t *ml; 63115745Speter int i; 64115745Speter 65115745Speter for (i = 0; i < (int) (sizeof (strs) / sizeof (strs[0])); i++) { 66115745Speter if (!(str = lookupname(strs[i])) || str->t_type != STRUCT) 67115745Speter continue; 68115745Speter 69115745Speter for (ml = str->t_members; ml; ml = ml->ml_next) { 70115745Speter if (streq(ml->ml_name, mems[i])) 71180080Sdas break; 72114309Speter } 731849Swollman if (!ml) 74184547Speter continue; 751849Swollman 7671579Sdeischen if (ml->ml_type->t_type != POINTER || ml->ml_type->t_name || 7771579Sdeischen ml->ml_type->t_tdesc->t_type != POINTER || 7856345Sjasone ml->ml_type->t_tdesc->t_name) 79114309Speter continue; 80114309Speter 81114309Speter act = ml->ml_type->t_tdesc->t_tdesc; 82114835Speter if (act->t_type != STRUCT || !streq(act->t_name, acts[i])) 83114309Speter continue; 84114829Speter 85227023Skib if (!(tgt = lookupname(tgts[i])) || tgt->t_type != TYPEDEF) 86287480Skib continue; 87227023Skib 88114309Speter /* We have an instance of the bug */ 89114309Speter p2 = xcalloc(sizeof (*p2)); 90114309Speter p2->t_type = POINTER; 91180080Sdas p2->t_id = td->td_nextid++; 92180080Sdas p2->t_tdesc = tgt; 93180080Sdas 94180080Sdas p1 = xcalloc(sizeof (*p1)); 95180080Sdas p1->t_type = POINTER; 96180080Sdas p1->t_id = td->td_nextid++; 97180080Sdas p1->t_tdesc = p2; 98180080Sdas 99180080Sdas ml->ml_type = p1; 100114309Speter 101114309Speter debug(3, "Fixed %s->%s => ptrptr struct %s bug\n", 102114309Speter strs[i], mems[i], acts[i]); 103114309Speter } 104114309Speter} 105114309Speter 106114309Speterstatic void 107114309Speterfix_ptr_to_struct(tdata_t *td) 108114309Speter{ 109114309Speter const char *strs[2] = { "vmem", "id_space" }; 110114309Speter const char *mems[2] = { NULL, "is_vmem" }; 1111849Swollman tdesc_t *ptr = NULL; 112114309Speter tdesc_t *str, *vmt; 113114309Speter mlist_t *ml; 1141849Swollman int i; 115184547Speter 116217106Skib if ((vmt = lookupname("vmem_t")) == NULL || vmt->t_type != TYPEDEF) 117217106Skib return; 118 119 for (i = 0; i < (int) (sizeof (strs) / sizeof (strs[0])); i++) { 120 if (!(str = lookupname(strs[i])) || str->t_type != STRUCT) 121 continue; 122 123 for (ml = str->t_members; ml; ml = ml->ml_next) { 124 if (mems[i] && !streq(ml->ml_name, mems[i])) 125 continue; 126 127 if (ml->ml_type->t_type != POINTER || 128 ml->ml_type->t_name || 129 (ml->ml_type->t_tdesc->t_type != STRUCT && 130 ml->ml_type->t_tdesc->t_type != FORWARD) || 131 !streq(ml->ml_type->t_tdesc->t_name, "vmem")) 132 continue; 133 134 debug(3, "Fixed %s->%s => ptr struct vmem bug\n", 135 strs[i], ml->ml_name); 136 137 if (!ptr) { 138 ptr = xcalloc(sizeof (*ptr)); 139 ptr->t_type = POINTER; 140 ptr->t_id = td->td_nextid++; 141 ptr->t_tdesc = vmt; 142 } 143 144 ml->ml_type = ptr; 145 } 146 } 147} 148 149/* 150 * Fix stabs generation bugs. These routines must be run before the 151 * post-conversion merge 152 */ 153void 154cvt_fixstabs(tdata_t *td) 155{ 156 fix_ptrptr_to_struct(td); 157 fix_ptr_to_struct(td); 158} 159 160struct match { 161 tdesc_t *m_ret; 162 const char *m_name; 163}; 164 165static int 166matching_iidesc(void *arg1, void *arg2) 167{ 168 iidesc_t *iidesc = arg1; 169 struct match *match = arg2; 170 if (!streq(iidesc->ii_name, match->m_name)) 171 return (0); 172 173 if (iidesc->ii_type != II_TYPE && iidesc->ii_type != II_SOU) 174 return (0); 175 176 match->m_ret = iidesc->ii_dtype; 177 return (-1); 178} 179 180static tdesc_t * 181lookup_tdesc(tdata_t *td, char const *name) 182{ 183 struct match match = { NULL, name }; 184 iter_iidescs_by_name(td, name, matching_iidesc, &match); 185 return (match.m_ret); 186} 187 188/* 189 * The cpu structure grows, with the addition of a machcpu member, if 190 * _MACHDEP is defined. This means that, for example, the cpu structure 191 * in unix is different from the cpu structure in genunix. As one might 192 * expect, this causes merges to fail. Since everyone indirectly contains 193 * a pointer to a CPU structure, the failed merges can cause massive amounts 194 * of duplication. In the case of unix uniquifying against genunix, upwards 195 * of 50% of the structures were unmerged due to this problem. We fix this 196 * by adding a cpu_m member. If machcpu hasn't been defined in our module, 197 * we make a forward node for it. 198 */ 199static void 200fix_small_cpu_struct(tdata_t *td, size_t ptrsize) 201{ 202 tdesc_t *cput, *cpu; 203 tdesc_t *machcpu; 204 mlist_t *ml, *lml; 205 mlist_t *cpum; 206 int foundcpucyc = 0; 207 208 /* 209 * We're going to take the circuitous route finding the cpu structure, 210 * because we want to make sure that we find the right one. It would 211 * be nice if we could verify the header name too. DWARF might not 212 * have the cpu_t, so we let this pass. 213 */ 214 if ((cput = lookup_tdesc(td, "cpu_t")) != NULL) { 215 if (cput->t_type != TYPEDEF) 216 return; 217 cpu = cput->t_tdesc; 218 } else { 219 cpu = lookup_tdesc(td, "cpu"); 220 } 221 222 if (cpu == NULL) 223 return; 224 225 if (!streq(cpu->t_name, "cpu") || cpu->t_type != STRUCT) 226 return; 227 228 for (ml = cpu->t_members, lml = NULL; ml; 229 lml = ml, ml = ml->ml_next) { 230 if (strcmp(ml->ml_name, "cpu_cyclic") == 0) 231 foundcpucyc = 1; 232 } 233 234 if (foundcpucyc == 0 || lml == NULL || 235 strcmp(lml->ml_name, "cpu_m") == 0) 236 return; 237 238 /* 239 * We need to derive the right offset for the fake cpu_m member. To do 240 * that, we require a special unused member to be the last member 241 * before the 'cpu_m', that we encode knowledge of here. ABI alignment 242 * on all platforms is such that we only need to add a pointer-size 243 * number of bits to get the right offset for cpu_m. This would most 244 * likely break if gcc's -malign-double were ever used, but that option 245 * breaks the ABI anyway. 246 */ 247 if (!streq(lml->ml_name, "cpu_m_pad") && 248 getenv("CTFCONVERT_PERMISSIVE") == NULL) { 249 terminate("last cpu_t member before cpu_m is %s; " 250 "it must be cpu_m_pad.\n", lml->ml_name); 251 } 252 253 if ((machcpu = lookup_tdesc(td, "machcpu")) == NULL) { 254 machcpu = xcalloc(sizeof (*machcpu)); 255 machcpu->t_name = xstrdup("machcpu"); 256 machcpu->t_id = td->td_nextid++; 257 machcpu->t_type = FORWARD; 258 } else if (machcpu->t_type != STRUCT) { 259 return; 260 } 261 262 debug(3, "Adding cpu_m machcpu %s to cpu struct\n", 263 (machcpu->t_type == FORWARD ? "forward" : "struct")); 264 265 cpum = xmalloc(sizeof (*cpum)); 266 cpum->ml_offset = lml->ml_offset + (ptrsize * NBBY); 267 cpum->ml_size = 0; 268 cpum->ml_name = xstrdup("cpu_m"); 269 cpum->ml_type = machcpu; 270 cpum->ml_next = NULL; 271 272 lml->ml_next = cpum; 273} 274 275void 276cvt_fixups(tdata_t *td, size_t ptrsize) 277{ 278 fix_small_cpu_struct(td, ptrsize); 279} 280