xref: /freebsd-src/cddl/contrib/opensolaris/tools/ctf/cvt/fixup_tdescs.c (revision 7be9a3b45356747f9fcb6d69a722c1c95f8060bf)
11673e404SJohn Birrell /*
21673e404SJohn Birrell  * CDDL HEADER START
31673e404SJohn Birrell  *
41673e404SJohn Birrell  * The contents of this file are subject to the terms of the
51673e404SJohn Birrell  * Common Development and Distribution License (the "License").
61673e404SJohn Birrell  * You may not use this file except in compliance with the License.
71673e404SJohn Birrell  *
81673e404SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91673e404SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
101673e404SJohn Birrell  * See the License for the specific language governing permissions
111673e404SJohn Birrell  * and limitations under the License.
121673e404SJohn Birrell  *
131673e404SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
141673e404SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151673e404SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
161673e404SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
171673e404SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
181673e404SJohn Birrell  *
191673e404SJohn Birrell  * CDDL HEADER END
201673e404SJohn Birrell  */
211673e404SJohn Birrell /*
221673e404SJohn Birrell  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
231673e404SJohn Birrell  * Use is subject to license terms.
241673e404SJohn Birrell  */
251673e404SJohn Birrell 
261673e404SJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
271673e404SJohn Birrell 
281673e404SJohn Birrell /*
291673e404SJohn Birrell  * Workarounds for stabs generation bugs in the compiler and general needed
301673e404SJohn Birrell  * fixups.
311673e404SJohn Birrell  */
321673e404SJohn Birrell 
331673e404SJohn Birrell #include <stdio.h>
341673e404SJohn Birrell #include <strings.h>
351673e404SJohn Birrell 
361673e404SJohn Birrell #include "ctf_headers.h"
371673e404SJohn Birrell #include "ctftools.h"
381673e404SJohn Birrell #include "hash.h"
391673e404SJohn Birrell #include "memory.h"
401673e404SJohn Birrell 
411673e404SJohn Birrell struct match {
421673e404SJohn Birrell 	tdesc_t *m_ret;
431673e404SJohn Birrell 	const char *m_name;
441673e404SJohn Birrell };
451673e404SJohn Birrell 
461673e404SJohn Birrell static int
matching_iidesc(void * arg1,void * arg2)47*4cc75139SJohn Birrell matching_iidesc(void *arg1, void *arg2)
481673e404SJohn Birrell {
49*4cc75139SJohn Birrell 	iidesc_t *iidesc = arg1;
50*4cc75139SJohn Birrell 	struct match *match = arg2;
511673e404SJohn Birrell 	if (!streq(iidesc->ii_name, match->m_name))
521673e404SJohn Birrell 		return (0);
531673e404SJohn Birrell 
541673e404SJohn Birrell 	if (iidesc->ii_type != II_TYPE && iidesc->ii_type != II_SOU)
551673e404SJohn Birrell 		return (0);
561673e404SJohn Birrell 
571673e404SJohn Birrell 	match->m_ret = iidesc->ii_dtype;
581673e404SJohn Birrell 	return (-1);
591673e404SJohn Birrell }
601673e404SJohn Birrell 
611673e404SJohn Birrell static tdesc_t *
lookup_tdesc(tdata_t * td,char const * name)62*4cc75139SJohn Birrell lookup_tdesc(tdata_t *td, char const *name)
631673e404SJohn Birrell {
641673e404SJohn Birrell 	struct match match = { NULL, name };
65*4cc75139SJohn Birrell 	iter_iidescs_by_name(td, name, matching_iidesc, &match);
661673e404SJohn Birrell 	return (match.m_ret);
671673e404SJohn Birrell }
681673e404SJohn Birrell 
691673e404SJohn Birrell /*
701673e404SJohn Birrell  * The cpu structure grows, with the addition of a machcpu member, if
711673e404SJohn Birrell  * _MACHDEP is defined.  This means that, for example, the cpu structure
721673e404SJohn Birrell  * in unix is different from the cpu structure in genunix.  As one might
731673e404SJohn Birrell  * expect, this causes merges to fail.  Since everyone indirectly contains
741673e404SJohn Birrell  * a pointer to a CPU structure, the failed merges can cause massive amounts
751673e404SJohn Birrell  * of duplication.  In the case of unix uniquifying against genunix, upwards
761673e404SJohn Birrell  * of 50% of the structures were unmerged due to this problem.  We fix this
771673e404SJohn Birrell  * by adding a cpu_m member.  If machcpu hasn't been defined in our module,
781673e404SJohn Birrell  * we make a forward node for it.
791673e404SJohn Birrell  */
801673e404SJohn Birrell static void
fix_small_cpu_struct(tdata_t * td,size_t ptrsize)811673e404SJohn Birrell fix_small_cpu_struct(tdata_t *td, size_t ptrsize)
821673e404SJohn Birrell {
831673e404SJohn Birrell 	tdesc_t *cput, *cpu;
841673e404SJohn Birrell 	tdesc_t *machcpu;
851673e404SJohn Birrell 	mlist_t *ml, *lml;
861673e404SJohn Birrell 	mlist_t *cpum;
871673e404SJohn Birrell 	int foundcpucyc = 0;
881673e404SJohn Birrell 
891673e404SJohn Birrell 	/*
901673e404SJohn Birrell 	 * We're going to take the circuitous route finding the cpu structure,
911673e404SJohn Birrell 	 * because we want to make sure that we find the right one.  It would
921673e404SJohn Birrell 	 * be nice if we could verify the header name too.  DWARF might not
931673e404SJohn Birrell 	 * have the cpu_t, so we let this pass.
941673e404SJohn Birrell 	 */
951673e404SJohn Birrell 	if ((cput = lookup_tdesc(td, "cpu_t")) != NULL) {
961673e404SJohn Birrell 		if (cput->t_type != TYPEDEF)
971673e404SJohn Birrell 			return;
981673e404SJohn Birrell 		cpu = cput->t_tdesc;
991673e404SJohn Birrell 	} else {
1001673e404SJohn Birrell 		cpu = lookup_tdesc(td, "cpu");
1011673e404SJohn Birrell 	}
1021673e404SJohn Birrell 
1031673e404SJohn Birrell 	if (cpu == NULL)
1041673e404SJohn Birrell 		return;
1051673e404SJohn Birrell 
1061673e404SJohn Birrell 	if (!streq(cpu->t_name, "cpu") || cpu->t_type != STRUCT)
1071673e404SJohn Birrell 		return;
1081673e404SJohn Birrell 
1091673e404SJohn Birrell 	for (ml = cpu->t_members, lml = NULL; ml;
1101673e404SJohn Birrell 	    lml = ml, ml = ml->ml_next) {
1111673e404SJohn Birrell 		if (strcmp(ml->ml_name, "cpu_cyclic") == 0)
1121673e404SJohn Birrell 			foundcpucyc = 1;
1131673e404SJohn Birrell 	}
1141673e404SJohn Birrell 
1151673e404SJohn Birrell 	if (foundcpucyc == 0 || lml == NULL ||
1161673e404SJohn Birrell 	    strcmp(lml->ml_name, "cpu_m") == 0)
1171673e404SJohn Birrell 		return;
1181673e404SJohn Birrell 
1191673e404SJohn Birrell 	/*
1201673e404SJohn Birrell 	 * We need to derive the right offset for the fake cpu_m member.  To do
1211673e404SJohn Birrell 	 * that, we require a special unused member to be the last member
1221673e404SJohn Birrell 	 * before the 'cpu_m', that we encode knowledge of here.  ABI alignment
1231673e404SJohn Birrell 	 * on all platforms is such that we only need to add a pointer-size
1241673e404SJohn Birrell 	 * number of bits to get the right offset for cpu_m.  This would most
1251673e404SJohn Birrell 	 * likely break if gcc's -malign-double were ever used, but that option
1261673e404SJohn Birrell 	 * breaks the ABI anyway.
1271673e404SJohn Birrell 	 */
1281673e404SJohn Birrell 	if (!streq(lml->ml_name, "cpu_m_pad") &&
1291673e404SJohn Birrell 	    getenv("CTFCONVERT_PERMISSIVE") == NULL) {
1301673e404SJohn Birrell 		terminate("last cpu_t member before cpu_m is %s; "
1311673e404SJohn Birrell 		    "it must be cpu_m_pad.\n", lml->ml_name);
1321673e404SJohn Birrell 	}
1331673e404SJohn Birrell 
1341673e404SJohn Birrell 	if ((machcpu = lookup_tdesc(td, "machcpu")) == NULL) {
1351673e404SJohn Birrell 		machcpu = xcalloc(sizeof (*machcpu));
1361673e404SJohn Birrell 		machcpu->t_name = xstrdup("machcpu");
1371673e404SJohn Birrell 		machcpu->t_id = td->td_nextid++;
1381673e404SJohn Birrell 		machcpu->t_type = FORWARD;
1391673e404SJohn Birrell 	} else if (machcpu->t_type != STRUCT) {
1401673e404SJohn Birrell 		return;
1411673e404SJohn Birrell 	}
1421673e404SJohn Birrell 
1431673e404SJohn Birrell 	debug(3, "Adding cpu_m machcpu %s to cpu struct\n",
1441673e404SJohn Birrell 	    (machcpu->t_type == FORWARD ? "forward" : "struct"));
1451673e404SJohn Birrell 
1461673e404SJohn Birrell 	cpum = xmalloc(sizeof (*cpum));
1471673e404SJohn Birrell 	cpum->ml_offset = lml->ml_offset + (ptrsize * NBBY);
1481673e404SJohn Birrell 	cpum->ml_size = 0;
1491673e404SJohn Birrell 	cpum->ml_name = xstrdup("cpu_m");
1501673e404SJohn Birrell 	cpum->ml_type = machcpu;
1511673e404SJohn Birrell 	cpum->ml_next = NULL;
1521673e404SJohn Birrell 
1531673e404SJohn Birrell 	lml->ml_next = cpum;
1541673e404SJohn Birrell }
1551673e404SJohn Birrell 
1561673e404SJohn Birrell void
cvt_fixups(tdata_t * td,size_t ptrsize)1571673e404SJohn Birrell cvt_fixups(tdata_t *td, size_t ptrsize)
1581673e404SJohn Birrell {
1591673e404SJohn Birrell 	fix_small_cpu_struct(td, ptrsize);
1601673e404SJohn Birrell }
161