Print this page
10823 should ignore DW_TAG_subprogram with DW_AT_declaration tags
10824 GCC7-derived CTF can double qualifiers on arrays
10825 ctfdump -c drops last type
10826 ctfdump -c goes off the rails with a missing parent
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Jason King <jason.king@joyent.com>
Approved by: Jerry Jelinek <jerry.jelinek@joyent.com>


  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 2007 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 /*
  26  * Copyright 2012 Jason King.  All rights reserved.
  27  * Use is subject to license terms.
  28  */
  29 
  30 /*
  31  * Copyright 2019 Joyent, Inc.
  32  */
  33 
  34 /*
  35  * CTF DWARF conversion theory.
  36  *
  37  * DWARF data contains a series of compilation units. Each compilation unit
  38  * generally refers to an object file or what once was, in the case of linked
  39  * binaries and shared objects. Each compilation unit has a series of what DWARF
  40  * calls a DIE (Debugging Information Entry). The set of entries that we care
  41  * about have type information stored in a series of attributes. Each DIE also
  42  * has a tag that identifies the kind of attributes that it has.
  43  *
  44  * A given DIE may itself have children. For example, a DIE that represents a
  45  * structure has children which represent members. Whenever we encounter a DIE
  46  * that has children or other values or types associated with it, we recursively
  47  * process those children first so that way we can then refer to the generated
  48  * CTF type id while processing its parent. This reduces the amount of unknowns
  49  * and fixups that we need. It also ensures that we don't accidentally add types
  50  * that an overzealous compiler might add to the DWARF data but aren't used by
  51  * anything in the system.


1468                 return (ret);
1469         if (rtag != DW_TAG_subrange_type) {
1470                 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1471                     "encountered array without DW_TAG_subrange_type child\n");
1472                 return (ECTF_CONVBKERR);
1473         }
1474 
1475         /*
1476          * The compiler may opt to describe a multi-dimensional array as one
1477          * giant array or it may opt to instead encode it as a series of
1478          * subranges. If it's the latter, then for each subrange we introduce a
1479          * type. We can always use the base type.
1480          */
1481         if ((ret = ctf_dwarf_create_array_range(cup, rdie, idp, tid,
1482             isroot)) != 0)
1483                 return (ret);
1484         ctf_dprintf("Got back id %d\n", *idp);
1485         return (ctf_dwmap_add(cup, *idp, die, B_FALSE));
1486 }
1487 















1488 static int






















































1489 ctf_dwarf_create_reference(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp,
1490     int kind, int isroot)
1491 {
1492         int ret;
1493         ctf_id_t id;
1494         Dwarf_Die tdie;
1495         char *name;
1496         size_t namelen;
1497 
1498         if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 &&
1499             ret != ENOENT)
1500                 return (ret);
1501         if (ret == ENOENT) {
1502                 name = NULL;
1503                 namelen = 0;
1504         } else {
1505                 namelen = strlen(name);
1506         }
1507 
1508         ctf_dprintf("reference kind %d %s\n", kind, name != NULL ? name : "<>");
1509 
1510         if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) != 0) {
1511                 if (ret != ENOENT) {
1512                         ctf_free(name, namelen);
1513                         return (ret);
1514                 }
1515                 if ((id = ctf_dwarf_void(cup)) == CTF_ERR) {
1516                         ctf_free(name, namelen);
1517                         return (ctf_errno(cup->cu_ctfp));
1518                 }
1519         } else {
1520                 if ((ret = ctf_dwarf_convert_type(cup, tdie, &id,
1521                     CTF_ADD_NONROOT)) != 0) {
1522                         ctf_free(name, namelen);
1523                         return (ret);
1524                 }
1525         }
1526 











1527         if ((*idp = ctf_add_reftype(cup->cu_ctfp, isroot, name, id, kind)) ==
1528             CTF_ERR) {
1529                 ctf_free(name, namelen);
1530                 return (ctf_errno(cup->cu_ctfp));
1531         }
1532 
1533         ctf_free(name, namelen);
1534         return (ctf_dwmap_add(cup, *idp, die, B_FALSE));
1535 }
1536 
1537 static int
1538 ctf_dwarf_create_enum(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, int isroot)
1539 {
1540         int ret;
1541         ctf_id_t id;
1542         Dwarf_Die child;
1543         char *name;
1544 
1545         if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 &&
1546             ret != ENOENT)


1804 }
1805 
1806 static int
1807 ctf_dwarf_function_count(ctf_cu_t *cup, Dwarf_Die die, ctf_funcinfo_t *fip,
1808     boolean_t fptr)
1809 {
1810         int ret;
1811         Dwarf_Die child, sib, arg;
1812 
1813         if ((ret = ctf_dwarf_child(cup, die, &child)) != 0)
1814                 return (ret);
1815 
1816         arg = child;
1817         while (arg != NULL) {
1818                 Dwarf_Half tag;
1819 
1820                 if ((ret = ctf_dwarf_tag(cup, arg, &tag)) != 0)
1821                         return (ret);
1822 
1823                 /*
1824                  * We have to check for a varargs type decleration. This will
1825                  * happen in one of two ways. If we have a function pointer
1826                  * type, then it'll be done with a tag of type
1827                  * DW_TAG_unspecified_parameters. However, it only means we have
1828                  * a variable number of arguments, if we have more than one
1829                  * argument found so far. Otherwise, when we have a function
1830                  * type, it instead uses a formal parameter whose name is '...'
1831                  * to indicate a variable arguments member.
1832                  *
1833                  * Also, if we have a function pointer, then we have to expect
1834                  * that we might not get a name at all.
1835                  */
1836                 if (tag == DW_TAG_formal_parameter && fptr == B_FALSE) {
1837                         char *name;
1838                         if ((ret = ctf_dwarf_string(cup, die, DW_AT_name,
1839                             &name)) != 0)
1840                                 return (ret);
1841                         if (strcmp(name, DWARF_VARARGS_NAME) == 0)
1842                                 fip->ctc_flags |= CTF_FUNC_VARARG;
1843                         else
1844                                 fip->ctc_argc++;


1889                         /*
1890                          * Once we hit argc entries, we're done. This ensures we
1891                          * don't accidentally hit a varargs which should be the
1892                          * last entry.
1893                          */
1894                         if (i == fip->ctc_argc)
1895                                 break;
1896                 }
1897 
1898                 if ((ret = ctf_dwarf_sib(cup, arg, &sib)) != 0)
1899                         return (ret);
1900                 arg = sib;
1901         }
1902 
1903         return (0);
1904 }
1905 
1906 static int
1907 ctf_dwarf_convert_function(ctf_cu_t *cup, Dwarf_Die die)
1908 {
1909         int ret;
1910         char *name;
1911         ctf_dwfunc_t *cdf;
1912         Dwarf_Die tdie;



1913 
1914         /*
1915          * Functions that don't have a name are generally functions that have
1916          * been inlined and thus most information about them has been lost. If
1917          * we can't get a name, then instead of returning ENOENT, we silently
1918          * swallow the error.
1919          */
1920         if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0) {
1921                 if (ret == ENOENT)
1922                         return (0);
1923                 return (ret);
1924         }
1925 
1926         ctf_dprintf("beginning work on function %s\n", name);


















1927         if ((cdf = ctf_alloc(sizeof (ctf_dwfunc_t))) == NULL) {
1928                 ctf_free(name, strlen(name) + 1);
1929                 return (ENOMEM);
1930         }
1931         bzero(cdf, sizeof (ctf_dwfunc_t));
1932         cdf->cdf_name = name;
1933 
1934         if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) == 0) {
1935                 if ((ret = ctf_dwarf_convert_type(cup, tdie,
1936                     &(cdf->cdf_fip.ctc_return), CTF_ADD_ROOT)) != 0) {
1937                         ctf_free(name, strlen(name) + 1);
1938                         ctf_free(cdf, sizeof (ctf_dwfunc_t));
1939                         return (ret);
1940                 }
1941         } else if (ret != ENOENT) {
1942                 ctf_free(name, strlen(name) + 1);
1943                 ctf_free(cdf, sizeof (ctf_dwfunc_t));
1944                 return (ret);
1945         } else {
1946                 if ((cdf->cdf_fip.ctc_return = ctf_dwarf_void(cup)) ==




  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 2007 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 /*
  26  * Copyright 2012 Jason King.  All rights reserved.
  27  * Use is subject to license terms.
  28  */
  29 
  30 /*
  31  * Copyright 2019, Joyent, Inc.
  32  */
  33 
  34 /*
  35  * CTF DWARF conversion theory.
  36  *
  37  * DWARF data contains a series of compilation units. Each compilation unit
  38  * generally refers to an object file or what once was, in the case of linked
  39  * binaries and shared objects. Each compilation unit has a series of what DWARF
  40  * calls a DIE (Debugging Information Entry). The set of entries that we care
  41  * about have type information stored in a series of attributes. Each DIE also
  42  * has a tag that identifies the kind of attributes that it has.
  43  *
  44  * A given DIE may itself have children. For example, a DIE that represents a
  45  * structure has children which represent members. Whenever we encounter a DIE
  46  * that has children or other values or types associated with it, we recursively
  47  * process those children first so that way we can then refer to the generated
  48  * CTF type id while processing its parent. This reduces the amount of unknowns
  49  * and fixups that we need. It also ensures that we don't accidentally add types
  50  * that an overzealous compiler might add to the DWARF data but aren't used by
  51  * anything in the system.


1468                 return (ret);
1469         if (rtag != DW_TAG_subrange_type) {
1470                 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1471                     "encountered array without DW_TAG_subrange_type child\n");
1472                 return (ECTF_CONVBKERR);
1473         }
1474 
1475         /*
1476          * The compiler may opt to describe a multi-dimensional array as one
1477          * giant array or it may opt to instead encode it as a series of
1478          * subranges. If it's the latter, then for each subrange we introduce a
1479          * type. We can always use the base type.
1480          */
1481         if ((ret = ctf_dwarf_create_array_range(cup, rdie, idp, tid,
1482             isroot)) != 0)
1483                 return (ret);
1484         ctf_dprintf("Got back id %d\n", *idp);
1485         return (ctf_dwmap_add(cup, *idp, die, B_FALSE));
1486 }
1487 
1488 /*
1489  * Given "const int const_array3[11]", GCC7 at least will create a DIE tree of
1490  * DW_TAG_const_type:DW_TAG_array_type:DW_Tag_const_type:<member_type>.
1491  *
1492  * Given C's syntax, this renders out as "const const int const_array3[11]".  To
1493  * get closer to round-tripping (and make the unit tests work), we'll peek for
1494  * this case, and avoid adding the extraneous qualifier if we see that the
1495  * underlying array referent already has the same qualifier.
1496  *
1497  * This is unfortunately less trivial than it could be: this issue applies to
1498  * qualifier sets like "const volatile", as well as multi-dimensional arrays, so
1499  * we need to descend down those.
1500  *
1501  * Returns CTF_ERR on error, or a boolean value otherwise.
1502  */
1503 static int
1504 needed_array_qualifier(ctf_cu_t *cup, int kind, ctf_id_t ref_id)
1505 {
1506         const ctf_type_t *t;
1507         ctf_arinfo_t arinfo;
1508         int akind;
1509 
1510         if (kind != CTF_K_CONST && kind != CTF_K_VOLATILE &&
1511             kind != CTF_K_RESTRICT)
1512                 return (1);
1513 
1514         if ((t = ctf_dyn_lookup_by_id(cup->cu_ctfp, ref_id)) == NULL)
1515                 return (CTF_ERR);
1516 
1517         if (LCTF_INFO_KIND(cup->cu_ctfp, t->ctt_info) != CTF_K_ARRAY)
1518                 return (1);
1519 
1520         if (ctf_dyn_array_info(cup->cu_ctfp, ref_id, &arinfo) != 0)
1521                 return (CTF_ERR);
1522 
1523         ctf_id_t id = arinfo.ctr_contents;
1524 
1525         for (;;) {
1526                 if ((t = ctf_dyn_lookup_by_id(cup->cu_ctfp, id)) == NULL)
1527                         return (CTF_ERR);
1528 
1529                 akind = LCTF_INFO_KIND(cup->cu_ctfp, t->ctt_info);
1530 
1531                 if (akind == kind)
1532                         break;
1533 
1534                 if (akind == CTF_K_ARRAY) {
1535                         if (ctf_dyn_array_info(cup->cu_ctfp,
1536                             id, &arinfo) != 0)
1537                                 return (CTF_ERR);
1538                         id = arinfo.ctr_contents;
1539                         continue;
1540                 }
1541 
1542                 if (akind != CTF_K_CONST && akind != CTF_K_VOLATILE &&
1543                     akind != CTF_K_RESTRICT)
1544                         break;
1545 
1546                 id = t->ctt_type;
1547         }
1548 
1549         if (kind == akind) {
1550                 ctf_dprintf("ignoring extraneous %s qualifier for array %d\n",
1551                     ctf_kind_name(cup->cu_ctfp, kind), ref_id);
1552         }
1553 
1554         return (kind != akind);
1555 }
1556 
1557 static int
1558 ctf_dwarf_create_reference(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp,
1559     int kind, int isroot)
1560 {
1561         int ret;
1562         ctf_id_t id;
1563         Dwarf_Die tdie;
1564         char *name;
1565         size_t namelen;
1566 
1567         if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 &&
1568             ret != ENOENT)
1569                 return (ret);
1570         if (ret == ENOENT) {
1571                 name = NULL;
1572                 namelen = 0;
1573         } else {
1574                 namelen = strlen(name);
1575         }
1576 
1577         ctf_dprintf("reference kind %d %s\n", kind, name != NULL ? name : "<>");
1578 
1579         if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) != 0) {
1580                 if (ret != ENOENT) {
1581                         ctf_free(name, namelen);
1582                         return (ret);
1583                 }
1584                 if ((id = ctf_dwarf_void(cup)) == CTF_ERR) {
1585                         ctf_free(name, namelen);
1586                         return (ctf_errno(cup->cu_ctfp));
1587                 }
1588         } else {
1589                 if ((ret = ctf_dwarf_convert_type(cup, tdie, &id,
1590                     CTF_ADD_NONROOT)) != 0) {
1591                         ctf_free(name, namelen);
1592                         return (ret);
1593                 }
1594         }
1595 
1596         if ((ret = needed_array_qualifier(cup, kind, id)) <= 0) {
1597                 if (ret != 0) {
1598                         ret = (ctf_errno(cup->cu_ctfp));
1599                 } else {
1600                         *idp = id;
1601                 }
1602 
1603                 ctf_free(name, namelen);
1604                 return (ret);
1605         }
1606 
1607         if ((*idp = ctf_add_reftype(cup->cu_ctfp, isroot, name, id, kind)) ==
1608             CTF_ERR) {
1609                 ctf_free(name, namelen);
1610                 return (ctf_errno(cup->cu_ctfp));
1611         }
1612 
1613         ctf_free(name, namelen);
1614         return (ctf_dwmap_add(cup, *idp, die, B_FALSE));
1615 }
1616 
1617 static int
1618 ctf_dwarf_create_enum(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, int isroot)
1619 {
1620         int ret;
1621         ctf_id_t id;
1622         Dwarf_Die child;
1623         char *name;
1624 
1625         if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 &&
1626             ret != ENOENT)


1884 }
1885 
1886 static int
1887 ctf_dwarf_function_count(ctf_cu_t *cup, Dwarf_Die die, ctf_funcinfo_t *fip,
1888     boolean_t fptr)
1889 {
1890         int ret;
1891         Dwarf_Die child, sib, arg;
1892 
1893         if ((ret = ctf_dwarf_child(cup, die, &child)) != 0)
1894                 return (ret);
1895 
1896         arg = child;
1897         while (arg != NULL) {
1898                 Dwarf_Half tag;
1899 
1900                 if ((ret = ctf_dwarf_tag(cup, arg, &tag)) != 0)
1901                         return (ret);
1902 
1903                 /*
1904                  * We have to check for a varargs type declaration. This will
1905                  * happen in one of two ways. If we have a function pointer
1906                  * type, then it'll be done with a tag of type
1907                  * DW_TAG_unspecified_parameters. However, it only means we have
1908                  * a variable number of arguments, if we have more than one
1909                  * argument found so far. Otherwise, when we have a function
1910                  * type, it instead uses a formal parameter whose name is '...'
1911                  * to indicate a variable arguments member.
1912                  *
1913                  * Also, if we have a function pointer, then we have to expect
1914                  * that we might not get a name at all.
1915                  */
1916                 if (tag == DW_TAG_formal_parameter && fptr == B_FALSE) {
1917                         char *name;
1918                         if ((ret = ctf_dwarf_string(cup, die, DW_AT_name,
1919                             &name)) != 0)
1920                                 return (ret);
1921                         if (strcmp(name, DWARF_VARARGS_NAME) == 0)
1922                                 fip->ctc_flags |= CTF_FUNC_VARARG;
1923                         else
1924                                 fip->ctc_argc++;


1969                         /*
1970                          * Once we hit argc entries, we're done. This ensures we
1971                          * don't accidentally hit a varargs which should be the
1972                          * last entry.
1973                          */
1974                         if (i == fip->ctc_argc)
1975                                 break;
1976                 }
1977 
1978                 if ((ret = ctf_dwarf_sib(cup, arg, &sib)) != 0)
1979                         return (ret);
1980                 arg = sib;
1981         }
1982 
1983         return (0);
1984 }
1985 
1986 static int
1987 ctf_dwarf_convert_function(ctf_cu_t *cup, Dwarf_Die die)
1988 {


1989         ctf_dwfunc_t *cdf;
1990         Dwarf_Die tdie;
1991         Dwarf_Bool b;
1992         char *name;
1993         int ret;
1994 
1995         /*
1996          * Functions that don't have a name are generally functions that have
1997          * been inlined and thus most information about them has been lost. If
1998          * we can't get a name, then instead of returning ENOENT, we silently
1999          * swallow the error.
2000          */
2001         if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0) {
2002                 if (ret == ENOENT)
2003                         return (0);
2004                 return (ret);
2005         }
2006 
2007         ctf_dprintf("beginning work on function %s (die %llx)\n",
2008             name, ctf_die_offset(die));
2009 
2010         if ((ret = ctf_dwarf_boolean(cup, die, DW_AT_declaration, &b)) != 0) {
2011                 if (ret != ENOENT)
2012                         return (ret);
2013         } else if (b != 0) {
2014                 /*
2015                  * GCC7 at least creates empty DW_AT_declarations for functions
2016                  * defined in headers.  As they lack details on the function
2017                  * prototype, we need to ignore them.  If we later actually
2018                  * see the relevant function's definition, we will see another
2019                  * DW_TAG_subprogram that is more complete.
2020                  */
2021                 ctf_dprintf("ignoring declaration of function %s (die %llx)\n",
2022                     name, ctf_die_offset(die));
2023                 return (0);
2024         }
2025 
2026         if ((cdf = ctf_alloc(sizeof (ctf_dwfunc_t))) == NULL) {
2027                 ctf_free(name, strlen(name) + 1);
2028                 return (ENOMEM);
2029         }
2030         bzero(cdf, sizeof (ctf_dwfunc_t));
2031         cdf->cdf_name = name;
2032 
2033         if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) == 0) {
2034                 if ((ret = ctf_dwarf_convert_type(cup, tdie,
2035                     &(cdf->cdf_fip.ctc_return), CTF_ADD_ROOT)) != 0) {
2036                         ctf_free(name, strlen(name) + 1);
2037                         ctf_free(cdf, sizeof (ctf_dwfunc_t));
2038                         return (ret);
2039                 }
2040         } else if (ret != ENOENT) {
2041                 ctf_free(name, strlen(name) + 1);
2042                 ctf_free(cdf, sizeof (ctf_dwfunc_t));
2043                 return (ret);
2044         } else {
2045                 if ((cdf->cdf_fip.ctc_return = ctf_dwarf_void(cup)) ==