diff options
37 files changed, 13886 insertions, 598 deletions
@@ -1,3 +1,7 @@ +2009-01-10 Roland McGrath <roland@redhat.com> + + * configure.ac: Add AC_PROG_CXX. + 2009-04-19 Roland McGrath <roland@redhat.com> * configure.ac (eu_version): Round down here, not in version.h macros. diff --git a/configure.ac b/configure.ac index 4931530c..ddf8e874 100644 --- a/configure.ac +++ b/configure.ac @@ -63,6 +63,7 @@ AC_PROG_CC AC_PROG_RANLIB AC_PROG_YACC AM_PROG_LEX +AC_PROG_CXX AC_CACHE_CHECK([for gcc with C99 support], ac_cv_c99, [dnl old_CFLAGS="$CFLAGS" diff --git a/lib/ChangeLog b/lib/ChangeLog index 06991826..11ad4d44 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,7 @@ +2009-01-25 Roland McGrath <roland@redhat.com> + + * eu-config.h (__STDC_LIMIT_MACROS): Define it. + 2009-01-23 Roland McGrath <roland@redhat.com> * eu-config.h: Add multiple inclusion protection. diff --git a/lib/eu-config.h b/lib/eu-config.h index 32f501ae..5e809d99 100644 --- a/lib/eu-config.h +++ b/lib/eu-config.h @@ -182,5 +182,9 @@ asm (".section predict_data, \"aw\"; .previous\n" /* This macro is used by the tests conditionalize for standalone building. */ #define ELFUTILS_HEADER(name) <lib##name.h> +/* C++ needs this for <stdint.h> to define UINT64_MAX et al. */ +#ifndef __STDC_LIMIT_MACROS +# define __STDC_LIMIT_MACROS +#endif #endif /* eu-config.h */ diff --git a/libdw/ChangeLog b/libdw/ChangeLog index 0e317d7c..bd694639 100644 --- a/libdw/ChangeLog +++ b/libdw/ChangeLog @@ -1,3 +1,60 @@ +2009-04-02 Roland McGrath <roland@redhat.com> + + * Makefile.am (noinst_HEADERS): Add known-dwarf.h and + c++/dwarf-knowledge.cc here. + +2009-03-29 Roland McGrath <roland@redhat.com> + + * c++/exception.cc: New file. + * Makefile.am (libdwpp_a_SOURCES): Add it. + +2009-03-25 Roland McGrath <roland@redhat.com> + + * c++/dwarf, c++/values.cc: Proper dwarf_constant support. + * c++/dwarf_edit, c++/edit-values.cc: Likewise. + * c++/known.cc: New file. + * Makefile.am (libdwpp_a_SOURCES): Add it. + +2009-03-24 Petr Machata <pmachata@redhat.com> + + * c++/dwarf (dwarf::debug_info_entry::raw_attributes): + Fix iteration over attributes. + +2009-03-24 Roland McGrath <roland@redhat.com> + + * Makefile.am (libdwpp_a_SOURCES): New variable. + (lib_LIBRARIES): Add libdwpp.a to it. + +2009-02-26 Roland McGrath <roland@redhat.com> + + * c++/dwarf (dwarf::attr_value): Add _m_tag private member. + (dwarf::attr_value, dwarf::attribute): Update initializers. + (dwarf::raw_attributes::const_iterator): Update caller. + * c++/values.cc (attr_value::what_space): Use it. + +2009-02-01 Roland McGrath <roland@redhat.com> + + * c++/edit-values.cc: New file. + +2009-01-28 Roland McGrath <roland@redhat.com> + + * c++/dwarf: Add line table support. + * c++/values.cc: New file. + * c++/line_info.cc: New file. + * c++/subr.hh: New file. + * Makefile.am (pkginclude_HEADERS): Add it. + +2009-01-26 Roland McGrath <roland@redhat.com> + + * c++/dwarf (dwarf_output): Renamed to dwarf_edit and moved to ... + * c++/dwarf_edit: ... here. New file. + * Makefile.am (pkginclude_HEADERS): Add it. + +2009-01-10 Roland McGrath <roland@redhat.com> + + * c++/dwarf: New file. + * Makefile.am (pkginclude_HEADERS): Add it. + 2009-05-07 Roland McGrath <roland@redhat.com> * dwarf_getmacros.c (dwarf_getmacros): Use absolute section offset in diff --git a/libdw/Makefile.am b/libdw/Makefile.am index e624ac10..2e35cdc1 100644 --- a/libdw/Makefile.am +++ b/libdw/Makefile.am @@ -47,7 +47,9 @@ noinst_PROGRAMS = $(noinst_LIBRARIES:_pic.a=.so) endif include_HEADERS = dwarf.h -pkginclude_HEADERS = libdw.h +pkginclude_HEADERS = libdw.h \ + c++/subr.hh \ + c++/dwarf c++/dwarf_edit libdw_a_SOURCES = dwarf_begin.c dwarf_begin_elf.c dwarf_end.c dwarf_getelf.c \ dwarf_getpubnames.c dwarf_getabbrev.c dwarf_tag.c \ @@ -85,6 +87,15 @@ libdw_a_SOURCES = dwarf_begin.c dwarf_begin_elf.c dwarf_end.c dwarf_getelf.c \ libdw_visit_scopes.c \ dwarf_entry_breakpoints.c +# XXX need to figure out C++ dso crapola +lib_LIBRARIES += libdwpp.a +libdwpp_a_SOURCES = c++/values.cc \ + c++/exception.cc \ + c++/known.cc \ + c++/line_info.cc \ + c++/edit-values.cc +noinst_HEADERS = c++/dwarf-knowledge.cc + if MAINTAINER_MODE BUILT_SOURCES = $(srcdir)/known-dwarf.h MAINTAINERCLEANFILES = $(srcdir)/known-dwarf.h @@ -134,7 +145,7 @@ endif libdw_a_LIBADD = $(addprefix ../libdwfl/,$(shell $(AR) t ../libdwfl/libdwfl.a)) -noinst_HEADERS = libdwP.h memory-access.h dwarf_abbrev_hash.h +noinst_HEADERS += libdwP.h memory-access.h dwarf_abbrev_hash.h known-dwarf.h EXTRA_DIST = libdw.map diff --git a/libdw/c++/dwarf b/libdw/c++/dwarf new file mode 100644 index 00000000..45dc558f --- /dev/null +++ b/libdw/c++/dwarf @@ -0,0 +1,2263 @@ +/* -*- C++ -*- interfaces for libdw. + Copyright (C) 2009 Red Hat, Inc. + This file is part of Red Hat elfutils. + + Red Hat elfutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by the + Free Software Foundation; version 2 of the License. + + Red Hat elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Red Hat elfutils; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. + + In addition, as a special exception, Red Hat, Inc. gives You the + additional right to link the code of Red Hat elfutils with code licensed + under any Open Source Initiative certified open source license + (http://www.opensource.org/licenses/index.php) which requires the + distribution of source code with any binary distribution and to + distribute linked combinations of the two. Non-GPL Code permitted under + this exception must only link to the code of Red Hat elfutils through + those well defined interfaces identified in the file named EXCEPTION + found in the source code files (the "Approved Interfaces"). The files + of Non-GPL Code may instantiate templates or use macros or inline + functions from the Approved Interfaces without causing the resulting + work to be covered by the GNU General Public License. Only Red Hat, + Inc. may make changes or additions to the list of Approved Interfaces. + Red Hat's grant of this exception is conditioned upon your not adding + any new exceptions. If you wish to add a new Approved Interface or + exception, please contact Red Hat. You must obey the GNU General Public + License in all respects for all of the Red Hat elfutils code and other + code used in conjunction with Red Hat elfutils except the Non-GPL Code + covered by this exception. If you modify this file, you may extend this + exception to your version of the file, but you are not obligated to do + so. If you do not wish to provide this exception without modification, + you must delete this exception statement from your version and license + this file solely under the GPL without exception. + + Red Hat elfutils is an included package of the Open Invention Network. + An included package of the Open Invention Network is a package for which + Open Invention Network licensees cross-license their patents. No patent + license is granted, either expressly or impliedly, by designation as an + included package. Should you wish to participate in the Open Invention + Network licensing program, please visit www.openinventionnetwork.com + <http://www.openinventionnetwork.com>. */ + +#ifndef _ELFUTILS_DWARF +#define _ELFUTILS_DWARF 1 + +#include "libdw.h" +#include "dwarf.h" +#include "subr.hh" +#include <stdexcept> + +#include <list> +#include <map> +#include <set> +#include <vector> +#include <stack> +#include <algorithm> +#include <functional> + +/* Abstractly, one DWARF object file consists of a few containers. + (We omit .debug_frame for now. It does not interact with the others.) + + 1. list of compilation units (.debug_info) + 2. map of PC ranges to CU (.debug_aranges) + 3. map of global names to CU+DIE (.debug_pubnames) + 4. map of type names to CU+DIE (.debug_pubtypes) + + These maps refer to the CUs in .debug_info and optimize lookups + compared to simple iteration. + + A compile_unit is a debug_info_entry. + A debug_info_entry consists of a tag (int/enum), and two containers: + children and attributes. The attributes are an unordered map of name + (int/enum) to attribute value (complex variant record). Children are + in an ordered list, each also a debug_info_entry. + + dwarf.compile_units () works like list<compile_unit> + -> compile_unit : debug_info_entry + .attributes () like unordered_map<int, attr_value> + .children () works like list<debug_info_entry> + -> debug_info_entry + .attributes () + .children () + + A compile_unit is not deeply special, it's just a debug_info_entry. + It has its own class just for some convenience methods that only + make sense for a compile_unit DIE. + + This is the "logical" view of the file, grafting and eliding parts of the + raw information that are purely the structural elements of DWARF and not + part of the abstract semantics. In the file reader (elfutils::dwarf), + these containers form a layer above the raw containers that expose the + file data directly (as the libdw C interfaces do). + + dwarf.raw_compile_units () works like list<compile_unit> + -> compile_unit : debug_info_entry + .raw_attributes () like unordered_map<int, attr_value> + .raw_children () works like list<debug_info_entry> + -> debug_info_entry + .raw_attributes () + .raw_children () + + compile_units () elides DW_TAG_partial_unit members, + raw_compile_units () includes them. + + attributes () elides DW_AT_sibling, raw_attributes () includes it. + + raw_children () reports DW_TAG_imported_unit as any other child. + children () flattens imported units into the containing list. + + The == and != comparisons for dwarf and debug_info_entry objects compare + their logical containers, not the raw containers. The comparisons are + defined via templates, so you can compare elfutils::dwarf with any other + class that implements the same structure of containers with input iterators. + + The elfutils::dwarf class and its inner classes form a thin, read-only + layer of virtual containers that ideally could inline away entirely to + calls into the C libdw API and small amounts of stack storage. The tree + of objects described above never exists in memory in its entirety. The + objects are constructed on the fly in each call to a container method. + + See the dwarf_edit and dwarf_output headers for other classes that are + template-compatible with the "logical view" interface above, but do not + support any of the "raw" container variants. These == and != comparisons + are template-driven too, so all different classes can be compared. + + The output classes have template-driven copy constructors, so they can be + copied from files or substructures of the elfutils::dwarf input classes. + + ------ XXX to be done: more file-level containers + + input side only: + + units_by_addr : map<pair<begin,end>, CU> and map<address, CU> + use dwarf_getarange_addr + + pub{names,types} : map<string, debug_info_entry> (across all CUs) + + output too: + + pubnames_map : map<string, debug_info_entry> + pub{names,types}_units : map<compile_unit, pubnames_map> + too much lang knowledge to autogenerate for now, + output will do it explicitly + + */ + +// DWARF reader interfaces: front end to <libdw.h> routines +namespace elfutils +{ + template<typename key1, typename value1, class pair2> + inline bool operator== (const std::pair<key1, value1> &a, const pair2 &b) + { + return a.first == b.first && a.second == b.second; + } + + // Used like std::vector<elt>, but is really just a simple fixed array. + template<typename elt> + class const_vector + { + private: + size_t _m_size; + const elt *_m_array; + + public: + typedef const elt *const_iterator; + + const_vector () + : _m_size (0), _m_array (NULL) {} + const_vector (const const_vector &v) + : _m_size (v._m_size), _m_array (v._m_array) {} + const_vector (const elt *start, const elt *stop) + : _m_size (stop - start), _m_array (start) {} + const_vector (const ::Dwarf_Block &b) + : _m_size (b.length), _m_array (reinterpret_cast<const elt *> (b.data)) {} + + inline const_vector &operator= (const const_vector &v) + { + _m_size = v._m_size; + _m_array = v._m_array; + return *this; + } + + inline size_t size () const + { + return _m_size; + } + inline bool empty () const + { + return _m_size == 0; + } + + const_iterator begin () const + { + return _m_array; + } + const_iterator end () const + { + return &_m_array[_m_size]; + } + + template<typename other> + inline operator other () const + { + return other (begin (), end ()); + } + + template<typename vec> + inline bool operator== (const vec &other) const + { + return (other.size () == size () + && std::equal (begin (), end (), other.begin ())); + } + template<typename vec> + inline bool operator!= (const vec &other) const + { + return !(*this == other); + } + + }; + + // One DWARF object file. + class dwarf + { + private: + static const char *known_tag (int); + static const char *known_attribute (int); + + public: + typedef subr::known<__typeof ("DW_TAG_"), known_tag> tags; + typedef subr::known<__typeof ("DW_AT_"), known_attribute> attributes; + + template<typename attribute> + static inline std::string attribute_name (const attribute &attr) + { + int code = attr.first; + return attributes::name (code); + } + + template<int key> + class known_enum + { + public: + static size_t prefix_length (); + static const char *identifier (int); + inline static const char *name (int value) + { + const char *id = identifier (value); + return id != NULL ? id + prefix_length () : NULL; + } + + // XXX perhaps have iterator/lookup methods like a read-only map? + }; + + typedef known_enum< ::DW_AT_producer> forms; + typedef known_enum< ::DW_AT_location> ops; + + private: + static void throw_libdw (::Dwarf *dw); // XXX raises (...) + static void throw_libdw (::Dwarf_CU *); // XXX raises (...) + + inline void xif (bool fail) const + { + if (unlikely (fail)) + throw_libdw (_m_dw); + } + static inline void xif (::Dwarf_CU *cu, bool fail) + { + if (unlikely (fail)) + throw_libdw (cu); + } + + static inline void xif (const ::Dwarf_Attribute *attr, bool fail) + { + xif (attr->cu, fail); + } + static inline void xif (const ::Dwarf_Die *die, bool fail) + { + xif (die->cu, fail); + } + + template<typename raw, typename raw_element, typename element, + bool skip (const raw_element &)> + class skipping_wrapper + { + protected: + typedef typename raw::const_iterator raw_iterator; + + raw _m_raw; + + protected: + inline skipping_wrapper (const raw &raw) : _m_raw (raw) {} + + public: + inline skipping_wrapper (const skipping_wrapper &w) : _m_raw (w._m_raw) {} + + /* + iterator: wraps raw iterator, skips DW_AT_sibling + size/empty: search for DW_AT_sibling, adjust raw size + */ + + class const_iterator + : public std::iterator<std::input_iterator_tag, element> + { + friend class skipping_wrapper<raw, raw_element, element, skip>; + private: + raw_iterator _m_raw; + const raw_iterator _m_end; + + inline void jiggle () + { + while (_m_raw != _m_end && unlikely (skip (*_m_raw))) + ++_m_raw; + } + + public: + + const_iterator (const const_iterator &i) + : _m_raw (i._m_raw), _m_end (i._m_end) {} + + // Start at the raw position and skip as necessary. + const_iterator (const raw_iterator &begin, const raw_iterator &end) + : _m_raw (begin), _m_end (end) + { + jiggle (); + } + + inline const_iterator &operator= (const const_iterator &other) + { + _m_raw = other._m_raw; + return *this; + } + + inline bool operator== (const const_iterator &other) const + { + return _m_raw == other._m_raw; + } + inline bool operator!= (const const_iterator &other) const + { + return !(*this == other); + } + + inline const_iterator &operator++ () // prefix + { + ++_m_raw; + jiggle (); + return *this; + } + inline const_iterator operator++ (int) // postfix + { + const_iterator prev = *this; + ++*this; + return prev; + } + + inline element operator* () const + { + return static_cast<element> (*_m_raw); + } + }; + + inline const_iterator begin () const + { + return const_iterator (_m_raw.begin (), _m_raw.end ()); + } + inline const_iterator end () const + { + const raw_iterator raw_end = _m_raw.end (); + return const_iterator (raw_end, raw_end); + } + }; + + public: + /* + getstring + */ + + class attribute; + class attr_value; + class location_attr; + class range_list; + class ranges; + class line_info_table; + class directory_table; + class file_table; + class line_table; + class line_entry; + class dwarf_enum; + + class debug_info_entry + { + private: + ::Dwarf_Die _m_die; + inline ::Dwarf_Die *thisdie () const + { + return const_cast< ::Dwarf_Die *> (&_m_die); + } + + friend class dwarf; + friend class attr_value; + protected: + + inline void xif (bool fail) const + { + dwarf::xif (_m_die.cu, fail); + } + + inline debug_info_entry () + { + memset (&_m_die, 0, sizeof _m_die); + } + + inline debug_info_entry (const dwarf &dw, ::Dwarf_Off off) + { + dw.xif (::dwarf_offdie (dw._m_dw, off, &_m_die) == NULL); + } + + public: + debug_info_entry (const debug_info_entry &die) : _m_die (die._m_die) {} + + // Containers, see class definitions below. + class raw_children; + inline raw_children raw_children () const; + class raw_attributes; + raw_attributes raw_attributes () const; + class children; + inline children children () const; + class attributes; + attributes attributes () const; + + inline int tag () const + { + int t = ::dwarf_tag (thisdie ()); + xif (t <= 0); + return t; + } + + bool has_children () const + { + int has = ::dwarf_haschildren (thisdie ()); + xif (has < 0); + return has != 0; + } + + /* + const char *tag_name () const + const_string tag_name () const // "name" or "0x123" + */ + + template<typename die> + bool operator== (const die &other) const + { + return (attributes () == other.attributes () + && children () == other.children ()); + } + template<typename die> + bool operator!= (const die &other) const + { + return !(*this == other); + } + + ::Dwarf_Off offset () const + { + return ::dwarf_dieoffset (thisdie ()); + } + + inline const dwarf::ranges ranges () const + { + return dwarf::ranges (*this); + } + }; + + // Container for raw list of child DIEs, intended to be a compatible with + // a read-only, unidirectional subset of std::list<debug_info_entry>. + class debug_info_entry::raw_children + { + friend class debug_info_entry; + private: + const debug_info_entry &_m_die; + + protected: + inline raw_children (const debug_info_entry &die) : _m_die (die) {} + + public: + typedef debug_info_entry value_type; + + inline raw_children (const raw_children &c) : _m_die (c._m_die) {} + + bool empty () const + { + return begin () == end (); + } + + class const_iterator + : public std::iterator<std::input_iterator_tag, debug_info_entry> + { + friend class debug_info_entry; + friend class attr_value; + private: + debug_info_entry _m_die; + + inline const_iterator () {} + inline const_iterator (const debug_info_entry &parent) + { + int result = ::dwarf_child (parent.thisdie (), &_m_die._m_die); + parent.xif (result < 0); + } + + // Construct from a reference attribute. + inline const_iterator (Dwarf_Attribute *attr) + { + dwarf::xif (attr, ::dwarf_formref_die (attr, &_m_die._m_die) == NULL); + } + + public: + inline const_iterator (const const_iterator &i) : _m_die (i._m_die) {} + + inline const debug_info_entry &operator* () const + { + if (unlikely (_m_die._m_die.addr == NULL)) + throw std::runtime_error ("dereferencing end iterator"); + return _m_die; + } + inline const debug_info_entry *operator-> () const + { + return &(operator* ()); + } + + inline const_iterator &operator= (const const_iterator &other) + { + _m_die = other._m_die; + return *this; + } + + inline bool operator== (const const_iterator &other) const + { + return _m_die._m_die.addr == other._m_die._m_die.addr; + } + inline bool operator!= (const const_iterator &other) const + { + return !(*this == other); + } + + inline const_iterator &operator++ () // prefix + { + int result = ::dwarf_siblingof (&_m_die._m_die, &_m_die._m_die); + _m_die.xif (result < 0); + if (result > 0) // Hit the end. + *this = const_iterator (); + return *this; + } + inline const_iterator operator++ (int) // postfix + { + const_iterator prev = *this; + ++*this; + return prev; + } + }; + const_iterator begin () const + { + return const_iterator (_m_die); + } + inline const_iterator end () const + { + return const_iterator (); + } + + template<typename other_children> + bool operator== (const other_children &other) const + { + return subr::container_equal (*this, other); + } + template<typename other_children> + bool operator!= (const other_children &other) const + { + return !(*this == other); + } + }; + + // Container for list of raw attributes as (name, value) pairs, + // intended to be compatible with a read-only, unidirectional + // subset of std::list<std::pair<int, attr_value>>. + class debug_info_entry::raw_attributes + { + friend class debug_info_entry; + private: + const debug_info_entry &_m_die; + + raw_attributes (const debug_info_entry &die) : _m_die (die) {} + + public: + typedef attribute value_type; + + inline raw_attributes (const raw_attributes &a) : _m_die (a._m_die) {} + + size_t size () const; + inline bool empty () const + { + return size () == 0; + } + + class const_iterator + : public std::iterator<std::input_iterator_tag, attribute> + { + friend class raw_attributes; + private: + debug_info_entry _m_die; + ptrdiff_t _m_offset; // Offset for next iteration in dwarf_getattrs. + ::Dwarf_Attribute _m_attr; + + /* We get called up to twice per iteration. The first time, we + store *ATTR in the instance variable and return DWARF_CB_OK so + that we might be called again. The second time, we return + DWARF_CB_ABORT so that the iteration will stop at the next + attribute's offset. */ + static int getattrs_callback (Dwarf_Attribute *attr, void *arg) + { + const_iterator *i = static_cast<const_iterator *> (arg); + if (i->_m_attr.valp == NULL) + { + i->_m_attr = *attr; + return DWARF_CB_OK; + } + return DWARF_CB_ABORT; + } + + inline const_iterator (const debug_info_entry &die, ptrdiff_t offset) + : _m_die (die), _m_offset (offset), _m_attr () {} + + public: + inline const_iterator (const const_iterator &i) + : _m_die (i._m_die), _m_offset (i._m_offset), _m_attr (i._m_attr) {} + + inline const_iterator &operator= (const const_iterator &other) + { + _m_die = other._m_die; + _m_offset = other._m_offset; + _m_attr = other._m_attr; + return *this; + } + + inline bool operator== (const const_iterator &other) const + { + return (_m_die._m_die.addr == other._m_die._m_die.addr + && _m_attr.valp == other._m_attr.valp); + } + inline bool operator!= (const const_iterator &other) const + { + return !(*this == other); + } + + inline const_iterator &operator++ () // prefix + { + _m_attr.valp = NULL; + int result = ::dwarf_getattrs (&_m_die._m_die, &getattrs_callback, + (void *) this, _m_offset); + _m_die.xif (result < 0); + _m_offset = result; + return *this; + } + inline const_iterator operator++ (int) // postfix + { + const_iterator prev = *this; + ++*this; + return prev; + } + + inline attribute operator* () const + { + if (unlikely (_m_attr.valp == NULL)) + throw std::runtime_error ("dereferencing end iterator"); + return attribute (_m_die, _m_attr); + } + }; + inline const_iterator begin () const + { + const_iterator i = const_iterator (_m_die, 0); + return ++i; + } + inline const_iterator end () const + { + return const_iterator (_m_die, 1); + } + + // XXX can do faster internal (?) + inline const_iterator find (int name) const + { + const_iterator i = begin (); + while (i != end () && (*i).first != name) + ++i; + return i; + } + }; + + // Container for list of child DIEs, intended to be a compatible with + // a read-only, unidirectional subset of std::list<debug_info_entry>. + // Same as raw_children, but flattens DW_TAG_imported_unit children. + class debug_info_entry::children : public debug_info_entry::raw_children + { + friend class debug_info_entry; + private: + + inline children (const debug_info_entry &die) + : raw_children::raw_children (die) {} + + public: + typedef debug_info_entry value_type; + + inline children (const children &c) : raw_children (c) {} + + class const_iterator + : public std::iterator<std::input_iterator_tag, debug_info_entry> + { + friend class children; + private: + + typedef raw_children::const_iterator raw_iterator; + std::stack<raw_iterator> _m_stack; + const raw_iterator _m_end; + + /* Push and pop until either _m_stack.top () == _m_end or + it's looking at a DIE other than DW_TAG_imported_unit. */ + inline void jiggle () + { + while (true) + { + raw_iterator &i = _m_stack.top (); + + if (i == _m_end) + { + /* We're at the end of this raw DIE. + Pop out to the iterator on the importing unit. */ + _m_stack.pop (); + + if (_m_stack.empty ()) + // That was the outermost unit, this is the end. + break; + + continue; + } + + if (i->tag () == ::DW_TAG_imported_unit) + // We have an imported unit. Look at its referent. + _m_stack.push (i->attributes ().at (::DW_AT_import) + .reference ()->raw_children ().begin ()); + else + // This is some other DIE. Iterate on it. + break; + } + } + + inline const_iterator (const raw_iterator &end) : _m_end (end) {} + + inline const_iterator (const raw_iterator &end, const raw_iterator &i) + : _m_end (end) + { + _m_stack.push (i); + jiggle (); + } + + public: + inline const_iterator (const const_iterator &i) + : _m_stack (i._m_stack), _m_end (i._m_end) {} + + inline const_iterator &operator= (const const_iterator &other) + { + _m_stack = other._m_stack; + return *this; + } + + inline bool operator== (const const_iterator &other) const + { + return _m_stack == other._m_stack; + } + inline bool operator!= (const const_iterator &other) const + { + return !(*this == other); + } + + inline const debug_info_entry &operator* () const + { + return *_m_stack.top (); + } + inline const debug_info_entry *operator-> () const + { + return &(operator* ()); + } + + inline const_iterator &operator++ () // prefix + { + ++_m_stack.top (); + jiggle (); + return *this; + } + inline const_iterator operator++ (int) // postfix + { + const_iterator prev = *this; + ++*this; + return prev; + } + }; + + const_iterator begin () const + { + return const_iterator (raw_children::end (), + raw_children::begin ()); + } + const_iterator end () const + { + return const_iterator (raw_children::end ()); + } + + template<typename other_children> + bool operator== (const other_children &other) const + { + return subr::container_equal (*this, other); + } + template<typename other_children> + bool operator!= (const other_children &other) const + { + return !(*this == other); + } + }; + + private: + static inline bool skip_sibling (const attribute &attr) + { + return attr.first == ::DW_AT_sibling; + } + + // Circumvent C++ namespace lookup. + typedef class debug_info_entry::raw_attributes debug_info_entry_raw_attrs; + typedef skipping_wrapper<debug_info_entry_raw_attrs, + attribute, attribute, skip_sibling> + attributes_base; + + public: + // Container for attributes, indexed by name, intended to be compatible + // with a read-only subset of std::unordered_map<int, attr_value>. + // This excludes DW_AT_sibling. + class debug_info_entry::attributes : public attributes_base + { + friend class dwarf; + private: + inline attributes (const class raw_attributes &raw) + : attributes_base (raw) {} + + public: + typedef int key_type; + typedef attr_value mapped_type; + typedef attribute value_type; + + inline attributes (const class attributes &a) + : attributes_base (a) {} + + typedef attributes_base::const_iterator const_iterator; + + /* + iterator: wraps raw_attributes iterator, skips DW_AT_sibling + size/empty: search for DW_AT_sibling, adjust raw_attributes size + */ + + inline const_iterator find (int name) const + { + if (unlikely (name == ::DW_AT_sibling)) + return end (); + return const_iterator (_m_raw.find (name), _m_raw.end ()); + } + + inline const attr_value at (int name) + { + const_iterator i = find (name); + if (unlikely (i == end ())) + throw std::out_of_range ("XXX"); + return (*i).second; + } + inline const attr_value operator[] (int name) + { + return at (name); + } + + // We are rvalue-coercible into a std::map, which is sorted by name. + inline operator std::map<int, attr_value> () const + { + return std::map<int, attr_value> (begin (), end ()); + } + /* + template<typename attrs> + inline operator attrs () const + { + return attrs (begin (), end ()); + } + */ + + template<typename attrs> + bool operator== (const attrs &other) const + { + /* Our container is unordered (i.e., in file order). A set of + attributes is conceptually equal if all the pairs match, + regardless of the order. But the container_equal algorithm will + compare corresponding elements in order. So we need an ordered + map of our attributes for the comparison. */ + const std::map<int, attr_value> mine = *this; + const std::map<int, typename attrs::mapped_type> his = other; + return mine.size () == his.size () && subr::container_equal (mine, his); + } + + template<typename attrs> + bool operator!= (const attrs &other) const + { + return !(*this == other); + } + }; + + class compile_unit : public debug_info_entry + { + public: + inline compile_unit (const debug_info_entry &die) + : debug_info_entry (die) {} + + // Fetch the CU's DW_AT_stmt_list. + const line_info_table line_info () const; + + // Convenience methods for line_info_table sub-containers. + inline const directory_table include_directories () const + { + return line_info ().include_directories (); + } + const file_table files () const + { + return line_info ().files (); + } + const line_table lines () const + { + return line_info ().lines (); + } + + /* + containers/iterators: + + XXX macros + + abbrevs (punt) + + */ + }; + + // These are the kinds of values that attributes can have. + enum value_space + { + // These spaces refer purely to DWARF concepts. + VS_flag, // Boolean. + VS_dwarf_constant, // Known DW_X_* space of integer constants. + VS_discr_list, // Block as used for DW_AT_discr_list. + VS_reference, // Pointer to another DIE. + VS_unit_reference, // Pointer to another CU DIE. + VS_lineptr, // Pointer into .debug_line section. + VS_macptr, // Pointer into .debug_macinfo section. + VS_rangelistptr, // Pointer into .debug_ranges section. + + // These spaces refer to textual details of the program source. + VS_identifier, // String, identifier in source language. + VS_string, // String, miscellaneous use. + VS_source_file, // Source file, string or index into file table. + VS_source_line, // Line number in source file. + VS_source_column, // Column number in source file. + + // These spaces refer to target-format values in the debuggee. + VS_address, // Address constant. + VS_constant, // Other constant, integer or in target formats. + VS_location, // Location expression or location list. + }; + + /* A source file can be just a file name. When represented in the + .debug_line file table, it can also have a modtime and a file size. + If the modtime or size stored is zero, it doesn't count. */ + class source_file + { + friend class attr_value; + friend class file_table; + friend class line_entry; + private: + ::Dwarf_Attribute _m_attr; + inline ::Dwarf_Attribute *thisattr () const + { + return const_cast< ::Dwarf_Attribute *> (&_m_attr); + } + + source_file (const Dwarf_Attribute &attr) : _m_attr (attr) {} + + public: + std::string to_string () const; + + const char *name () const; + ::Dwarf_Word mtime () const; + ::Dwarf_Word size () const; + + template<typename other_file> + bool operator== (const other_file &other) const + { + if (mtime () != 0) + { + ::Dwarf_Word other_mtime = other.mtime (); + if (other_mtime != 0 && other_mtime != mtime ()) + return false; + } + if (size () != 0) + { + ::Dwarf_Word other_size = other.size (); + if (other_size != 0 && other_size != size ()) + return false; + } + return subr::name_equal<typeof (other.name ())> () (name (), + other.name ()); + } + template<typename other_file> + inline bool operator!= (const other_file &other) const + { + return !(*this == other); + } + }; + + // This describes the value of an attribute. + class attr_value + { + friend class attribute; + friend class location_attr; + friend class range_list; + friend class dwarf_enum; + private: + const int _m_tag; + ::Dwarf_Attribute _m_attr; + inline ::Dwarf_Attribute *thisattr () const + { + return const_cast< ::Dwarf_Attribute *> (&_m_attr); + } + inline int whatattr () const + { + return ::dwarf_whatattr (thisattr ()); + } + + attr_value (int tag, const ::Dwarf_Attribute &attr) + : _m_tag (tag), _m_attr (attr) {} + + inline bool same (const attr_value &other) const + { + return _m_attr.valp == other._m_attr.valp; + } + + public: + // not copyable, don't worry about ref lifetime(?) + // attr_value (const attr_value &v) : _m_attr (v.attr) {} + + value_space what_space () const; + + std::string to_string () const; + + // Return an iterator on which * will yield the referent debug_info_entry. + inline debug_info_entry::raw_children::const_iterator reference () const + { + return debug_info_entry::raw_children::const_iterator (thisattr ()); + } + inline debug_info_entry::raw_children::const_iterator + unit_reference () const + { + return reference (); + } + + // XXX reloc, dwfl + ::Dwarf_Addr address () const; + + bool flag () const; + + const location_attr location () const; + + const char *string () const; + inline const char *identifier () const + { + return string (); + } + + const dwarf::source_file source_file () const; + inline unsigned int source_line () const + { + return constant (); + } + inline unsigned int source_column () const + { + return constant (); + } + + // XXX reloc + ::Dwarf_Word constant () const; + ::Dwarf_Sword signed_constant () const; + const_vector<uint8_t> constant_block () const; + bool constant_is_integer () const; + + inline const dwarf_enum dwarf_constant () const + { + return dwarf_enum (*this); + } + + inline const range_list ranges () const + { + return range_list (*this); + } + + const line_info_table line_info () const; + + // XXX macptr + + template<typename value> + inline bool operator== (const value &other) const + { + const value_space what = what_space (); + if (likely (other.what_space () == what)) + switch (what) + { + case VS_reference: + case VS_unit_reference: + return true; // XXX Reference identity check (?) + //return reference ()->offset () == other.reference ()->offset (); + + case VS_flag: + return flag () == other.flag (); + + case VS_rangelistptr: + return ranges () == other.ranges (); + + case VS_lineptr: + return line_info () == other.line_info (); + + case VS_macptr: // XXX punt for now, treat as constant + /*FALLTHRU*/ + case VS_dwarf_constant: + return constant () == other.constant (); + + case VS_constant: + if (constant_is_integer ()) + return (other.constant_is_integer () + && constant () == other.constant ()); + return (!other.constant_is_integer () + && constant_block () == other.constant_block ()); + + case VS_source_line: + return source_line () == other.source_line (); + case VS_source_column: + return source_column () == other.source_column (); + + case VS_identifier: + return subr::name_equal<typeof (other.identifier ())> () + (identifier (), other.identifier ()); + + case VS_string: + return subr::name_equal<typeof (other.string ())> () + (string (), other.string ()); + + case VS_address: + return address () == other.address (); + + case VS_source_file: + return source_file () == other.source_file (); + + case VS_location: + return location () == other.location (); + + case VS_discr_list: + throw std::runtime_error ("XXX unimplemented"); + } + return false; + } + template<typename value> + inline bool operator!= (const value &other) const + { + return !(*this == other); + } + }; + + /* A location attribute yields a location expression. + Either it's a single expression, or a map of PC to location. */ + class location_attr + { + friend class attr_value; + private: + attr_value _m_attr; + + location_attr (const attr_value &attr) : _m_attr (attr) {} + + inline bool same (const location_attr &it) const + { + return _m_attr.same (it._m_attr); + } + + template<typename pair> + struct nonempty : public std::unary_function<pair, bool> + { + inline bool operator () (const pair &x) + { + return !x.second.empty (); + } + }; + + template<typename pair> + struct any : public std::unary_function<pair, bool> + { + inline bool operator () (const pair &x) + { + return true; + } + }; + + public: + typedef size_t size_type; + typedef ptrdiff_t difference_type; + // XXX need proper type for exprs + typedef const_vector<uint8_t> mapped_type; + typedef std::pair< ::Dwarf_Addr, ::Dwarf_Addr> key_type; // XXX reloc + typedef std::pair<const key_type, mapped_type> value_type; + class const_iterator; // below + + std::string to_string () const; + + bool is_list () const; + + inline mapped_type location () const + { + if (is_list ()) + throw std::runtime_error ("location is list, not single location"); + return _m_attr.constant_block (); + } + + inline const_iterator begin () const; + inline const_iterator end () const; + + inline bool empty () const + { + if (is_list ()) + return std::find_if (begin (), end (), + nonempty<value_type> ()) == end (); + return location ().empty (); + } + inline size_type size () const + { + if (is_list ()) + return std::count_if (begin (), end (), any<value_type> ()); + return location ().empty () ? 0 : 1; + } + + template<typename other_attr> + bool operator== (const other_attr &other) const + { + if (empty ()) + return (other.empty () + || std::find_if (other.begin (), other.end (), + nonempty<typename other_attr::value_type> () + ) == other.end ()); + if (!is_list ()) + return (!other.is_list () && !other.empty () + && location () == other.location ()); + + return other.is_list () && subr::container_equal (*this, other); + } + template<typename other_file> + inline bool operator!= (const other_file &other) const + { + return !(*this == other); + } + + /* + XXX missing: find, at; by key_type or by PC + XXX worse than that: multiple overlapping matches! + */ + }; + + class location_attr::const_iterator + : public std::iterator<std::input_iterator_tag, location_attr::value_type> + { + friend class location_attr; + private: + ::Dwarf_Addr _m_base; // XXX reloc + ::Dwarf_Addr _m_begin; // XXX reloc + ::Dwarf_Addr _m_end; // XXX reloc + location_attr _m_attr; + ptrdiff_t _m_offset; + + const_iterator (const location_attr &loc, ptrdiff_t offset) + : _m_base (-1), _m_begin (1), _m_end (0), + _m_attr (loc), _m_offset (offset) {} + + public: + + inline bool operator== (const const_iterator &it) const + { + return _m_offset == it._m_offset && _m_attr.same (it._m_attr); + } + inline bool operator!= (const const_iterator &it) const + { + return !(*this == it); + }; + + const_iterator &operator++ (); // prefix + inline const_iterator operator++ (int) // postfix + { + const_iterator prev = *this; + ++*this; + return prev; + } + + inline value_type operator* () const + { + if (unlikely (_m_offset == 1)) + throw std::runtime_error ("dereferencing end iterator"); + + return value_type (key_type (_m_base + _m_begin, _m_base + _m_end), + _m_attr.location ()); + } + }; + + /* The DW_AT_ranges attribute yields a range list. + XXX reloc + This is equivalent to unordered_set<pair<Dwarf_Addr, Dwarf_Addr> >. */ + class range_list + { + friend class attr_value; + private: + const attr_value _m_attr; + + range_list (const attr_value &attr) : _m_attr (attr) {} + + public: + typedef std::pair< ::Dwarf_Addr, ::Dwarf_Addr> key_type; // XXX reloc + typedef key_type value_type; + + range_list (const range_list &other) : _m_attr (other._m_attr) {} + + std::string to_string () const; + + class const_iterator + : public std::iterator<std::input_iterator_tag, value_type> + { + friend class range_list; + private: + ::Dwarf_Addr _m_base; // XXX reloc + ::Dwarf_Addr _m_begin; // XXX reloc + ::Dwarf_Addr _m_end; // XXX reloc + ::Dwarf_CU *_m_cu; + ptrdiff_t _m_offset; + + const_iterator (Dwarf_Attribute *, ptrdiff_t); + + public: + inline const_iterator (const const_iterator &i) + : _m_base (i._m_base), _m_begin (i._m_begin), _m_end (i._m_end), + _m_cu (i._m_cu), _m_offset (i._m_offset) {} + + inline value_type operator* () const + { + if (unlikely (_m_offset == 1)) + throw std::runtime_error ("dereferencing end iterator"); + return std::make_pair (_m_base + _m_begin, _m_base + _m_end); + } + + inline const_iterator &operator= (const const_iterator &other) + { + _m_base = other._m_base; + _m_begin = other._m_begin; + _m_end = other._m_end; + _m_cu = other._m_cu; + _m_offset = other._m_offset; + return *this; + } + + inline bool operator== (const const_iterator &other) const + { + return _m_offset == other._m_offset && _m_cu == other._m_cu; + } + inline bool operator!= (const const_iterator &other) const + { + return !(*this == other); + } + + const_iterator &operator++ (); // prefix + inline const_iterator operator++ (int) // postfix + { + const_iterator prev = *this; + ++*this; + return prev; + } + }; + + const_iterator begin () const + { + const_iterator it (_m_attr.thisattr (), 0); + return ++it; + } + const_iterator end () const + { + return const_iterator (_m_attr.thisattr (), 1); + } + + const_iterator find (const key_type &match) const + { + return std::find (begin (), end (), match); + } + + private: + struct entry_contains + : public std::binary_function<key_type, ::Dwarf_Addr, bool> + { + inline bool operator() (const key_type &range, const ::Dwarf_Addr addr) + const + { + return addr >= range.first && addr < range.second; + } + }; + + public: + const_iterator find (const ::Dwarf_Addr addr) const + { + return std::find_if (begin (), end (), + std::bind2nd (entry_contains (), addr)); + } + + inline operator std::set<key_type> () const + { + return std::set<key_type> (begin (), end ()); + } + + template<typename ranges> + inline bool operator== (const ranges &other) const + { + /* Our container is unordered (i.e., in file order). A range list + is conceptually equal if all the pairs match, regardless of the + order. But the std::equal algorithm will compare corresponding + elements in order. So we need an ordered set for comparison. */ + std::set<key_type> mine = *this; + coalesce (mine); + std::set<key_type> his = other; + coalesce (his); + return mine == his; + } + template<typename ranges> + inline bool operator!= (const ranges &other) const + { + return !(*this == other); + } + }; + + // This describes a CU's directory table, a simple array of strings. + class directory_table + { + private: + ::Dwarf_Files *_m_files; + + template<typename table> + inline bool table_equal (const table &other) const + { + /* We ignore the first element, the compilation directory. + This is not encoded in the .debug_line table, but in + the DW_AT_comp_dir attribute of the referring CU. + The directory table itself matches regardless. */ + const_iterator i = begin (); + typename table::const_iterator j = other.begin (); + return subr::container_equal + (++i, end (), ++j, other.end (), + subr::name_equal<typename table::value_type> ()); + } + + public: + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef const char *value_type; + + inline directory_table (::Dwarf_Files *const files) + : _m_files (files) {} + inline directory_table (const directory_table &t) + : _m_files (t._m_files) {} + + typedef const char *const *const_iterator; + + inline bool empty () const + { + return size () == 0; + } + + size_t size () const; + const_iterator begin () const; + const_iterator end () const; + + template<typename table> + inline bool operator== (const table &other) const + { + return table_equal (other); + } + template<typename table> + inline bool operator!= (const table &other) const + { + return !(*this == other); + } + // Short-circuit for comparing to self. + inline bool operator== (const directory_table &other) const + { + return _m_files == other._m_files || table_equal (other); + } + }; + + /* This describes a CU's file table. It works like a read-only + std::vector<source_file>, and also supports lookup by name. */ + class file_table + { + private: + ::Dwarf_Files *_m_files; + + public: + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef source_file value_type; + + inline file_table (::Dwarf_Files *const files) + : _m_files (files) {} + inline file_table (const file_table &t) + : _m_files (t._m_files) {} + + inline file_table &operator= (const file_table &t) + { + _m_files = t._m_files; + return *this; + } + + typedef subr::indexed_iterator<file_table> const_iterator; + + inline bool empty () const + { + return size () == 0; + } + + size_t size () const; + + inline const_iterator begin () const + { + return const_iterator (*this, 0); + } + inline const_iterator end () const + { + return const_iterator (*this, size ()); + } + + const source_file at (size_t idx) const; + const source_file operator[] (size_t idx) const + { + return at (idx); + } + + // Look up by matching file name. + const_iterator find (const source_file &) const; + const_iterator find (const char *filename) const + { + const_iterator i = begin (); + while (i != end () && strcmp ((*i).name (), filename) != 0) + ++i; + return i; + } + template<typename string> + const_iterator find (const string &filename) const + { + const_iterator i = begin (); + while (i != end () && filename != (*i).name ()) + ++i; + return i; + } + }; + + // This describes one entry in the line information table. + class line_entry + { + private: + ::Dwarf_Line *_m_line; + + public: + line_entry (::Dwarf_Line *entry) : _m_line (entry) {} + line_entry (const line_entry &entry) : _m_line (entry._m_line) {} + + // XXX reloc, dwfl + ::Dwarf_Addr address () const; + + bool statement () const; + bool basic_block () const; + bool end_sequence () const; + bool prologue_end () const; + bool epilogue_begin () const; + + const source_file file () const; + unsigned int line () const; + unsigned int column () const; + + template<typename entry> + bool operator< (const entry &other) const + { + return address () < other.address (); + } + template<typename entry> + bool operator> (const entry &other) const + { + return address () > other.address (); + } + template<typename entry> + bool operator<= (const entry &other) const + { + return address () <= other.address (); + } + template<typename entry> + bool operator>= (const entry &other) const + { + return address () >= other.address (); + } + + template<typename entry> + inline bool operator== (const entry &other) const + { + return (address () == other.address () + && line () == other.line () + && column () == other.column () + && statement () == other.statement () + && basic_block () == other.basic_block () + && end_sequence () == other.end_sequence () + && prologue_end () == other.prologue_end () + && epilogue_begin () == other.epilogue_begin () + && file () == other.file ()); + } + template<typename entry> + inline bool operator!= (const entry &other) const + { + return !(*this == other); + } + // Short-circuit for our own type. + bool operator== (const line_entry &other) const; + }; + + /* This describes a CU's line information table. + It works like a read-only std::vector<line_entry>, + and also supports lookup by address. + XXX later, by file/line + */ + class line_table + { + private: + ::Dwarf_Lines *_m_lines; + + public: + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef line_entry value_type; + + inline line_table (::Dwarf_Lines *const lines) + : _m_lines (lines) {} + inline line_table (const line_table &t) + : _m_lines (t._m_lines) {} + + inline line_table &operator= (const line_table &t) + { + _m_lines = t._m_lines; + return *this; + } + + typedef subr::indexed_iterator<line_table> const_iterator; + + inline bool empty () const + { + return size () == 0; + } + + size_t size () const; + + inline const_iterator begin () const + { + return const_iterator (*this, 0); + } + inline const_iterator end () const + { + return const_iterator (*this, size ()); + } + + const line_entry at (size_t idx) const; + const line_entry operator[] (size_t idx) const + { + return at (idx); + } + + template<typename table> + inline bool operator== (const table &other) const + { + return size () == other.size () && subr::container_equal (*this, other); + } + template<typename table> + inline bool operator!= (const table &other) const + { + return !(*this == other); + } + // Short-circuit for comparing to self. + inline bool operator== (const line_table &other) const + { + return (_m_lines == other._m_lines + || subr::container_equal (*this, other)); + } + + // Look up by matching address. + const_iterator find (::Dwarf_Addr) const; + }; + + // The DW_AT_stmt_list attribute yields a line info table. + class line_info_table + { + private: + ::Dwarf_Files *_m_files; + + public: + inline line_info_table (::Dwarf_Files *const t) + : _m_files (t) {} + inline line_info_table (const line_info_table &t) + : _m_files (t._m_files) {} + + inline line_info_table &operator= (const line_info_table &t) + { + _m_files = t._m_files; + return *this; + } + + inline const directory_table include_directories () const + { + return directory_table (_m_files); + } + inline const file_table files () const + { + return file_table (_m_files); + } + const line_table lines () const; + + template<typename table> + inline bool operator== (const table &other) const + { + return (include_directories () == other.include_directories () + && lines () == other.lines ()); + } + template<typename table> + inline bool operator!= (const table &other) const + { + return !(*this == other); + } + }; + + class dwarf_enum + { + friend class attr_value; + private: + const attr_value _m_attr; + + dwarf_enum (const attr_value &attr) : _m_attr (attr) {} + + public: + inline operator unsigned int () const + { + return _m_attr.constant (); + } + + std::string to_string () const; + + const char *identifier () const; + const char *name () const; + }; + + // This describes one attribute, equivalent to pair<const int, attr_value>. + class attribute + { + friend class debug_info_entry::raw_attributes::const_iterator; + friend class attr_value; + private: + inline ::Dwarf_Attribute *thisattr () const + { + return second.thisattr (); + } + + class lhs + { + friend class attribute; + private: + const attribute &_m_attr; + + lhs (attribute &attr) : _m_attr (attr) {} + + public: + operator int () const + { + return ::dwarf_whatattr (_m_attr.thisattr ()); + } + }; + + attribute (const debug_info_entry &die, const ::Dwarf_Attribute &attr) + : first (*this), second (die.tag (), attr) {} + + public: + lhs first; + attr_value second; + + inline attribute (const attribute &a) + : first (*this), second (a.second) {} + + // This lets pair<...> x = (attribute) y work. + template<typename value> + operator std::pair<const int, value> () const + { + return std::make_pair (static_cast<int> (first), value (second)); + } + + template<typename pair> + inline bool operator== (const pair &other) const + { + return first == other.first && second == other.second; + } + template<typename pair> + inline bool operator!= (const pair &other) const + { + return !(*this == other); + } + + inline std::string to_string () const + { + std::string result = attributes::name (::dwarf_whatattr (thisattr ())); + result += "="; + result += second.to_string (); + return result; + } + }; + + /* This works like range_list, but is based on a debug_info_entry using + dwarf_ranges. If the entry has DW_AT_low_pc and DW_AT_high_pc, this + will present a singleton list; if it has a DW_AT_ranges, it will be + the same as the range_list presentation. If neither, an empty list. */ + class ranges + { + friend class debug_info_entry; + private: + debug_info_entry _m_die; + + ranges (const debug_info_entry &die) : _m_die (die) {} + + public: + typedef std::pair< ::Dwarf_Addr, ::Dwarf_Addr> key_type; // XXX reloc + typedef key_type value_type; + + ranges (const ranges &other) : _m_die (other._m_die) {} + + std::string to_string () const; + + class const_iterator + : public std::iterator<std::input_iterator_tag, value_type> + { + friend class ranges; + private: + debug_info_entry _m_die; + ::Dwarf_Addr _m_base; // XXX reloc + ::Dwarf_Addr _m_begin; // XXX reloc + ::Dwarf_Addr _m_end; // XXX reloc + ptrdiff_t _m_offset; + + inline const_iterator (const debug_info_entry &die) + : _m_die (die), _m_offset (0) {} + + public: + inline const_iterator (const const_iterator &i) + : _m_die (i._m_die), _m_base (i._m_base), + _m_begin (i._m_begin), _m_end (i._m_end), + _m_offset (i._m_offset) {} + + inline value_type operator* () const + { + if (unlikely (_m_offset == 0)) + throw std::runtime_error ("dereferencing end iterator"); + return std::make_pair (_m_begin, _m_end); + } + + inline const_iterator &operator= (const const_iterator &other) + { + _m_die = other._m_die; + _m_base = other._m_base; + _m_begin = other._m_begin; + _m_end = other._m_end; + _m_offset = other._m_offset; + return *this; + } + + inline bool operator== (const const_iterator &other) const + { + return (_m_die._m_die.addr == other._m_die._m_die.addr + && _m_offset == other._m_offset); + } + inline bool operator!= (const const_iterator &other) const + { + return !(*this == other); + } + + const_iterator &operator++ () // prefix + { + do + _m_offset = dwarf_ranges (_m_die.thisdie (), _m_offset, + &_m_base, &_m_begin, &_m_end); + // Skip over empty ranges. + while (_m_offset != 0 && _m_begin == _m_end); + return *this; + } + inline const_iterator operator++ (int) // postfix + { + const_iterator prev = *this; + ++*this; + return prev; + } + }; + + const_iterator begin () const + { + const_iterator it (_m_die); + return ++it; + } + const_iterator end () const + { + return const_iterator (_m_die); + } + + inline bool empty () const + { + return begin () == end (); + } + + const_iterator find (const key_type &match) const + { + return std::find (begin (), end (), match); + } + + private: + struct entry_contains + : public std::binary_function<key_type, ::Dwarf_Addr, bool> + { + inline bool operator() (const key_type &range, const ::Dwarf_Addr addr) + const + { + return addr >= range.first && addr < range.second; + } + }; + + public: + const_iterator find (const ::Dwarf_Addr addr) const + { + return std::find_if (begin (), end (), + std::bind2nd (entry_contains (), addr)); + } + + inline operator std::set<key_type> () const + { + return std::set<key_type> (begin (), end ()); + } + + template<typename ranges> + inline bool operator== (const ranges &other) const + { + /* Our container is unordered (i.e., in file order). A range list + is conceptually equal if all the pairs match, regardless of the + order. But the std::equal algorithm will compare corresponding + elements in order. So we need an ordered set for comparison. */ + std::set<key_type> mine = *this; + coalesce (mine); + std::set<key_type> his = other; + coalesce (his); + return mine == his; + } + template<typename ranges> + inline bool operator!= (const ranges &other) const + { + return !(*this == other); + } + }; + + // Container for raw CUs in file order, intended to be compatible + // with a read-only subset of std::list<compile_unit>. + class raw_compile_units + { + friend class dwarf; + private: + const dwarf &_m_file; + + raw_compile_units (const dwarf &file) : _m_file (file) {} + + public: + typedef compile_unit value_type; + + inline raw_compile_units (const raw_compile_units &u) + : _m_file (u._m_file) {} + + class const_iterator + : public std::iterator<std::input_iterator_tag, compile_unit> + { + friend class raw_compile_units; + private: + debug_info_entry _m_die; + const dwarf *_m_file; // XXX + ::Dwarf_Off _m_next; // XXX + + inline const_iterator (const dwarf &file, ::Dwarf_Off next) + : _m_file (&file), _m_next (next) {} + + public: + inline const_iterator (const const_iterator &i) + : _m_die (i._m_die), _m_file (i._m_file), _m_next (i._m_next) {} + + inline const debug_info_entry &operator* () const + { + if (unlikely (_m_next == (::Dwarf_Off) -1)) + throw std::runtime_error ("dereferencing end iterator"); + return _m_die; + } + inline const debug_info_entry *operator-> () const + { + return &(operator* ()); + } + + inline const_iterator &operator= (const const_iterator &other) + { + _m_die = other._m_die; + _m_next = other._m_next; + _m_file = other._m_file; // XXX + return *this; + } + + inline bool operator== (const const_iterator &other) const + { + return _m_file == other._m_file && _m_next == other._m_next; + } + inline bool operator!= (const const_iterator &other) const + { + return !(*this == other); + } + + inline const_iterator &operator++ () // prefix + { + // XXX should be rewritten to use libdw_findcu internals + // slow way for first crack to avoid DSO issues + _m_next = _m_file->nextcu (_m_next, _m_die.thisdie ()); + return *this; + } + inline const_iterator operator++ (int) // postfix + { + const_iterator prev = *this; + ++*this; + return prev; + } + }; + + const_iterator begin () const + { + const_iterator it (_m_file, 0); + return ++it; + } + inline const_iterator end () const + { + return const_iterator (_m_file, -1); + } + }; + inline raw_compile_units raw_compile_units () const + { + return raw_compile_units::raw_compile_units (*this); + } + + private: + static inline bool skip_partial_unit (const compile_unit &unit) + { + switch (unit.tag ()) + { + case ::DW_TAG_partial_unit: + return true; + case ::DW_TAG_compile_unit: + return false; + default: + throw std::exception(); // XXX invalid dwarf + } + } + + typedef skipping_wrapper<class raw_compile_units, + compile_unit, compile_unit, + skip_partial_unit> compile_units_base; + + public: + + // Container for logical CUs in file order, intended to be compatible + // with a read-only subset of std::list<compile_unit>. + class compile_units : public compile_units_base + { + friend class dwarf; + private: + compile_units (class raw_compile_units raw) : compile_units_base (raw) {} + + public: + typedef compile_unit value_type; + + compile_units (const compile_units &u) : compile_units_base (u) {} + + template<typename units> + bool operator== (const units &other) const + { + return subr::container_equal (*this, other); + } + template<typename units> + bool operator!= (const units &other) const + { + return !(*this == other); + } + }; + inline class compile_units compile_units () const + { + return compile_units::compile_units (raw_compile_units ()); + } + + private: + ::Dwarf *_m_dw; + + public: + // XXX temp hack + inline ::Dwarf_Off nextcu (::Dwarf_Off offset, ::Dwarf_Die *die) const + { + ::Dwarf_Off next; + ::size_t header_size; + int result = ::dwarf_nextcu (_m_dw, offset, &next, &header_size, + NULL, NULL, NULL); + xif (result < 0); + if (result == 0) + xif (::dwarf_offdie (_m_dw, offset + header_size, die) == NULL); + else + memset (die, 0, sizeof *die); + return next; + } + + inline dwarf (::Dwarf *dw) : _m_dw (dw) {}; + + inline dwarf (const dwarf &dw) : _m_dw (dw._m_dw) {}; + + template<typename file> + inline bool operator== (const file &other) const + { + return compile_units () == other.compile_units (); + } + template<typename file> + inline bool operator!= (const file &other) const + { + return !(*this == other); + } + + // XXX reloc + class arange_list + : public std::set<std::pair< ::Dwarf_Addr, ::Dwarf_Addr> > + { + private: + typedef std::set<std::pair< ::Dwarf_Addr, ::Dwarf_Addr> > _base; + + protected: + template<typename iterator> + arange_list (iterator first, iterator last) : _base (first, last) {} + + public: + typedef _base::key_type key_type; + typedef _base::value_type value_type; + typedef _base::iterator iterator; + typedef _base::const_iterator const_iterator; + + arange_list () {} + arange_list (const arange_list &other) + : _base (static_cast<const _base &> (other)) {} + + std::string to_string () const; + }; + + private: + struct arange_less + : public std::binary_function<compile_unit, compile_unit, bool> + { + inline bool operator() (const compile_unit &a, const compile_unit &b) + const + { + return a.offset () < b.offset (); + } + }; + + public: + typedef std::map<compile_unit, arange_list, arange_less> aranges_map; + + aranges_map aranges () const; + + private: + static bool adjacency (const arange_list::key_type &a, + const arange_list::key_type &b) + { + return a.second == b.first; + } + + // Coalesce adjacent ranges. + static void coalesce (std::set<arange_list::key_type> &set) + { + for (std::set<arange_list::key_type>::iterator i = set.begin (); + (i = std::adjacent_find (i, set.end (), adjacency)) != set.end (); + ++i) + { + std::set<arange_list::key_type>::iterator j = i; + std::set<arange_list::key_type>::iterator k = ++j; + while (++k != set.end () && adjacency (*j, *k)) + ++j; + const arange_list::key_type joined (i->first, j->second); + set.erase (i, k); + i = set.insert (joined).first; + } + } + }; + + inline class dwarf::debug_info_entry::raw_children + dwarf::debug_info_entry::raw_children () const + { + return raw_children::raw_children (*this); + } + + inline class dwarf::debug_info_entry::children + dwarf::debug_info_entry::children () const + { + return children::children (*this); + } + + inline class dwarf::debug_info_entry::raw_attributes + dwarf::debug_info_entry::raw_attributes () const + { + return raw_attributes::raw_attributes (*this); + } + + inline class dwarf::debug_info_entry::attributes + dwarf::debug_info_entry::attributes () const + { + return attributes::attributes (raw_attributes ()); + } + + inline dwarf::location_attr::const_iterator + dwarf::location_attr::begin () const + { + const_iterator it (*this, 0); + if (is_list ()) + ++it; + return it; + } + inline dwarf::location_attr::const_iterator dwarf::location_attr::end () const + { + return const_iterator (*this, 1); + } +}; + +#endif // <elfutils/dwarf> diff --git a/libdw/c++/dwarf-knowledge.cc b/libdw/c++/dwarf-knowledge.cc new file mode 100644 index 00000000..de526fe5 --- /dev/null +++ b/libdw/c++/dwarf-knowledge.cc @@ -0,0 +1,158 @@ +#include <config.h> +#include "dwarf" + +using namespace std; +using namespace elfutils; + +#define VS(what) (1U << dwarf::VS_##what) + +/* Return a bitmask of value spaces expected for this attribute of this tag. + Primarily culled from the DWARF 3 spec: 7.5.4, Figure 20. */ + +static unsigned int +expected_value_space (int attr, int tag) +{ + + switch (attr) + { + case DW_AT_sibling: + case DW_AT_common_reference: + case DW_AT_containing_type: + case DW_AT_default_value: + case DW_AT_abstract_origin: + case DW_AT_base_types: + case DW_AT_friend: + case DW_AT_priority: + case DW_AT_specification: + case DW_AT_type: + case DW_AT_use_location: + case DW_AT_data_location: + case DW_AT_extension: + case DW_AT_small: + case DW_AT_object_pointer: + case DW_AT_namelist_item: + return VS(reference); + + case DW_AT_location: + case DW_AT_string_length: + case DW_AT_return_addr: + case DW_AT_data_member_location: + case DW_AT_frame_base: + case DW_AT_segment: + case DW_AT_static_link: + case DW_AT_vtable_elem_location: + return VS(location); + + case DW_AT_name: + switch (tag) + { + case DW_TAG_compile_unit: + case DW_TAG_partial_unit: + return VS(source_file); + default: + return VS(identifier); + } + + case DW_AT_ordering: + case DW_AT_language: + case DW_AT_visibility: + case DW_AT_inline: + case DW_AT_accessibility: + case DW_AT_address_class: + case DW_AT_calling_convention: + case DW_AT_encoding: + case DW_AT_identifier_case: + case DW_AT_virtuality: + case DW_AT_endianity: + return VS(dwarf_constant); + + case DW_AT_byte_size: + case DW_AT_byte_stride: + case DW_AT_bit_size: + case DW_AT_bit_offset: + case DW_AT_bit_stride: + case DW_AT_lower_bound: + case DW_AT_upper_bound: + case DW_AT_count: + case DW_AT_allocated: + case DW_AT_associated: + return VS(reference) | VS(constant) | VS(location); // XXX non-loc expr + + case DW_AT_stmt_list: + return VS(lineptr); + case DW_AT_macro_info: + return VS(macptr); + case DW_AT_ranges: + return VS(rangelistptr); + + case DW_AT_low_pc: + case DW_AT_high_pc: + case DW_AT_entry_pc: + return VS(address); + + case DW_AT_discr: + return VS(reference); + case DW_AT_discr_value: + return VS(constant); + case DW_AT_discr_list: + return VS(discr_list); + + case DW_AT_import: + return VS(unit_reference); + + case DW_AT_comp_dir: + return VS(source_file); + + case DW_AT_const_value: + return VS(constant) | VS(string); + + case DW_AT_is_optional: + case DW_AT_prototyped: + case DW_AT_artificial: + case DW_AT_declaration: + case DW_AT_external: + case DW_AT_variable_parameter: + case DW_AT_use_UTF8: + case DW_AT_mutable: + case DW_AT_threads_scaled: + case DW_AT_explicit: + case DW_AT_elemental: + case DW_AT_pure: + case DW_AT_recursive: + return VS(flag); + + case DW_AT_producer: + return VS(string); + + case DW_AT_start_scope: + return VS(constant); + + case DW_AT_binary_scale: + case DW_AT_decimal_scale: + case DW_AT_decimal_sign: + case DW_AT_digit_count: + return VS(constant); + + case DW_AT_decl_file: + case DW_AT_call_file: + return VS(source_file); + case DW_AT_decl_line: + case DW_AT_call_line: + return VS(source_line); + case DW_AT_decl_column: + case DW_AT_call_column: + return VS(source_column); + + case DW_AT_trampoline: + return VS(address) | VS(flag) | VS(reference) | VS(string); + + case DW_AT_description: + case DW_AT_picture_string: + return VS(string); + + case DW_AT_MIPS_linkage_name: + return VS(identifier); + } + + return 0; +} diff --git a/libdw/c++/dwarf_edit b/libdw/c++/dwarf_edit new file mode 100644 index 00000000..e2811bbd --- /dev/null +++ b/libdw/c++/dwarf_edit @@ -0,0 +1,1085 @@ +/* elfutils::dwarf_edit -- mutable DWARF representation in -*- C++ -*- + Copyright (C) 2009 Red Hat, Inc. + This file is part of Red Hat elfutils. + + Red Hat elfutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by the + Free Software Foundation; version 2 of the License. + + Red Hat elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Red Hat elfutils; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. + + In addition, as a special exception, Red Hat, Inc. gives You the + additional right to link the code of Red Hat elfutils with code licensed + under any Open Source Initiative certified open source license + (http://www.opensource.org/licenses/index.php) which requires the + distribution of source code with any binary distribution and to + distribute linked combinations of the two. Non-GPL Code permitted under + this exception must only link to the code of Red Hat elfutils through + those well defined interfaces identified in the file named EXCEPTION + found in the source code files (the "Approved Interfaces"). The files + of Non-GPL Code may instantiate templates or use macros or inline + functions from the Approved Interfaces without causing the resulting + work to be covered by the GNU General Public License. Only Red Hat, + Inc. may make changes or additions to the list of Approved Interfaces. + Red Hat's grant of this exception is conditioned upon your not adding + any new exceptions. If you wish to add a new Approved Interface or + exception, please contact Red Hat. You must obey the GNU General Public + License in all respects for all of the Red Hat elfutils code and other + code used in conjunction with Red Hat elfutils except the Non-GPL Code + covered by this exception. If you modify this file, you may extend this + exception to your version of the file, but you are not obligated to do + so. If you do not wish to provide this exception without modification, + you must delete this exception statement from your version and license + this file solely under the GPL without exception. + + Red Hat elfutils is an included package of the Open Invention Network. + An included package of the Open Invention Network is a package for which + Open Invention Network licensees cross-license their patents. No patent + license is granted, either expressly or impliedly, by designation as an + included package. Should you wish to participate in the Open Invention + Network licensing program, please visit www.openinventionnetwork.com + <http://www.openinventionnetwork.com>. */ + +#ifndef _ELFUTILS_DWARF_EDIT +#define _ELFUTILS_DWARF_EDIT 1 + +#include "dwarf" +#include <bitset> + +/* Read the comments for elfutils::dwarf first. + + The elfutils::dwarf_edit class is template-compatible with the logical + containers described in elfutils::dwarf, and copy-constructible from the + input class. + + The elfutils::dwarf_edit containers are mutable, unlike the input + classes. You can modify the DWARF directly in all the normal ways the + corresponding std containers have, or build it up from scratch. When + you have it how you want it, you can pass it into elfutils::dwarf_output. + + The dwarf_edit classes will use unreasonable amounts of memory for large + DWARF data sets, like from reading in whole large program and DSO files. + To transform input files efficiently, you should construct dwarf_output + directly from input dwarf with transformations applied on the fly, and + not use dwarf_edit at all. + + dwarf_edit is the only mutable representation and so it's easy to use in + a straightforward imperative style. Use it for transformations on small + data files, or for creating small data sets from scratch. */ + +// DWARF manipulation interfaces (pure object construction) +namespace elfutils +{ + class dwarf_edit + { + public: + class compile_units; + class attr_value; + + class debug_info_entry + { + public: + + class children : public std::list<debug_info_entry> + { + friend class debug_info_entry; + private: + children () {} + + template<typename childrens> + children (const childrens &other) + : std::list<debug_info_entry> (other.begin (), other.end ()) {} + + public: + typedef debug_info_entry value_type; + }; + + class attributes : public std::map<int, attr_value> + { + friend class debug_info_entry; + private: + typedef std::map<int, attr_value> base_type; + + attributes () {} + + template<typename attrs> + attributes (const attrs &other) + : std::map<int, attr_value> (other.begin (), other.end ()) {} + + public: + typedef base_type::key_type key_type; + typedef base_type::value_type value_type; + typedef base_type::mapped_type mapped_type; + + template<typename attrs> + inline operator attrs () const + { + return attrs (begin (), end ()); + } + }; + + private: + const int _m_tag; + attributes _m_attributes; + children _m_children; + + public: + debug_info_entry (int t) : _m_tag (t) + { + if (unlikely (t <= 0)) + throw std::invalid_argument ("invalid tag"); + } + + /* The template constructor lets us copy in from any class that has + compatibly iterable containers for attributes and children. */ + template<typename die> + debug_info_entry (const die &die) + : _m_tag (die.tag ()), + _m_attributes (die.attributes ()), + _m_children (die.children ()) + {} + + inline int tag () const + { + return _m_tag; + } + + inline bool has_children () const + { + return !_m_children.empty (); + } + + inline class children &children () + { + return _m_children; + } + inline const class children &children () const + { + return _m_children; + } + + inline class attributes &attributes () + { + return _m_attributes; + } + inline const class attributes &attributes () const + { + return _m_attributes; + } + + template<typename die> + bool operator== (const die &other) const + { + return (other.attributes () == attributes () + && other.children () == children ()); + } + template<typename die> + bool operator!= (const die &other) const + { + return !(*this == other);; + } + }; + + typedef debug_info_entry::attributes::value_type attribute; + + class compile_unit : public debug_info_entry + { + friend class compile_units; + private: + inline compile_unit () : debug_info_entry (::DW_TAG_compile_unit) {} + + // XXX should be private + public: + template<typename die> + compile_unit (const die &die) : debug_info_entry (die) + { + if (die.tag () != ::DW_TAG_compile_unit) + throw std::invalid_argument ("not a compile_unit entry"); + } + + /* XXX doesn't help + public: + compile_unit (const compile_unit &u) : debug_info_entry (u) {} + */ + }; + + // Main container anchoring all the output. + class compile_units : public std::list<compile_unit> + { + friend class dwarf_edit; + private: + typedef std::list<compile_unit> _base; + + // Default constructor: an empty container, no CUs. + inline compile_units () {} + + // Constructor copying CUs from input container. + template<typename input> + compile_units(const input &units) + : _base (units.begin (), units.end ()) {} + + public: + typedef compile_unit value_type; + typedef _base::iterator iterator; + typedef _base::const_iterator const_iterator; + + inline compile_unit &new_unit () + { + compile_unit nu; + push_back (nu); + return back (); + } + + template<typename other_children> + bool operator== (const other_children &other) const + { + return subr::container_equal (*this, other); + } + template<typename other_children> + bool operator!= (const other_children &other) const + { + return !(*this == other); + } + }; + + // Same as set<pair<Dwarf_Addr, Dwarf_Addr>>. + class range_list : public dwarf::arange_list + { + public: + template<typename list> + range_list (const list &other) + : dwarf::arange_list (other.begin (), other.end ()) {} + }; + + class source_file + { + private: + std::string _m_name; + ::Dwarf_Word _m_mtime; + ::Dwarf_Word _m_size; + + public: + source_file () : _m_name (), _m_mtime (0), _m_size (0) {} + source_file (const std::string &n, ::Dwarf_Word m = 0, ::Dwarf_Word s = 0) + : _m_name (n), _m_mtime (m), _m_size (s) {} + + template<typename file> + source_file (const file &other) + : _m_name (other.name ()), + _m_mtime (other.mtime ()), _m_size (other.size ()) {} + + template<typename file> + inline source_file &operator= (const file &other) + { + _m_name = other.name (); + _m_mtime = other.mtime (); + _m_size = other.size (); + return *this; + } + inline source_file &operator= (const std::string &n) + { + _m_name = n; + _m_mtime = 0; + _m_size = 0; + return *this; + } + inline source_file &operator= (const char *n) + { + _m_name = n; + _m_mtime = 0; + _m_size = 0; + return *this; + } + + inline std::string &name () + { + return _m_name; + } + inline const std::string &name () const + { + return _m_name; + } + inline ::Dwarf_Word &mtime () + { + return _m_mtime; + } + inline ::Dwarf_Word mtime () const + { + return _m_mtime; + } + inline ::Dwarf_Word &size () + { + return _m_size; + } + inline ::Dwarf_Word size () const + { + return _m_size; + } + + template<typename other_file> + bool operator== (const other_file &other) const + { + if (mtime () != 0) + { + ::Dwarf_Word other_mtime = other.mtime (); + if (other_mtime != 0 && other_mtime != mtime ()) + return false; + } + if (size () != 0) + { + ::Dwarf_Word other_size = other.size (); + if (other_size != 0 && other_size != size ()) + return false; + } + return name () == other.name (); + } + template<typename other_file> + inline bool operator!= (const other_file &other) const + { + return !(*this == other); + } + }; + + // This describes a CU's directory table, a simple array of strings. + class directory_table : public std::vector<std::string> + { + private: + typedef std::vector<std::string> _base; + + public: + directory_table () {} + + template<typename table> + directory_table (const table &other) + : _base (other.begin (), other.end ()) {} + + template<typename table> + inline bool operator== (const table &other) const + { + return size () == other.size () && subr::container_equal (*this, other); + } + template<typename table> + inline bool operator!= (const table &other) const + { + return !(*this == other); + } + }; + + class line_entry + { + private: + ::Dwarf_Addr _m_addr; // XXX dwfl, reloc + source_file _m_file; + unsigned int _m_line; + unsigned int _m_column; + + enum flag_bit + { + flag_statement, + flag_basic_block, + flag_end_sequence, + flag_prologue_end, + flag_epilogue_begin, + flag_count + }; + std::bitset<flag_count> _m_flags; + + public: + line_entry (::Dwarf_Addr addr) + : _m_addr (addr), _m_file (), _m_line (0), _m_column (0) {} + + template<typename entry> + line_entry (const entry &other) + : _m_addr (0), _m_file (), _m_line (0), _m_column (0) + { + *this = other; + } + + template<typename entry> + line_entry &operator= (const entry &other) + { + _m_addr = other.address (); + _m_file = other.file (); + _m_line = other.line (); + _m_column = other.column (); + statement () = other.statement (); + basic_block () = other.basic_block (); + end_sequence () = other.end_sequence (); + prologue_end () = other.prologue_end (); + epilogue_begin () = other.epilogue_begin (); + return *this; + } + + inline ::Dwarf_Addr &address () + { + return _m_addr; + } + inline ::Dwarf_Addr address () const + { + return _m_addr; + } + inline source_file &file () + { + return _m_file; + } + inline const source_file &file () const + { + return _m_file; + } + inline unsigned int &line () + { + return _m_line; + } + inline unsigned int line () const + { + return _m_line; + } + inline unsigned int &column () + { + return _m_column; + } + inline unsigned int column () const + { + return _m_column; + } + +#define _DWARF_EDIT_LE_FLAG(what) \ + bool what () const \ + { \ + return _m_flags[flag_##what]; \ + } \ + std::bitset<flag_count>::reference what () \ + { \ + return _m_flags[flag_##what]; \ + } + _DWARF_EDIT_LE_FLAG (statement) + _DWARF_EDIT_LE_FLAG (basic_block) + _DWARF_EDIT_LE_FLAG (end_sequence) + _DWARF_EDIT_LE_FLAG (prologue_end) + _DWARF_EDIT_LE_FLAG (epilogue_begin) +#undef _DWARF_EDIT_LE_FLAG + + template<typename entry> + bool operator< (const entry &other) const + { + return address () < other.address (); + } + template<typename entry> + bool operator> (const entry &other) const + { + return address () > other.address (); + } + template<typename entry> + bool operator<= (const entry &other) const + { + return address () <= other.address (); + } + template<typename entry> + bool operator>= (const entry &other) const + { + return address () >= other.address (); + } + + template<typename entry> + inline bool operator== (const entry &other) const + { + return (address () == other.address () + && line () == other.line () + && column () == other.column () + && statement () == other.statement () + && basic_block () == other.basic_block () + && end_sequence () == other.end_sequence () + && prologue_end () == other.prologue_end () + && epilogue_begin () == other.epilogue_begin () + && file () == other.file ()); + } + template<typename entry> + inline bool operator!= (const entry &other) const + { + return !(*this == other); + } + }; + + class line_table : public std::vector<line_entry> + { + private: + typedef std::vector<line_entry> _base; + + public: + typedef _base::size_type size_type; + typedef _base::difference_type difference_type; + typedef _base::value_type value_type; + typedef _base::iterator iterator; + typedef _base::const_iterator const_iterator; + + line_table () {} + + template<typename table> + line_table (const table &other) : _base (other.begin (), other.end ()) {} + + template<typename table> + inline bool operator== (const table &other) const + { + return size () == other.size () && subr::container_equal (*this, other); + } + template<typename table> + inline bool operator!= (const table &other) const + { + return !(*this == other); + } + + // Look up by matching address. + iterator find (::Dwarf_Addr); + const_iterator find (::Dwarf_Addr) const; + }; + + class line_info_table + { + private: + directory_table _m_dirs; + line_table _m_lines; + + public: + line_info_table () : _m_dirs (), _m_lines () {} + + template<typename table> + line_info_table (const table &other) + : _m_dirs (other.include_directories ()), _m_lines (other.lines ()) {} + + template<typename table> + inline line_info_table &operator= (const table &other) + { + _m_dirs = directory_table (other.include_directories ()); + _m_lines = line_table (other.lines ()); + return *this; + } + + inline directory_table &include_directories () + { + return _m_dirs; + } + inline const directory_table &include_directories () const + { + return _m_dirs; + } + inline line_table &lines () + { + return _m_lines; + } + inline const line_table &lines () const + { + return _m_lines; + } + + template<typename table> + inline bool operator== (const table &other) const + { + return (include_directories () == other.include_directories () + && lines () == other.lines ()); + } + template<typename table> + inline bool operator!= (const table &other) const + { + return !(*this == other); + } + }; + + class location_attr : public std::map<dwarf::location_attr::key_type, + std::vector<uint8_t> > + { + private: + typedef std::map<dwarf::location_attr::key_type, + std::vector<uint8_t> > _base; + + public: + typedef _base::size_type size_type; + typedef _base::difference_type difference_type; + typedef _base::key_type key_type; + typedef _base::mapped_type mapped_type; + typedef _base::value_type value_type; + typedef _base::iterator iterator; + typedef _base::const_iterator const_iterator; + + inline location_attr () : _base () {} + inline location_attr (const location_attr &other) + : _base (static_cast<const _base &> (other)) {} + template<typename loc> + inline location_attr (const loc &other) : _base () + { + *this = other; + } + + template<typename loc> + inline location_attr &operator= (const loc &other) + { + clear (); + if (other.empty ()) + ; + else if (other.is_list ()) + for (typename loc::const_iterator i = other.begin (); + i != other.end (); + ++i) + { + const typename loc::mapped_type &x = (*i).second; + (*this)[(*i).first] = mapped_type (x.begin (), x.end ()); + } + else + (*this)[key_type (0, -1)] = other.location (); + return *this; + } + + inline bool is_list () const + { + if (empty ()) + return false; + if (size () > 1) + return true; + + const key_type &elt = begin ()->first; + return !(elt.first == 0 && elt.second == (Dwarf_Addr) -1); + } + + inline mapped_type &location () + { + if (empty ()) + return (*this)[key_type (0, -1)]; + + value_type &v = *begin (); + if (v.first.first != 0 || v.first.second != (Dwarf_Addr) -1 + || size () > 1) + throw std::runtime_error ("location is list, not single location"); + + return v.second; + } + inline const mapped_type &location () const + { + if (size () == 1) + { + const value_type &v = *begin (); + if (v.first.first == 0 && v.first.second == (Dwarf_Addr) -1) + return v.second; + } + throw std::runtime_error ("location is list, not single location"); + } + }; + + private: + class value_dispatch + { + public: + virtual ~value_dispatch () {} + }; + + struct value_string : public value_dispatch, public std::string + { + template<typename string> + value_string (const string &s) : std::string (s) {} + + std::string to_string () const + { + std::string result ("\""); + result += *this; + result += "\""; + return result; + } + }; + + struct value_identifier : public value_string + { + template<typename id> + value_identifier (const id &s) : value_string (s) {} + }; + + struct value_reference : public value_dispatch + { + debug_info_entry::children::iterator ref; + value_reference (const debug_info_entry::children::iterator &i) + : ref (i) {} + + template<typename iter> // XXX dummy + value_reference (const iter &i) : ref () {} + }; + struct value_unit_reference : public value_dispatch + { + compile_units::iterator ref; + value_unit_reference (const compile_units::iterator &i) : ref (i) {} + + template<typename iter> // XXX dummy + value_unit_reference (const iter &i) : ref () {} + }; + + struct value_flag : public value_dispatch + { + bool flag; + value_flag (bool t) : flag (t) {} + }; + + struct value_address : public value_dispatch + { + // XXX dwfl, reloc + ::Dwarf_Addr addr; + value_address (::Dwarf_Addr a) : addr (a) {} + }; + + struct value_rangelistptr : public value_dispatch, public range_list + { + template<typename list> + value_rangelistptr (const list &other) : range_list (other) {} + }; + + struct value_lineptr : public value_dispatch, public line_info_table + { + template<typename table> + value_lineptr (const table &other) : line_info_table (other) {} + }; + + struct value_constant : public value_dispatch + { + union + { + ::Dwarf_Word word; + ::Dwarf_Sword sword; + }; + value_constant (::Dwarf_Word x) : word (x) {} + }; + + struct value_constant_block : public value_dispatch, + public std::vector<uint8_t> + { + template<typename block> + value_constant_block (const block &b) + : std::vector<uint8_t> (b.begin (), b.end ()) {} + }; + + struct value_dwarf_constant : public value_constant + { + value_dwarf_constant (unsigned int x) : value_constant (x) {} + }; + + struct value_source_file : public value_dispatch, public source_file + { + template<typename file> + value_source_file (const file &other) : source_file (other) {} + }; + + struct value_source_line : public value_dispatch + { + unsigned int n; + value_source_line (unsigned int m) : n (m) {} + }; + typedef value_source_line value_source_column; + + struct value_macptr : public value_dispatch {}; + + struct value_location : public value_dispatch, public location_attr + { + template<typename loc> + value_location (const loc &other) : location_attr (other) {} + }; + + public: + class attr_value + { + private: + value_dispatch *_m_value; + + template<typename value> + inline void init (const value &other) + { + switch (other.what_space ()) + { + case dwarf::VS_identifier: + _m_value = new value_identifier (other.identifier ()); + break; + case dwarf::VS_string: + _m_value = new value_string (other.string ()); + break; + case dwarf::VS_flag: + _m_value = new value_flag (other.flag ()); + break; + case dwarf::VS_rangelistptr: + _m_value = new value_rangelistptr (other.ranges ()); + break; + case dwarf::VS_lineptr: + _m_value = new value_lineptr (other.line_info ()); + break; + case dwarf::VS_address: + _m_value = new value_address (other.address ()); + break; + case dwarf::VS_constant: + if (other.constant_is_integer ()) + _m_value = new value_constant (other.constant ()); + else + _m_value = new value_constant_block (other.constant_block ()); + break; + case dwarf::VS_source_line: + _m_value = new value_source_line (other.source_line ()); + break; + case dwarf::VS_source_column: + _m_value = new value_source_column (other.source_column ()); + break; + case dwarf::VS_source_file: + _m_value = new value_source_file (other.source_file ()); + break; + case dwarf::VS_dwarf_constant: + _m_value = new value_dwarf_constant (other.dwarf_constant ()); + break; + case dwarf::VS_reference: + _m_value = new value_reference (other.reference ()); + break; + case dwarf::VS_unit_reference: + _m_value = new value_unit_reference (other.unit_reference ()); + break; + case dwarf::VS_location: + _m_value = new value_location (other.location ()); + break; +#if 0 + case dwarf::VS_macptr: + _m_value = new value_macptr (other.macptr ()); + break; +#endif + default: + case dwarf::VS_discr_list: + throw std::runtime_error ("XXX unimplemented"); + } + } + + template<typename flavor> + inline flavor &variant () const + { + flavor *p = dynamic_cast<flavor *> (_m_value); + if (p == NULL) + throw std::runtime_error ("wrong value type"); + return *p; + } + + public: + attr_value (const attr_value &other) : _m_value (NULL) + { + init (other); + } + template<typename value> + attr_value (const value &other) : _m_value (NULL) + { + init (other); + } + + attr_value () : _m_value (NULL) {} + + ~attr_value () + { + if (_m_value != NULL) + delete _m_value; + } + + inline attr_value &operator= (const attr_value &other) + { + if (_m_value != NULL) + { + delete _m_value; + _m_value = NULL; + } + init (other); + return *this; + } + template<typename value> + inline attr_value &operator= (const value &other) + { + if (_m_value != NULL) + { + delete _m_value; + _m_value = NULL; + } + init (other); + return *this; + } + + dwarf::value_space what_space () const; + std::string to_string () const; + + inline bool &flag () const + { + return variant<value_flag> ().flag; + } + + // XXX dwfl, reloc + inline ::Dwarf_Addr &address () const + { + return variant<value_address> ().addr; + } + + inline debug_info_entry::children::iterator reference () const + { + return variant<value_reference> ().ref; + } + inline compile_units::iterator unit_reference () const + { + return variant<value_unit_reference> ().ref; + } + + inline location_attr &location () const + { + return static_cast<location_attr &> (variant<value_location> ()); + } + + inline std::string &string () const + { + return static_cast<std::string &> (variant<value_string> ()); + } + inline std::string &identifier () const + { + return string (); + } + + inline dwarf_edit::source_file &source_file () const + { + return static_cast<dwarf_edit::source_file &> + (variant<value_source_file> ()); + } + + inline unsigned int &source_line () const + { + return variant<value_source_line> ().n; + } + + inline unsigned int &source_column () const + { + return variant<value_source_column> ().n; + } + + inline ::Dwarf_Word &constant () const + { + return variant<value_constant> ().word; + } + + inline ::Dwarf_Sword &signed_constant () const + { + return variant<value_constant> ().sword; + } + + inline std::vector<uint8_t> &constant_block () const + { + return static_cast<std::vector<uint8_t> &> + (variant<value_constant_block> ()); + } + + inline ::Dwarf_Word &dwarf_constant () const + { + return variant<value_dwarf_constant> ().word; + } + + inline bool constant_is_integer () const + { + return dynamic_cast<value_constant *> (_m_value) != NULL; + } + + inline range_list &ranges () const + { + return static_cast<range_list &> (variant<value_rangelistptr> ()); + } + + inline line_info_table &line_info () const + { + return static_cast<line_info_table &> (variant<value_lineptr> ()); + } + + // macptr + + template<typename value> + inline bool operator== (const value &other) const + { + const dwarf::value_space what = what_space (); + if (likely (other.what_space () == what)) + switch (what) + { + case dwarf::VS_identifier: + return identifier () == other.identifier (); + case dwarf::VS_string: + return string () == other.string (); + case dwarf::VS_reference: + return reference () == other.reference (); + case dwarf::VS_unit_reference: + return unit_reference () == other.unit_reference (); + case dwarf::VS_flag: + return flag () == other.flag (); + case dwarf::VS_rangelistptr: + return ranges () == other.ranges (); + case dwarf::VS_lineptr: + return line_info () == other.line_info (); + case dwarf::VS_constant: + if (constant_is_integer ()) + return (other.constant_is_integer () + && constant () == other.constant ()); + return (!other.constant_is_integer () + && constant_block () == other.constant_block ()); + case dwarf::VS_source_file: + return source_file () == other.source_file (); + case dwarf::VS_source_line: + return source_line () == other.source_line (); + case dwarf::VS_source_column: + return source_column () == other.source_column (); + case dwarf::VS_address: + return address () == other.address (); + case dwarf::VS_location: + return location () == other.location (); + case dwarf::VS_dwarf_constant: + return constant () == other.constant (); // XXX +#if 0 + case dwarf::VS_macptr: + return macptr () == other.macptr (); +#endif + default: + case dwarf::VS_discr_list: + throw std::runtime_error ("XXX unimplemented"); + } + return false; + } + template<typename value> + inline bool operator!= (const value &other) const + { + return !(*this == other); + } + }; + + private: + compile_units _m_units; + + public: + class compile_units &compile_units () + { + return _m_units; + } + const class compile_units &compile_units () const + { + return _m_units; + } + + public: + // Default constructor: an empty container, no CUs. + inline dwarf_edit () {} + + // Constructor copying CUs from an input file (dwarf or dwarf_edit). + template<typename input> + dwarf_edit (const input &dw) : _m_units (dw.compile_units ()) {} + + template<typename file> + inline bool operator== (const file &other) const + { + return compile_units () == other.compile_units (); + } + template<typename file> + inline bool operator!= (const file &other) const + { + return !(*this == other); + } + }; +}; + +#endif // <elfutils/dwarf_edit> diff --git a/libdw/c++/dwarf_output b/libdw/c++/dwarf_output new file mode 100644 index 00000000..d4d2ee61 --- /dev/null +++ b/libdw/c++/dwarf_output @@ -0,0 +1,224 @@ +/* elfutils::dwarf_output -- DWARF file generation in -*- C++ -*- + Copyright (C) 2009 Red Hat, Inc. + This file is part of Red Hat elfutils. + + Red Hat elfutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by the + Free Software Foundation; version 2 of the License. + + Red Hat elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Red Hat elfutils; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. + + In addition, as a special exception, Red Hat, Inc. gives You the + additional right to link the code of Red Hat elfutils with code licensed + under any Open Source Initiative certified open source license + (http://www.opensource.org/licenses/index.php) which requires the + distribution of source code with any binary distribution and to + distribute linked combinations of the two. Non-GPL Code permitted under + this exception must only link to the code of Red Hat elfutils through + those well defined interfaces identified in the file named EXCEPTION + found in the source code files (the "Approved Interfaces"). The files + of Non-GPL Code may instantiate templates or use macros or inline + functions from the Approved Interfaces without causing the resulting + work to be covered by the GNU General Public License. Only Red Hat, + Inc. may make changes or additions to the list of Approved Interfaces. + Red Hat's grant of this exception is conditioned upon your not adding + any new exceptions. If you wish to add a new Approved Interface or + exception, please contact Red Hat. You must obey the GNU General Public + License in all respects for all of the Red Hat elfutils code and other + code used in conjunction with Red Hat elfutils except the Non-GPL Code + covered by this exception. If you modify this file, you may extend this + exception to your version of the file, but you are not obligated to do + so. If you do not wish to provide this exception without modification, + you must delete this exception statement from your version and license + this file solely under the GPL without exception. + + Red Hat elfutils is an included package of the Open Invention Network. + An included package of the Open Invention Network is a package for which + Open Invention Network licensees cross-license their patents. No patent + license is granted, either expressly or impliedly, by designation as an + included package. Should you wish to participate in the Open Invention + Network licensing program, please visit www.openinventionnetwork.com + <http://www.openinventionnetwork.com>. */ + +#ifndef _ELFUTILS_DWARF_OUTPUT +#define _ELFUTILS_DWARF_OUTPUT 1 + +#include "dwarf" + +/* Read the comments for elfutils::dwarf first. + + The elfutils::dwarf_output class is template-compatible with the logical + containers described in elfutils::dwarf and elfutils::dwarf_edit. + + The dwarf_output representation of the DWARF data is immutable once + created. The only way to create the object is by copy-construction + from another compatible object: dwarf, dwarf_edit, or dwarf_output. + Construction collects all the information necessary to generate the + formatted DWARF sections. */ + +namespace elfutils +{ + class dwarf_output + { + public: + class compile_units; + + // XXX later + class attr_value : public dwarf::attr_value + { + public: + attr_value (const dwarf::attr_value &v) : dwarf::attr_value (v) {} + }; + + class debug_info_entry + { + public: + + class children : public std::vector<debug_info_entry> + { + friend class debug_info_entry; + private: + children () {} + + template<typename childrens> + children (const childrens &other) + : std::vector<debug_info_entry> (other.begin (), other.end ()) {} + }; + + class attributes : public std::map<int, attr_value> + { + friend class debug_info_entry; + private: + attributes () {} + + template<typename attrs> + attributes (const attrs &other) + : std::map<int, attr_value> (other.begin (), other.end ()) {} + + public: + template<typename attrs> + inline operator attrs () const + { + return attrs (begin (), end ()); + } + }; + + private: + const int _m_tag; + const attributes _m_attributes; + const children _m_children; + + public: + /* The template constructor lets us copy in from any class that has + compatibly iterable containers for attributes and children. */ + template<typename die> + debug_info_entry (const die &die) + : _m_tag (die.tag ()), + _m_attributes (die.attributes ()), + _m_children (die.children ()) + {} + + inline int tag () const + { + return _m_tag; + } + + inline bool has_children () const + { + return !_m_children.empty (); + } + + inline class children &children () + { + return _m_children; + } + inline const class children &children () const + { + return _m_children; + } + + inline class attributes &attributes () + { + return _m_attributes; + } + inline const class attributes &attributes () const + { + return _m_attributes; + } + + template<typename die> + bool operator== (const die &other) const + { + return (other.attributes () == attributes () + && other.children () == children ()); + } + template<typename die> + bool operator!= (const die &other) const + { + return !(*this == other);; + } + }; + + typedef debug_info_entry::attributes::value_type attribute; + + class compile_unit : public debug_info_entry + { + }; + + // Main container anchoring all the output. + class compile_units : public std::vector<compile_unit> + { + friend class dwarf_output; + private: + // Constructor copying CUs from input container. + template<typename input> + compile_units(const input &units) + : std::vector<compile_unit> (units.begin (), units.end ()) + {} + + public: + template<typename other_children> + bool operator== (const other_children &other) const + { + return std::equal (begin (), end (), other.begin ()); + } + template<typename other_children> + bool operator!= (const other_children &other) const + { + return !(*this == other); + } + }; + + private: + const compile_units _m_units; + + public: + const class compile_units &compile_units () const + { + return _m_units; + } + + public: + // Constructor copying CUs from an input file (dwarf or dwarf_output). + template<typename input> + dwarf_output (const input &dw) : _m_units (dw.compile_units ()) {} + + template<typename file> + inline bool operator== (const file &other) const + { + return compile_units () == other.compile_units (); + } + template<typename file> + inline bool operator!= (const file &other) const + { + return !(*this == other); + } + }; +} diff --git a/libdw/c++/edit-values.cc b/libdw/c++/edit-values.cc new file mode 100644 index 00000000..f502c66c --- /dev/null +++ b/libdw/c++/edit-values.cc @@ -0,0 +1,94 @@ +/* elfutils::dwarf_edit attribute value interfaces. + Copyright (C) 2009 Red Hat, Inc. + This file is part of Red Hat elfutils. + + Red Hat elfutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by the + Free Software Foundation; version 2 of the License. + + Red Hat elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Red Hat elfutils; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. + + In addition, as a special exception, Red Hat, Inc. gives You the + additional right to link the code of Red Hat elfutils with code licensed + under any Open Source Initiative certified open source license + (http://www.opensource.org/licenses/index.php) which requires the + distribution of source code with any binary distribution and to + distribute linked combinations of the two. Non-GPL Code permitted under + this exception must only link to the code of Red Hat elfutils through + those well defined interfaces identified in the file named EXCEPTION + found in the source code files (the "Approved Interfaces"). The files + of Non-GPL Code may instantiate templates or use macros or inline + functions from the Approved Interfaces without causing the resulting + work to be covered by the GNU General Public License. Only Red Hat, + Inc. may make changes or additions to the list of Approved Interfaces. + Red Hat's grant of this exception is conditioned upon your not adding + any new exceptions. If you wish to add a new Approved Interface or + exception, please contact Red Hat. You must obey the GNU General Public + License in all respects for all of the Red Hat elfutils code and other + code used in conjunction with Red Hat elfutils except the Non-GPL Code + covered by this exception. If you modify this file, you may extend this + exception to your version of the file, but you are not obligated to do + so. If you do not wish to provide this exception without modification, + you must delete this exception statement from your version and license + this file solely under the GPL without exception. + + Red Hat elfutils is an included package of the Open Invention Network. + An included package of the Open Invention Network is a package for which + Open Invention Network licensees cross-license their patents. No patent + license is granted, either expressly or impliedly, by designation as an + included package. Should you wish to participate in the Open Invention + Network licensing program, please visit www.openinventionnetwork.com + <http://www.openinventionnetwork.com>. */ + +#include <config.h> +#include "dwarf_edit" + +#include <typeinfo> + +using namespace elfutils; + + +dwarf::value_space +dwarf_edit::attr_value::what_space () const +{ + if (typeid (*_m_value) == typeid (value_flag)) + return dwarf::VS_flag; + if (typeid (*_m_value) == typeid (value_dwarf_constant)) + return dwarf::VS_dwarf_constant; + if (typeid (*_m_value) == typeid (value_reference)) + return dwarf::VS_reference; + if (typeid (*_m_value) == typeid (value_unit_reference)) + return dwarf::VS_unit_reference; + if (typeid (*_m_value) == typeid (value_lineptr)) + return dwarf::VS_lineptr; + if (typeid (*_m_value) == typeid (value_macptr)) + return dwarf::VS_macptr; + if (typeid (*_m_value) == typeid (value_rangelistptr)) + return dwarf::VS_rangelistptr; + if (typeid (*_m_value) == typeid (value_identifier)) + return dwarf::VS_identifier; + if (typeid (*_m_value) == typeid (value_string)) + return dwarf::VS_string; + if (typeid (*_m_value) == typeid (value_source_file)) + return dwarf::VS_source_file; + if (typeid (*_m_value) == typeid (value_source_line)) + return dwarf::VS_source_line; + if (typeid (*_m_value) == typeid (value_source_column)) + return dwarf::VS_source_column; + if (typeid (*_m_value) == typeid (value_address)) + return dwarf::VS_address; + if (typeid (*_m_value) == typeid (value_constant) + || typeid (*_m_value) == typeid (value_constant_block)) + return dwarf::VS_constant; + if (typeid (*_m_value) == typeid (value_location)) + return dwarf::VS_location; + + throw std::runtime_error ("XXX impossible"); +} diff --git a/libdw/c++/exception.cc b/libdw/c++/exception.cc new file mode 100644 index 00000000..15a0cef1 --- /dev/null +++ b/libdw/c++/exception.cc @@ -0,0 +1,76 @@ +/* -*- C++ -*- exceptions for libdw. + Copyright (C) 2009 Red Hat, Inc. + This file is part of Red Hat elfutils. + + Red Hat elfutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by the + Free Software Foundation; version 2 of the License. + + Red Hat elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Red Hat elfutils; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. + + In addition, as a special exception, Red Hat, Inc. gives You the + additional right to link the code of Red Hat elfutils with code licensed + under any Open Source Initiative certified open source license + (http://www.opensource.org/licenses/index.php) which requires the + distribution of source code with any binary distribution and to + distribute linked combinations of the two. Non-GPL Code permitted under + this exception must only link to the code of Red Hat elfutils through + those well defined interfaces identified in the file named EXCEPTION + found in the source code files (the "Approved Interfaces"). The files + of Non-GPL Code may instantiate templates or use macros or inline + functions from the Approved Interfaces without causing the resulting + work to be covered by the GNU General Public License. Only Red Hat, + Inc. may make changes or additions to the list of Approved Interfaces. + Red Hat's grant of this exception is conditioned upon your not adding + any new exceptions. If you wish to add a new Approved Interface or + exception, please contact Red Hat. You must obey the GNU General Public + License in all respects for all of the Red Hat elfutils code and other + code used in conjunction with Red Hat elfutils except the Non-GPL Code + covered by this exception. If you modify this file, you may extend this + exception to your version of the file, but you are not obligated to do + so. If you do not wish to provide this exception without modification, + you must delete this exception statement from your version and license + this file solely under the GPL without exception. + + Red Hat elfutils is an included package of the Open Invention Network. + An included package of the Open Invention Network is a package for which + Open Invention Network licensees cross-license their patents. No patent + license is granted, either expressly or impliedly, by designation as an + included package. Should you wish to participate in the Open Invention + Network licensing program, please visit www.openinventionnetwork.com + <http://www.openinventionnetwork.com>. */ + +#include <config.h> +#include <cassert> +#include "dwarf" + +extern "C" +{ +#include "libdwP.h" +} + +using namespace elfutils; +using namespace std; + + +/* Throw + */ +void +dwarf::throw_libdw (::Dwarf *dw) +{ + throw std::runtime_error (::dwarf_errmsg (-1)); +} + +// This is just for things that can't find the Dwarf pointer directly. +void +dwarf::throw_libdw (::Dwarf_CU *cu) +{ + throw_libdw (cu->dbg); +} diff --git a/libdw/c++/known.cc b/libdw/c++/known.cc new file mode 100644 index 00000000..81e4417e --- /dev/null +++ b/libdw/c++/known.cc @@ -0,0 +1,197 @@ +/* Known named integer values in DWARF. + Copyright (C) 2009 Red Hat, Inc. + This file is part of Red Hat elfutils. + + Red Hat elfutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by the + Free Software Foundation; version 2 of the License. + + Red Hat elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Red Hat elfutils; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. + + In addition, as a special exception, Red Hat, Inc. gives You the + additional right to link the code of Red Hat elfutils with code licensed + under any Open Source Initiative certified open source license + (http://www.opensource.org/licenses/index.php) which requires the + distribution of source code with any binary distribution and to + distribute linked combinations of the two. Non-GPL Code permitted under + this exception must only link to the code of Red Hat elfutils through + those well defined interfaces identified in the file named EXCEPTION + found in the source code files (the "Approved Interfaces"). The files + of Non-GPL Code may instantiate templates or use macros or inline + functions from the Approved Interfaces without causing the resulting + work to be covered by the GNU General Public License. Only Red Hat, + Inc. may make changes or additions to the list of Approved Interfaces. + Red Hat's grant of this exception is conditioned upon your not adding + any new exceptions. If you wish to add a new Approved Interface or + exception, please contact Red Hat. You must obey the GNU General Public + License in all respects for all of the Red Hat elfutils code and other + code used in conjunction with Red Hat elfutils except the Non-GPL Code + covered by this exception. If you modify this file, you may extend this + exception to your version of the file, but you are not obligated to do + so. If you do not wish to provide this exception without modification, + you must delete this exception statement from your version and license + this file solely under the GPL without exception. + + Red Hat elfutils is an included package of the Open Invention Network. + An included package of the Open Invention Network is a package for which + Open Invention Network licensees cross-license their patents. No patent + license is granted, either expressly or impliedly, by designation as an + included package. Should you wish to participate in the Open Invention + Network licensing program, please visit www.openinventionnetwork.com + <http://www.openinventionnetwork.com>. */ + +#include <config.h> +#include "dwarf" +#include "known-dwarf.h" + +using namespace elfutils; +using namespace std; + + +const char * +dwarf::known_tag (int tag) +{ + switch (tag) + { +#define ONE_KNOWN_DW_TAG(name, id) case id: return #id; +#define ONE_KNOWN_DW_TAG_DESC(name, id, desc) ONE_KNOWN_DW_TAG (name, id) + ALL_KNOWN_DW_TAG + } + return NULL; +} + +const char * +dwarf::known_attribute (int name) +{ + switch (name) + { +#define ONE_KNOWN_DW_AT(name, id) case id: return #id; +#define ONE_KNOWN_DW_AT_DESC(name, id, desc) ONE_KNOWN_DW_AT (name, id) + ALL_KNOWN_DW_AT + } + return NULL; +} + +namespace elfutils +{ + template<int key> + size_t + dwarf::known_enum<key>::prefix_length () + { + return 0; + } + + template<int key> + const char * + dwarf::known_enum<key>::identifier (int value) + { + return NULL; + } + +#define ALL_KNOWN_ENUM \ + KNOWN_ENUM (accessibility, ACCESS) \ + KNOWN_ENUM (encoding, ATE) \ + KNOWN_ENUM (calling_convention, CC) \ + KNOWN_ENUM (decimal_sign, DS) \ + KNOWN_ENUM (endianity, END) \ + KNOWN_ENUM (identifier_case, ID) \ + KNOWN_ENUM (inline, INL) \ + KNOWN_ENUM (language, LANG) \ + KNOWN_ENUM (ordering, ORD) \ + KNOWN_ENUM (virtuality, VIRTUALITY) \ + KNOWN_ENUM (visibility, VIS) + +#define ONE_KNOWN_DW_ACCESS(name, id) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_ATE(name, id) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_CC(name, id) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_DS(name, id) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_END(name, id) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_ID(name, id) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_INL(name, id) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_LANG(name, id) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_LANG_DESC(name, id, desc) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_ORD(name, id) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_INL(name, id) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_VIRTUALITY(name, id) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_VIS(name, id) KNOWN_ENUM_CASE (id) + + // Stupid C++ doesn't do [x] = y initializers. +#define KNOWN_ENUM(attr, enum) \ + template<> \ + size_t \ + dwarf::known_enum<DW_AT_##attr>::prefix_length () \ + { \ + return sizeof ("DW_" #enum "_") - 1; \ + } \ + template<> \ + const char * \ + dwarf::known_enum<DW_AT_##attr>::identifier (int value) \ + { \ + switch (value) \ + { \ + ALL_KNOWN_DW_##enum \ + } \ + return NULL; \ + } +#define KNOWN_ENUM_CASE(id) case id: return #id; + + ALL_KNOWN_ENUM + + // Not really enum cases, but pretend they are. +#define ONE_KNOWN_DW_FORM(name, id) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_OP(name, id) KNOWN_ENUM_CASE (id) +#define ONE_KNOWN_DW_OP_DESC(name, id, desc) KNOWN_ENUM_CASE (id) + KNOWN_ENUM (producer, FORM) + KNOWN_ENUM (location, OP) + +#undef KNOWN_ENUM +#undef KNOWN_ENUM_CASE +}; + +const char * +dwarf::dwarf_enum::identifier () const +{ + switch (_m_attr.whatattr ()) + { +# define KNOWN_ENUM(attr, enum) \ + case DW_AT_##attr: \ + return dwarf::known_enum<DW_AT_##attr>::identifier (*this); + + ALL_KNOWN_ENUM + +# undef KNOWN_ENUM + } + + return NULL; +} + +const char * +dwarf::dwarf_enum::name () const +{ + switch (_m_attr.whatattr ()) + { +# define KNOWN_ENUM(attr, enum) \ + case DW_AT_##attr: \ + return dwarf::known_enum<DW_AT_##attr>::name (*this); + + ALL_KNOWN_ENUM + +# undef KNOWN_ENUM + } + + return NULL; +} + +std::string +dwarf::dwarf_enum::to_string () const +{ + const char *known = name (); + return known == NULL ? subr::hex_string (*this) : std::string (known); +} diff --git a/libdw/c++/line_info.cc b/libdw/c++/line_info.cc new file mode 100644 index 00000000..676f81ff --- /dev/null +++ b/libdw/c++/line_info.cc @@ -0,0 +1,338 @@ +#include <config.h> +#include <cassert> +#include "dwarf" + +extern "C" +{ +#include "libdwP.h" +} + +using namespace elfutils; +using namespace std; + + +// dwarf::line_info_table + +const dwarf::line_info_table +dwarf::attr_value::line_info () const +{ + assert (dwarf_whatattr (thisattr ()) == DW_AT_stmt_list); + + CUDIE (cudie, _m_attr.cu); + Dwarf_Lines *lines; + size_t n; + xif (thisattr (), dwarf_getsrclines (&cudie, &lines, &n) < 0); + + return line_info_table (_m_attr.cu->files); +} + +const dwarf::line_table +dwarf::line_info_table::lines () const +{ + return line_table (_m_files->cu->lines); +} + +// dwarf::source_file + +const class dwarf::source_file +dwarf::attr_value::source_file () const +{ + switch (what_space ()) + { + case VS_string: + case VS_source_file: + break; + default: + throw std::runtime_error ("XXX not a file name"); + } + return source_file::source_file (_m_attr); +} + +static bool +stringform (Dwarf_Attribute *attr) +{ + if (attr->valp != NULL) + switch (dwarf_whatform (attr)) + { + case DW_FORM_string: + case DW_FORM_strp: + return true; + } + return false; +} + +/* Mock up a dummy attribute with a special kludge that get_files groks. + We use these for source_file objects consed directly from an index + rather than from a real attribute. */ +static inline const Dwarf_Attribute +dummy_source_file (Dwarf_CU *cu, unsigned int idx) +{ + const Dwarf_Attribute dummy = { idx, DW_FORM_indirect, NULL, cu }; + return dummy; +} + +static bool +get_files (const Dwarf_Attribute *attr, Dwarf_Files **files, Dwarf_Word *idx) +{ + if (attr->valp == NULL) + { + // Dummy hack created by dummy_source_file, above. + assert (attr->form == DW_FORM_indirect); + *files = attr->cu->files; + *idx = attr->code; + return false; + } + + CUDIE (cudie, attr->cu); + return (dwarf_formudata (const_cast<Dwarf_Attribute *> (attr), idx) < 0 + || dwarf_getsrcfiles (&cudie, files, NULL) < 0); +} + +Dwarf_Word +dwarf::source_file::mtime () const +{ + if (stringform (thisattr ())) + return 0; + + Dwarf_Files *files; + Dwarf_Word idx; + xif (thisattr (), get_files (thisattr (), &files, &idx)); + + Dwarf_Word result; + xif (thisattr (), dwarf_filesrc (files, idx, &result, NULL) == NULL); + return result; +} + +Dwarf_Word +dwarf::source_file::size () const +{ + if (stringform (thisattr ())) + return 0; + + Dwarf_Files *files; + Dwarf_Word idx; + xif (thisattr (), get_files (thisattr (), &files, &idx)); + + Dwarf_Word result; + xif (thisattr (), dwarf_filesrc (files, idx, NULL, &result) == NULL); + return result; +} + +const char * +dwarf::source_file::name () const +{ + if (stringform (thisattr ())) + return dwarf_formstring (thisattr ()); + + Dwarf_Files *files; + Dwarf_Word idx; + xif (thisattr (), get_files (thisattr (), &files, &idx)); + + const char *result = dwarf_filesrc (files, idx, NULL, NULL); + xif (thisattr (), result == NULL); + return result; +} + +static inline string +plain_string (const char *filename) +{ + return string ("\"") + filename + "\""; +} + +string +dwarf::source_file::to_string () const +{ + if (stringform (thisattr ())) + return plain_string (dwarf_formstring (thisattr ())); + + Dwarf_Files *files; + Dwarf_Word idx; + xif (thisattr (), get_files (thisattr (), &files, &idx)); + + Dwarf_Word file_mtime; + Dwarf_Word file_size; + const char *result = dwarf_filesrc (files, idx, &file_mtime, &file_size); + xif (thisattr (), result == NULL); + + if (likely (file_mtime == 0) && likely (file_size == 0)) + return plain_string (result); + + std::ostringstream os; + os << "{\"" << result << "," << file_mtime << "," << file_size << "}"; + return os.str (); +} + +// dwarf::directory_table + +size_t +dwarf::directory_table::size () const +{ + return _m_files->ndirs; +} + +static inline dwarf::directory_table::const_iterator +directory_table_array (Dwarf_Files *files) +{ + // See dwarf_getsrcdirs. + return reinterpret_cast<const char *const *> (&files->info[files->nfiles]); +} + +dwarf::directory_table::const_iterator +dwarf::directory_table::begin () const +{ + return directory_table_array (_m_files); +} + +dwarf::directory_table::const_iterator +dwarf::directory_table::end () const +{ + return directory_table_array (_m_files) + _m_files->ndirs; +} + +// dwarf::file_table + +size_t +dwarf::file_table::size () const +{ + return _m_files->nfiles; +} + +const dwarf::source_file +dwarf::file_table::at (size_t idx) const +{ + if (unlikely (idx >= _m_files->nfiles)) + throw std::out_of_range ("XXX fileidx"); + + return dwarf::source_file (dummy_source_file (_m_files->cu, idx)); +} + +dwarf::file_table::const_iterator +dwarf::file_table::find (const source_file &src) const +{ + if (src._m_attr.cu->files == _m_files) + { + // Same table, just cons an iterator using its index. + Dwarf_Files *files; + Dwarf_Word idx; + xif (files->cu, get_files (&src._m_attr, &files, &idx)); + return const_iterator (*this, idx); + } + + // Not from this table, just match on file name. + return find (src.name ()); +} + +// dwarf::line_table + +size_t +dwarf::line_table::size () const +{ + return _m_lines->nlines; +} + +const dwarf::line_entry +dwarf::line_table::at (size_t idx) const +{ + if (unlikely (idx >= _m_lines->nlines)) + throw std::out_of_range ("XXX line table index"); + + return line_entry (reinterpret_cast<Dwarf_Line *> (&_m_lines->info[idx])); +} + +dwarf::line_table::const_iterator +dwarf::line_table::find (Dwarf_Addr address) const +{ + size_t idx = _m_lines->nlines; // end () + if (likely (idx > 0)) + { + CUDIE (cudie, _m_lines->info[0].files->cu); + Dwarf_Line *line = dwarf_getsrc_die (&cudie, address); + if (line != NULL) + idx = line - &_m_lines->info[0]; + } + return const_iterator (*this, idx); +} + +// dwarf::line_entry + +const dwarf::source_file +dwarf::line_entry::file () const +{ + return dwarf::source_file (dummy_source_file (_m_line->files->cu, + _m_line->file)); +} + +#define LINEFIELD(type, method, field) \ + type \ + dwarf::line_entry::method () const \ + { \ + return _m_line->field; \ + } + +LINEFIELD (Dwarf_Addr, address, addr) // XXX dwfl? +LINEFIELD (unsigned int, line, line) +LINEFIELD (unsigned int, column, column) +LINEFIELD (bool, statement, is_stmt) +LINEFIELD (bool, basic_block, basic_block) +LINEFIELD (bool, end_sequence, end_sequence) +LINEFIELD (bool, prologue_end, prologue_end) +LINEFIELD (bool, epilogue_begin, epilogue_begin) + +#undef LINEFIELD + +bool +dwarf::line_entry::operator== (const dwarf::line_entry &other) const +{ + Dwarf_Line *const a = _m_line; + Dwarf_Line *const b = other._m_line; + + if (a == b) + return true; + + if (a->addr != b->addr + || a->line != b->line + || a->column != b->column + || a->is_stmt != b->is_stmt + || a->basic_block != b->basic_block + || a->end_sequence != b->end_sequence + || a->prologue_end != b->prologue_end + || a->epilogue_begin != b->epilogue_begin) + return false; + + // Everything else matches, now have to try the file. + if (a->files == b->files) + // Same table, just compare indices. + return a->file == b->file; + + Dwarf_Word atime; + Dwarf_Word asize; + const char *aname = dwarf_linesrc (a, &atime, &asize); + xif (a->files->cu, aname == NULL); + Dwarf_Word btime; + Dwarf_Word bsize; + const char *bname = dwarf_linesrc (b, &btime, &bsize); + xif (b->files->cu, bname == NULL); + + /* The mtime and size only count when encoded as nonzero. + If either side is zero, we don't consider the field. */ + + if (atime != btime && atime != 0 && btime != 0) + return false; + + if (asize != bsize && asize != 0 && bsize != 0) + return false; + + return !strcmp (aname, bname); +} + +// dwarf::compile_unit convenience functions. + +const dwarf::line_info_table +dwarf::compile_unit::line_info () const +{ + Dwarf_Lines *l; + size_t n; + xif (dwarf_getsrclines (thisdie (), &l, &n) < 0); + + return line_info_table (thisdie ()->cu->files); +} diff --git a/libdw/c++/subr.hh b/libdw/c++/subr.hh new file mode 100644 index 00000000..d8862e53 --- /dev/null +++ b/libdw/c++/subr.hh @@ -0,0 +1,213 @@ +/* Private helper classes for elfutils -*- C++ -*- interfaces. + + */ + +#ifndef _ELFUTILS_SUBR_HH +#define _ELFUTILS_SUBR_HH 1 + +#include <iterator> +#include <functional> +#include <cstring> +#include <iostream> +#include <sstream> + +namespace elfutils +{ + namespace subr + { + template<typename string> + struct name_equal : public std::binary_function<const char *, string, bool> + { + inline bool operator () (const char *me, const string &you) + { + return you == me; + } + }; + + // Explicit specialization. + template<> + struct name_equal<const char *> + : public std::binary_function<const char *, const char *, bool> + { + bool operator () (const char *me, const char *you) + { + return !strcmp (me, you); + } + }; + + static inline std::string hex_string (int code) + { + std::ostringstream os; + os.setf(std::ios::hex, std::ios::basefield); + os << code; + return os.str (); + } + + template<typename prefix_type, const char *lookup_known (int)> + struct known + { + // The names in the table are the identifiers, with prefix. + static inline std::string identifier (int code) + { + const char *known = lookup_known (code); + return known == NULL ? hex_string (code) : std::string (known); + } + + // For the pretty name, skip over the prefix. + static inline std::string name (int code) + { + const char *known = lookup_known (code); + return (known == NULL ? hex_string (code) + : std::string (&known[sizeof (prefix_type) - 1])); + } + }; + + template<typename t1, typename t2> + struct equal_to : public std::binary_function<t1, t2, bool> + { + inline bool operator () (const t1 &a, const t2 &b) + { + return a == b; + } + }; + + template<typename iter1, typename iter2, typename pred_type> + inline bool container_equal (iter1 first1, iter1 last1, + iter2 first2, iter2 last2, + pred_type pred) + { + while (first1 != last1) + if (first2 == last2 || !pred (*first1++, *first2++)) + return false; + return first2 == last2; + } + + template<typename t1, typename t2> + inline bool container_equal (const t1 &a, const t2 &b) + { + typename t1::const_iterator first1 = a.begin (); + typename t1::const_iterator last1 = a.end (); + typename t2::const_iterator first2 = b.begin (); + typename t2::const_iterator last2 = b.end (); + return container_equal (first1, last1, first2, last2, + equal_to<typename t1::value_type, + typename t2::value_type> ()); + } + + template<typename array, typename element = typename array::value_type> + class indexed_iterator + : public std::iterator<std::random_access_iterator_tag, + typename array::value_type, + typename array::difference_type> + { + private: + typedef typename array::size_type index_type; + + array _m_contents; + index_type _m_idx; + + public: + indexed_iterator (array contents, index_type idx) + : _m_contents (contents), _m_idx (idx) {} + indexed_iterator (const indexed_iterator &i) + : _m_contents (i._m_contents), _m_idx (i._m_idx) {} + + inline element operator* () const + { + return _m_contents[_m_idx]; + } + template<typename elt> + inline elt operator* () const + { + return _m_contents[_m_idx]; + } + template<typename elt> + inline elt *operator-> () const + { + return &_m_contents[_m_idx]; + } + template<typename elt> + inline elt operator[] (const index_type &n) const + { + return _m_contents[_m_idx + n]; + } + + inline indexed_iterator operator+ (const indexed_iterator &i) + { + return indexed_iterator (_m_contents, _m_idx + i._m_idx); + } + inline indexed_iterator operator+ (const typename array::difference_type + &i) + { + return indexed_iterator (_m_contents, _m_idx + i); + } + inline typename array::difference_type + operator- (const indexed_iterator &i) + { + return _m_idx - i._m_idx; + } + + inline bool operator== (const indexed_iterator &i) + { + return _m_idx == i._m_idx; + } + inline bool operator!= (const indexed_iterator &i) + { + return _m_idx != i._m_idx; + } + inline bool operator< (const indexed_iterator &i) + { + return _m_idx < i._m_idx; + } + inline bool operator> (const indexed_iterator &i) + { + return _m_idx > i._m_idx; + } + inline bool operator<= (const indexed_iterator &i) + { + return _m_idx <= i._m_idx; + } + inline bool operator>= (const indexed_iterator &i) + { + return _m_idx >= i._m_idx; + } + + inline indexed_iterator &operator= (const indexed_iterator &i) + { + _m_idx = i._m_idx; + return *this; + } + inline indexed_iterator &operator+= (const index_type &n) + { + _m_idx += n; + return *this; + } + inline indexed_iterator &operator-= (const index_type &n) + { + _m_idx -= n; + return *this; + } + + inline indexed_iterator &operator++ () // prefix + { + ++_m_idx; + return *this; + } + inline indexed_iterator operator++ (int) // postfix + { + return indexed_iterator (_m_contents, _m_idx++); + } + inline indexed_iterator &operator-- () // prefix + { + --_m_idx; + return *this; + } + inline indexed_iterator operator-- (int) // postfix + { + return indexed_iterator (_m_contents, _m_idx--); + } + }; + }; +}; + +#endif // <elfutils/subr.hh> diff --git a/libdw/c++/values.cc b/libdw/c++/values.cc new file mode 100644 index 00000000..8b278c49 --- /dev/null +++ b/libdw/c++/values.cc @@ -0,0 +1,547 @@ +/* -*- C++ -*- interfaces for libdw. + Copyright (C) 2009 Red Hat, Inc. + This file is part of Red Hat elfutils. + + Red Hat elfutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by the + Free Software Foundation; version 2 of the License. + + Red Hat elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Red Hat elfutils; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. + + In addition, as a special exception, Red Hat, Inc. gives You the + additional right to link the code of Red Hat elfutils with code licensed + under any Open Source Initiative certified open source license + (http://www.opensource.org/licenses/index.php) which requires the + distribution of source code with any binary distribution and to + distribute linked combinations of the two. Non-GPL Code permitted under + this exception must only link to the code of Red Hat elfutils through + those well defined interfaces identified in the file named EXCEPTION + found in the source code files (the "Approved Interfaces"). The files + of Non-GPL Code may instantiate templates or use macros or inline + functions from the Approved Interfaces without causing the resulting + work to be covered by the GNU General Public License. Only Red Hat, + Inc. may make changes or additions to the list of Approved Interfaces. + Red Hat's grant of this exception is conditioned upon your not adding + any new exceptions. If you wish to add a new Approved Interface or + exception, please contact Red Hat. You must obey the GNU General Public + License in all respects for all of the Red Hat elfutils code and other + code used in conjunction with Red Hat elfutils except the Non-GPL Code + covered by this exception. If you modify this file, you may extend this + exception to your version of the file, but you are not obligated to do + so. If you do not wish to provide this exception without modification, + you must delete this exception statement from your version and license + this file solely under the GPL without exception. + + Red Hat elfutils is an included package of the Open Invention Network. + An included package of the Open Invention Network is a package for which + Open Invention Network licensees cross-license their patents. No patent + license is granted, either expressly or impliedly, by designation as an + included package. Should you wish to participate in the Open Invention + Network licensing program, please visit www.openinventionnetwork.com + <http://www.openinventionnetwork.com>. */ + +#include <config.h> +#include <cassert> +#include "dwarf" + +extern "C" +{ +#include "libdwP.h" +} + +using namespace elfutils; +using namespace std; + +#include "dwarf-knowledge.cc" + + +// dwarf::attr_value disambiguation and dispatch. + +/* For ambiguous the forms, we need to look up the expected + value spaces for this attribute to disambiguate. +*/ +dwarf::value_space +dwarf::attr_value::what_space () const +{ + unsigned int expected = expected_value_space (dwarf_whatattr (thisattr ()), + _m_tag); + unsigned int possible = 0; + switch (dwarf_whatform (thisattr ())) + { + case DW_FORM_flag: + return VS_flag; + + case DW_FORM_addr: + return VS_address; + + case DW_FORM_block: + case DW_FORM_block1: + case DW_FORM_block2: + case DW_FORM_block4: + /* Location expression or target constant. */ + possible = VS(location) | VS(constant); + if ((expected & possible) == possible) + /* When both are expected, a block is a location expression. */ + return VS_location; + break; + + case DW_FORM_data1: + case DW_FORM_data2: + case DW_FORM_data4: + case DW_FORM_data8: + case DW_FORM_udata: + case DW_FORM_sdata: + /* Target constant, known DWARF constant, or *ptr. */ + possible = (VS(dwarf_constant) | VS(constant) + | VS(source_file) | VS(source_line) | VS(source_column) + | VS(location) // loclistptr + | VS(lineptr) | VS(macptr) | VS(rangelistptr)); + if ((expected & possible) == (VS(constant) | VS(location))) + /* When both are expected, a constant is not a loclistptr. */ + return VS_constant; + break; + + case DW_FORM_string: + case DW_FORM_strp: + /* Identifier, file name, or string. */ + possible = VS(identifier) | VS(source_file) | VS(string); + break; + + case DW_FORM_ref_addr: + case DW_FORM_ref1: + case DW_FORM_ref2: + case DW_FORM_ref4: + case DW_FORM_ref8: + case DW_FORM_ref_udata: + possible = VS(unit_reference) | VS(reference); + break; + + default: + throw std::runtime_error ("XXX bad form"); + } + + if (unlikely ((expected & possible) == 0)) + { + if (expected == 0 && possible == (VS(unit_reference) | VS(reference))) + // An unknown reference is a reference, not a unit_reference. + return VS_reference; + + // Otherwise we don't know enough to treat it robustly. + throw std::runtime_error ("XXX ambiguous form in unexpected attribute"); + } + + const int first = ffs (expected & possible) - 1; + if (likely ((expected & possible) == (1U << first))) + return static_cast<value_space> (first); + + throw std::runtime_error ("XXX ambiguous form"); +} + +static string +hex_string (Dwarf_Word value, const char *before = "", const char *after = "") +{ + std::ostringstream os; + os.setf (std::ios::hex, std::ios::basefield); + os.setf (std::ios::showbase); + os << before << value << after; + return os.str (); +} + +static string +dec_string (Dwarf_Word value) +{ + std::ostringstream os; + os << value; + return os.str (); +} + +static string +addr_string (Dwarf_Addr value) +{ + // XXX some hook for symbol resolver?? + return hex_string (value); +} + +static inline string +plain_string (const char *filename) +{ + return string ("\"") + filename + "\""; +} + +string +dwarf::attr_value::to_string () const +{ + switch (what_space ()) + { + case VS_flag: + return flag () ? "1" : "0"; + + case VS_rangelistptr: + return ranges ().to_string (); + + case VS_lineptr: // XXX punt for now, treat as constant + case VS_macptr: // XXX punt for now, treat as constant + case VS_constant: + return hex_string (constant ()); + + case VS_dwarf_constant: + return dwarf_constant ().to_string (); + + case VS_source_line: + case VS_source_column: + return dec_string (constant ()); + + case VS_identifier: + return plain_string (identifier ()); + + case VS_string: + return plain_string (string ()); + + case VS_address: + return addr_string (address ()); + + case VS_reference: + case VS_unit_reference: + return hex_string (reference ()->offset (), "[", "]"); + + case VS_source_file: + return source_file ().to_string (); + + case VS_location: + return location ().to_string (); + + case VS_discr_list: + break; // XXX DW_AT_discr_list unimplemented + } + + throw std::runtime_error ("XXX unsupported value space"); +} + +// A few cases are trivial. +#define SIMPLE(type, name, form) \ + type \ + dwarf::attr_value::name () const \ + { \ + type result; \ + xif (thisattr (), dwarf_form##form (thisattr (), &result) < 0); \ + return result; \ + } + +SIMPLE (bool, flag, flag) + +// XXX check value_space is really constantish?? vs *ptr +SIMPLE (Dwarf_Word, constant, udata) +SIMPLE (Dwarf_Sword, signed_constant, sdata) + +SIMPLE (Dwarf_Addr, address, addr) + +const char * +dwarf::attr_value::string () const +{ + const char *result = dwarf_formstring (thisattr ()); + xif (thisattr(), result == NULL); + return result; +} + +bool +dwarf::attr_value::constant_is_integer () const +{ + switch (dwarf_whatform (thisattr ())) + { + case DW_FORM_block: + case DW_FORM_block1: + case DW_FORM_block2: + case DW_FORM_block4: + return false; + + case DW_FORM_data1: + case DW_FORM_data2: + case DW_FORM_data4: + case DW_FORM_data8: + case DW_FORM_udata: + case DW_FORM_sdata: + return true; + + default: + throw std::runtime_error ("XXX wrong form"); + } +} + + +const_vector<uint8_t> +dwarf::attr_value::constant_block () const +{ + Dwarf_Block block; + + switch (dwarf_whatform (thisattr ())) + { + case DW_FORM_block: + case DW_FORM_block1: + case DW_FORM_block2: + case DW_FORM_block4: + xif (thisattr(), dwarf_formblock (thisattr (), &block) < 0); + break; + + case DW_FORM_data1: + block.length = 1; + block.data = thisattr ()->valp; + break; + + case DW_FORM_data2: + block.length = 2; + block.data = thisattr ()->valp; + break; + + case DW_FORM_data4: + block.length = 4; + block.data = thisattr ()->valp; + break; + + case DW_FORM_data8: + block.length = 8; + block.data = thisattr ()->valp; + break; + + case DW_FORM_udata: + case DW_FORM_sdata: + // XXX ? + if ((*(const uint8_t *) thisattr ()->valp & 0x80) == 0) + { + block.length = 1; + block.data = thisattr ()->valp; + break; + } + + default: + throw std::runtime_error ("XXX wrong form"); + } + + return const_vector<uint8_t> (block); +} + +// dwarf::range_list + +dwarf::range_list::const_iterator::const_iterator (Dwarf_Attribute *attr, + ptrdiff_t offset) + : _m_base (-1), _m_begin (0), _m_end (0), _m_cu (attr->cu), _m_offset (offset) +{ +} + +static bool +range_list_advance (int secndx, + Dwarf_CU *cu, + Dwarf_Addr &base, + Dwarf_Addr &begin, + Dwarf_Addr &end, + ptrdiff_t &offset, + unsigned char **valp) +{ + const Elf_Data *d = cu->dbg->sectiondata[secndx]; + if (unlikely (d == NULL)) + throw std::runtime_error ("XXX no ranges"); + + if (unlikely (offset < 0) || unlikely ((size_t) offset >= d->d_size)) + throw std::runtime_error ("XXX bad offset in ranges iterator"); + + unsigned char *readp = reinterpret_cast<unsigned char *> (d->d_buf) + offset; + unsigned char *const readendp + = reinterpret_cast<unsigned char *> (d->d_buf) + d->d_size; + + while (true) + { + if (readendp - readp < cu->address_size * 2) + throw std::runtime_error ("XXX bad ranges"); + + if (cu->address_size == 8) + { + begin = read_8ubyte_unaligned_inc (cu->dbg, readp); + end = read_8ubyte_unaligned_inc (cu->dbg, readp); + if (begin == (uint64_t) -1l) /* Base address entry. */ + { + base = end; + continue; + } + } + else + { + begin = read_4ubyte_unaligned_inc (cu->dbg, readp); + end = read_4ubyte_unaligned_inc (cu->dbg, readp); + if (begin == (uint32_t) -1) /* Base address entry. */ + { + base = end; + continue; + } + } + + break; + } + + if (begin == 0 && end == 0) /* End of list entry. */ + offset = 1; + else + { + if (valp) + *valp = readp; + offset = readp - reinterpret_cast<unsigned char *> (d->d_buf); + + if (base == (Dwarf_Addr) -1) + { + CUDIE (cudie, cu); + + /* Find the base address of the compilation unit. It will + normally be specified by DW_AT_low_pc. In DWARF-3 draft 4, + the base address could be overridden by DW_AT_entry_pc. It's + been removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc + for compilation units with discontinuous ranges. */ + Dwarf_Attribute attr_mem; + if (unlikely (dwarf_lowpc (&cudie, &base) != 0) + && dwarf_formaddr (dwarf_attr (&cudie, + DW_AT_entry_pc, + &attr_mem), + &base) != 0) + { + return true; // XXX + } + } + } + + return false; +} + +dwarf::range_list::const_iterator & +dwarf::range_list::const_iterator::operator++ () +{ + xif (_m_cu, range_list_advance (IDX_debug_ranges, _m_cu, _m_base, + _m_begin, _m_end, _m_offset, NULL)); + return *this; +} + + +template<typename container> +string +__libdw_ranges_to_string (const container &c) +{ + std::ostringstream os; + os.setf (std::ios::hex, std::ios::basefield); + os.setf (std::ios::showbase); + + os << "<"; + + bool first = true; + for (typename container::const_iterator i = c.begin (); i != c.end (); ++i) + { + const typename container::value_type range = *i; + if (!first) + os << ","; + os << range.first << "-" << range.second; + first = false; + } + + os << ">"; + + return os.str (); +} + +string +dwarf::range_list::to_string () const +{ + return __libdw_ranges_to_string (*this); +} + +string +dwarf::ranges::to_string () const +{ + return __libdw_ranges_to_string (*this); +} + +string +dwarf::arange_list::to_string () const +{ + return __libdw_ranges_to_string (*this); +} + +dwarf::aranges_map +dwarf::aranges () const +{ + Dwarf_Aranges *these; + xif (dwarf_getaranges (_m_dw, &these, NULL) < 0); + + if (these == NULL) + return aranges_map (); + + aranges_map result; + for (const Dwarf_Aranges_s::Dwarf_Arange_s *r = &these->info[0]; + r < &these->info[these->naranges]; + ++r) + result[compile_unit (debug_info_entry (_m_dw, r->offset))].insert + (arange_list::value_type (r->addr, r->addr + r->length)); + + return result; +} + +// dwarf::location_attr + +const dwarf::location_attr +dwarf::attr_value::location () const +{ + if (what_space () != VS_location) + throw std::runtime_error ("XXX not a location"); + + return location_attr (*this); +} + +bool +dwarf::location_attr::is_list () const +{ + switch (dwarf_whatform (_m_attr.thisattr ())) + { + case DW_FORM_block: + case DW_FORM_block1: + case DW_FORM_block2: + case DW_FORM_block4: + return false; + } + + return true; +} + +dwarf::location_attr::const_iterator & +dwarf::location_attr::const_iterator::operator++ () +{ + if (unlikely (_m_offset == 1)) + throw std::runtime_error ("incrementing end iterator"); + else if (_m_offset == 0) + // Singleton, now at end. + _m_offset = 1; + else + { + // Advance to next list entry. + xif (_m_attr._m_attr.thisattr (), + range_list_advance (IDX_debug_loc, _m_attr._m_attr._m_attr.cu, + _m_base, _m_begin, _m_end, _m_offset, + &_m_attr._m_attr._m_attr.valp)); + if (_m_offset > 1) + { + _m_attr._m_attr._m_attr.form = DW_FORM_block2; + _m_offset += read_2ubyte_unaligned (_m_attr._m_attr._m_attr.cu->dbg, + _m_attr._m_attr._m_attr.valp); + } + } + + return *this; +} + +string +dwarf::location_attr::to_string () const +{ + if (is_list ()) + return hex_string (_m_attr.constant (), "#"); + return "XXX-expr"; +} diff --git a/libdw/libdwP.h b/libdw/libdwP.h index 1d5a9b27..c7d296a1 100644 --- a/libdw/libdwP.h +++ b/libdw/libdwP.h @@ -300,14 +300,21 @@ struct Dwarf_CU #define DIE_OFFSET_FROM_CU_OFFSET(cu_offset, offset_size) \ ((cu_offset) + 3 * (offset_size) - 4 + 3) -#define CUDIE(fromcu) \ - ((Dwarf_Die) \ - { \ - .cu = (fromcu), \ - .addr = ((char *) (fromcu)->dbg->sectiondata[IDX_debug_info]->d_buf \ - + (fromcu)->start + 3 * (fromcu)->offset_size - 4 + 3), \ +#define CUDIE_ADDR(fromcu) \ + ((char *) (fromcu)->dbg->sectiondata[IDX_debug_info]->d_buf \ + + DIE_OFFSET_FROM_CU_OFFSET ((fromcu)->start, (fromcu)->offset_size)) + +#ifdef __cplusplus +# define CUDIE(name, fromcu) \ + Dwarf_Die name = { CUDIE_ADDR (fromcu), (fromcu), NULL, 0l } +#else +# define CUDIE(fromcu) \ + ((Dwarf_Die) \ + { \ + .cu = (fromcu), \ + .addr = CUDIE_ADDR (fromcu), \ }) - +#endif /* Macro information. */ struct Dwarf_Macro_s diff --git a/src/ChangeLog b/src/ChangeLog index f6fd86f3..85326e09 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,171 @@ +2009-04-02 Roland McGrath <roland@redhat.com> + + * Makefile.am (noinst_HEADERS): Add dwarfstrings.h here. + (dwarflint_SOURCES): Add dwarflint-expected.hh and dwarflint.h here. + (noinst_HEADERS): Add expr_opcodes.h too. + +2009-03-24 Roland McGrath <roland@redhat.com> + + * Makefile.am (libdwplusplus_SOURCES): Variable removed. + (dwarfcmp_SOURCES, dwarflint_SOURCES): Don't use it. + (libdwpp): New variable. + (dwarfcmp_LDADD, dwarflint_LDADD): Use it. + +2009-03-05 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Add --nohl command line switch. + +2009-01-28 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Coding style cleanups. + (found_hole): Tolerate zero-padding to given alignment. + +2009-01-27 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Implement validation of .debug_ranges and + references from .debug_info to .debug_ranges. + +2009-01-27 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Check that the base address selection entry + actually changes base address. + +2009-01-27 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Match the way dwarflint reports offsets with the + way elfutils does it. + +2009-01-26 Petr Machata <pmachata@redhat.com> + + * dwarflint.c (check_aranges_structural): Check that each CU is + referenced from one aranges section only. + (check_pub_structural): Likewise for pubnames and pubtypes. + (read_ctx_read_uleb128): Allow ten-byte ULEB128. + +2009-01-23 Petr Machata <pmachata@redhat.com> + + * dwarflint.c (check_debug_info_structural): Check that all CUs + have the same address size. + +2009-01-17 Roland McGrath <roland@redhat.com> + + * expr_opcodes.h: Fix DW_OP_deref entry. + Add DW_OP_GNU_push_tls_address, DW_OP_GNU_uninit. + +2009-01-17 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Better location handling: references now carry + along where did they originate (i.e. which section, offset, etc.), + all messages are location-aware. + (struct where): New structure that captures location inside the + Dwarf file. Can be chained to achieve chains of "caused by this + reference" messages ala GCC. + +2009-01-15 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Implement validation of .debug_loc and references + from .debug_info to .debug_loc. + +2009-01-14 Petr Machata <pmachata@redhat.com> + + * dwarfstrings.h (dwarf_locexpr_opcode_string): New. + +2009-01-14 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Validation .debug_pubtypes. + +2009-01-13 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Adjust check_pubnames_structural to be able to + validate also .debug_pubtypes. + +2009-01-13 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Rewrite message macros to functions. This makes it + possible to rewrite several other macros to functions. + +2009-01-12 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Implement validation of .debug_pubnames. + +2009-01-12 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Check padding between sections in .debug_aranges. + +2009-01-12 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: For DIE references, remember both referrer and + referree. Add a new data structure ref_record to support that. + Add new message category, mc_die_rel_ref. Rename other DIE + relationship categories to mc_die_rel_* pattern. + +2009-01-12 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Changes in data structures for recording DIEs and CUs. + +2009-01-12 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Implement validation of .debug_aranges. + +2009-01-11 Petr Machata <pmachata@redhat.com> + + * dwarflint.c (process_file): Handle absence of .debug_info, + .debug_abbrev and .debug_str gracefully. + +2009-01-11 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: A couple small fixes across the code. + (check_debug_info_structural): Return bool. + (check_cu_structural): Likewise. + (check_addr_record_addr): Likewise. + (check_die_references): Likewise. + +2009-01-10 Roland McGrath <roland@redhat.com> + + * dwarfcmp.cc (test_writer): New variable. + (options, parse_opt): Grok -T/--test-writer to set it. + (main): When set, exercise dwarf_output constructors and comparators. + + * dwarflint.c (options, parse_opt): Replace --no-debug with + -i/--ignore-missing, to match dwarfcmp. + +2009-01-10 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Implement --no-debug: silently accept file, if its + debug info is not available. + +2009-01-10 Roland McGrath <roland@redhat.com> + + * dwarfcmp.cc: New file. + * Makefile.am (bin_PROGRAMS): Add dwarfcmp. + (dwarfcmp_SOURCES): New variable. + (dwarfcmp_no_Wformat, dwarfcmp_LDADD): New variables. + +2009-01-10 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Implement fine-grained message selection. Each + message has a category, which is bitwise OR of category options. + There are acceptance and rejection criteria for warnings and + errors, which can be tuned using command-line options (currently + --strict and --gnu). + Use dwarfstrings.h in two messages. + +2009-01-10 Petr Machata <pmachata@redhat.com> + + * readelf.c: Extract functions that format dwarf enums into a file + of its own, so it can be shared with dwarflint. + * dwarfstrings.h: That file. + +2009-01-09 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Sort the abbrev tables and look up abbreviations + with bisect search. + +2009-01-09 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Checking for zero padding and unreferenced bytes. + CU size and padding at the end of CU are now checked. + 2009-04-29 Ulrich Drepper <drepper@redhat.com> * elflint.c (check_symtab): Add tests of st_other field. @@ -174,6 +342,19 @@ * ld.c (ld_new_searchdir): Fix adding to search path list. +2009-01-07 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Check that the sibling doesn't point to the + terminating DIE (the one with the code of 0). + +2009-01-07 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: More checking DW_AT_sibling correctness. + +2009-01-06 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Checking DW_AT_sibling correctness. + 2009-01-06 Ulrich Drepper <drepper@redhat.com> * readelf.c: Implement call frame debug section dumping. @@ -200,6 +381,21 @@ * strip.c: Likewise. * unstrip.c: Likewise. +2009-01-01 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: Tweaks across the code: + - Memory allocation now done with xrealloc, xcalloc functions. As + a consequence, backoff for out of memory conditions was turned off. + - Coding style was improved on many places. + - Abbrev tag and name validation changed, everything up to the + hi_user is now accepted. + - Ambiguous references to NULL DIEs and Abbrevs were changed to + explicitly mention DIE with zero code and similar. + +2008-12-15 Petr Machata <pmachata@redhat.com> + + * dwarflint.c: New file. + 2008-12-11 Roland McGrath <roland@redhat.com> * nm.c (sym_name): New function. diff --git a/src/Makefile.am b/src/Makefile.am index c644a062..ab25a39b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -40,6 +40,9 @@ INCLUDES = -I$(srcdir) -I$(srcdir)/../libelf -I$(srcdir)/../libebl \ -I$(srcdir)/../libdw -I$(srcdir)/../libdwfl \ -I$(srcdir)/../libasm -I$(srcdir)/../lib -I.. +AM_CXXFLAGS = $(AM_CFLAGS:gnu99=gnu++0x) \ + -Wno-unused-parameter + AM_LDFLAGS = -Wl,-rpath-link,../libelf:../libdw YACC = @YACC@ -d @@ -52,8 +55,8 @@ native_ld = @native_ld@ base_cpu = @base_cpu@ bin_PROGRAMS = readelf nm size strip ld elflint findtextrel addr2line \ - elfcmp objdump ranlib strings ar unstrip - + elfcmp objdump ranlib strings ar unstrip \ + dwarflint dwarfcmp ld_dsos = libld_elf_i386_pic.a if NATIVE_LD @@ -78,8 +81,17 @@ ld_SOURCES = ld.c ldgeneric.c ldlex.l ldscript.y symbolhash.c sectionhash.c \ libar_a_SOURCES = arlib.c arlib2.c +dwarfcmp_SOURCES = dwarfcmp.cc + +dwarflint_SOURCES = dwarflint.c dwarflint.h dwarflint-hl.cc \ + dwarflint-expected-at.cc dwarflint-expected.hh \ + dwarfstrings.c dwarflint-coverage.c dwarflint-coverage.h + +readelf_SOURCES = readelf.c dwarfstrings.c + noinst_HEADERS = ld.h symbolhash.h sectionhash.h versionhash.h \ - ldscript.h xelf.h unaligned.h + ldscript.h xelf.h unaligned.h dwarfstrings.h \ + expr_opcodes.h EXTRA_DIST = elf32-i386.script libld_elf_i386.map $(ld_modules) arlib.h \ debugpred.h @@ -105,12 +117,16 @@ endif libebl = ../libebl/libebl.a libeu = ../lib/libeu.a +# XXX later the C++ stuff will be in libdw.so directly +libdwpp = ../libdw/libdwpp.a $(libdw) + nm_no_Wformat = yes size_no_Wformat = yes strings_no_Wformat = yes addr2line_no_Wformat = yes # XXX While the file is not finished, don't warn about this ldgeneric_no_Wunused = yes +dwarfcmp_no_Wformat = yes readelf_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl nm_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl @@ -131,6 +147,8 @@ ranlib_LDADD = libar.a $(libelf) $(libeu) $(libmudflap) strings_LDADD = $(libelf) $(libeu) $(libmudflap) ar_LDADD = libar.a $(libelf) $(libeu) $(libmudflap) unstrip_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(libmudflap) -ldl +dwarfcmp_LDADD = $(libdwpp) $(libmudflap) -ldl +dwarflint_LDADD = $(libebl) $(libelf) $(libdwpp) $(libeu) $(libmudflap) -ldl ldlex.o: ldscript.c ldlex_no_Werror = yes diff --git a/src/dwarfcmp.cc b/src/dwarfcmp.cc new file mode 100644 index 00000000..522b719e --- /dev/null +++ b/src/dwarfcmp.cc @@ -0,0 +1,395 @@ +/* Compare semantic content of two DWARF files. + Copyright (C) 2009 Red Hat, Inc. + This file is part of Red Hat elfutils. + + Red Hat elfutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by the + Free Software Foundation; version 2 of the License. + + Red Hat elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Red Hat elfutils; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. + + Red Hat elfutils is an included package of the Open Invention Network. + An included package of the Open Invention Network is a package for which + Open Invention Network licensees cross-license their patents. No patent + license is granted, either expressly or impliedly, by designation as an + included package. Should you wish to participate in the Open Invention + Network licensing program, please visit www.openinventionnetwork.com + <http://www.openinventionnetwork.com>. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <argp.h> +#include <assert.h> +#include <errno.h> +#include <error.h> +#include <fcntl.h> +#include <locale.h> +#include <libintl.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <cstdint> +#include "../libdw/libdwP.h" // XXX + +#include "c++/dwarf" +#include "c++/dwarf_edit" + +using namespace elfutils; +using namespace std; + + +/* Name and version of program. */ +static void print_version (FILE *stream, struct argp_state *state); +void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version; + +/* Bug report address. */ +const char *argp_program_bug_address = PACKAGE_BUGREPORT; + +/* Values for the parameters which have no short form. */ +#define OPT_XXX 0x100 + +/* Definitions of arguments for argp functions. */ +static const struct argp_option options[] = +{ + { NULL, 0, NULL, 0, N_("Control options:"), 0 }, + { "quiet", 'q', NULL, 0, N_("Output nothing; yield exit status only"), 0 }, + { "ignore-missing", 'i', NULL, 0, + N_("Don't complain if both files have no DWARF at all"), 0 }, + + { "test-writer", 'T', NULL, 0, N_("Test DWARF output classes"), 0 }, + + { NULL, 0, NULL, 0, N_("Miscellaneous:"), 0 }, + { NULL, 0, NULL, 0, NULL, 0 } +}; + +/* Short description of program. */ +static const char doc[] = N_("\ +Compare two DWARF files for semantic equality."); + +/* Strings for arguments in help texts. */ +static const char args_doc[] = N_("FILE1 FILE2"); + +/* Prototype for option handler. */ +static error_t parse_opt (int key, char *arg, struct argp_state *state); + +/* Data structure to communicate with argp functions. */ +static struct argp argp = +{ + options, parse_opt, args_doc, doc, NULL, NULL, NULL +}; + +/* Nonzero if only exit status is wanted. */ +static bool quiet; + +/* Nonzero if missing DWARF is equal DWARF. */ +static bool missing_ok; + +/* Nonzero to test writer classes. */ +static bool test_writer; + + +static Dwarf * +open_file (const char *fname, int *fdp) +{ + int fd = open (fname, O_RDONLY); + if (unlikely (fd == -1)) + error (2, errno, gettext ("cannot open '%s'"), fname); + Dwarf *dw = dwarf_begin (fd, DWARF_C_READ); + *fdp = fd; + if (dw == NULL) + { + int code = dwarf_errno (); + if (code != DWARF_E_NO_DWARF || !missing_ok) + error (2, 0, + gettext ("cannot create DWARF descriptor for '%s': %s"), + fname, dwarf_errmsg (code)); + } + return dw; +} + + +// XXX make translation-friendly +struct context +{ + const dwarf::debug_info_entry *a_; + const dwarf::debug_info_entry *b_; + const char *container_; + + context (const dwarf::debug_info_entry &a, const dwarf::debug_info_entry &b) + : a_ (&a), b_ (&b), container_ (NULL) {} + context () : a_ (NULL), b_ (NULL), container_ ("compilation units") {} + + ostream &location () const + { + if (a_ == NULL) + cout << "files differ: "; + else + cout << hex << a_->offset () << " vs " << b_->offset () << ": "; + return cout; + } + + void container (const char *msg) const + { + location () << msg << " " << container_ << endl; + } + + void missing () const + { + container ("missing"); + } + + void extra () const + { + container ("extra"); + } + + void tag () const + { + location () << "different tag" << endl; + } + + void attributes () const + { + location () << "different attributes" << endl; + } + + void values (const string &a, const string &b) const + { + location () << "attribute " << a << " vs " << b << endl; + } +}; + +template<typename container1, typename container2> +static int +describe_mismatch (const container1 &a, const container2 &b, const context &say) +{ + typename container1::const_iterator i = a.begin (); + typename container2::const_iterator j = b.begin (); + int result = 0; + while (i != a.end ()) + { + if (j == b.end ()) + { + say.missing (); // b lacks some of a. + result = 1; + break; + } + result = describe_mismatch (*i, *j, say); + assert ((result != 0) == (*i != *j)); + if (result != 0) + break; + ++i; + ++j; + } + if (result == 0 && j != b.end ()) + { + say.extra (); // a lacks some of b. + result = 1; + } + return result; +} + +template<> +int +describe_mismatch (const dwarf::debug_info_entry &a, + const dwarf::debug_info_entry &b, + const context &ctx) +{ + context here (a, b); + + int result = a.tag () != b.tag (); + if (result != 0) + here.tag (); + + if (result == 0) + { + here.container_ = "attributes"; + result = describe_mismatch (a.attributes (), b.attributes (), here); + assert ((result != 0) == (a.attributes () != b.attributes ())); + } + if (result == 0) + { + here.container_ = "children"; + result = describe_mismatch (a.children (), b.children (), here); + assert ((result != 0) == (a.children () != b.children ())); + } + return result; +} + +template<> +int +describe_mismatch (const dwarf::compile_unit &a, const dwarf::compile_unit &b, + const context &ctx) +{ + return describe_mismatch (static_cast<const dwarf::debug_info_entry &> (a), + static_cast<const dwarf::debug_info_entry &> (b), + ctx); +} + +template<> +int +describe_mismatch (const dwarf::attribute &a, const dwarf::attribute &b, + const context &say) +{ + int result = a.first != b.first; + if (result != 0) + say.attributes (); + else + { + result = a.second != b.second; + if (result != 0) + say.values (a.to_string (), b.to_string ()); + } + return result; +} + +int +main (int argc, char *argv[]) +{ + /* Set locale. */ + (void) setlocale (LC_ALL, ""); + + /* Make sure the message catalog can be found. */ + (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR); + + /* Initialize the message catalog. */ + (void) textdomain (PACKAGE_TARNAME); + + /* Parse and process arguments. */ + int remaining; + (void) argp_parse (&argp, argc, argv, 0, &remaining, NULL); + + /* We expect exactly two non-option parameters. */ + if (unlikely (remaining + 2 != argc)) + { + fputs (gettext ("Invalid number of parameters.\n"), stderr); + argp_help (&argp, stderr, ARGP_HELP_SEE, program_invocation_short_name); + exit (1); + } + const char *const fname1 = argv[remaining]; + int fd1; + Dwarf *dw1 = open_file (fname1, &fd1); + + const char *const fname2 = argv[remaining + 1]; + int fd2; + Dwarf *dw2 = open_file (fname2, &fd2); + + int result = 0; + + if (dw1 == NULL || dw2 == NULL) + { + result = (dw1 == NULL) != (dw2 == NULL); + if (result != 0 && !quiet) + { + if (dw1 == NULL) + cout << "unexpectedly has DWARF"; + else + cout << "has no DWARF"; + } + } + else + { + dwarf file1 (dw1); + dwarf file2 (dw2); + + if (quiet) + result = !(file1 == file2); + else + result = describe_mismatch (file1.compile_units (), + file2.compile_units (), + context ()); + + if (test_writer) + { + dwarf_edit out1 (file1); + dwarf_edit out2 (file2); + +# define compare_self(x, y) \ + assert (x == y); \ + assert (!(x != y)) +# define compare_other(x, y) \ + assert (!(x == y) == result); \ + assert (!(x != y) == !result) + + // Compare self, same type. + compare_self (out1, out1); + compare_self (out2, out2); + + // Compare self, output == input. + compare_self (out1, file1); + compare_self (out2, file2); + + // Compare self, input == output. + compare_self (file1, out1); + compare_self (file2, out2); + + // Compare files, output == output. + compare_other (out1, out2); + compare_other (out2, out1); + + // Compare files, output vs input. + compare_other (out1, file2); + compare_other (out2, file1); + + // Compare files, input vs output. + compare_other (file2, out1); + compare_other (file1, out2); + +#undef compare_self +#undef compare_other + } + } + + return result; +} + + +/* Print the version information. */ +static void +print_version (FILE *stream, struct argp_state *state __attribute__ ((unused))) +{ + fprintf (stream, "dwarfcmp (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION); + fprintf (stream, gettext ("\ +Copyright (C) %s Red Hat, Inc.\n\ +This is free software; see the source for copying conditions. There is NO\n\ +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ +"), "2009"); +} + + +/* Handle program arguments. */ +static error_t +parse_opt (int key, char *arg, + struct argp_state *state __attribute__ ((unused))) +{ + switch (key) + { + case 'q': + quiet = true; + break; + + case 'i': + missing_ok = true; + break; + + case 'T': + test_writer = true; + break; + + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} diff --git a/src/dwarflint-coverage.c b/src/dwarflint-coverage.c new file mode 100644 index 00000000..a0d058c0 --- /dev/null +++ b/src/dwarflint-coverage.c @@ -0,0 +1,165 @@ +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "dwarflint-coverage.h" +#include <stdbool.h> +#include <assert.h> +#include <system.h> +#include <string.h> +#include <inttypes.h> + +static struct cov_range * +coverage_find (struct coverage *cov, uint64_t start) +{ + assert (cov->size > 0); + + size_t a = 0; + size_t b = cov->size; + + while (a < b) + { + size_t i = (a + b) / 2; + struct cov_range *r = cov->ranges + i; + + if (r->start > start) + b = i; + else if (r->start < start) + a = i + 1; + else + return r; + } + + return cov->ranges + a; +} + +void +coverage_add (struct coverage *cov, uint64_t start, uint64_t length) +{ + struct cov_range nr = (struct cov_range){start, length}; + if (cov->size == 0) + { + REALLOC (cov, ranges); + cov->ranges[cov->size++] = nr; + return; + } + + struct cov_range *r = coverage_find (cov, start); + + struct cov_range *insert = &nr; + struct cov_range *coalesce = &nr; + struct cov_range *end = cov->ranges + cov->size; + + // Coalesce with previous range? + struct cov_range *p = r - 1; + if (p >= cov->ranges && coalesce->start <= p->start + p->length) + { + uint64_t coalesce_end = coalesce->start + coalesce->length; + if (coalesce_end > p->start + p->length) + { + p->length = coalesce_end - p->start; + coalesce = p; + } + else + coalesce = NULL; + insert = NULL; + } + + // Coalesce with one or more following ranges? + if (coalesce != NULL && coalesce != end) + { + p = r; + while (p != end && coalesce->start + coalesce->length >= p->start) + { + uint64_t p_end = p->start + p->length; + if (p_end > coalesce->start + coalesce->length) + coalesce->length = p_end - coalesce->start; + if (insert != NULL) + { + *p = *insert; + insert = NULL; + coalesce = p; + assert (p == r); + ++r; // when doing memory moves, don't overwrite this range + } + ++p; + } + if (p > r) + { + size_t rem = cov->size - (p - cov->ranges); + memmove (r, p, sizeof (*cov->ranges) * rem); + cov->size -= p - r; + } + } + + if (insert != NULL) + { + size_t rem = end - r; + size_t idx = r - cov->ranges; + REALLOC (cov, ranges); + r = cov->ranges + idx; + + cov->size++; + if (rem > 0) + memmove (r + 1, r, sizeof (*cov->ranges) * rem); + *r = nr; + } +} + +bool +coverage_is_covered (struct coverage *cov, uint64_t address) +{ + if (cov->size == 0) + return false; + + struct cov_range *r = coverage_find (cov, address); + if (r < cov->ranges + cov->size) + if (address >= r->start && address < r->start + r->length) + return true; + if (r > cov->ranges) + { + --r; + if (address >= r->start && address < r->start + r->length) + return true; + } + return false; +} + +bool +coverage_find_holes (struct coverage *cov, uint64_t start, uint64_t length, + bool (*hole)(uint64_t start, uint64_t length, void *data), + void *data) +{ + if (cov->size == 0) + return hole (start, length, data); + + uint64_t end (size_t i) + { + return cov->ranges[i].start + cov->ranges[i].length; + } + + if (start < cov->ranges[0].start) + if (!hole (start, cov->ranges[0].start - start, data)) + return false; + + for (size_t i = 0; i < cov->size - 1; ++i) + { + uint64_t end_i = end (i); + if (!hole (end_i, cov->ranges[i+1].start - end_i, data)) + return false; + } + + if (start + length > end (cov->size - 1)) + { + uint64_t end_last = end (cov->size - 1); + return hole (end_last, start + length - end_last, data); + } + + return true; +} + +void +coverage_free (struct coverage *cov) +{ + free (cov->ranges); +} diff --git a/src/dwarflint-coverage.h b/src/dwarflint-coverage.h new file mode 100644 index 00000000..ac80dbf6 --- /dev/null +++ b/src/dwarflint-coverage.h @@ -0,0 +1,43 @@ +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> + +#define REALLOC(A, BUF) \ + do { \ + typeof ((A)) _a = (A); \ + if (_a->size == _a->alloc) \ + { \ + if (_a->alloc == 0) \ + _a->alloc = 8; \ + else \ + _a->alloc *= 2; \ + _a->BUF = xrealloc (_a->BUF, \ + sizeof (*_a->BUF) * _a->alloc); \ + } \ + } while (0) + +#define WIPE(OBJ) memset (&OBJ, 0, sizeof (OBJ)) + +/* Functions and data structures for handling of address range + coverage. We use that to find holes of unused bytes in DWARF + string table. */ + +struct cov_range +{ + uint64_t start; + uint64_t length; +}; + +struct coverage +{ + struct cov_range *ranges; + size_t size; + size_t alloc; +}; + +void coverage_add (struct coverage *ar, uint64_t begin, uint64_t end); +bool coverage_is_covered (struct coverage *ar, uint64_t address); +bool coverage_find_holes (struct coverage *cov, uint64_t start, uint64_t length, + bool (*cb)(uint64_t start, uint64_t length, void *data), + void *data); +void coverage_free (struct coverage *ar); diff --git a/src/dwarflint-expected-at.cc b/src/dwarflint-expected-at.cc new file mode 100644 index 00000000..ed47121b --- /dev/null +++ b/src/dwarflint-expected-at.cc @@ -0,0 +1,729 @@ +#include "dwarflint-expected.hh" +#include "../libdw/dwarf.h" + +expected_at_map::expected_at_map () +{ + std::set <int> at_set_decl; + at_set_decl.insert (DW_AT_decl_column); + at_set_decl.insert (DW_AT_decl_file); + at_set_decl.insert (DW_AT_decl_line); + + m_map [DW_TAG_access_declaration] + .optional (at_set_decl) + .optional (DW_AT_accessibility) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + ; + + m_map[DW_TAG_array_type] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_bit_stride) + .optional (DW_AT_byte_size) + .optional (DW_AT_data_location) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_ordering) + .optional (DW_AT_sibling) + .optional (DW_AT_specification) + .optional (DW_AT_start_scope) + .optional (DW_AT_type) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_base_type] + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_binary_scale) + .optional (DW_AT_bit_offset) + .optional (DW_AT_bit_size) + .optional (DW_AT_byte_size) + .optional (DW_AT_data_location) + .optional (DW_AT_decimal_scale) + .optional (DW_AT_decimal_sign) + .optional (DW_AT_description) + .optional (DW_AT_digit_count) + .optional (DW_AT_encoding) + .optional (DW_AT_endianity) + .optional (DW_AT_name) + .optional (DW_AT_picture_string) + .optional (DW_AT_sibling) + .optional (DW_AT_small) + ; + + m_map [DW_TAG_catch_block] + .optional (DW_AT_abstract_origin) + .optional (DW_AT_high_pc) + .optional (DW_AT_low_pc) + .optional (DW_AT_ranges) + .optional (DW_AT_segment) + .optional (DW_AT_sibling) + ; + + m_map [DW_TAG_class_type] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_byte_size) + .optional (DW_AT_data_location) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_specification) + .optional (DW_AT_start_scope) + .optional (DW_AT_visibility) + .optional (DW_AT_containing_type) // XXX added to reflect reality + ; + + m_map [DW_TAG_common_block] + .optional (at_set_decl) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_location) + .optional (DW_AT_name) + .optional (DW_AT_segment) + .optional (DW_AT_sibling) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_common_inclusion] + .optional (at_set_decl) + .optional (DW_AT_common_reference) + .optional (DW_AT_declaration) + .optional (DW_AT_sibling) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_compile_unit] + .optional (DW_AT_base_types) + .optional (DW_AT_comp_dir) + .optional (DW_AT_identifier_case) + .optional (DW_AT_high_pc) + .optional (DW_AT_language) + .optional (DW_AT_low_pc) + .optional (DW_AT_macro_info) + .optional (DW_AT_name) + .optional (DW_AT_producer) + .optional (DW_AT_ranges) + .optional (DW_AT_segment) + .optional (DW_AT_stmt_list) + .optional (DW_AT_use_UTF8) + .optional (DW_AT_entry_pc) // XXX added to reflect reality + ; + + m_map [DW_TAG_condition] + .optional (at_set_decl) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + ; + + m_map [DW_TAG_const_type] + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_data_location) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + ; + + m_map [DW_TAG_constant] + .optional (at_set_decl) + .optional (DW_AT_accessibility) + .optional (DW_AT_const_value) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_endianity) + .optional (DW_AT_external) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_start_scope) + .optional (DW_AT_type) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_dwarf_procedure] + .optional (DW_AT_location) + ; + + m_map [DW_TAG_entry_point] + .optional (DW_AT_address_class) + .optional (DW_AT_description) + .optional (DW_AT_frame_base) + .optional (DW_AT_low_pc) + .optional (DW_AT_name) + .optional (DW_AT_return_addr) + .optional (DW_AT_segment) + .optional (DW_AT_sibling) + .optional (DW_AT_static_link) + .optional (DW_AT_type) + ; + + m_map [DW_TAG_enumeration_type] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_bit_stride) + .optional (DW_AT_byte_size) + .optional (DW_AT_byte_stride) + .optional (DW_AT_data_location) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_specification) + .optional (DW_AT_start_scope) + .optional (DW_AT_type) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_enumerator] + .optional (at_set_decl) + .optional (DW_AT_const_value) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + ; + + m_map [DW_TAG_file_type] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_byte_size) + .optional (DW_AT_data_location) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_start_scope) + .optional (DW_AT_type) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_formal_parameter] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_artificial) + .optional (DW_AT_const_value) + .optional (DW_AT_default_value) + .optional (DW_AT_description) + .optional (DW_AT_endianity) + .optional (DW_AT_is_optional) + .optional (DW_AT_location) + .optional (DW_AT_name) + .optional (DW_AT_segment) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + .optional (DW_AT_variable_parameter) + ; + + m_map [DW_TAG_friend] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_friend) + .optional (DW_AT_sibling) + ; + + m_map [DW_TAG_imported_declaration] + .optional (at_set_decl) + .optional (DW_AT_accessibility) + .optional (DW_AT_description) + .optional (DW_AT_import) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_start_scope) + ; + + m_map [DW_TAG_imported_module] + .optional (at_set_decl) + .optional (DW_AT_import) + .optional (DW_AT_sibling) + .optional (DW_AT_start_scope) + ; + + m_map [DW_TAG_imported_unit] + .optional (DW_AT_import) + ; + + m_map [DW_TAG_inheritance] + .optional (at_set_decl) + .optional (DW_AT_accessibility) + .optional (DW_AT_data_member_location) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + .optional (DW_AT_virtuality) + ; + + m_map [DW_TAG_inlined_subroutine] + .optional (DW_AT_abstract_origin) + .optional (DW_AT_call_column) + .optional (DW_AT_call_file) + .optional (DW_AT_call_line) + .optional (DW_AT_entry_pc) + .optional (DW_AT_high_pc) + .optional (DW_AT_low_pc) + .optional (DW_AT_ranges) + .optional (DW_AT_return_addr) + .optional (DW_AT_segment) + .optional (DW_AT_sibling) + .optional (DW_AT_start_scope) + .optional (DW_AT_trampoline) + ; + + m_map [DW_TAG_interface_type] + .optional (at_set_decl) + .optional (DW_AT_accessibility) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_start_scope) + ; + + m_map [DW_TAG_label] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_description) + .optional (DW_AT_low_pc) + .optional (DW_AT_name) + .optional (DW_AT_segment) + .optional (DW_AT_start_scope) + .optional (DW_AT_sibling) + ; + + m_map [DW_TAG_lexical_block] + .optional (DW_AT_abstract_origin) + .optional (DW_AT_description) + .optional (DW_AT_high_pc) + .optional (DW_AT_low_pc) + .optional (DW_AT_name) + .optional (DW_AT_ranges) + .optional (DW_AT_segment) + .optional (DW_AT_sibling) + ; + + m_map [DW_TAG_member] + .optional (at_set_decl) + .optional (DW_AT_accessibility) + .optional (DW_AT_bit_offset) + .optional (DW_AT_bit_size) + .optional (DW_AT_byte_size) + .optional (DW_AT_data_member_location) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_mutable) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_module] + .optional (at_set_decl) + .optional (DW_AT_accessibility) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_entry_pc) + .optional (DW_AT_high_pc) + .optional (DW_AT_low_pc) + .optional (DW_AT_name) + .optional (DW_AT_priority) + .optional (DW_AT_ranges) + .optional (DW_AT_segment) + .optional (DW_AT_sibling) + .optional (DW_AT_specification) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_namelist] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_declaration) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_namelist_item] + .optional (at_set_decl) + .optional (DW_AT_namelist_item) + .optional (DW_AT_sibling) + ; + + m_map [DW_TAG_namespace] + .optional (at_set_decl) + .optional (DW_AT_description) + .optional (DW_AT_extension) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_start_scope) + ; + + m_map [DW_TAG_packed_type] + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_data_location) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + ; + + m_map [DW_TAG_partial_unit] + .optional (DW_AT_base_types) + .optional (DW_AT_comp_dir) + .optional (DW_AT_description) + .optional (DW_AT_identifier_case) + .optional (DW_AT_high_pc) + .optional (DW_AT_language) + .optional (DW_AT_low_pc) + .optional (DW_AT_macro_info) + .optional (DW_AT_name) + .optional (DW_AT_producer) + .optional (DW_AT_ranges) + .optional (DW_AT_segment) + .optional (DW_AT_stmt_list) + .optional (DW_AT_use_UTF8) + ; + + m_map [DW_TAG_pointer_type] + .optional (DW_AT_address_class) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_data_location) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_specification) + .optional (DW_AT_type) + .optional (DW_AT_byte_size) // XXX added to reflect reality + ; + + m_map [DW_TAG_ptr_to_member_type] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_address_class) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_containing_type) + .optional (DW_AT_data_location) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + .optional (DW_AT_use_location) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_reference_type] + .optional (DW_AT_address_class) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_data_location) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + .optional (DW_AT_byte_size) // XXX added to reflect reality + ; + + m_map [DW_TAG_restrict_type] + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_data_location) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + ; + + m_map [DW_TAG_set_type] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_byte_size) + .optional (DW_AT_data_location) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_start_scope) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_shared_type] + .optional (at_set_decl) + .optional (DW_AT_count) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + ; + + m_map [DW_TAG_string_type] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_byte_size) + .optional (DW_AT_data_location) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_start_scope) + .optional (DW_AT_string_length) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_structure_type] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_byte_size) + .optional (DW_AT_data_location) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_specification) + .optional (DW_AT_start_scope) + .optional (DW_AT_visibility) + .optional (DW_AT_containing_type) // XXX added to reflect reality + ; + + m_map [DW_TAG_subprogram] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_address_class) + .optional (DW_AT_artificial) + .optional (DW_AT_calling_convention) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_elemental) + .optional (DW_AT_entry_pc) + .optional (DW_AT_explicit) + .optional (DW_AT_external) + .optional (DW_AT_frame_base) + .optional (DW_AT_high_pc) + .optional (DW_AT_inline) + .optional (DW_AT_low_pc) + .optional (DW_AT_name) + .optional (DW_AT_object_pointer) + .optional (DW_AT_prototyped) + .optional (DW_AT_pure) + .optional (DW_AT_ranges) + .optional (DW_AT_recursive) + .optional (DW_AT_return_addr) + .optional (DW_AT_segment) + .optional (DW_AT_sibling) + .optional (DW_AT_specification) + .optional (DW_AT_start_scope) + .optional (DW_AT_static_link) + .optional (DW_AT_trampoline) + .optional (DW_AT_type) + .optional (DW_AT_visibility) + .optional (DW_AT_virtuality) + .optional (DW_AT_vtable_elem_location) + .optional (DW_AT_MIPS_linkage_name) // XXX added to reflect reality + .optional (DW_AT_containing_type) // XXX added to reflect reality + ; + + m_map [DW_TAG_subrange_type] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_bit_stride) + .optional (DW_AT_byte_size) + .optional (DW_AT_byte_stride) + .optional (DW_AT_count) + .optional (DW_AT_data_location) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_lower_bound) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_threads_scaled) + .optional (DW_AT_type) + .optional (DW_AT_upper_bound) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_subroutine_type] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_address_class) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_data_location) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_prototyped) + .optional (DW_AT_sibling) + .optional (DW_AT_start_scope) + .optional (DW_AT_type) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_template_type_parameter] + .optional (at_set_decl) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + ; + + m_map [DW_TAG_template_value_parameter ] + .optional (at_set_decl) + .optional (DW_AT_const_value) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + ; + + m_map [DW_TAG_thrown_type] + .optional (at_set_decl) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_data_location) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + ; + + m_map [DW_TAG_try_block] + .optional (DW_AT_abstract_origin) + .optional (DW_AT_high_pc) + .optional (DW_AT_low_pc) + .optional (DW_AT_ranges) + .optional (DW_AT_segment) + .optional (DW_AT_sibling) + ; + + m_map [DW_TAG_typedef] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_data_location) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_start_scope) + .optional (DW_AT_type) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_union_type] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_byte_size) + .optional (DW_AT_data_location) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_specification) + .optional (DW_AT_start_scope) + .optional (DW_AT_visibility) + ; + + m_map [DW_TAG_unspecified_parameters] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_artificial) + .optional (DW_AT_sibling) + ; + + m_map [DW_TAG_unspecified_type] + .optional (at_set_decl) + .optional (DW_AT_description) + .optional (DW_AT_name) + ; + + m_map [DW_TAG_variable] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_const_value) + .optional (DW_AT_declaration) + .optional (DW_AT_description) + .optional (DW_AT_endianity) + .optional (DW_AT_external) + .optional (DW_AT_location) + .optional (DW_AT_name) + .optional (DW_AT_segment) + .optional (DW_AT_sibling) + .optional (DW_AT_specification) + .optional (DW_AT_start_scope) + .optional (DW_AT_type) + .optional (DW_AT_visibility) + .optional (DW_AT_MIPS_linkage_name) // XXX added to reflect reality + .optional (DW_AT_artificial) // XXX added to reflect reality + ; + + m_map [DW_TAG_variant] + .optional (at_set_decl) + .optional (DW_AT_accessibility) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_declaration) + .optional (DW_AT_discr_list) + .optional (DW_AT_discr_value) + .optional (DW_AT_sibling) + ; + + m_map [DW_TAG_variant_part] + .optional (at_set_decl) + .optional (DW_AT_abstract_origin) + .optional (DW_AT_accessibility) + .optional (DW_AT_declaration) + .optional (DW_AT_discr) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + ; + + m_map [DW_TAG_volatile_type] + .optional (at_set_decl) + .optional (DW_AT_allocated) + .optional (DW_AT_associated) + .optional (DW_AT_data_location) + .optional (DW_AT_name) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + ; + + m_map [DW_TAG_with_stmt] + .optional (DW_AT_accessibility) + .optional (DW_AT_address_class) + .optional (DW_AT_declaration) + .optional (DW_AT_high_pc) + .optional (DW_AT_location) + .optional (DW_AT_low_pc) + .optional (DW_AT_ranges) + .optional (DW_AT_segment) + .optional (DW_AT_sibling) + .optional (DW_AT_type) + .optional (DW_AT_visibility) + ; +} diff --git a/src/dwarflint-expected.hh b/src/dwarflint-expected.hh new file mode 100644 index 00000000..429d64c1 --- /dev/null +++ b/src/dwarflint-expected.hh @@ -0,0 +1,79 @@ +#include <map> +#include <set> +#include <stdexcept> +#include <sstream> +#include <cassert> + +enum optionality +{ + opt_optional = 0, // may or may not be present + opt_required, // bogus if missing + opt_expected, // suspicious if missing +}; + +template <class T> +std::string +to_string (T x) +{ + std::ostringstream o; + o << x; + return o.str(); +} + +struct expected_set +{ + typedef std::map <int, optionality> expectation_map; + +private: + expectation_map m_map; + +public: +#define DEF_FILLER(WHAT) \ + expected_set &WHAT (int attribute) \ + { \ + assert (m_map.find (attribute) == m_map.end ()); \ + m_map.insert (std::make_pair (attribute, opt_##WHAT)); \ + return *this; \ + } \ + expected_set &WHAT (std::set <int> const &attributes) \ + { \ + for (std::set <int>::const_iterator it = attributes.begin (); \ + it != attributes.end (); ++it) \ + WHAT (*it); \ + return *this; \ + } + + DEF_FILLER (required) + DEF_FILLER (expected) + DEF_FILLER (optional) +#undef DEF_FILLER + + expectation_map const &map () const + { + return m_map; + } +}; + +class expected_map +{ + typedef std::map <int, expected_set> expected_map_t; + +protected: + expected_map_t m_map; + expected_map () {} + +public: + expected_set::expectation_map const &map (int tag) const + { + expected_map_t::const_iterator it = m_map.find (tag); + if (it == m_map.end ()) + throw std::runtime_error ("Unknown tag #" + to_string (tag)); + return it->second.map (); + } +}; + +struct expected_at_map + : public expected_map +{ + expected_at_map (); +}; diff --git a/src/dwarflint-hl.cc b/src/dwarflint-hl.cc new file mode 100644 index 00000000..1b8961a2 --- /dev/null +++ b/src/dwarflint-hl.cc @@ -0,0 +1,263 @@ +/* Pedantic checking of DWARF files. + Copyright (C) 2009 Red Hat, Inc. + This file is part of Red Hat elfutils. + Written by Petr Machata <pmachata@redhat.com>, 2009. + + Red Hat elfutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by the + Free Software Foundation; version 2 of the License. + + Red Hat elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Red Hat elfutils; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. + + Red Hat elfutils is an included package of the Open Invention Network. + An included package of the Open Invention Network is a package for which + Open Invention Network licensees cross-license their patents. No patent + license is granted, either expressly or impliedly, by designation as an + included package. Should you wish to participate in the Open Invention + Network licensing program, please visit www.openinventionnetwork.com + <http://www.openinventionnetwork.com>. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <iostream> +#include <set> +#include <algorithm> +#include <cinttypes> +#include <cstdarg> +#include <cassert> +#include <iterator> + +#include "dwarflint.h" +#include "dwarflint-expected.hh" +#include "dwarfstrings.h" +#include "c++/dwarf" +#include "../libdw/libdwP.h" +#include "../libdw/c++/dwarf-knowledge.cc" + +namespace +{ + message_category cat (message_category c1, + message_category c2, + message_category c3 = mc_none) + { + return static_cast<message_category> (c1 | c2 | c3); + } +} + +struct hl_ctx +{ + elfutils::dwarf dw; + + hl_ctx (Dwarf *dwarf) + : dw (dwarf) + { + } +}; + +hl_ctx * +hl_ctx_new (Dwarf *dwarf) +{ + return new hl_ctx (dwarf); +} + +void +hl_ctx_delete (hl_ctx *hlctx) +{ + delete hlctx; +} + +static const expected_at_map expected_at; +//static const expected_children_map expected_children; + +bool +check_matching_ranges (hl_ctx *hlctx) +{ + struct where where_ref = WHERE (sec_info, NULL); + struct where where_ar = WHERE (sec_aranges, NULL); + where_ar.ref = &where_ref; + struct where where_r = WHERE (sec_ranges, NULL); + where_r.ref = &where_ref; + + const elfutils::dwarf::aranges_map &aranges = hlctx->dw.aranges (); + for (elfutils::dwarf::aranges_map::const_iterator i = aranges.begin (); + i != aranges.end (); ++i) + { + const elfutils::dwarf::compile_unit &cu = i->first; + where_reset_1 (&where_ref, 0); + where_reset_2 (&where_ref, cu.offset ()); + + std::set<elfutils::dwarf::ranges::key_type> + cu_aranges = i->second, + cu_ranges = cu.ranges (); + + typedef std::vector <elfutils::dwarf::arange_list::value_type> range_vec; + range_vec missing; + std::back_insert_iterator <range_vec> i_missing (missing); + + std::set_difference (cu_aranges.begin (), cu_aranges.end (), + cu_ranges.begin (), cu_ranges.end (), + i_missing); + + for (range_vec::iterator it = missing.begin (); + it != missing.end (); ++it) + wr_message (cat (mc_ranges, mc_aranges, mc_impact_3), &where_r, + ": missing range %#" PRIx64 "..%#" PRIx64 + ", present in .debug_aranges.\n", + it->first, it->second); + + missing.clear (); + std::set_difference (cu_ranges.begin (), cu_ranges.end (), + cu_aranges.begin (), cu_aranges.end (), + i_missing); + + for (range_vec::iterator it = missing.begin (); + it != missing.end (); ++it) + wr_message (cat (mc_ranges, mc_aranges, mc_impact_3), &where_ar, + ": missing range %#" PRIx64 "..%#" PRIx64 + ", present in .debug_ranges.\n", + it->first, it->second); + } + + return true; +} + +struct name_extractor { + int operator () (elfutils::dwarf::attribute const &at) { + return at.first; + } +} extract_name; + +std::ostream & +operator << (std::ostream &o, elfutils::dwarf::value_space vs) +{ + using namespace elfutils; + switch (vs) + { + case dwarf::VS_flag: return o << "flag"; + case dwarf::VS_dwarf_constant: return o << "dwarf_constant"; + case dwarf::VS_discr_list: return o << "discr_list"; + case dwarf::VS_reference: return o << "reference"; + case dwarf::VS_unit_reference: return o << "unit_reference"; + case dwarf::VS_lineptr: return o << "lineptr"; + case dwarf::VS_macptr: return o << "macptr"; + case dwarf::VS_rangelistptr: return o << "rangelistptr"; + case dwarf::VS_identifier: return o << "identifier"; + case dwarf::VS_string: return o << "string"; + case dwarf::VS_source_file: return o << "source_file"; + case dwarf::VS_source_line: return o << "source_line"; + case dwarf::VS_source_column: return o << "source_column"; + case dwarf::VS_address: return o << "address"; + case dwarf::VS_constant: return o << "constant"; + case dwarf::VS_location: return o << "location"; + }; + + abort (); +} + +static void +recursively_validate (elfutils::dwarf::compile_unit const &cu, + elfutils::dwarf::debug_info_entry const &parent) +{ + struct where where = WHERE (sec_info, NULL); + where_reset_1 (&where, cu.offset ()); + where_reset_2 (&where, parent.offset ()); + + int parent_tag = parent.tag (); + + // Set of attributes of this DIE. + std::set <int> attributes; + std::transform (parent.attributes ().begin (), + parent.attributes ().end (), + std::inserter (attributes, attributes.end ()), + extract_name); + + // Attributes that we expect at this DIE. + expected_set::expectation_map const &expect + = expected_at.map (parent_tag); + + // Check missing attributes. + for (expected_set::expectation_map::const_iterator jt + = expect.begin (); jt != expect.end (); ++jt) + { + std::set <int>::iterator kt = attributes.find (jt->first); + if (kt == attributes.end ()) + switch (jt->second) + { + case opt_required: + wr_message (cat (mc_impact_4, mc_info), &where, + ": %s lacks required attribute %s.\n", + dwarf_tag_string (parent_tag), + dwarf_attr_string (jt->first)); + break; + + case opt_expected: + wr_message (cat (mc_impact_2, mc_info), &where, + ": %s should contain attribute %s.\n", + dwarf_tag_string (parent_tag), + dwarf_attr_string (jt->first)); + case opt_optional: + break; + }; + } + + // Check present attributes for expected-ness, and validate value + // space. + for (elfutils::dwarf::debug_info_entry::attributes::const_iterator jt + = parent.attributes ().begin (), jte = parent.attributes ().end (); + jt != jte; ++jt) + { + unsigned name = extract_name (*jt); + + expected_set::expectation_map::const_iterator kt = expect.find (name); + if (kt == expect.end ()) + wr_message (cat (mc_impact_3, mc_info), &where, + ": DIE \"%s\" has attribute \"%s\", which is not expected.\n", + dwarf_tag_string (parent_tag), + dwarf_attr_string (name)); + try + { + unsigned exp_vs = expected_value_space (name, parent_tag); + elfutils::dwarf::value_space vs = (*jt).second.what_space (); + if ((exp_vs & (1U << vs)) == 0) + wr_message (cat (mc_impact_3, mc_info), &where, + ": in DIE \"%s\", attribute \"%s\" has value of unexpected type \"%s\".\n", + dwarf_tag_string (parent_tag), + dwarf_attr_string (name), + to_string (vs).c_str ()); + } + catch (...) + { + wr_message (cat (mc_impact_4, mc_info, mc_error), &where, + ": in DIE \"%s\", couldn't obtain type of attribute \"%s\".\n", + dwarf_tag_string (parent_tag), + dwarf_attr_string (name)); + } + } + + // Check children recursively. + class elfutils::dwarf::debug_info_entry::children const &children + = parent.children (); + for (elfutils::dwarf::debug_info_entry::children::const_iterator jt + = children.begin (); jt != children.end (); ++jt) + recursively_validate (cu, *jt); +} + +bool +check_expected_trees (hl_ctx *hlctx) +{ + class elfutils::dwarf::compile_units const &cus = hlctx->dw.compile_units (); + for (elfutils::dwarf::compile_units::const_iterator it = cus.begin (); + it != cus.end (); ++it) + recursively_validate (*it, *it); + + return true; +} diff --git a/src/dwarflint.c b/src/dwarflint.c new file mode 100644 index 00000000..b0b8ca7a --- /dev/null +++ b/src/dwarflint.c @@ -0,0 +1,5311 @@ +/* Pedantic checking of DWARF files. + Copyright (C) 2008,2009 Red Hat, Inc. + This file is part of Red Hat elfutils. + Written by Petr Machata <pmachata@redhat.com>, 2008. + + Red Hat elfutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by the + Free Software Foundation; version 2 of the License. + + Red Hat elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Red Hat elfutils; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. + + Red Hat elfutils is an included package of the Open Invention Network. + An included package of the Open Invention Network is a package for which + Open Invention Network licensees cross-license their patents. No patent + license is granted, either expressly or impliedly, by designation as an + included package. Should you wish to participate in the Open Invention + Network licensing program, please visit www.openinventionnetwork.com + <http://www.openinventionnetwork.com>. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <sys/stat.h> +#include <sys/types.h> +#include <argp.h> +#include <assert.h> +#include <error.h> +#include <fcntl.h> +#include <gelf.h> +#include <inttypes.h> +#include <libintl.h> +#include <locale.h> +#include <stdarg.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <system.h> +#include <unistd.h> + +#include "../libdw/dwarf.h" +#include "../libdw/libdwP.h" +#include "../libdw/known-dwarf.h" +#include "../libebl/libebl.h" +#include "dwarfstrings.h" +#include "dwarflint.h" + +/* Bug report address. */ +const char *argp_program_bug_address = PACKAGE_BUGREPORT; + +#define ARGP_strict 300 +#define ARGP_gnu 301 +#define ARGP_tolerant 302 +#define ARGP_ref 303 +#define ARGP_nohl 304 + +#undef FIND_SECTION_HOLES + +/* Definitions of arguments for argp functions. */ +static const struct argp_option options[] = +{ + { "strict", ARGP_strict, NULL, 0, + N_("Be extremely strict, flag level 2 features."), 0 }, + { "quiet", 'q', NULL, 0, N_("Do not print anything if successful"), 0 }, + { "ignore-missing", 'i', NULL, 0, + N_("Don't complain if files have no DWARF at all"), 0 }, + { "gnu", ARGP_gnu, NULL, 0, + N_("Binary has been created with GNU toolchain and is therefore known to be \ +broken in certain ways"), 0 }, + { "tolerant", ARGP_tolerant, NULL, 0, + N_("Don't output certain common error messages"), 0 }, + { "ref", ARGP_ref, NULL, 0, + N_("When validating .debug_loc and .debug_ranges, display information about \ +the DIE referring to the entry in consideration"), 0 }, + { "nohl", ARGP_nohl, NULL, 0, + N_("Don't run high-level tests"), 0 }, + { "verbose", 'v', NULL, 0, + N_("Be verbose"), 0 }, + { NULL, 0, NULL, 0, NULL, 0 } +}; + +/* Short description of program. */ +static const char doc[] = N_("\ +Pedantic checking of DWARF stored in ELF files."); + +/* Strings for arguments in help texts. */ +static const char args_doc[] = N_("FILE..."); + +/* Prototype for option handler. */ +static error_t parse_opt (int key, char *arg, struct argp_state *state); + +/* Data structure to communicate with argp functions. */ +static struct argp argp = +{ + options, parse_opt, args_doc, doc, NULL, NULL, NULL +}; + +/* If true, we accept silently files without debuginfo. */ +static bool tolerate_nodebug = false; + +static void process_file (Dwarf *dwarf, const char *fname, bool only_one); + +struct message_term +{ + /* Given a term like A && !B && C && !D, we decompose it thus: */ + enum message_category positive; /* non-zero bits for plain predicates */ + enum message_category negative; /* non-zero bits for negated predicates */ +}; + +struct message_criteria +{ + struct message_term *terms; + size_t size; + size_t alloc; +}; + +static bool +message_accept (struct message_criteria *cri, enum message_category cat) +{ + for (size_t i = 0; i < cri->size; ++i) + { + struct message_term *t = cri->terms + i; + if ((t->positive & cat) == t->positive + && (t->negative & cat) == 0) + return true; + } + return false; +} + +static const char * +message_term_str (struct message_term *t) +{ + static char *names[] = { +#define MC(CAT, ID) [ID] = #CAT, + MESSAGE_CATEGORIES +#undef MC + }; + + unsigned max = 0; +#define MC(CAT, ID) max = ID; + MESSAGE_CATEGORIES +#undef MC + + static char buf[512]; + char *ptr = buf; + ptr = stpcpy (ptr, "("); + + bool got = false; + for (unsigned i = 0; i <= max; ++i) + { + unsigned mask = 1u << i; + if ((t->positive & mask) != 0 + || (t->negative & mask) != 0) + { + if (got) + ptr = stpcpy (ptr, " & "); + if ((t->negative & (1u << i)) != 0) + ptr = stpcpy (ptr, "~"); + ptr = stpcpy (ptr, names[i]); + got = true; + } + } + + if (ptr == buf + 1) + ptr = stpcpy (ptr, "1"); + ptr = stpcpy (ptr, ")"); + return buf; +} + +static const char * +message_cri_str (struct message_criteria *cri) +{ + static char buf[512]; + char *ptr = buf; + *ptr = 0; + + for (size_t i = 0; i < cri->size; ++i) + { + struct message_term *t = cri->terms + i; + if (i > 0) + ptr = stpcpy (ptr, " | "); + ptr = stpcpy (ptr, message_term_str (t)); + } + + return buf; +} + +static void +message_cri_and (struct message_criteria *cri, struct message_term *term) +{ + assert ((term->positive & term->negative) == 0); + for (size_t i = 0; i < cri->size; ) + { + struct message_term *t = cri->terms + i; + t->positive |= term->positive; + t->negative |= term->negative; + if ((t->positive & t->negative) != 0) + /* A ^ ~A -> drop the term. */ + cri->terms[i] = cri->terms[--cri->size]; + else + ++i; + } +} + +static void +message_cri_or (struct message_criteria *cri, struct message_term *term) +{ + assert ((term->positive & term->negative) == 0); + REALLOC (cri, terms); + cri->terms[cri->size++] = *term; +} + +/* NEG(a&b&~c) -> (~a + ~b + c) */ +static struct message_criteria +message_cri_neg (struct message_term *term) +{ + assert ((term->positive & term->negative) == 0); + + unsigned max = 0; +#define MC(CAT, ID) max = ID; + MESSAGE_CATEGORIES +#undef MC + + struct message_criteria ret; + WIPE (ret); + for (size_t i = 0; i < max; ++i) + { + unsigned mask = 1u << i; + if ((term->positive & mask) != 0) + message_cri_or (&ret, &(struct message_term){1u << i, mc_none}); + else if ((term->negative & mask) != 0) + message_cri_or (&ret, &(struct message_term){mc_none, 1u << i}); + } + + return ret; +} + +/* MUL((a&b + c&d), (e&f + g&h)) -> (a&b&e&f + a&b&g&h + c&d&e&f + c&d&g&h) */ +static void +message_cri_mul (struct message_criteria *cri, struct message_criteria *rhs) +{ + struct message_criteria ret; + WIPE (ret); + + for (size_t i = 0; i < cri->size; ++i) + for (size_t j = 0; j < rhs->size; ++j) + { + struct message_term t1 = cri->terms[i]; + struct message_term *t2 = rhs->terms + j; + t1.positive |= t2->positive; + t1.negative |= t2->negative; + if (t1.positive & t1.negative) + /* A ^ ~A -> drop the term. */ + continue; + message_cri_or (&ret, &t1); + } + + free (cri->terms); + *cri = ret; +} + +/* Reject message if TERM passes. */ +static void +message_cri_and_not (struct message_criteria *cri, struct message_term *term) +{ + struct message_criteria tmp + = message_cri_neg (&(struct message_term) {term->negative, term->positive}); + message_cri_mul (cri, &tmp); + free (tmp.terms); +} + +/* Messages that are accepted (and made into warning). */ +static struct message_criteria warning_criteria; + +/* Accepted (warning) messages, that are turned into errors. */ +static struct message_criteria error_criteria; + +static unsigned error_count = 0; + +static bool +check_category (enum message_category cat) +{ + return message_accept (&warning_criteria, cat); +} + +static void +wr_verror (const struct where *wh, const char *format, va_list ap) +{ + printf ("error: %s", where_fmt (wh, NULL)); + vprintf (format, ap); + where_fmt_chain (wh, "error"); + ++error_count; +} + +static void +wr_vwarning (const struct where *wh, const char *format, va_list ap) +{ + printf ("warning: %s", where_fmt (wh, NULL)); + vprintf (format, ap); + where_fmt_chain (wh, "warning"); + ++error_count; +} + +void +wr_error (const struct where *wh, const char *format, ...) +{ + va_list ap; + va_start (ap, format); + wr_verror (wh, format, ap); + va_end (ap); +} + +void +wr_warning (const struct where *wh, const char *format, ...) +{ + va_list ap; + va_start (ap, format); + wr_vwarning (wh, format, ap); + va_end (ap); +} + +void +wr_message (enum message_category category, const struct where *wh, + const char *format, ...) +{ + va_list ap; + va_start (ap, format); + if (message_accept (&warning_criteria, category)) + { + if (message_accept (&error_criteria, category)) + wr_verror (wh, format, ap); + else + wr_vwarning (wh, format, ap); + } + va_end (ap); +} + +void +wr_format_padding_message (enum message_category category, + struct where *wh, + uint64_t start, uint64_t end, char *kind) +{ + wr_message (category, wh, + ": 0x%" PRIx64 "..0x%" PRIx64 ": %s.\n", start, end, kind); +} + +void +wr_format_leb128_message (struct where *where, const char *what, + const char *purpose, + const unsigned char *begin, const unsigned char *end) +{ + enum message_category category = mc_leb128 | mc_acc_bloat | mc_impact_3; + char buf[(end - begin) * 3 + 1]; // 2 hexa digits+" " per byte, and term. 0 + char *ptr = buf; + for (; begin < end; ++begin) + ptr += sprintf (ptr, " %02x", *begin); + wr_message (category, where, + ": %s: value %s encoded as `%s'.\n", + what, purpose, buf + 1); +} + +void +wr_message_padding_0 (enum message_category category, + struct where *wh, + uint64_t start, uint64_t end) +{ + wr_format_padding_message (category | mc_acc_bloat | mc_impact_1, + wh, start, end, + "unnecessary padding with zero bytes"); +} + +void +wr_message_padding_n0 (enum message_category category, + struct where *wh, + uint64_t start, uint64_t end) +{ + wr_format_padding_message (category | mc_acc_bloat | mc_impact_1, + wh, start, end, + "unreferenced non-zero bytes"); +} + +/* True if no message is to be printed if the run is succesful. */ +static bool be_quiet = false; /* -q */ +static bool be_verbose = false; /* -v */ +static bool be_strict = false; /* --strict */ +static bool be_gnu = false; /* --gnu */ +static bool be_tolerant = false; /* --tolerant */ +static bool show_refs = false; /* --ref */ +static bool do_high_level = true; /* ! --nohl */ + +static int +layout_rel_file (Elf *elf) +{ + GElf_Ehdr ehdr; + if (gelf_getehdr (elf, &ehdr) == NULL) + return 1; + + if (ehdr.e_type != ET_REL) + return 0; + + /* Taken from libdwfl. */ + GElf_Addr base = 0; + GElf_Addr start = 0, end = 0, bias = 0; + + bool first = true; + Elf_Scn *scn = NULL; + while ((scn = elf_nextscn (elf, scn)) != NULL) + { + GElf_Shdr shdr_mem; + GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); + if (unlikely (shdr == NULL)) + return 1; + + if (shdr->sh_flags & SHF_ALLOC) + { + const GElf_Xword align = shdr->sh_addralign ?: 1; + const GElf_Addr next = (end + align - 1) & -align; + if (shdr->sh_addr == 0 + /* Once we've started doing layout we have to do it all, + unless we just layed out the first section at 0 when + it already was at 0. */ + || (bias == 0 && end > start && end != next)) + { + shdr->sh_addr = next; + if (end == base) + /* This is the first section assigned a location. + Use its aligned address as the module's base. */ + start = base = shdr->sh_addr; + else if (unlikely (base & (align - 1))) + { + /* If BASE has less than the maximum alignment of + any section, we eat more than the optimal amount + of padding and so make the module's apparent + size come out larger than it would when placed + at zero. So reset the layout with a better base. */ + + start = end = base = (base + align - 1) & -align; + Elf_Scn *prev_scn = NULL; + do + { + prev_scn = elf_nextscn (elf, prev_scn); + GElf_Shdr prev_shdr_mem; + GElf_Shdr *prev_shdr = gelf_getshdr (prev_scn, + &prev_shdr_mem); + if (unlikely (prev_shdr == NULL)) + return 1; + if (prev_shdr->sh_flags & SHF_ALLOC) + { + const GElf_Xword prev_align + = prev_shdr->sh_addralign ?: 1; + + prev_shdr->sh_addr + = (end + prev_align - 1) & -prev_align; + end = prev_shdr->sh_addr + prev_shdr->sh_size; + + if (unlikely (! gelf_update_shdr (prev_scn, + prev_shdr))) + return 1; + } + } + while (prev_scn != scn); + continue; + } + + end = shdr->sh_addr + shdr->sh_size; + if (likely (shdr->sh_addr != 0) + && unlikely (! gelf_update_shdr (scn, shdr))) + return 1; + } + else + { + /* The address is already assigned. Just track it. */ + if (first || end < shdr->sh_addr + shdr->sh_size) + end = shdr->sh_addr + shdr->sh_size; + if (first || bias > shdr->sh_addr) + /* This is the lowest address in the module. */ + bias = shdr->sh_addr; + + if ((shdr->sh_addr - bias + base) & (align - 1)) + /* This section winds up misaligned using BASE. + Adjust BASE upwards to make it congruent to + the lowest section address in the file modulo ALIGN. */ + base = (((base + align - 1) & -align) + + (bias & (align - 1))); + } + + first = false; + } + } + return 0; +} + +int +main (int argc, char *argv[]) +{ + /* Set locale. */ + setlocale (LC_ALL, ""); + + /* Initialize the message catalog. */ + textdomain (PACKAGE_TARNAME); + + /* Parse and process arguments. */ + int remaining; + argp_parse (&argp, argc, argv, 0, &remaining, NULL); + + /* Initialize warning & error criteria. */ + message_cri_or (&warning_criteria, + &(struct message_term){mc_none, mc_none}); + + message_cri_or (&error_criteria, + &(struct message_term){mc_impact_4, mc_none}); + message_cri_or (&error_criteria, + &(struct message_term){mc_error, mc_none}); + + /* Configure warning & error criteria according to configuration. */ + if (tolerate_nodebug) + message_cri_and (&warning_criteria, + &(struct message_term){mc_none, mc_elf}); + + if (be_gnu) + { + message_cri_and (&warning_criteria, + &(struct message_term){mc_none, mc_acc_bloat}); + } + + if (!be_strict) + { + message_cri_and (&warning_criteria, + &(struct message_term){mc_none, mc_strings}); + message_cri_and_not (&warning_criteria, + &(struct message_term) + {mc_line | mc_header | mc_acc_bloat, mc_none}); + message_cri_and (&warning_criteria, + &(struct message_term){mc_none, mc_pubtypes}); + } + + if (be_tolerant) + { + message_cri_and (&warning_criteria, + &(struct message_term){mc_none, mc_loc}); + message_cri_and (&warning_criteria, + &(struct message_term){mc_none, mc_ranges}); + } + + if (be_verbose) + { + printf ("warning criteria: %s\n", message_cri_str (&warning_criteria)); + printf ("error criteria: %s\n", message_cri_str (&error_criteria)); + } + + /* Before we start tell the ELF library which version we are using. */ + elf_version (EV_CURRENT); + + /* Now process all the files given at the command line. */ + bool only_one = remaining + 1 == argc; + do + { + /* Open the file. */ + int fd = open (argv[remaining], O_RDONLY); + if (fd == -1) + { + error (0, errno, gettext ("cannot open input file")); + continue; + } + + /* Create an `Elf' descriptor. */ + Elf *elf = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, NULL); + if (elf == NULL) + invalid_elf: + wr_error (NULL, + gettext ("Error processing ELF file: %s\n"), + elf_errmsg (-1)); + else + { + unsigned int prev_error_count = error_count; + if (layout_rel_file (elf)) + goto invalid_elf; + + Dwarf *dwarf = dwarf_begin_elf (elf, DWARF_C_READ, NULL); + if (dwarf == NULL) + { + if (!tolerate_nodebug) + wr_error (NULL, + gettext ("cannot generate Dwarf descriptor: %s\n"), + dwarf_errmsg (-1)); + } + else + { + process_file (dwarf, argv[remaining], only_one); + + if (dwarf_end (dwarf) != 0) + wr_error (NULL, + gettext ("error while closing Dwarf descriptor: %s\n"), + dwarf_errmsg (-1)); + } + + if (elf_end (elf) != 0) + wr_error (NULL, + gettext ("error while closing Elf descriptor: %s\n"), + elf_errmsg (-1)); + + if (prev_error_count == error_count && !be_quiet) + puts (gettext ("No errors")); + } + + close (fd); + } + while (++remaining < argc); + + return error_count != 0; +} + +/* Handle program arguments. */ +static error_t +parse_opt (int key, char *arg __attribute__ ((unused)), + struct argp_state *state __attribute__ ((unused))) +{ + switch (key) + { + case ARGP_strict: + be_strict = true; + break; + + case ARGP_gnu: + be_gnu = true; + break; + + case ARGP_tolerant: + be_tolerant = true; + break; + + case ARGP_ref: + show_refs = true; + break; + + case ARGP_nohl: + do_high_level = false; + break; + + case 'i': + tolerate_nodebug = true; + break; + + case 'q': + be_quiet = true; + be_verbose = false; + break; + + case 'v': + be_quiet = false; + be_verbose = true; + break; + + case ARGP_KEY_NO_ARGS: + fputs (gettext ("Missing file name.\n"), stderr); + argp_help (&argp, stderr, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR, + program_invocation_short_name); + exit (1); + + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} + +#define PRI_CU "CU 0x%" PRIx64 +#define PRI_DIE "DIE 0x%" PRIx64 +#define PRI_NOT_ENOUGH ": not enough data for %s.\n" +#define PRI_LACK_RELOCATION ": %s seems to lack a relocation.\n" + +struct relocation +{ + uint64_t offset; + uint64_t addend; + int symndx; + int type; + bool invalid; /* Whether this one relocation should be + ignored. Necessary so that we don't + double-report invalid & missing + relocation. */ +}; + +struct relocation_data +{ + struct elf_file *file; + Elf_Data *symdata; /* Symbol table associated with this + relocation section. */ + size_t type; /* SHT_REL or SHT_RELA. */ + + struct relocation *rel; /* Array of relocations. May be NULL + if there are no associated + relocation data. */ + size_t size; + size_t alloc; + size_t index; /* Current index. */ +}; + +struct section_data +{ + struct elf_file *file; + size_t secndx; /* Index into file->sec. */ + Elf_Data *data; + struct relocation_data rel; +}; + +static struct sec * +data_get_sec (struct section_data *data) +{ + return data->file->sec + data->secndx; +} + +/* Functions and data structures related to bounds-checked + reading. */ + +struct read_ctx +{ + Dwarf *dbg; + Elf_Data *data; + const unsigned char *ptr; + const unsigned char *begin; + const unsigned char *end; +}; + + +static void read_ctx_init (struct read_ctx *ctx, Dwarf *dbg, + Elf_Data *data); +static bool read_ctx_init_sub (struct read_ctx *ctx, + struct read_ctx *parent, + const unsigned char *begin, + const unsigned char *end); +static uint64_t read_ctx_get_offset (struct read_ctx *ctx); +static bool read_ctx_need_data (struct read_ctx *ctx, size_t length); +static bool read_ctx_read_ubyte (struct read_ctx *ctx, unsigned char *ret); +static int read_ctx_read_uleb128 (struct read_ctx *ctx, uint64_t *ret); +static int read_ctx_read_sleb128 (struct read_ctx *ctx, int64_t *ret); +static bool read_ctx_read_2ubyte (struct read_ctx *ctx, uint16_t *ret); +static bool read_ctx_read_4ubyte (struct read_ctx *ctx, uint32_t *ret); +static bool read_ctx_read_8ubyte (struct read_ctx *ctx, uint64_t *ret); +static bool read_ctx_read_offset (struct read_ctx *ctx, bool dwarf64, + uint64_t *ret); +static bool read_ctx_read_var (struct read_ctx *ctx, int width, uint64_t *ret); +static const char *read_ctx_read_str (struct read_ctx *ctx); +static bool read_ctx_skip (struct read_ctx *ctx, uint64_t len); +static bool read_ctx_eof (struct read_ctx *ctx); + + +/* Functions and data structures related to raw (i.e. unassisted by + libdw) Dwarf abbreviation handling. */ + +struct abbrev +{ + uint64_t code; + + /* While ULEB128 can hold numbers > 32bit, these are not legal + values of many enum types. So just use as large type as + necessary to cover valid values. */ + uint16_t tag; + bool has_children; + + struct where where; + + /* Whether some DIE uses this abbrev. */ + bool used; + + /* Attributes. */ + struct abbrev_attrib + { + uint16_t name; + uint8_t form; + struct where where; + } *attribs; + size_t size; + size_t alloc; +}; + +struct abbrev_table +{ + uint64_t offset; + struct abbrev *abbr; + size_t size; + size_t alloc; + struct abbrev_table *next; +}; + +static struct abbrev_table *abbrev_table_load (struct read_ctx *ctx); +static void abbrev_table_free (struct abbrev_table *abbr); +static struct abbrev *abbrev_table_find_abbrev (struct abbrev_table *abbrevs, + uint64_t abbrev_code); + + +/* Functions and data structures for address record handling. We use + that to check that all DIE references actually point to an existing + die, not somewhere mid-DIE, where it just happens to be + interpretable as a DIE. */ + +struct addr_record +{ + size_t size; + size_t alloc; + uint64_t *addrs; +}; + +static size_t addr_record_find_addr (struct addr_record *ar, uint64_t addr); +static bool addr_record_has_addr (struct addr_record *ar, uint64_t addr); +static void addr_record_add (struct addr_record *ar, uint64_t addr); +static void addr_record_free (struct addr_record *ar); + + +/* Functions and data structures for reference handling. Just like + the above, we use this to check validity of DIE references. Unlike + the above, this is not stored as sorted set, but simply as an array + of records, because duplicates are unlikely. */ + +struct ref +{ + uint64_t addr; // Referree address + struct where who; // Referrer +}; + +struct ref_record +{ + size_t size; + size_t alloc; + struct ref *refs; +}; + +static void ref_record_add (struct ref_record *rr, uint64_t addr, struct where *referrer); +static void ref_record_free (struct ref_record *rr); + + +/* Functions and data structures for CU handling. */ + +struct cu +{ + uint64_t offset; + uint64_t cudie_offset; + uint64_t length; + int address_size; // Address size in bytes on the target machine. + uint64_t base; // DW_AT_low_pc value of CU DIE, 0 if not present. + struct addr_record die_addrs; // Addresses where DIEs begin in this CU. + struct ref_record die_refs; // DIE references into other CUs from this CU. + struct ref_record loc_refs; // references into .debug_loc from this CU. + struct ref_record range_refs; // references into .debug_ranges from this CU. + struct ref_record line_refs; // references into .debug_line from this CU. + struct where where; // Where was this section defined. + bool has_arange; // Whether we saw arange section pointing to this CU. + bool has_pubnames; // Likewise for pubnames. + bool has_pubtypes; // Likewise for pubtypes. + struct cu *next; +}; + +static void cu_free (struct cu *cu_chain); +static struct cu *cu_find_cu (struct cu *cu_chain, uint64_t offset); + + +/* Functions for checking of structural integrity. */ + +static struct cu * check_info_structural (struct section_data *data, + struct abbrev_table *abbrev_chain, + Elf_Data *strings); + +static bool check_aranges_structural (struct section_data *data, + struct cu *cu_chain); + +static bool check_pub_structural (struct section_data *data, + struct cu *cu_chain); + +static bool check_location_expression (struct read_ctx *ctx, + uint64_t init_off, + struct relocation_data *reloc, + size_t length, + struct where *wh, + bool addr_64); + +static bool check_loc_or_range_structural (struct section_data *data, + struct cu *cu_chain); + +static bool read_rel (struct section_data *secdata, + Elf_Data *reldata, + bool elf_64); + +static bool check_line_structural (struct section_data *data, + struct cu *cu_chain); + +const char * +where_fmt (const struct where *wh, char *ptr) +{ + if (wh == NULL) + return ""; + + static char buf[256]; + + struct section_info + { + const char *name; + const char *addr1n; + const char *addr1f; + const char *addr2n; + const char *addr2f; + const char *addr3n; + const char *addr3f; + }; + + static struct section_info section_names[] = + { + [sec_info] = {".debug_info", "CU", "%"PRId64, + "DIE", "%#"PRIx64, NULL, NULL}, + + [sec_abbrev] = {".debug_abbrev", "section", "%"PRId64, + "abbreviation", "%"PRId64, "abbr. attribute", "%#"PRIx64}, + + [sec_aranges] = {".debug_aranges", "table", "%"PRId64, + "arange", "%#"PRIx64, NULL, NULL}, + + [sec_pubnames] = {".debug_pubnames", "pubname table", "%"PRId64, + "pubname", "%#"PRIx64, NULL, NULL}, + + [sec_pubtypes] = {".debug_pubtypes", "pubtype table", "%"PRId64, + "pubtype", "%#"PRIx64, NULL, NULL}, + + [sec_str] = {".debug_str", "offset", "%#"PRIx64, + NULL, NULL, NULL, NULL}, + + [sec_line] = {".debug_line", "table", "%"PRId64, + "offset", "%#"PRIx64, NULL, NULL}, + + [sec_loc] = {".debug_loc", "loclist", "%#"PRIx64, + "offset", "%#"PRIx64, NULL, NULL}, + + [sec_mac] = {".debug_mac", NULL, NULL, NULL, NULL, NULL, NULL}, + + [sec_ranges] = {".debug_ranges", "rangelist", "%#"PRIx64, + "offset", "%#"PRIx64, NULL, NULL}, + + [sec_locexpr] = {"location expression", "offset", "%#"PRIx64, + NULL, NULL, NULL, NULL}, + + [sec_rel] = {".rel", "relocation", "%"PRId64, + "offset", "%#"PRIx64, NULL, NULL}, + [sec_rela] = {".rela", "relocation", "%"PRId64, + "offset", "%#"PRIx64, NULL, NULL}, + }; + + static struct section_info special_formats[] = + { + [wf_cudie] = {".debug_info", "CU DIE", "%"PRId64, NULL, NULL, NULL, NULL} + }; + + assert (wh->section < sizeof (section_names) / sizeof (*section_names)); + struct section_info *inf + = (wh->formatting == wf_plain) + ? section_names + wh->section + : special_formats + wh->formatting; + + assert (inf->name); + + assert ((inf->addr1n == NULL) == (inf->addr1f == NULL)); + assert ((inf->addr2n == NULL) == (inf->addr2f == NULL)); + assert ((inf->addr3n == NULL) == (inf->addr3f == NULL)); + + assert ((wh->addr1 != (uint64_t)-1) ? inf->addr1n != NULL : true); + assert ((wh->addr2 != (uint64_t)-1) ? inf->addr2n != NULL : true); + assert ((wh->addr3 != (uint64_t)-1) ? inf->addr3n != NULL : true); + + assert ((wh->addr3 != (uint64_t)-1) ? (wh->addr2 != (uint64_t)-1) : true); + assert ((wh->addr2 != (uint64_t)-1) ? (wh->addr1 != (uint64_t)-1) : true); + + /* GCC insists on checking format parameters and emits a warning + when we don't use string literal. With -Werror this ends up + being hard error. So instead we walk around this warning by + using function pointer. */ + int (*x_asprintf)(char **strp, const char *fmt, ...) = asprintf; + +#define SETUP_ADDR(N) \ + char *addr##N##s; \ + if (wh->addr##N == (uint64_t)-1) \ + addr##N##s = NULL; \ + else if (x_asprintf (&addr##N##s, inf->addr##N##f, wh->addr##N) < 0) \ + addr##N##s = "(fmt error)" + + SETUP_ADDR (1); + SETUP_ADDR (2); + SETUP_ADDR (3); +#undef SETUP_ADDR + + char *orig = ptr; + bool is_reloc = wh->section == sec_rel || wh->section == sec_rela; + if (ptr == NULL) + { + ptr = stpcpy (buf, inf->name); + if (is_reloc) + { + struct where *ref = wh->ref; + assert (ref != NULL); + if (ref->section == sec_locexpr) + { + ref = ref->next; + assert (ref != NULL); + assert (ref->section != sec_locexpr); + } + ptr = stpcpy (ptr, section_names[ref->section].name); + } + + if (addr1s != NULL) + ptr = stpcpy (ptr, ": "); + } + + if (addr3s != NULL) + ptr = stpcpy (stpcpy (stpcpy (ptr, inf->addr3n), " "), addr3s); + else if (addr2s != NULL) + ptr = stpcpy (stpcpy (stpcpy (ptr, inf->addr2n), " "), addr2s); + else if (addr1s != NULL) + ptr = stpcpy (stpcpy (stpcpy (ptr, inf->addr1n), " "), addr1s); + + if (wh->ref != NULL && !is_reloc) + { + ptr = stpcpy (ptr, " ("); + ptr = (char *)where_fmt (wh->ref, ptr); + *ptr++ = ')'; + *ptr = 0; + } + + if (orig == NULL) + return buf; + else + return ptr; +} + +void +where_fmt_chain (const struct where *wh, const char *severity) +{ + if (wh != NULL && show_refs) + for (struct where *it = wh->next; it != NULL; it = it->next) + printf ("%s: %s: caused by this reference.\n", + severity, where_fmt (it, NULL)); +} + +void +where_reset_1 (struct where *wh, uint64_t addr) +{ + wh->addr1 = addr; + wh->addr2 = wh->addr3 = (uint64_t)-1; +} + +void +where_reset_2 (struct where *wh, uint64_t addr) +{ + wh->addr2 = addr; + wh->addr3 = (uint64_t)-1; +} + +void +where_reset_3 (struct where *wh, uint64_t addr) +{ + wh->addr3 = addr; +} + +#define WHERE_SECDATA(DATA, PARENT) (WHERE (data_get_sec (DATA)->id, PARENT)) + +static void +process_file (Dwarf *dwarf, const char *fname, bool only_one) +{ + if (!only_one) + printf ("\n%s:\n", fname); + + struct elf_file file; + WIPE (file); + + file.dwarf = dwarf; + file.ebl = ebl_openbackend (dwarf->elf); + if (file.ebl == NULL) + goto invalid_elf; + if (gelf_getehdr (dwarf->elf, &file.ehdr) == NULL) + goto invalid_elf; + file.addr_64 = file.ehdr.e_ident[EI_CLASS] == ELFCLASS64; + +#define DEF_SECDATA(VAR, SEC) \ + struct section_data VAR = {&file, (size_t)-1, NULL, \ + {&file, NULL, SHT_NULL, NULL, 0, 0, 0}} + + DEF_SECDATA (abbrev_data, sec_abbrev); + DEF_SECDATA (aranges_data, sec_aranges); + DEF_SECDATA (info_data, sec_info); + DEF_SECDATA (line_data, sec_line); + DEF_SECDATA (loc_data, sec_loc); + DEF_SECDATA (pubnames_data, sec_pubnames); + DEF_SECDATA (pubtypes_data, sec_pubtypes); + DEF_SECDATA (ranges_data, sec_ranges); + DEF_SECDATA (str_data, sec_str); + +#undef DEF_SECDATA + + struct secinfo + { + const char *name; + struct section_data *dataptr; + Elf_Data *reldata; + enum section_id sec; + }; + struct secinfo secinfo[] = { +#define DEF_SECINFO(SEC) {".debug_" #SEC, &SEC##_data, NULL, sec_##SEC} +#define DEF_NOINFO(SEC) {".debug_" #SEC, NULL, NULL, sec_##SEC} + DEF_SECINFO (abbrev), + DEF_SECINFO (aranges), + DEF_SECINFO (info), + DEF_SECINFO (line), + DEF_SECINFO (loc), + DEF_SECINFO (pubnames), + DEF_SECINFO (pubtypes), + DEF_SECINFO (ranges), + DEF_SECINFO (str), +#undef DEF_NOINFO +#undef DEF_SECINFO + }; + + Elf_Scn *reloc_symtab = NULL; + + struct secinfo *find_secentry (const char *secname) + { + for (size_t i = 0; i < sizeof (secinfo) / sizeof (*secinfo); ++i) + if (strcmp (secinfo[i].name, secname) == 0) + return secinfo + i; + return NULL; + } + + struct section_data *find_secdata (const char *secname) + { + struct secinfo *info = find_secentry (secname); + if (info != NULL) + return info->dataptr; + else + return NULL; + } + + /* Now find all necessary debuginfo sections and associated + relocation sections. */ + + Elf_Scn *scn = NULL; + + /* Section 0 is special, skip it. */ + REALLOC (&file, sec); + file.sec[file.size++].id = sec_invalid; + + while ((scn = elf_nextscn (dwarf->elf, scn)) != NULL) + { + REALLOC (&file, sec); + size_t curndx = file.size++; + struct sec *cursec = file.sec + curndx; + + GElf_Shdr *shdr = gelf_getshdr (scn, &cursec->shdr); + if (shdr == NULL) + { + invalid_elf: + /* A "can't happen". libdw already managed to parse the Elf + file when constructing the Dwarf object. */ + wr_error (NULL, "Broken ELF.\n"); + goto skip_rel; + } + + const char *scnname = elf_strptr (dwarf->elf, file.ehdr.e_shstrndx, + shdr->sh_name); + if (scnname == NULL) + goto invalid_elf; + + struct secinfo *secentry = find_secentry (scnname); + struct section_data *secdata = secentry != NULL ? secentry->dataptr : NULL; + cursec->scn = scn; + cursec->id = secentry != NULL ? secentry->sec : sec_invalid; + cursec->name = scnname; + + if (secdata != NULL) + { + if (secdata->secndx == (size_t)-1) + { + secdata->data = elf_getdata (scn, NULL); + if (secdata->data == NULL || secdata->data->d_buf == NULL) + wr_error (NULL, "Data-less section %s.\n", scnname); + secdata->secndx = curndx; + } + else + wr_error (NULL, "Multiple occurrences of section %s.\n", scnname); + } + else if (shdr->sh_type == SHT_RELA || shdr->sh_type == SHT_REL) + { + /* Get data of section that this REL(A) section relocates. */ + Elf_Scn *relocated_scn = elf_getscn (dwarf->elf, shdr->sh_info); + Elf_Scn *symtab_scn = elf_getscn (dwarf->elf, shdr->sh_link); + if (relocated_scn == NULL || symtab_scn == NULL) + goto invalid_elf; + + GElf_Shdr relocated_shdr_mem; + GElf_Shdr *relocated_shdr = gelf_getshdr (relocated_scn, + &relocated_shdr_mem); + if (relocated_shdr == NULL) + goto invalid_elf; + + const char *relocated_scnname + = elf_strptr (dwarf->elf, file.ehdr.e_shstrndx, + relocated_shdr->sh_name); + + struct secinfo *relocated + = find_secentry (relocated_scnname); + + if (relocated != NULL && relocated->dataptr != NULL) + { + if (relocated->reldata != NULL) + wr_error (NULL, + "Several relocation sections for debug section %s." + " Ignoring %s.\n", + relocated_scnname, scnname); + else + { + relocated->reldata = elf_getdata (scn, NULL); + if (relocated->reldata == NULL) + wr_error (NULL, + "Data-less relocation section %s.\n", scnname); + relocated->dataptr->rel.type = shdr->sh_type; + } + + if (reloc_symtab == NULL) + reloc_symtab = symtab_scn; + else if (reloc_symtab != symtab_scn) + wr_error (NULL, + "Relocation sections use multiple symbol tables.\n"); + } + } + } + + Elf_Data *reloc_symdata = NULL; + if (reloc_symtab != NULL) + { + reloc_symdata = elf_getdata (reloc_symtab, NULL); + if (reloc_symdata == NULL) + { + wr_error (NULL, + "Couldn't obtain symtab data.\n"); + /* But carry on, we can check a lot of stuff even without + symbol table. */ + } + } + + /* Check relocation sections that we've got. */ + for (size_t i = 0; i < sizeof (secinfo) / sizeof (*secinfo); ++i) + { + struct secinfo *cur = secinfo + i; + if (cur->dataptr != NULL + && cur->reldata != NULL) + { + if (cur->dataptr->data == NULL) + wr_error (&WHERE_SECDATA (secinfo[i].dataptr, NULL), + ": this data-less section has a relocation section.\n"); + else if (read_rel (cur->dataptr, cur->reldata, file.addr_64)) + cur->dataptr->rel.symdata = reloc_symdata; + } + } + + if (str_data.rel.size > 0) + wr_message (mc_impact_2 | mc_elf, &WHERE (sec_str, NULL), + ": there's a relocation section associated with this section.\n"); + + skip_rel:; + struct abbrev_table *abbrev_chain = NULL; + struct cu *cu_chain = NULL; + struct read_ctx ctx; + struct hl_ctx *hlctx = hl_ctx_new (dwarf); + + /* If we got Dwarf pointer, .debug_abbrev and .debug_info are + present inside the file. But let's be paranoid. */ + if (likely (abbrev_data.data != NULL)) + { + read_ctx_init (&ctx, dwarf, abbrev_data.data); + abbrev_chain = abbrev_table_load (&ctx); + } + else if (!tolerate_nodebug) + /* Hard error, not a message. We can't debug without this. */ + wr_error (NULL, ".debug_abbrev data not found.\n"); + + if (abbrev_chain != NULL) + { + if (info_data.data != NULL) + { + cu_chain = check_info_structural (&info_data, abbrev_chain, + str_data.data); + if (cu_chain != NULL && do_high_level) + check_expected_trees (hlctx); + } + else if (!tolerate_nodebug) + /* Hard error, not a message. We can't debug without this. */ + wr_error (NULL, ".debug_info data not found.\n"); + } + + bool ranges_sound; + if (ranges_data.data != NULL && cu_chain != NULL) + ranges_sound = check_loc_or_range_structural (&ranges_data, cu_chain); + else + ranges_sound = false; + + if (loc_data.data != NULL && cu_chain != NULL) + check_loc_or_range_structural (&loc_data, cu_chain); + + if (aranges_data.data != NULL) + { + read_ctx_init (&ctx, dwarf, aranges_data.data); + if (check_aranges_structural (&aranges_data, cu_chain) + && ranges_sound && do_high_level) + check_matching_ranges (hlctx); + } + + if (pubnames_data.data != NULL) + check_pub_structural (&pubnames_data, cu_chain); + else + wr_message (mc_impact_4 | mc_acc_suboptimal | mc_elf, + &WHERE (sec_pubnames, NULL), ": data not found.\n"); + + if (pubtypes_data.data != NULL) + check_pub_structural (&pubtypes_data, cu_chain); + else + wr_message (mc_impact_4 | mc_acc_suboptimal | mc_elf | mc_pubtypes, + &WHERE (sec_pubtypes, NULL), ": data not found.\n"); + + if (line_data.data != NULL) + check_line_structural (&line_data, cu_chain); + else + wr_message (mc_impact_4 | mc_acc_suboptimal | mc_elf | mc_loc, + &WHERE (sec_line, NULL), ": data not found.\n"); + + cu_free (cu_chain); + abbrev_table_free (abbrev_chain); + if (file.ebl != NULL) + ebl_closebackend (file.ebl); + free (file.sec); + hl_ctx_delete (hlctx); +} + +static void +read_ctx_init (struct read_ctx *ctx, Dwarf *dbg, Elf_Data *data) +{ + if (data == NULL) + abort (); + + ctx->dbg = dbg; + ctx->data = data; + ctx->begin = data->d_buf; + ctx->end = data->d_buf + data->d_size; + ctx->ptr = data->d_buf; +} + +static bool +read_ctx_init_sub (struct read_ctx *ctx, struct read_ctx *parent, + const unsigned char *begin, const unsigned char *end) +{ + if (parent == NULL) + abort (); + + if (begin < parent->begin + || end > parent->end) + return false; + + ctx->dbg = parent->dbg; + ctx->data = parent->data; + ctx->begin = begin; + ctx->end = end; + ctx->ptr = begin; + return true; +} + +static uint64_t +read_ctx_get_offset (struct read_ctx *ctx) +{ + assert (ctx->ptr >= ctx->begin); + return (uint64_t)(ctx->ptr - ctx->begin); +} + +static bool +read_ctx_need_data (struct read_ctx *ctx, size_t length) +{ + const unsigned char *ptr = ctx->ptr + length; + return ptr <= ctx->end && (length == 0 || ptr > ctx->ptr); +} + +static bool +read_ctx_read_ubyte (struct read_ctx *ctx, unsigned char *ret) +{ + if (!read_ctx_need_data (ctx, 1)) + return false; + if (ret != NULL) + *ret = *ctx->ptr; + ctx->ptr++; + return true; +} + +static int +read_ctx_read_uleb128 (struct read_ctx *ctx, uint64_t *ret) +{ + uint64_t result = 0; + int shift = 0; + int size = 8 * sizeof (result); + bool zero_tail = false; + + while (1) + { + uint8_t byte; + if (!read_ctx_read_ubyte (ctx, &byte)) + return -1; + + uint8_t payload = byte & 0x7f; + zero_tail = payload == 0 && shift > 0; + result |= (uint64_t)payload << shift; + shift += 7; + if (shift > size && byte != 0x1) + return -1; + if ((byte & 0x80) == 0) + break; + } + + if (ret != NULL) + *ret = result; + return zero_tail ? 1 : 0; +} + +static bool +checked_read_uleb128 (struct read_ctx *ctx, uint64_t *ret, + struct where *where, const char *what) +{ + const unsigned char *ptr = ctx->ptr; + int st = read_ctx_read_uleb128 (ctx, ret); + if (st < 0) + wr_error (where, ": can't read %s.\n", what); + else if (st > 0) + { + char buf[19]; // 16 hexa digits, "0x", terminating zero + sprintf (buf, "%#" PRIx64, *ret); + wr_format_leb128_message (where, what, buf, ptr, ctx->ptr); + } + return st >= 0; +} + +static int +read_ctx_read_sleb128 (struct read_ctx *ctx, int64_t *ret) +{ + int64_t result = 0; + int shift = 0; + int size = 8 * sizeof (result); + bool zero_tail = false; + bool sign = false; + + while (1) + { + uint8_t byte; + if (!read_ctx_read_ubyte (ctx, &byte)) + return -1; + + uint8_t payload = byte & 0x7f; + zero_tail = shift > 0 && ((payload == 0x7f && sign) + || (payload == 0 && !sign)); + sign = (byte & 0x40) != 0; /* Set sign for rest of loop & next round. */ + result |= (int64_t)payload << shift; + shift += 7; + if ((byte & 0x80) == 0) + { + if (shift < size && sign) + result |= -((int64_t)1 << shift); + break; + } + if (shift > size) + return -1; + } + + if (ret != NULL) + *ret = result; + return zero_tail ? 1 : 0; +} + +static bool +checked_read_sleb128 (struct read_ctx *ctx, int64_t *ret, + struct where *where, const char *what) +{ + const unsigned char *ptr = ctx->ptr; + int st = read_ctx_read_sleb128 (ctx, ret); + if (st < 0) + wr_error (where, ": can't read %s.\n", what); + else if (st > 0) + { + char buf[20]; // sign, "0x", 16 hexa digits, terminating zero + int64_t val = *ret; + sprintf (buf, "%s%#" PRIx64, val < 0 ? "-" : "", val < 0 ? -val : val); + wr_format_leb128_message (where, what, buf, ptr, ctx->ptr); + } + return st >= 0; +} + +static bool +read_ctx_read_2ubyte (struct read_ctx *ctx, uint16_t *ret) +{ + if (!read_ctx_need_data (ctx, 2)) + return false; + uint16_t val = read_2ubyte_unaligned_inc (ctx->dbg, ctx->ptr); + if (ret != NULL) + *ret = val; + return true; +} + +static bool +read_ctx_read_4ubyte (struct read_ctx *ctx, uint32_t *ret) +{ + if (!read_ctx_need_data (ctx, 4)) + return false; + uint32_t val = read_4ubyte_unaligned_inc (ctx->dbg, ctx->ptr); + if (ret != NULL) + *ret = val; + return true; +} + +static bool +read_ctx_read_8ubyte (struct read_ctx *ctx, uint64_t *ret) +{ + if (!read_ctx_need_data (ctx, 8)) + return false; + uint64_t val = read_8ubyte_unaligned_inc (ctx->dbg, ctx->ptr); + if (ret != NULL) + *ret = val; + return true; +} + +static bool +read_ctx_read_offset (struct read_ctx *ctx, bool dwarf64, uint64_t *ret) +{ + if (dwarf64) + return read_ctx_read_8ubyte (ctx, ret); + + uint32_t v; + if (!read_ctx_read_4ubyte (ctx, &v)) + return false; + + if (ret != NULL) + *ret = (uint64_t)v; + return true; +} + +static bool +read_ctx_read_var (struct read_ctx *ctx, int width, uint64_t *ret) +{ + switch (width) + { + case 4: + case 8: + return read_ctx_read_offset (ctx, width == 8, ret); + case 2: + { + uint16_t val; + if (!read_ctx_read_2ubyte (ctx, &val)) + return false; + *ret = val; + return true; + } + case 1: + { + uint8_t val; + if (!read_ctx_read_ubyte (ctx, &val)) + return false; + *ret = val; + return true; + } + default: + return false; + }; +} + +/* The value passed back in uint64_t VALUEP may actually be + type-casted int64_t. WHAT and WHERE describe error message and + context for LEB128 loading. */ +static bool +read_ctx_read_form (struct read_ctx *ctx, bool addr_64, uint8_t form, + uint64_t *valuep, struct where *where, const char *what) +{ + switch (form) + { + case DW_FORM_addr: + return read_ctx_read_offset (ctx, addr_64, valuep); + case DW_FORM_udata: + return checked_read_uleb128 (ctx, valuep, where, what); + case DW_FORM_sdata: + return checked_read_sleb128 (ctx, (int64_t *)valuep, where, what); + case DW_FORM_data1: + { + uint8_t v; + if (!read_ctx_read_ubyte (ctx, &v)) + return false; + if (valuep != NULL) + *valuep = v; + return true; + } + case DW_FORM_data2: + { + uint16_t v; + if (!read_ctx_read_2ubyte (ctx, &v)) + return false; + if (valuep != NULL) + *valuep = v; + return true; + } + case DW_FORM_data4: + { + uint32_t v; + if (!read_ctx_read_4ubyte (ctx, &v)) + return false; + if (valuep != NULL) + *valuep = v; + return true; + } + case DW_FORM_data8: + return read_ctx_read_8ubyte (ctx, valuep); + }; + + return false; +} + +static const char * +read_ctx_read_str (struct read_ctx *ctx) +{ + const char *ret = (const char *)ctx->ptr; + uint8_t byte; + do + if (!read_ctx_read_ubyte (ctx, &byte)) + return NULL; + while (byte != 0); + return ret; +} + +static bool +read_ctx_skip (struct read_ctx *ctx, uint64_t len) +{ + if (!read_ctx_need_data (ctx, len)) + return false; + ctx->ptr += len; + return true; +} + +static bool +read_ctx_eof (struct read_ctx *ctx) +{ + return !read_ctx_need_data (ctx, 1); +} + +static bool +attrib_form_valid (uint64_t form) +{ + return form > 0 && form <= DW_FORM_indirect; +} + +static int +check_sibling_form (uint64_t form) +{ + switch (form) + { + case DW_FORM_indirect: + /* Tolerate this in abbrev loading, even during the DIE loading. + We check that dereferenced indirect form yields valid form. */ + case DW_FORM_ref1: + case DW_FORM_ref2: + case DW_FORM_ref4: + case DW_FORM_ref8: + case DW_FORM_ref_udata: + return 0; + + case DW_FORM_ref_addr: + return -1; + + default: + return -2; + }; +} + +/* Check that given form may in fact be valid in some CU. */ +static bool +check_abbrev_location_form (uint64_t form) +{ + switch (form) + { + case DW_FORM_indirect: + + /* loclistptr */ + case DW_FORM_data4: + case DW_FORM_data8: + + /* block */ + case DW_FORM_block1: + case DW_FORM_block2: + case DW_FORM_block4: + case DW_FORM_block: + return true; + + default: + return false; + }; +} + +static bool +is_location_attrib (uint64_t name) +{ + switch (name) + { + case DW_AT_location: + case DW_AT_frame_base: + case DW_AT_data_location: + case DW_AT_data_member_location: + return true; + default: + return false; + } +} + +static struct abbrev_table * +abbrev_table_load (struct read_ctx *ctx) +{ + struct abbrev_table *section_chain = NULL; + struct abbrev_table *section = NULL; + uint64_t first_attr_off = 0; + struct where where = WHERE (sec_abbrev, NULL); + where.addr1 = 0; + + while (!read_ctx_eof (ctx)) + { + uint64_t abbr_off; + uint64_t abbr_code; + { + uint64_t prev_abbr_off = (uint64_t)-1; + uint64_t prev_abbr_code = (uint64_t)-1; + uint64_t zero_seq_off = (uint64_t)-1; + + while (!read_ctx_eof (ctx)) + { + abbr_off = read_ctx_get_offset (ctx); + where_reset_2 (&where, abbr_off); + + /* Abbreviation code. */ + if (!checked_read_uleb128 (ctx, &abbr_code, &where, "abbrev code")) + goto free_and_out; + + if (abbr_code == 0 && prev_abbr_code == 0 + && zero_seq_off == (uint64_t)-1) + zero_seq_off = abbr_off; + + if (abbr_code != 0) + break; + else + section = NULL; + + prev_abbr_code = abbr_code; + prev_abbr_off = abbr_off; + } + + if (zero_seq_off != (uint64_t)-1) + { + /* Don't report abbrev address, this is section-wide + padding. */ + struct where wh = WHERE (where.section, NULL); + wr_message_padding_0 (mc_abbrevs | mc_header, &wh, + zero_seq_off, abbr_off - 1); + } + } + + if (read_ctx_eof (ctx)) + break; + + if (section == NULL) + { + section = xcalloc (1, sizeof (*section)); + section->offset = abbr_off; + section->next = section_chain; + section_chain = section; + + where_reset_1 (&where, abbr_off); + where_reset_2 (&where, abbr_off); + } + REALLOC (section, abbr); + + struct abbrev *cur = section->abbr + section->size++; + WIPE (*cur); + + cur->code = abbr_code; + cur->where = where; + + /* Abbreviation tag. */ + uint64_t abbr_tag; + if (!checked_read_uleb128 (ctx, &abbr_tag, &where, "abbrev tag")) + goto free_and_out; + + if (abbr_tag > DW_TAG_hi_user) + { + wr_error (&where, ": invalid abbrev tag 0x%" PRIx64 ".\n", abbr_tag); + goto free_and_out; + } + cur->tag = (typeof (cur->tag))abbr_tag; + + /* Abbreviation has_children. */ + uint8_t has_children; + if (!read_ctx_read_ubyte (ctx, &has_children)) + { + wr_error (&where, ": can't read abbrev has_children.\n"); + goto free_and_out; + } + + if (has_children != DW_CHILDREN_no + && has_children != DW_CHILDREN_yes) + { + wr_error (&where, + ": invalid has_children value 0x%x.\n", cur->has_children); + goto free_and_out; + } + cur->has_children = has_children == DW_CHILDREN_yes; + + bool null_attrib; + uint64_t sibling_attr = 0; + bool low_pc = false, high_pc = false; + do + { + uint64_t attr_off = read_ctx_get_offset (ctx); + uint64_t attrib_name, attrib_form; + if (first_attr_off == 0) + first_attr_off = attr_off; + /* Shift to match elfutils reporting. */ + where_reset_3 (&where, attr_off - first_attr_off); + + /* Load attribute name and form. */ + if (!checked_read_uleb128 (ctx, &attrib_name, &where, + "attribute name")) + goto free_and_out; + + if (!checked_read_uleb128 (ctx, &attrib_form, &where, + "attribute form")) + goto free_and_out; + + null_attrib = attrib_name == 0 && attrib_form == 0; + + /* Now if both are zero, this was the last attribute. */ + if (!null_attrib) + { + /* Otherwise validate name and form. */ + if (attrib_name > DW_AT_hi_user) + { + wr_error (&where, + ": invalid name 0x%" PRIx64 ".\n", attrib_name); + goto free_and_out; + } + + if (!attrib_form_valid (attrib_form)) + { + wr_error (&where, + ": invalid form 0x%" PRIx64 ".\n", attrib_form); + goto free_and_out; + } + } + + REALLOC (cur, attribs); + + struct abbrev_attrib *acur = cur->attribs + cur->size++; + WIPE (*acur); + + /* We do structural checking of sibling attribute, so make + sure our assumptions in actual DIE-loading code are + right. We expect at most one DW_AT_sibling attribute, + with form from reference class, but only CU-local, not + DW_FORM_ref_addr. */ + if (attrib_name == DW_AT_sibling) + { + if (sibling_attr != 0) + wr_error (&where, + ": Another DW_AT_sibling attribute in one abbreviation. " + "(First was 0x%" PRIx64 ".)\n", sibling_attr); + else + { + assert (attr_off > 0); + sibling_attr = attr_off; + + if (!cur->has_children) + wr_message (mc_die_rel | mc_acc_bloat | mc_impact_1, + &where, + ": Excessive DW_AT_sibling attribute at childless abbrev.\n"); + } + + switch (check_sibling_form (attrib_form)) + { + case -1: + wr_message (mc_die_rel | mc_impact_2, &where, + ": DW_AT_sibling attribute with form DW_FORM_ref_addr.\n"); + break; + + case -2: + wr_error (&where, + ": DW_AT_sibling attribute with non-reference form \"%s\".\n", + dwarf_form_string (attrib_form)); + }; + } + /* Similar for DW_AT_location and friends. */ + else if (is_location_attrib (attrib_name)) + { + if (!check_abbrev_location_form (attrib_form)) + wr_error (&where, + ": %s with invalid form \"%s\".\n", + dwarf_attr_string (attrib_name), + dwarf_form_string (attrib_form)); + } + /* Similar for DW_AT_ranges. */ + else if (attrib_name == DW_AT_ranges + || attrib_name == DW_AT_stmt_list) + { + if (attrib_form != DW_FORM_data4 + && attrib_form != DW_FORM_data8 + && attrib_form != DW_FORM_indirect) + wr_error (&where, + ": %s with invalid form \"%s\".\n", + dwarf_attr_string (attrib_name), + dwarf_form_string (attrib_form)); + } + /* Similar for DW_AT_{low,high}_pc, plus also make sure we + don't see high_pc without low_pc. */ + else if (attrib_name == DW_AT_low_pc + || attrib_name == DW_AT_high_pc) + { + if (attrib_form != DW_FORM_addr + && attrib_form != DW_FORM_ref_addr) + wr_error (&where, + ": %s with invalid form \"%s\".\n", + dwarf_attr_string (attrib_name), + dwarf_form_string (attrib_form)); + + if (attrib_name == DW_AT_low_pc) + low_pc = true; + else if (attrib_name == DW_AT_high_pc) + high_pc = true; + } + + acur->name = attrib_name; + acur->form = attrib_form; + acur->where = where; + } + while (!null_attrib); + + where_reset_2 (&where, where.addr2); // drop addr 3 + if (high_pc && !low_pc) + wr_error (&where, + ": the abbrev has DW_AT_high_pc without also having DW_AT_low_pc.\n"); + } + + for (section = section_chain; section != NULL; section = section->next) + { + int cmp_abbrs (const void *a, const void *b) + { + struct abbrev *aa = (struct abbrev *)a; + struct abbrev *bb = (struct abbrev *)b; + return aa->code - bb->code; + } + + /* The array is most likely already sorted in the file, but just + to be sure... */ + qsort (section->abbr, section->size, sizeof (*section->abbr), cmp_abbrs); + } + + return section_chain; + + free_and_out: + abbrev_table_free (section_chain); + return NULL; +} + +static void +abbrev_table_free (struct abbrev_table *abbr) +{ + for (struct abbrev_table *it = abbr; it != NULL; ) + { + for (size_t i = 0; i < it->size; ++i) + free (it->abbr[i].attribs); + free (it->abbr); + + struct abbrev_table *temp = it; + it = it->next; + free (temp); + } +} + +static struct abbrev * +abbrev_table_find_abbrev (struct abbrev_table *abbrevs, uint64_t abbrev_code) +{ + size_t a = 0; + size_t b = abbrevs->size; + struct abbrev *ab = NULL; + + while (a < b) + { + size_t i = (a + b) / 2; + ab = abbrevs->abbr + i; + + if (ab->code > abbrev_code) + b = i; + else if (ab->code < abbrev_code) + a = i + 1; + else + return ab; + } + + return NULL; +} + +static size_t +addr_record_find_addr (struct addr_record *ar, uint64_t addr) +{ + size_t a = 0; + size_t b = ar->size; + + while (a < b) + { + size_t i = (a + b) / 2; + uint64_t v = ar->addrs[i]; + + if (v > addr) + b = i; + else if (v < addr) + a = i + 1; + else + return i; + } + + return a; +} + +static bool +addr_record_has_addr (struct addr_record *ar, uint64_t addr) +{ + if (ar->size == 0 + || addr < ar->addrs[0] + || addr > ar->addrs[ar->size - 1]) + return false; + + size_t a = addr_record_find_addr (ar, addr); + return a < ar->size && ar->addrs[a] == addr; +} + +static void +addr_record_add (struct addr_record *ar, uint64_t addr) +{ + size_t a = addr_record_find_addr (ar, addr); + if (a >= ar->size || ar->addrs[a] != addr) + { + REALLOC (ar, addrs); + size_t len = ar->size - a; + memmove (ar->addrs + a + 1, ar->addrs + a, len * sizeof (*ar->addrs)); + + ar->addrs[a] = addr; + ar->size++; + } +} + +static void +addr_record_free (struct addr_record *ar) +{ + if (ar != NULL) + free (ar->addrs); +} + + +static void +ref_record_add (struct ref_record *rr, uint64_t addr, struct where *referrer) +{ + REALLOC (rr, refs); + struct ref *ref = rr->refs + rr->size++; + ref->addr = addr; + ref->who = *referrer; +} + +static void +ref_record_free (struct ref_record *rr) +{ + if (rr != NULL) + free (rr->refs); +} + +bool +coverage_pristine (struct coverage *cov, uint64_t begin, uint64_t length) +{ + for (uint64_t i = 0; i < length; ++i) + if (coverage_is_covered (cov, begin + i)) + return false; + return true; +} + +bool +found_hole (uint64_t start, uint64_t length, void *data) +{ + struct hole_info *info = (struct hole_info *)data; + bool all_zeroes = true; + for (uint64_t i = start; i < start + length; ++i) + if (((char*)info->data)[i] != 0) + { + all_zeroes = false; + break; + } + + uint64_t end = start + length; + if (all_zeroes) + { + /* Zero padding is valid, if it aligns on the bounds of + info->align bytes, and is not excessive. */ + if (!(info->align != 0 && info->align != 1 + && (end % info->align == 0) && (start % 4 != 0) + && (length < info->align))) + wr_message_padding_0 (info->category, &WHERE (info->section, NULL), + start, end - 1); + } + else + /* XXX: This actually lies when the unreferenced portion is + composed of sequences of zeroes and non-zeroes. */ + wr_message_padding_n0 (info->category, &WHERE (info->section, NULL), + start, end - 1); + + return true; +} + +bool +coverage_map_found_hole (uint64_t begin, uint64_t end, + struct section_coverage *sco, void *user) +{ + struct coverage_map_hole_info *info = (struct coverage_map_hole_info *)user; + + struct where where = WHERE (info->info.section, NULL); + const char *scnname = sco->sec->name; + + Elf_Data *data = elf_getdata (sco->sec->scn, NULL); + if (data == NULL) + { + wr_error (&where, ": couldn't read the data of section %s.\n", scnname); + return false; + } + + /* We don't expect some sections to be covered. But if they + are at least partially covered, we expect the same + coverage criteria as for .text. */ + if (!sco->hit + && ((sco->sec->shdr.sh_flags & SHF_EXECINSTR) == 0 + || strcmp (scnname, ".init") == 0 + || strcmp (scnname, ".fini") == 0 + || strcmp (scnname, ".plt") == 0)) + return true; + + /* For REL files, don't print addresses mangled by our layout. */ + uint64_t base = info->elf->ehdr.e_type == ET_REL ? 0 : sco->sec->shdr.sh_addr; + + /* If we get stripped debuginfo file, the data simply may not be + available. In that case simply report the hole. */ + if (data->d_buf != NULL) + { + bool zeroes = true; + for (uint64_t j = begin; j < end; ++j) + if (((char *)data->d_buf)[j] != 0) + { + zeroes = false; + break; + } + if (!zeroes) + return true; + } + + wr_message (info->info.category | mc_acc_suboptimal | mc_impact_4, &where, + ": addresses %#" PRIx64 "..%#" PRIx64 + " of section %s are not covered.\n", + begin + base, end + base, scnname); + return true; +} + + +void +section_coverage_init (struct section_coverage *sco, + struct sec *sec, bool warn) +{ + assert (sco != NULL); + assert (sec != NULL); + + sco->sec = sec; + WIPE (sco->cov); + sco->hit = false; + sco->warn = warn; +} + +bool +coverage_map_init (struct coverage_map *coverage_map, + struct elf_file *elf, + Elf64_Xword mask, + Elf64_Xword warn_mask, + bool allow_overlap) +{ + assert (coverage_map != NULL); + assert (elf != NULL); + + WIPE (*coverage_map); + coverage_map->elf = elf; + coverage_map->allow_overlap = allow_overlap; + + for (size_t i = 0; i < elf->size; ++i) + { + struct sec *sec = elf->sec + i; + + bool normal = (sec->shdr.sh_flags & mask) == mask; + bool warn = (sec->shdr.sh_flags & warn_mask) == warn_mask; + if (normal || warn) + { + REALLOC (coverage_map, scos); + section_coverage_init + (coverage_map->scos + coverage_map->size++, sec, !normal); + } + } + + return true; +} + +void +coverage_map_add (struct coverage_map *coverage_map, + uint64_t address, + uint64_t length, + struct where *where, + enum message_category cat) +{ + bool found = false; + bool crosses_boundary = false; + bool overlap = false; + uint64_t end = address + length; + + /* This is for analyzing how much of the current range falls into + sections in coverage map. Whatever is left uncovered doesn't + fall anywhere and is reported. */ + struct coverage range_cov; + WIPE (range_cov); + + for (size_t i = 0; i < coverage_map->size; ++i) + { + struct section_coverage *sco = coverage_map->scos + i; + GElf_Shdr *shdr = &sco->sec->shdr; + struct coverage *cov = &sco->cov; + + Elf64_Addr s_end = shdr->sh_addr + shdr->sh_size; + if (end <= shdr->sh_addr || address >= s_end) + /* no overlap */ + continue; + + if (found && !crosses_boundary) + { + /* While probably not an error, it's very suspicious. */ + wr_message (cat | mc_impact_2, where, + ": the range %#" PRIx64 "..%#" PRIx64 + " crosses section boundaries.\n", + address, end); + crosses_boundary = true; + } + + found = true; + + if (length == 0) + /* Empty range. That means no actual coverage, and we can + also be sure that there are no more sections that this one + falls into. */ + break; + + uint64_t cov_begin + = address < shdr->sh_addr ? 0 : address - shdr->sh_addr; + uint64_t cov_end + = end < s_end ? end - shdr->sh_addr : shdr->sh_size; + assert (cov_begin < cov_end); + + uint64_t r_delta = shdr->sh_addr - address; + uint64_t r_cov_begin = cov_begin + r_delta; + uint64_t r_cov_end = cov_end + r_delta; + + if (!overlap && !coverage_map->allow_overlap + && !coverage_pristine (cov, cov_begin, cov_end - cov_begin)) + { + /* Not a show stopper, this shouldn't derail high-level. */ + wr_message (cat | mc_impact_2 | mc_error, where, + ": the range %#" PRIx64 "..%#" PRIx64 + " overlaps with another one.\n", + address, end); + overlap = true; + } + + if (sco->warn) + wr_message (cat | mc_impact_2, where, + ": the range %#" PRIx64 "..%#" PRIx64 + " covers section %s.\n", + address, end, sco->sec->name); + + /* Section coverage... */ + coverage_add (cov, cov_begin, cov_end - cov_begin); + sco->hit = true; + + /* And range coverage... */ + coverage_add (&range_cov, r_cov_begin, r_cov_end - r_cov_begin); + } + + if (!found) + /* Not a show stopper. */ + wr_error (where, + ": couldn't find a section that the range %#" + PRIx64 "..%#" PRIx64 " covers.\n", address, end); + else if (length > 0) + { + bool range_hole (uint64_t h_start, uint64_t h_length, + void *user __attribute__ ((unused))) + { + assert (h_length != 0); + wr_error (where, + ": portion %#" PRIx64 "..%#" PRIx64 + ", of the range %#" PRIx64 "..%#" PRIx64 + " doesn't fall into any ALLOC section.\n", + h_start + address, h_start + address + h_length - 1, + address, end); + return true; + } + coverage_find_holes (&range_cov, 0, length, range_hole, NULL); + } + + coverage_free (&range_cov); +} + +bool +coverage_map_find_holes (struct coverage_map *coverage_map, + bool (*cb) (uint64_t begin, uint64_t end, + struct section_coverage *, void *), + void *user) +{ + for (size_t i = 0; i < coverage_map->size; ++i) + { + struct section_coverage *sco = coverage_map->scos + i; + + bool wrap_cb (uint64_t h_start, uint64_t h_length, void *h_user) + { + return cb (h_start, h_start + h_length - 1, sco, h_user); + } + + if (!coverage_find_holes (&sco->cov, 0, sco->sec->shdr.sh_size, + wrap_cb, user)) + return false; + } + + return true; +} + +void +coverage_map_free (struct coverage_map *coverage_map) +{ + for (size_t i = 0; i < coverage_map->size; ++i) + coverage_free (&coverage_map->scos[i].cov); + free (coverage_map->scos); +} + + +static void +cu_free (struct cu *cu_chain) +{ + for (struct cu *it = cu_chain; it != NULL; ) + { + addr_record_free (&it->die_addrs); + + struct cu *temp = it; + it = it->next; + free (temp); + } +} + +static struct cu * +cu_find_cu (struct cu *cu_chain, uint64_t offset) +{ + for (struct cu *it = cu_chain; it != NULL; it = it->next) + if (it->offset == offset) + return it; + return NULL; +} + + +static bool +check_die_references (struct cu *cu, + struct ref_record *die_refs) +{ + bool retval = true; + for (size_t i = 0; i < die_refs->size; ++i) + { + struct ref *ref = die_refs->refs + i; + if (!addr_record_has_addr (&cu->die_addrs, ref->addr)) + { + wr_error (&ref->who, + ": unresolved reference to " PRI_DIE ".\n", ref->addr); + retval = false; + } + } + return retval; +} + +static bool +check_global_die_references (struct cu *cu_chain) +{ + bool retval = true; + for (struct cu *it = cu_chain; it != NULL; it = it->next) + for (size_t i = 0; i < it->die_refs.size; ++i) + { + struct ref *ref = it->die_refs.refs + i; + struct cu *ref_cu = NULL; + for (struct cu *jt = cu_chain; jt != NULL; jt = jt->next) + if (addr_record_has_addr (&jt->die_addrs, ref->addr)) + { + ref_cu = jt; + break; + } + + if (ref_cu == NULL) + { + wr_error (&ref->who, + ": unresolved (non-CU-local) reference to " PRI_DIE ".\n", + ref->addr); + retval = false; + } + else if (ref_cu == it) + /* This is technically not a problem, so long as the + reference is valid, which it is. But warn about this + anyway, perhaps local reference could be formed on fewer + number of bytes. */ + wr_message (mc_impact_2 | mc_acc_suboptimal | mc_die_rel, + &ref->who, + ": local reference to " PRI_DIE " formed as global.\n", + ref->addr); + } + + return retval; +} + +static bool +read_size_extra (struct read_ctx *ctx, uint32_t size32, uint64_t *sizep, + bool *dwarf_64p, struct where *wh) +{ + if (size32 == DWARF3_LENGTH_64_BIT) + { + if (!read_ctx_read_8ubyte (ctx, sizep)) + { + wr_error (wh, ": can't read 64bit CU length.\n"); + return false; + } + + *dwarf_64p = true; + } + else if (size32 >= DWARF3_LENGTH_MIN_ESCAPE_CODE) + { + wr_error (wh, ": unrecognized CU length escape value: " + "%" PRIx32 ".\n", size32); + return false; + } + else + { + *sizep = size32; + *dwarf_64p = false; + } + + return true; +} + +static bool +check_zero_padding (struct read_ctx *ctx, + enum message_category category, + struct where *wh) +{ + assert (ctx->ptr != ctx->end); + const unsigned char *save_ptr = ctx->ptr; + while (!read_ctx_eof (ctx)) + if (*ctx->ptr++ != 0) + { + ctx->ptr = save_ptr; + return false; + } + + wr_message_padding_0 (category, wh, + (uint64_t)(save_ptr - ctx->begin), + (uint64_t)(ctx->end - ctx->begin)); + return true; +} + +static struct where +where_from_reloc (struct relocation_data *reloc, struct where *ref) +{ + struct where where + = WHERE (reloc->type == SHT_REL ? sec_rel : sec_rela, NULL); + where_reset_1 (&where, reloc->rel[reloc->index].offset); + where.ref = ref; + return where; +} + +enum skip_type +{ + skip_unref = 0, + skip_mismatched = 1, + skip_ok, +}; + +static struct relocation * +relocation_next (struct relocation_data *reloc, uint64_t offset, + struct where *where, enum skip_type st) +{ + if (reloc == NULL || reloc->rel == NULL) + return NULL; + + while (reloc->index < reloc->size) + { + struct where reloc_where = where_from_reloc (reloc, where); + + struct relocation *rel = reloc->rel + reloc->index; + where_reset_2 (&reloc_where, rel->offset); + + /* This relocation entry is ahead of us. */ + if (rel->offset > offset) + return NULL; + + reloc->index++; + + if (rel->invalid) + continue; + + if (rel->offset < offset) + { + if (st != skip_ok) + { + void (*w) (const struct where *, const char *, ...) = wr_error; + (*w) (&reloc_where, + ((const char *[]) + {": relocation targets unreferenced portion of the section.\n", + ": relocation is mismatched.\n"})[st]); + } + continue; + } + + return rel; + } + + return NULL; +} + +/* Skip all relocation up to offset, and leave cursor pointing at that + relocation, so that next time relocation_next is called, relocation + matching that offset is immediately yielded. */ +static void +relocation_skip (struct relocation_data *reloc, uint64_t offset, + struct where *where, enum skip_type st) +{ + if (reloc != NULL && reloc->rel != NULL) + relocation_next (reloc, offset - 1, where, st); +} + +/* Skip all the remaining relocations. */ +static void +relocation_skip_rest (struct section_data *data) +{ + if (data->rel.rel != NULL) + relocation_next (&data->rel, (uint64_t)-1, &WHERE_SECDATA (data, NULL), + skip_mismatched); +} + +/* SYMPTR may be NULL, otherwise (**SYMPTR) has to yield valid memory + location. When the function returns, (*SYMPTR) is either NULL, in + which case we failed or didn't get around to obtain the symbol from + symbol table, or non-NULL, in which case the symbol was initialized. */ +static void +relocate_one (struct relocation_data *reloc, struct relocation *rel, + unsigned width, uint64_t *value, struct where *where, + enum section_id offset_into, GElf_Sym **symptr) +{ + if (rel->invalid) + return; + + struct where reloc_where = where_from_reloc (reloc, where); + where_reset_2 (&reloc_where, rel->offset); + struct where reloc_ref_where = reloc_where; + reloc_ref_where.next = where; + + GElf_Sym symbol_mem, *symbol; + if (symptr != NULL) + { + symbol = *symptr; + *symptr = NULL; + } + else + symbol = &symbol_mem; + + if (offset_into == sec_invalid) + { + wr_message (mc_impact_3 | mc_reloc, &reloc_ref_where, + ": relocates a datum that shouldn't be relocated.\n"); + return; + } + + Elf_Type type = ebl_reloc_simple_type (reloc->file->ebl, rel->type); + + unsigned rel_width; + switch (type) + { + case ELF_T_BYTE: + rel_width = 1; + break; + + case ELF_T_HALF: + rel_width = 2; + break; + + case ELF_T_WORD: + case ELF_T_SWORD: + rel_width = 4; + break; + + case ELF_T_XWORD: + case ELF_T_SXWORD: + rel_width = 8; + break; + + default: + /* This has already been diagnosed during the isolated + validation of relocation section. */ + return; + }; + + if (rel_width != width) + wr_error (&reloc_ref_where, + ": %d-byte relocation relocates %d-byte datum.\n", + rel_width, width); + + /* Tolerate that we might have failed to obtain a symbol table. */ + if (reloc->symdata != NULL) + { + symbol = gelf_getsym (reloc->symdata, rel->symndx, symbol); + if (symptr != NULL) + *symptr = symbol; + if (symbol == NULL) + { + wr_error (&reloc_where, + ": couldn't obtain symbol #%d: %s.\n", + rel->symndx, elf_errmsg (-1)); + return; + } + + uint64_t section_index = symbol->st_shndx; + /* XXX We should handle SHN_XINDEX here. Or, instead, maybe it + would be possible to use dwfl, which already does XINDEX + translation. */ + + /* For ET_REL files, we do section layout manually. But we + don't update symbol table doing that. So instead of looking + at symbol value, look at section address. */ + uint64_t sym_value = symbol->st_value; + if (reloc->file->ehdr.e_type == ET_REL + && ELF64_ST_TYPE (symbol->st_info) == STT_SECTION) + { + assert (sym_value == 0); + sym_value = reloc->file->sec[section_index].shdr.sh_addr; + } + + /* It's target value, not section offset. */ + if (offset_into == rel_value + || offset_into == rel_address + || offset_into == rel_exec) + { + /* If a target value is what's expected, then complain if + it's not either SHN_ABS, an SHF_ALLOC section, or + SHN_UNDEF. For data forms of address_size, an SHN_UNDEF + reloc is acceptable, otherwise reject it. */ + if (!(section_index == SHN_ABS + || (offset_into == rel_address + && (section_index == SHN_UNDEF + || section_index == SHN_COMMON)))) + { + Elf_Scn *scn; + GElf_Shdr shdr_mem, *shdr; + if (offset_into != rel_address && section_index == SHN_UNDEF) + wr_error (&reloc_where, + ": relocation of an address is formed against SHN_UNDEF symbol" + " (symtab index %d).\n", rel->symndx); + else if ((scn = elf_getscn (reloc->file->dwarf->elf, + section_index)) == NULL) + wr_error (&reloc_where, + ": couldn't obtain associated section #%" PRId64 ".\n", + section_index); + else if ((shdr = gelf_getshdr (scn, &shdr_mem)) == NULL) + wr_error (&reloc_where, + ": couldn't obtain header of associated section #%" PRId64 ".\n", + section_index); + else + { + if ((shdr->sh_flags & SHF_ALLOC) != SHF_ALLOC) + wr_message (mc_reloc | mc_impact_3, &reloc_where, + ": associated section %s isn't SHF_ALLOC.\n", + reloc->file->sec[section_index].name); + if (offset_into == rel_exec + && (shdr->sh_flags & SHF_EXECINSTR) != SHF_EXECINSTR) + /* This may still be kosher, but it's suspicious. */ + wr_message (mc_reloc | mc_impact_2, &reloc_where, + ": relocation against %s is suspicious, expected executable section.\n", + reloc->file->sec[section_index].name); + } + } + } + else + { + enum section_id id; + /* If symtab[symndx].st_shndx does not match the expected + debug section's index, complain. */ + if (section_index >= reloc->file->size) + wr_error (&reloc_where, + ": invalid associated section #%" PRId64 ".\n", + section_index); + else if ((id = reloc->file->sec[section_index].id) != offset_into) + { + char *wh1 = id != sec_invalid + ? strdup (where_fmt (&WHERE (id, NULL), NULL)) + : (char *)reloc->file->sec[section_index].name; + char *wh2 = strdup (where_fmt (&WHERE (offset_into, NULL), NULL)); + wr_error (&reloc_where, + ": relocation references section %s, but %s was expected.\n", + wh1, wh2); + free (wh2); + if (id != sec_invalid) + free (wh1); + } + } + + /* Only do the actual relocation if we have ET_REL files. For + non-ET_REL files, only do the above checking. */ + if (reloc->file->ehdr.e_type == ET_REL) + { + *value = rel->addend + sym_value; + if (rel_width == 4) + *value = *value & (uint64_t)(uint32_t)-1; + } + } +} + +static enum section_id +reloc_target (uint8_t form, struct abbrev_attrib *at) +{ + switch (form) + { + case DW_FORM_strp: + return sec_str; + + case DW_FORM_addr: + + switch (at->name) + { + case DW_AT_low_pc: + case DW_AT_high_pc: + case DW_AT_entry_pc: + return rel_exec; + + case DW_AT_const_value: + /* Appears in some kernel modules. It's not allowed by the + standard, but leave that for high-level checks. */ + return rel_address; + }; + + break; + + case DW_FORM_ref_addr: + return sec_info; + + case DW_FORM_data1: + case DW_FORM_data2: + /* While these are technically legal, they are never used in + DWARF sections. So better mark them as illegal, and have + dwarflint flag them. */ + return sec_invalid; + + case DW_FORM_data4: + case DW_FORM_data8: + + switch (at->name) + { + case DW_AT_stmt_list: + return sec_line; + + case DW_AT_location: + case DW_AT_string_length: + case DW_AT_return_addr: + case DW_AT_data_member_location: + case DW_AT_frame_base: + case DW_AT_segment: + case DW_AT_static_link: + case DW_AT_use_location: + case DW_AT_vtable_elem_location: + return sec_loc; + + case DW_AT_mac_info: + return sec_mac; + + case DW_AT_ranges: + return sec_ranges; + } + + break; + + case DW_FORM_string: + case DW_FORM_ref1: + case DW_FORM_ref2: + case DW_FORM_ref4: + /* Shouldn't be relocated. */ + return sec_invalid; + + case DW_FORM_sdata: + case DW_FORM_udata: + case DW_FORM_flag: + case DW_FORM_ref_udata: + assert (!"Can't be relocated!"); + + case DW_FORM_block1: + case DW_FORM_block2: + case DW_FORM_block4: + case DW_FORM_block: + assert (!"Should be handled specially!"); + }; + + printf ("XXX don't know how to handle form=%s, at=%s\n", + dwarf_form_string (form), dwarf_attr_string (at->name)); + + return rel_value; +} + +static enum section_id +reloc_target_loc (uint8_t opcode) +{ + switch (opcode) + { + case DW_OP_call2: + case DW_OP_call4: + return sec_info; + + case DW_OP_addr: + return rel_address; + + case DW_OP_call_ref: + assert (!"Can't handle call_ref!"); + }; + + printf ("XXX don't know how to handle opcode=%s\n", + dwarf_locexpr_opcode_string (opcode)); + + return rel_value; +} + +static bool +supported_version (unsigned version, + size_t num_supported, struct where *where, ...) +{ + bool retval = false; + va_list ap; + va_start (ap, where); + for (size_t i = 0; i < num_supported; ++i) + { + unsigned v = va_arg (ap, unsigned); + if (version == v) + { + retval = true; + break; + } + } + va_end (ap); + + if (!retval) + wr_error (where, ": unsupported version %d.\n", version); + + return retval; +} + +static void +check_range_relocations (enum message_category cat, + struct where *where, + struct elf_file *file, + GElf_Sym *begin_symbol, + GElf_Sym *end_symbol, + const char *description) +{ + if (begin_symbol != NULL + && end_symbol != NULL + && begin_symbol->st_shndx != end_symbol->st_shndx) + wr_message (cat | mc_impact_2 | mc_reloc, where, + ": %s relocated against different sections (%s and %s).\n", + description, + file->sec[begin_symbol->st_shndx].name, + file->sec[end_symbol->st_shndx].name); +} + +/* + Returns: + -1 in case of error + +0 in case of no error, but the chain only consisted of a + terminating zero die. + +1 in case some dies were actually loaded + */ +static int +read_die_chain (struct read_ctx *ctx, + struct cu *cu, + struct abbrev_table *abbrevs, + Elf_Data *strings, + bool dwarf_64, bool addr_64, + struct ref_record *local_die_refs, + struct coverage *strings_coverage, + struct relocation_data *reloc, + struct elf_file *file) +{ + bool got_die = false; + uint64_t sibling_addr = 0; + uint64_t die_off, prev_die_off = 0; + struct abbrev *abbrev, *prev_abbrev = NULL; + struct where where = WHERE (sec_info, NULL); + + while (!read_ctx_eof (ctx)) + { + where = cu->where; + die_off = read_ctx_get_offset (ctx); + /* Shift reported DIE offset by CU offset, to match the way + readelf reports DIEs. */ + where_reset_2 (&where, die_off + cu->offset); + + uint64_t abbr_code; + + prev_die_off = die_off; + if (!checked_read_uleb128 (ctx, &abbr_code, &where, "abbrev code")) + return -1; + + /* Check sibling value advertised last time through the loop. */ + if (sibling_addr != 0) + { + if (abbr_code == 0) + wr_error (&where, + ": is the last sibling in chain, but has a DW_AT_sibling attribute.\n"); + else if (sibling_addr != die_off) + wr_error (&where, ": This DIE should have had its sibling at 0x%" + PRIx64 ", but it's at 0x%" PRIx64 " instead.\n", + sibling_addr, die_off); + sibling_addr = 0; + } + else if (prev_abbrev != NULL && prev_abbrev->has_children) + /* Even if it has children, the DIE can't have a sibling + attribute if it's the last DIE in chain. That's the reason + we can't simply check this when loading abbrevs. */ + wr_message (mc_die_rel | mc_acc_suboptimal | mc_impact_4, &where, + ": This DIE had children, but no DW_AT_sibling attribute.\n"); + + /* The section ended. */ + if (abbr_code == 0) + break; + if (read_ctx_eof (ctx)) + { + wr_error (&where, ": DIE chain not terminated with DIE with zero abbrev code.\n"); + break; + } + + prev_die_off = die_off; + got_die = true; + + /* Find the abbrev matching the code. */ + abbrev = abbrev_table_find_abbrev (abbrevs, abbr_code); + if (abbrev == NULL) + { + wr_error (&where, + ": abbrev section at 0x%" PRIx64 + " doesn't contain code %" PRIu64 ".\n", + abbrevs->offset, abbr_code); + return -1; + } + abbrev->used = true; + + addr_record_add (&cu->die_addrs, cu->offset + die_off); + + uint64_t low_pc = (uint64_t)-1, high_pc = (uint64_t)-1; + bool low_pc_relocated = false, high_pc_relocated = false; + GElf_Sym low_pc_symbol_mem, *low_pc_symbol = &low_pc_symbol_mem; + GElf_Sym high_pc_symbol_mem, *high_pc_symbol = &high_pc_symbol_mem; + + /* Attribute values. */ + for (struct abbrev_attrib *it = abbrev->attribs; + it->name != 0; ++it) + { + where.ref = &it->where; + + void record_ref (uint64_t addr, struct where *who, bool local) + { + struct ref_record *record = &cu->die_refs; + if (local) + { + assert (ctx->end > ctx->begin); + if (addr > (uint64_t)(ctx->end - ctx->begin)) + { + wr_error (&where, + ": invalid reference outside the CU: 0x%" PRIx64 ".\n", + addr); + return; + } + + /* Address holds a CU-local reference, so add CU + offset to turn it into section offset. */ + addr += cu->offset; + record = local_die_refs; + } + + if (record != NULL) + ref_record_add (record, addr, who); + } + + uint8_t form = it->form; + bool indirect = form == DW_FORM_indirect; + if (indirect) + { + uint64_t value; + if (!checked_read_uleb128 (ctx, &value, &where, + "indirect attribute form")) + return -1; + + if (!attrib_form_valid (value)) + { + wr_error (&where, + ": invalid indirect form 0x%" PRIx64 ".\n", value); + return -1; + } + form = value; + + if (it->name == DW_AT_sibling) + switch (check_sibling_form (form)) + { + case -1: + wr_message (mc_die_rel | mc_impact_2, &where, + ": DW_AT_sibling attribute with (indirect) form DW_FORM_ref_addr.\n"); + break; + + case -2: + wr_error (&where, + ": DW_AT_sibling attribute with non-reference (indirect) form \"%s\".\n", + dwarf_form_string (value)); + }; + } + + enum check_what_t + { + check_nothing = 0, + check_locptr, + check_lineptr, + check_rangeptr + }; + static enum message_category mc_check[] = + { + [check_nothing] = mc_none, + [check_locptr] = mc_loc, + [check_lineptr] = mc_line, + [check_rangeptr] = mc_ranges + }; + + void do_check_ptr (enum check_what_t what, uint64_t value) + { + assert (what != check_nothing); + + if (what == check_rangeptr && ((value % cu->address_size) != 0)) + wr_message (mc_ranges | mc_impact_2, &where, + ": rangeptr value %#" PRIx64 + " not aligned to CU address size.\n", value); + + struct ref_record *ref = NULL; + switch (what) + { + case check_rangeptr: + ref = &cu->range_refs; + break; + case check_lineptr: + ref = &cu->line_refs; + break; + case check_locptr: + ref = &cu->loc_refs; + case check_nothing: + break; + }; + + ref_record_add (ref, value, &where); + } + + enum check_what_t check_ptr = check_nothing; + + if (is_location_attrib (it->name)) + { + switch (form) + { + case DW_FORM_data8: + if (!dwarf_64) + wr_error (&where, + ": location attribute with form \"%s\" in 32-bit CU.\n", + dwarf_form_string (form)); + /* fall-through */ + case DW_FORM_data4: + check_ptr = check_locptr; + /* fall-through */ + case DW_FORM_block1: + case DW_FORM_block2: + case DW_FORM_block4: + case DW_FORM_block: + break; + + default: + /* Only print error if it's indirect. Otherwise we + gave diagnostic during abbrev loading. */ + if (indirect) + wr_error (&where, + ": location attribute with invalid (indirect) form \"%s\".\n", + dwarf_form_string (form)); + }; + } + else if (it->name == DW_AT_ranges + || it->name == DW_AT_stmt_list) + switch (form) + { + case DW_FORM_data8: + if (!dwarf_64) + wr_error (&where, + ": %s with form DW_FORM_data8 in 32-bit CU.\n", + dwarf_attr_string (it->name)); + /* fall-through */ + case DW_FORM_data4: + if (it->name == DW_AT_ranges) + check_ptr = check_rangeptr; + else + { + assert (it->name == DW_AT_stmt_list); + check_ptr = check_lineptr; + } + break; + + default: + /* Only print error if it's indirect. Otherwise we + gave diagnostic during abbrev loading. */ + if (indirect) + wr_error (&where, + ": %s with invalid (indirect) form \"%s\".\n", + dwarf_attr_string (it->name), + dwarf_form_string (form)); + }; + + uint64_t ctx_offset = read_ctx_get_offset (ctx) + cu->offset; + struct relocation *rel; + bool type_is_rel = file->ehdr.e_type == ET_REL; + + switch (form) + { + case DW_FORM_strp: + { + uint64_t addr; + if (!read_ctx_read_offset (ctx, dwarf_64, &addr)) + { + cant_read: + wr_error (&where, ": can't read attribute value.\n"); + return -1; + } + + if ((rel = relocation_next (reloc, ctx_offset, + &where, skip_mismatched))) + relocate_one (reloc, rel, dwarf_64 ? 8 : 4, + &addr, &where, sec_str, NULL); + else if (type_is_rel) + wr_message (mc_impact_2 | mc_die_other | mc_reloc | mc_strings, + &where, PRI_LACK_RELOCATION, "DW_FORM_strp"); + + if (strings == NULL) + wr_error (&where, + ": strp attribute, but no .debug_str section.\n"); + else if (addr >= strings->d_size) + wr_error (&where, + ": Invalid offset outside .debug_str: 0x%" PRIx64 ".\n", + addr); + else + { + /* Record used part of .debug_str. */ + const char *strp = (const char *)strings->d_buf + addr; + + if (strings_coverage != NULL) + coverage_add (strings_coverage, addr, strlen (strp) + 1); + } + + break; + } + + case DW_FORM_string: + { + if (!read_ctx_read_str (ctx)) + goto cant_read; + break; + } + + case DW_FORM_addr: + case DW_FORM_ref_addr: + { + uint64_t addr; + if (!read_ctx_read_offset (ctx, addr_64, &addr)) + goto cant_read; + + uint64_t *addrp = NULL; + bool *relocatedp = NULL; + GElf_Sym **symbolp = NULL; + + switch (it->name) + { + case DW_AT_low_pc: + relocatedp = &low_pc_relocated; + symbolp = &low_pc_symbol; + addrp = &low_pc; + break; + + case DW_AT_high_pc: + relocatedp = &high_pc_relocated; + symbolp = &high_pc_symbol; + addrp = &high_pc; + }; + + if ((rel = relocation_next (reloc, ctx_offset, + &where, skip_mismatched))) + { + relocate_one (reloc, rel, addr_64 ? 8 : 4, &addr, &where, + reloc_target (form, it), symbolp); + if (relocatedp != NULL) + *relocatedp = true; + if (addrp != NULL) + *addrp = addr; + } + else if (type_is_rel && addr != 0) + /* In non-rel files, neither addr, nor ref_addr + /need/ a relocation. We at least check that + ref_addr points to sensible datum by recording + the reference below. */ + wr_message (mc_impact_2 | mc_die_rel | mc_reloc, &where, + PRI_LACK_RELOCATION, dwarf_form_string (form)); + + if (form == DW_FORM_ref_addr) + record_ref (addr, &where, false); + + if (it->name == DW_AT_low_pc + && (abbrev->tag == DW_TAG_compile_unit + || abbrev->tag == DW_TAG_partial_unit)) + cu->base = addr; + + break; + } + + case DW_FORM_udata: + case DW_FORM_ref_udata: + { + uint64_t value; + if (!checked_read_uleb128 (ctx, &value, &where, + "attribute value")) + return -1; + + if (it->name == DW_AT_sibling) + sibling_addr = value; + else if (form == DW_FORM_ref_udata) + record_ref (value, &where, true); + break; + } + + case DW_FORM_flag: + case DW_FORM_data1: + case DW_FORM_ref1: + { + /* Neither of these should be relocated. */ + uint8_t value; + if (!read_ctx_read_ubyte (ctx, &value)) + goto cant_read; + + if (it->name == DW_AT_sibling) + sibling_addr = value; + else if (form == DW_FORM_ref1) + record_ref (value, &where, true); + break; + } + + case DW_FORM_data2: + case DW_FORM_ref2: + { + /* Neither of these should be relocated. */ + uint16_t value; + if (!read_ctx_read_2ubyte (ctx, &value)) + goto cant_read; + + if (it->name == DW_AT_sibling) + sibling_addr = value; + else if (form == DW_FORM_ref2) + record_ref (value, &where, true); + break; + } + + case DW_FORM_data4: + case DW_FORM_ref4: + { + uint32_t raw_value; + if (!read_ctx_read_4ubyte (ctx, &raw_value)) + goto cant_read; + + /* DW_FORM_ref4 shouldn't be relocated. */ + uint64_t value = raw_value; + if (form == DW_FORM_data4) + { + if ((rel = relocation_next (reloc, ctx_offset, + &where, skip_mismatched))) + relocate_one (reloc, rel, 4, &value, &where, + reloc_target (form, it), NULL); + else if (type_is_rel && check_ptr != check_nothing) + wr_message (mc_impact_2 | mc_die_other | mc_reloc + | mc_check[check_ptr], + &where, PRI_LACK_RELOCATION, + dwarf_form_string (form)); + } + + if (it->name == DW_AT_sibling) + sibling_addr = value; + else if (check_ptr != check_nothing) + do_check_ptr (check_ptr, value); + else if (form == DW_FORM_ref4) + record_ref (value, &where, true); + break; + } + + case DW_FORM_data8: + case DW_FORM_ref8: + { + uint64_t value; + if (!read_ctx_read_8ubyte (ctx, &value)) + goto cant_read; + + /* DW_FORM_ref8 shouldn't be relocated. */ + if (form == DW_FORM_data8) + { + if ((rel = relocation_next (reloc, ctx_offset, + &where, skip_mismatched))) + relocate_one (reloc, rel, 8, &value, &where, + reloc_target (form, it), NULL); + else if (type_is_rel && check_ptr != check_nothing) + wr_message (mc_impact_2 | mc_die_other | mc_reloc + | mc_check[check_ptr], + &where, PRI_LACK_RELOCATION, + dwarf_form_string (form)); + } + + if (it->name == DW_AT_sibling) + sibling_addr = value; + else if (check_ptr != check_nothing) + do_check_ptr (check_ptr, value); + else if (form == DW_FORM_ref8) + record_ref (value, &where, true); + break; + } + + case DW_FORM_sdata: + { + int64_t value; + if (!checked_read_sleb128 (ctx, &value, &where, + "attribute value")) + return -1; + break; + } + + case DW_FORM_block: + { + int width = 0; + uint64_t length; + goto process_DW_FORM_block; + + case DW_FORM_block1: + width = 1; + goto process_DW_FORM_block; + + case DW_FORM_block2: + width = 2; + goto process_DW_FORM_block; + + case DW_FORM_block4: + width = 4; + + process_DW_FORM_block: + if (width == 0) + { + if (!checked_read_uleb128 (ctx, &length, &where, + "attribute value")) + return -1; + } + else if (!read_ctx_read_var (ctx, width, &length)) + goto cant_read; + + if (is_location_attrib (it->name)) + { + uint64_t expr_start = cu->offset + read_ctx_get_offset (ctx); + if (!check_location_expression (ctx, expr_start, + reloc, length, + &where, addr_64)) + return -1; + } + else + relocation_skip (reloc, + read_ctx_get_offset (ctx) + length, + &where, skip_mismatched); + + if (!read_ctx_skip (ctx, length)) + goto cant_read; + + break; + } + + case DW_FORM_indirect: + wr_error (&where, ": indirect form is again indirect.\n"); + return -1; + + default: + wr_error (&where, + ": internal error: unhandled form 0x%x.\n", form); + } + } + where.ref = NULL; + + if (high_pc != (uint64_t)-1 && low_pc != (uint64_t)-1) + { + if (high_pc_relocated != low_pc_relocated) + wr_message (mc_die_other | mc_impact_2 | mc_reloc, &where, + ": only one of DW_AT_low_pc and DW_AT_high_pc is relocated.\n"); + else + check_range_relocations (mc_die_other, &where, + file, + low_pc_symbol, high_pc_symbol, + "DW_AT_low_pc and DW_AT_high_pc"); + } + + where.ref = &abbrev->where; + + if (abbrev->has_children) + { + int st = read_die_chain (ctx, cu, abbrevs, strings, + dwarf_64, addr_64, + local_die_refs, + strings_coverage, reloc, file); + if (st == -1) + return -1; + else if (st == 0) + wr_message (mc_impact_3 | mc_acc_suboptimal | mc_die_rel, + &where, + ": abbrev has_children, but the chain was empty.\n"); + } + } + + if (sibling_addr != 0) + wr_error (&where, + ": this DIE should have had its sibling at 0x%" + PRIx64 ", but the DIE chain ended.\n", sibling_addr); + + return got_die ? 1 : 0; +} + +static bool +check_cu_structural (struct read_ctx *ctx, + struct cu *const cu, + struct abbrev_table *abbrev_chain, + Elf_Data *strings, + bool dwarf_64, + struct coverage *strings_coverage, + struct relocation_data *reloc, + struct elf_file *file) +{ + uint8_t address_size; + bool retval = true; + + /* Version. */ + uint16_t version; + if (!read_ctx_read_2ubyte (ctx, &version)) + { + wr_error (&cu->where, ": can't read version.\n"); + return false; + } + if (!supported_version (version, 2, &cu->where, 2, 3)) + return false; + if (version == 2 && dwarf_64) + /* Keep going. It's a standard violation, but we may still be + able to read the unit under consideration and do high-level + checks. */ + wr_error (&cu->where, ": invalid 64-bit unit in DWARF 2 format.\n"); + + /* Abbrev offset. */ + uint64_t abbrev_offset; + uint64_t ctx_offset = read_ctx_get_offset (ctx) + cu->offset; + if (!read_ctx_read_offset (ctx, dwarf_64, &abbrev_offset)) + { + wr_error (&cu->where, ": can't read abbrev offset.\n"); + return false; + } + + struct relocation *rel + = relocation_next (reloc, ctx_offset, &cu->where, skip_mismatched); + if (rel != NULL) + relocate_one (reloc, rel, dwarf_64 ? 8 : 4, + &abbrev_offset, &cu->where, sec_abbrev, NULL); + else if (file->ehdr.e_type == ET_REL) + wr_message (mc_impact_2 | mc_info | mc_reloc, &cu->where, + PRI_LACK_RELOCATION, "abbrev offset"); + + /* Address size. */ + if (!read_ctx_read_ubyte (ctx, &address_size)) + { + wr_error (&cu->where, ": can't read address size.\n"); + return false; + } + if (address_size != 4 && address_size != 8) + { + wr_error (&cu->where, + ": invalid address size: %d (only 4 or 8 allowed).\n", + address_size); + return false; + } + cu->address_size = address_size; + + struct abbrev_table *abbrevs = abbrev_chain; + for (; abbrevs != NULL; abbrevs = abbrevs->next) + if (abbrevs->offset == abbrev_offset) + break; + + if (abbrevs == NULL) + { + wr_error (&cu->where, + ": couldn't find abbrev section with offset 0x%" PRIx64 ".\n", + abbrev_offset); + return false; + } + + struct ref_record local_die_refs; + WIPE (local_die_refs); + + cu->cudie_offset = read_ctx_get_offset (ctx) + cu->offset; + if (read_die_chain (ctx, cu, abbrevs, strings, + dwarf_64, address_size == 8, + &local_die_refs, strings_coverage, + (reloc != NULL && reloc->size > 0) ? reloc : NULL, + file) >= 0) + { + for (size_t i = 0; i < abbrevs->size; ++i) + if (!abbrevs->abbr[i].used) + wr_message (mc_impact_3 | mc_acc_bloat | mc_abbrevs, &cu->where, + ": abbreviation with code %" PRIu64 " is never used.\n", + abbrevs->abbr[i].code); + + if (!check_die_references (cu, &local_die_refs)) + retval = false; + } + else + retval = false; + + ref_record_free (&local_die_refs); + return retval; +} + +static struct cu * +check_info_structural (struct section_data *data, + struct abbrev_table *abbrev_chain, + Elf_Data *strings) +{ + struct read_ctx ctx; + read_ctx_init (&ctx, data->file->dwarf, data->data); + + struct ref_record die_refs; + WIPE (die_refs); + + struct cu *cu_chain = NULL; + + bool success = true; + + struct coverage strings_coverage_mem, *strings_coverage = NULL; + if (strings != NULL && check_category (mc_strings)) + { + WIPE (strings_coverage_mem); + strings_coverage = &strings_coverage_mem; + } + + struct relocation_data *reloc = data->rel.size > 0 ? &data->rel : NULL; + while (!read_ctx_eof (&ctx)) + { + const unsigned char *cu_begin = ctx.ptr; + struct where where = WHERE (sec_info, NULL); + where_reset_1 (&where, read_ctx_get_offset (&ctx)); + + struct cu *cur = xcalloc (1, sizeof (*cur)); + cur->offset = where.addr1; + cur->next = cu_chain; + cur->where = where; + cur->base = (uint64_t)-1; + cu_chain = cur; + + uint32_t size32; + uint64_t size; + bool dwarf_64 = false; + + /* Reading CU header is a bit tricky, because we don't know if + we have run into (superfluous but allowed) zero padding. */ + if (!read_ctx_need_data (&ctx, 4) + && check_zero_padding (&ctx, mc_info | mc_header, &where)) + break; + + /* CU length. */ + if (!read_ctx_read_4ubyte (&ctx, &size32)) + { + wr_error (&where, ": can't read CU length.\n"); + success = false; + break; + } + if (size32 == 0 && check_zero_padding (&ctx, mc_info | mc_header, &where)) + break; + + if (!read_size_extra (&ctx, size32, &size, &dwarf_64, &where)) + { + success = false; + break; + } + + if (!read_ctx_need_data (&ctx, size)) + { + wr_error (&where, + ": section doesn't have enough data" + " to read CU of size %" PRIx64 ".\n", size); + ctx.ptr = ctx.end; + success = false; + break; + } + + const unsigned char *cu_end = ctx.ptr + size; + cur->length = cu_end - cu_begin; // Length including the length field. + + /* version + debug_abbrev_offset + address_size */ + uint64_t cu_header_size = 2 + (dwarf_64 ? 8 : 4) + 1; + if (size < cu_header_size) + { + wr_error (&where, ": claimed length of %" PRIx64 + " doesn't even cover CU header.\n", size); + success = false; + break; + } + else + { + /* Make CU context begin just before the CU length, so that DIE + offsets are computed correctly. */ + struct read_ctx cu_ctx; + if (!read_ctx_init_sub (&cu_ctx, &ctx, cu_begin, cu_end)) + { + not_enough: + wr_error (&where, PRI_NOT_ENOUGH, "next CU"); + success = false; + break; + } + cu_ctx.ptr = ctx.ptr; + + if (!check_cu_structural (&cu_ctx, cur, abbrev_chain, + strings, dwarf_64, + strings_coverage, reloc, data->file)) + { + success = false; + break; + } + if (cu_ctx.ptr != cu_ctx.end + && !check_zero_padding (&cu_ctx, mc_info, &where)) + wr_message_padding_n0 (mc_info, &where, + read_ctx_get_offset (&ctx), + read_ctx_get_offset (&ctx) + size - 1); + } + + if (!read_ctx_skip (&ctx, size)) + goto not_enough; + } + + if (success) + { + if (ctx.ptr != ctx.end) + /* Did we read up everything? */ + wr_message (mc_die_other | mc_impact_4, + &WHERE (sec_info, NULL), + ": CU lengths don't exactly match Elf_Data contents."); + else + /* Did we consume all the relocations? */ + relocation_skip_rest (data); + } + + + int address_size = 0; + if (cu_chain != NULL) + { + uint64_t offset = 0; + for (struct cu *it = cu_chain; it != NULL; it = it->next) + if (address_size == 0) + { + address_size = it->address_size; + offset = it->where.addr1; + } + else if (address_size != it->address_size) + { + /* XXX would be nice to check consistency of CU address + size declared in various other .debug_* sections. */ + wr_message (mc_info, &it->where, + ": has different address size than CU 0x%" + PRIx64 ".\n", offset); + address_size = 0; + break; + } + } + + bool references_sound = check_global_die_references (cu_chain); + ref_record_free (&die_refs); + + if (strings_coverage != NULL) + { + if (success) + coverage_find_holes (strings_coverage, 0, strings->d_size, found_hole, + &((struct hole_info) + {sec_str, mc_strings, 0, strings->d_buf})); + coverage_free (strings_coverage); + } + + if (!success || !references_sound) + { + cu_free (cu_chain); + cu_chain = NULL; + } + + /* Reverse the chain, so that it's organized "naturally". Has + significant impact on performance when handling loc_ref and + range_ref fields in loc/range validation. */ + struct cu *last = NULL; + for (struct cu *it = cu_chain; it != NULL; ) + { + struct cu *next = it->next; + it->next = last; + last = it; + it = next; + } + cu_chain = last; + + return cu_chain; +} + +static struct coverage_map * +coverage_map_alloc_XA (struct elf_file *elf, bool allow_overlap) +{ + struct coverage_map *ret = xmalloc (sizeof (*ret)); + if (!coverage_map_init (ret, elf, + SHF_EXECINSTR | SHF_ALLOC, + SHF_ALLOC, + allow_overlap)) + { + free (ret); + return NULL; + } + return ret; +} + +static void +coverage_map_free_XA (struct coverage_map *coverage_map) +{ + if (coverage_map != NULL) + { + coverage_map_free (coverage_map); + free (coverage_map); + } +} + +static bool +check_aranges_structural (struct section_data *data, struct cu *cu_chain) +{ + struct read_ctx ctx; + read_ctx_init (&ctx, data->file->dwarf, data->data); + + bool retval = true; + + struct coverage_map *coverage_map; + if ((coverage_map = coverage_map_alloc_XA (data->file, false)) == NULL) + { + wr_error (&WHERE (sec_aranges, NULL), + ": couldn't read ELF, skipping coverage analysis.\n"); + retval = false; + } + + while (!read_ctx_eof (&ctx)) + { + struct where where = WHERE (sec_aranges, NULL); + where_reset_1 (&where, read_ctx_get_offset (&ctx)); + const unsigned char *atab_begin = ctx.ptr; + + /* Size. */ + uint32_t size32; + uint64_t size; + bool dwarf_64; + if (!read_ctx_read_4ubyte (&ctx, &size32)) + { + wr_error (&where, ": can't read table length.\n"); + return false; + } + if (!read_size_extra (&ctx, size32, &size, &dwarf_64, &where)) + return false; + + struct read_ctx sub_ctx; + const unsigned char *atab_end = ctx.ptr + size; + if (!read_ctx_init_sub (&sub_ctx, &ctx, atab_begin, atab_end)) + { + not_enough: + wr_error (&where, PRI_NOT_ENOUGH, "next table"); + return false; + } + + sub_ctx.ptr = ctx.ptr; + + /* Version. */ + uint16_t version; + if (!read_ctx_read_2ubyte (&sub_ctx, &version)) + { + wr_error (&where, ": can't read version.\n"); + retval = false; + goto next; + } + if (!supported_version (version, 1, &where, 2)) + { + retval = false; + goto next; + } + + /* CU offset. */ + uint64_t cu_offset; + uint64_t ctx_offset = sub_ctx.ptr - ctx.begin; + if (!read_ctx_read_offset (&sub_ctx, dwarf_64, &cu_offset)) + { + wr_error (&where, ": can't read debug info offset.\n"); + retval = false; + goto next; + } + + struct relocation *rel; + if ((rel = relocation_next (&data->rel, ctx_offset, + &where, skip_mismatched))) + relocate_one (&data->rel, rel, dwarf_64 ? 8 : 4, + &cu_offset, &where, sec_info, NULL); + else if (data->file->ehdr.e_type == ET_REL) + wr_message (mc_impact_2 | mc_aranges | mc_reloc | mc_header, &where, + PRI_LACK_RELOCATION, "debug info offset"); + + struct cu *cu = NULL; + if (cu_chain != NULL && (cu = cu_find_cu (cu_chain, cu_offset)) == NULL) + wr_error (&where, ": unresolved reference to " PRI_CU ".\n", cu_offset); + + struct where where_cudie; + if (cu != NULL) + { + where_cudie = WHERE (sec_info, NULL); + where_reset_1 (&where_cudie, cu->cudie_offset); + where.ref = &where_cudie; + where_cudie.formatting = wf_cudie; + if (cu->has_arange) + wr_message (mc_impact_2 | mc_aranges | mc_header, &where, + ": there has already been arange section for this CU.\n"); + else + cu->has_arange = true; + } + + /* Address size. */ + uint8_t address_size; + if (!read_ctx_read_ubyte (&sub_ctx, &address_size)) + { + wr_error (&where, ": can't read address size.\n"); + retval = false; + goto next; + } + if (cu != NULL) + { + if (address_size != cu->address_size) + { + wr_error (&where, + ": address size %d doesn't match referred CU.\n", + address_size); + retval = false; + } + } + /* Try to parse it anyway, unless the address size is wacky. */ + else if (address_size != 4 && address_size != 8) + { + wr_error (&where, ": invalid address size: %d.\n", address_size); + retval = false; + goto next; + } + + /* Segment size. */ + uint8_t segment_size; + if (!read_ctx_read_ubyte (&sub_ctx, &segment_size)) + { + wr_error (&where, ": can't read unit segment size.\n"); + retval = false; + goto next; + } + if (segment_size != 0) + { + wr_warning (&where, ": dwarflint can't handle segment_size != 0.\n"); + retval = false; + goto next; + } + + + /* 7.20: The first tuple following the header in each set begins + at an offset that is a multiple of the size of a single tuple + (that is, twice the size of an address). The header is + padded, if necessary, to the appropriate boundary. */ + const uint8_t tuple_size = 2 * address_size; + uint64_t off = read_ctx_get_offset (&sub_ctx); + if ((off % tuple_size) != 0) + { + uint64_t noff = ((off / tuple_size) + 1) * tuple_size; + for (uint64_t i = off; i < noff; ++i) + { + uint8_t c; + if (!read_ctx_read_ubyte (&sub_ctx, &c)) + { + wr_error (&where, + ": section ends after the header, but before the first entry.\n"); + retval = false; + goto next; + } + if (c != 0) + wr_message (mc_impact_2 | mc_aranges | mc_header, &where, + ": non-zero byte at 0x%" PRIx64 + " in padding before the first entry.\n", + read_ctx_get_offset (&sub_ctx)); + } + } + assert ((read_ctx_get_offset (&sub_ctx) % tuple_size) == 0); + + while (!read_ctx_eof (&sub_ctx)) + { + /* We would like to report aranges the same way that readelf + does. But readelf uses index of the arange in the array + as returned by dwarf_getaranges, which sorts the aranges + beforehand. We don't want to disturb the memory this + way, the better to catch structural errors accurately. + So report arange offset instead. If this becomes a + problem, we will achieve this by two-pass analysis. */ + where_reset_2 (&where, read_ctx_get_offset (&sub_ctx)); + + /* Record address. */ + uint64_t address; + ctx_offset = sub_ctx.ptr - ctx.begin; + bool address_relocated = false; + if (!read_ctx_read_var (&sub_ctx, address_size, &address)) + { + wr_error (&where, ": can't read address field.\n"); + retval = false; + goto next; + } + + if ((rel = relocation_next (&data->rel, ctx_offset, + &where, skip_mismatched))) + { + address_relocated = true; + relocate_one (&data->rel, rel, address_size, + &address, &where, rel_address, NULL); + } + else if (data->file->ehdr.e_type == ET_REL + && address != 0) + wr_message (mc_impact_2 | mc_aranges | mc_reloc, &where, + PRI_LACK_RELOCATION, "address field"); + + /* Record length. */ + uint64_t length; + if (!read_ctx_read_var (&sub_ctx, address_size, &length)) + { + wr_error (&where, ": can't read length field.\n"); + retval = false; + goto next; + } + + if (address == 0 && length == 0 && !address_relocated) + break; + + if (length == 0) + /* DWARF 3 spec, 6.1.2 Lookup by Address: Each descriptor + is a pair consisting of the beginning address [...], + followed by the _non-zero_ length of that range. */ + wr_error (&where, ": zero-length address range.\n"); + else if (retval) + /* Skip coverage analysis if we have errors. */ + coverage_map_add (coverage_map, address, length, &where, + mc_aranges); + } + + if (sub_ctx.ptr != sub_ctx.end + && !check_zero_padding (&sub_ctx, mc_pubtables, + &WHERE (where.section, NULL))) + { + wr_message_padding_n0 (mc_pubtables | mc_error, + &WHERE (where.section, NULL), + read_ctx_get_offset (&sub_ctx), + read_ctx_get_offset (&sub_ctx) + size - 1); + retval = false; + } + + next: + if (!read_ctx_skip (&ctx, size)) + /* A "can't happen" error. */ + goto not_enough; + } + + if (retval && coverage_map != NULL) + coverage_map_find_holes (coverage_map, &coverage_map_found_hole, + &(struct coverage_map_hole_info) + {{sec_aranges, mc_aranges, 0, NULL}, + coverage_map->elf}); + + coverage_map_free_XA (coverage_map); + + return retval; +} + +static bool +check_pub_structural (struct section_data *data, + struct cu *cu_chain) +{ + struct read_ctx ctx; + read_ctx_init (&ctx, data->file->dwarf, data->data); + bool retval = true; + + while (!read_ctx_eof (&ctx)) + { + struct where where = WHERE_SECDATA (data, NULL); + where_reset_1 (&where, read_ctx_get_offset (&ctx)); + const unsigned char *set_begin = ctx.ptr; + + /* Size. */ + uint32_t size32; + uint64_t size; + bool dwarf_64; + if (!read_ctx_read_4ubyte (&ctx, &size32)) + { + wr_error (&where, ": can't read table length.\n"); + return false; + } + if (!read_size_extra (&ctx, size32, &size, &dwarf_64, &where)) + return false; + + struct read_ctx sub_ctx; + const unsigned char *set_end = ctx.ptr + size; + if (!read_ctx_init_sub (&sub_ctx, &ctx, set_begin, set_end)) + { + not_enough: + wr_error (&where, PRI_NOT_ENOUGH, "next set"); + return false; + } + sub_ctx.ptr = ctx.ptr; + + /* Version. */ + uint16_t version; + if (!read_ctx_read_2ubyte (&sub_ctx, &version)) + { + wr_error (&where, ": can't read set version.\n"); + retval = false; + goto next; + } + if (!supported_version (version, 1, &where, 2)) + { + retval = false; + goto next; + } + + /* CU offset. */ + uint64_t cu_offset; /* Offset of related CU. */ + uint64_t ctx_offset = sub_ctx.ptr - ctx.begin; + if (!read_ctx_read_offset (&sub_ctx, dwarf_64, &cu_offset)) + { + wr_error (&where, ": can't read debug info offset.\n"); + retval = false; + goto next; + } + + struct relocation *rel; + if ((rel = relocation_next (&data->rel, ctx_offset, + &where, skip_mismatched))) + relocate_one (&data->rel, rel, dwarf_64 ? 8 : 4, + &cu_offset, &where, sec_info, NULL); + else if (data->file->ehdr.e_type == ET_REL) + wr_message (mc_impact_2 | mc_pubtables | mc_reloc | mc_header, &where, + PRI_LACK_RELOCATION, "debug info offset"); + + struct cu *cu = NULL; + enum section_id sec = data_get_sec (data)->id; + if (cu_chain != NULL && (cu = cu_find_cu (cu_chain, cu_offset)) == NULL) + wr_error (&where, ": unresolved reference to " PRI_CU ".\n", cu_offset); + if (cu != NULL) + { + where.ref = &cu->where; + bool *has = sec == sec_pubnames + ? &cu->has_pubnames : &cu->has_pubtypes; + if (*has) + wr_message (mc_impact_2 | mc_pubtables | mc_header, &where, + ": there has already been section for this CU.\n"); + else + *has = true; + } + + /* Covered length. */ + uint64_t cu_len; + if (!read_ctx_read_offset (&sub_ctx, dwarf_64, &cu_len)) + { + wr_error (&where, ": can't read covered length.\n"); + retval = false; + goto next; + } + if (cu != NULL && cu_len != cu->length) + { + wr_error (&where, + ": the table covers length %" PRId64 + " but CU has length %" PRId64 ".\n", cu_len, cu->length); + retval = false; + goto next; + } + + /* Records... */ + while (!read_ctx_eof (&sub_ctx)) + { + ctx_offset = sub_ctx.ptr - ctx.begin; + where_reset_2 (&where, ctx_offset); + + uint64_t offset; + if (!read_ctx_read_offset (&sub_ctx, dwarf_64, &offset)) + { + wr_error (&where, ": can't read offset field.\n"); + retval = false; + goto next; + } + if (offset == 0) + break; + + if (cu != NULL + && !addr_record_has_addr (&cu->die_addrs, offset + cu->offset)) + { + wr_error (&where, + ": unresolved reference to " PRI_DIE ".\n", offset); + retval = false; + goto next; + } + + uint8_t c; + do + if (!read_ctx_read_ubyte (&sub_ctx, &c)) + { + wr_error (&where, ": can't read symbol name.\n"); + retval = false; + goto next; + } + while (c); + } + + if (sub_ctx.ptr != sub_ctx.end + && !check_zero_padding (&sub_ctx, mc_pubtables, + &WHERE (sec, NULL))) + { + wr_message_padding_n0 (mc_pubtables | mc_error, + &WHERE (sec, NULL), + read_ctx_get_offset (&sub_ctx), + read_ctx_get_offset (&sub_ctx) + size - 1); + retval = false; + } + + next: + if (!read_ctx_skip (&ctx, size)) + goto not_enough; + } + + if (retval) + relocation_skip_rest (data); + + return retval; +} + + +/* Operands are passed back as attribute forms. In particular, + DW_FORM_dataX for X-byte operands, DW_FORM_[us]data for + ULEB128/SLEB128 operands, and DW_FORM_addr for 32b/64b operands. + If the opcode takes no operands, 0 is passed. + + Return value is false if we couldn't determine (i.e. invalid + opcode). + */ +static bool +get_location_opcode_operands (uint8_t opcode, uint8_t *op1, uint8_t *op2) +{ + switch (opcode) + { +#define DEF_DW_OP(OPCODE, OP1, OP2) \ + case OPCODE: *op1 = OP1; *op2 = OP2; return true; +# include "expr_opcodes.h" +#undef DEF_DW_OP + default: + return false; + }; +} + +static bool +check_location_expression (struct read_ctx *parent_ctx, + uint64_t init_off, + struct relocation_data *reloc, + size_t length, + struct where *wh, + bool addr_64) +{ + struct read_ctx ctx; + if (!read_ctx_init_sub (&ctx, parent_ctx, parent_ctx->ptr, + parent_ctx->ptr + length)) + { + wr_error (wh, PRI_NOT_ENOUGH, "location expression"); + return false; + } + + struct ref_record oprefs; + WIPE (oprefs); + + struct addr_record opaddrs; + WIPE (opaddrs); + + while (!read_ctx_eof (&ctx)) + { + struct where where = WHERE (sec_locexpr, wh); + uint64_t opcode_off = read_ctx_get_offset (&ctx) + init_off; + where_reset_1 (&where, opcode_off); + addr_record_add (&opaddrs, opcode_off); + + uint8_t opcode; + if (!read_ctx_read_ubyte (&ctx, &opcode)) + { + wr_error (&where, ": can't read opcode.\n"); + break; + } + + uint8_t op1, op2; + if (!get_location_opcode_operands (opcode, &op1, &op2)) + { + wr_error (&where, ": can't decode opcode \"%s\".\n", + dwarf_locexpr_opcode_string (opcode)); + break; + } + +#define READ_FORM(OP, STR, PTR) \ + do { \ + if (OP != 0) \ + { \ + uint64_t _off = read_ctx_get_offset (&ctx) + init_off; \ + uint64_t *_ptr = (PTR); \ + if (!read_ctx_read_form (&ctx, addr_64, (OP), \ + _ptr, &where, STR " operand")) \ + { \ + wr_error (&where, ": opcode \"%s\"" \ + ": can't read " STR " operand (form \"%s\").\n", \ + dwarf_locexpr_opcode_string (opcode), \ + dwarf_form_string ((OP))); \ + goto out; \ + } \ + struct relocation *_rel; \ + if ((_rel = relocation_next (reloc, _off, \ + &where, skip_mismatched))) \ + relocate_one (reloc, _rel, \ + addr_64 ? 8 : 4, _ptr, &where, \ + reloc_target_loc (opcode), NULL); \ + } \ + } while (0) + + uint64_t value1, value2; + READ_FORM (op1, "1st", &value1); + READ_FORM (op2, "2st", &value2); +#undef READ_FORM + + switch (opcode) + { + case DW_OP_bra: + case DW_OP_skip: + { + int16_t skip = (uint16_t)value1; + + if (skip == 0) + wr_message (mc_loc | mc_acc_bloat | mc_impact_3, &where, + ": %s with skip 0.\n", + dwarf_locexpr_opcode_string (opcode)); + else if (skip > 0 && !read_ctx_need_data (&ctx, (size_t)skip)) + wr_error (&where, ": %s branches out of location expression.\n", + dwarf_locexpr_opcode_string (opcode)); + /* Compare with the offset after the two-byte skip value. */ + else if (skip < 0 && ((uint64_t)-skip) > read_ctx_get_offset (&ctx)) + wr_error (&where, + ": %s branches before the beginning of location expression.\n", + dwarf_locexpr_opcode_string (opcode)); + else + ref_record_add (&oprefs, opcode_off + skip, &where); + + break; + } + + case DW_OP_const8u: + case DW_OP_const8s: + if (!addr_64) + wr_error (&where, ": %s on 32-bit machine.\n", + dwarf_locexpr_opcode_string (opcode)); + break; + + default: + if (!addr_64 + && (opcode == DW_OP_constu + || opcode == DW_OP_consts + || opcode == DW_OP_deref_size + || opcode == DW_OP_plus_uconst) + && (value1 > (uint64_t)(uint32_t)-1)) + wr_message (mc_loc | mc_acc_bloat | mc_impact_3, &where, + ": %s with operand %#" PRIx64 " on 32-bit machine.\n", + dwarf_locexpr_opcode_string (opcode), value1); + }; + } + + out: + for (size_t i = 0; i < oprefs.size; ++i) + { + struct ref *ref = oprefs.refs + i; + if (!addr_record_has_addr (&opaddrs, ref->addr)) + wr_error (&ref->who, + ": unresolved reference to opcode at %#" PRIx64 ".\n", + ref->addr); + } + + addr_record_free (&opaddrs); + ref_record_free (&oprefs); + + return true; +} + +static bool +check_loc_or_range_ref (const struct read_ctx *parent_ctx, + struct cu *cu, + struct section_data *data, + struct coverage *coverage, + struct coverage_map *coverage_map, + uint64_t addr, + bool addr_64, + struct where *wh, + enum message_category cat) +{ + struct read_ctx ctx; + read_ctx_init (&ctx, parent_ctx->dbg, parent_ctx->data); + + enum section_id sec = data_get_sec (data)->id; + + assert (sec == sec_loc || sec == sec_ranges); + assert (cat == mc_loc || cat == mc_ranges); + assert ((sec == sec_loc) == (cat == mc_loc)); + assert (coverage != NULL); + + if (!read_ctx_skip (&ctx, addr)) + { + wr_error (wh, ": invalid reference outside the section " + "%#" PRIx64 ", size only %#tx.\n", + addr, ctx.end - ctx.begin); + return false; + } + + bool retval = true; + bool contains_locations = sec == sec_loc; + + if (coverage_is_covered (coverage, addr)) + { + wr_error (wh, ": reference to %#" PRIx64 + " points at the middle of location or range list.\n", addr); + retval = false; + } + + uint64_t escape = addr_64 ? (uint64_t)-1 : (uint64_t)(uint32_t)-1; + + bool overlap = false; + uint64_t base = cu->base; + while (!read_ctx_eof (&ctx)) + { + struct where where = WHERE (sec, wh); + where_reset_1 (&where, read_ctx_get_offset (&ctx)); + +#define HAVE_OVERLAP \ + do { \ + wr_error (&where, ": range definitions overlap.\n"); \ + retval = false; \ + overlap = true; \ + } while (0) + + /* begin address */ + uint64_t begin_addr; + uint64_t begin_off = read_ctx_get_offset (&ctx); + GElf_Sym begin_symbol_mem, *begin_symbol = &begin_symbol_mem; + bool begin_relocated = false; + if (!overlap + && !coverage_pristine (coverage, begin_off, addr_64 ? 8 : 4)) + HAVE_OVERLAP; + + if (!read_ctx_read_offset (&ctx, addr_64, &begin_addr)) + { + wr_error (&where, ": can't read address range beginning.\n"); + return false; + } + + struct relocation *rel; + if ((rel = relocation_next (&data->rel, begin_off, + &where, skip_mismatched))) + { + begin_relocated = true; + relocate_one (&data->rel, rel, addr_64 ? 8 : 4, + &begin_addr, &where, rel_value, &begin_symbol); + } + + /* end address */ + uint64_t end_addr; + uint64_t end_off = read_ctx_get_offset (&ctx); + GElf_Sym end_symbol_mem, *end_symbol = &end_symbol_mem; + bool end_relocated = false; + if (!overlap + && !coverage_pristine (coverage, end_off, addr_64 ? 8 : 4)) + HAVE_OVERLAP; + + if (!read_ctx_read_offset (&ctx, addr_64, &end_addr)) + { + wr_error (&where, ": can't read address range ending.\n"); + return false; + } + + if ((rel = relocation_next (&data->rel, end_off, + &where, skip_mismatched))) + { + end_relocated = true; + relocate_one (&data->rel, rel, addr_64 ? 8 : 4, + &end_addr, &where, rel_value, &end_symbol); + if (begin_addr != escape) + { + if (!begin_relocated) + wr_message (cat | mc_impact_2 | mc_reloc, &where, + ": end of address range is relocated, but the beginning wasn't.\n"); + else + check_range_relocations (cat, &where, + data->file, + begin_symbol, end_symbol, + "begin and end address"); + } + } + else if (begin_relocated) + wr_message (cat | mc_impact_2 | mc_reloc, &where, + ": end of address range is not relocated, but the beginning was.\n"); + + bool done = false; + if (begin_addr == 0 && end_addr == 0 && !begin_relocated && !end_relocated) + done = true; + else if (begin_addr != escape) + { + if (base == (uint64_t)-1) + { + wr_error (&where, + ": address range with no base address set (%#" + PRIx64 "..%#" PRIx64 ").\n", begin_addr, end_addr); + /* This is not something that would derail high-level, + so carry on. */ + } + + if (end_addr < begin_addr) + wr_message (cat | mc_error, &where, + ": has negative range 0x%" PRIx64 "..0x%" PRIx64 ".\n", + begin_addr, end_addr); + else if (begin_addr == end_addr) + /* 2.6.6: A location list entry [...] whose beginning + and ending addresses are equal has no effect. */ + wr_message (cat | mc_acc_bloat | mc_impact_3, &where, + ": entry covers no range.\n"); + /* Skip coverage analysis if we have errors or have no base + (or just don't do coverage analysis at all). */ + else if (base < (uint64_t)-2 && retval && coverage_map != NULL) + { + uint64_t address = begin_addr + base; + uint64_t length = end_addr - begin_addr; + coverage_map_add (coverage_map, address, length, &where, cat); + } + + if (contains_locations) + { + /* location expression length */ + uint16_t len; + if (!overlap + && !coverage_pristine (coverage, + read_ctx_get_offset (&ctx), 2)) + HAVE_OVERLAP; + + if (!read_ctx_read_2ubyte (&ctx, &len)) + { + wr_error (&where, ": can't read length of location expression.\n"); + return false; + } + + /* location expression itself */ + uint64_t expr_start = read_ctx_get_offset (&ctx); + if (!check_location_expression (&ctx, expr_start, &data->rel, + len, &where, addr_64)) + return false; + uint64_t expr_end = read_ctx_get_offset (&ctx); + if (!overlap + && !coverage_pristine (coverage, + expr_start, expr_end - expr_start)) + HAVE_OVERLAP; + + if (!read_ctx_skip (&ctx, len)) + { + /* "can't happen" */ + wr_error (&where, PRI_NOT_ENOUGH, "location expression"); + return false; + } + } + } + else + { + if (end_addr == base) + wr_message (cat | mc_acc_bloat | mc_impact_3, &where, + ": base address selection doesn't change base address" + " (%#" PRIx64 ").\n", base); + else + base = end_addr; + } +#undef HAVE_OVERLAP + + coverage_add (coverage, where.addr1, read_ctx_get_offset (&ctx) - where.addr1); + if (done) + break; + } + + return retval; +} + +static bool +check_loc_or_range_structural (struct section_data *data, + struct cu *cu_chain) +{ + enum section_id sec = data_get_sec (data)->id; + assert (sec == sec_loc || sec == sec_ranges); + assert (cu_chain != NULL); + + struct read_ctx ctx; + read_ctx_init (&ctx, data->file->dwarf, data->data); + + bool retval = true; + + struct coverage_map *coverage_map = NULL; +#ifdef FIND_SECTION_HOLES + if ((coverage_map = coverage_map_alloc_XA (ctx.dbg->elf, + data->sec == sec_loc)) == NULL) + { + wr_error (&WHERE (data->sec, NULL), + ": couldn't read ELF, skipping coverage analysis.\n"); + retval = false; + } +#endif + + struct coverage coverage; + WIPE (coverage); + + enum message_category cat = sec == sec_loc ? mc_loc : mc_ranges; + + /* Relocation checking in the followings assumes that all the + references are organized in monotonously increasing order. That + doesn't have to be the case. So merge all the references into + them into one sorted array. */ + size_t size = 0; + for (struct cu *cu = cu_chain; cu != NULL; cu = cu->next) + { + struct ref_record *rec + = sec == sec_loc ? &cu->loc_refs : &cu->range_refs; + size += rec->size; + } + struct ref_cu + { + struct ref ref; + struct cu *cu; + }; + struct ref_cu *refs = xmalloc (sizeof (*refs) * size); + struct ref_cu *refptr = refs; + for (struct cu *cu = cu_chain; cu != NULL; cu = cu->next) + { + struct ref_record *rec + = sec == sec_loc ? &cu->loc_refs : &cu->range_refs; + for (size_t i = 0; i < rec->size; ++i) + *refptr++ = ((struct ref_cu){.ref = rec->refs[i], .cu = cu}); + } + int compare_refs (const void *a, const void *b) + { + const struct ref_cu *ref_a = (const struct ref_cu *)a; + const struct ref_cu *ref_b = (const struct ref_cu *)b; + + if (ref_a->ref.addr > ref_b->ref.addr) + return 1; + else if (ref_a->ref.addr < ref_b->ref.addr) + return -1; + else + return 0; + } + qsort (refs, size, sizeof (*refs), compare_refs); + + uint64_t last_off = 0; + for (size_t i = 0; i < size; ++i) + { + uint64_t off = refs[i].ref.addr; + if (i > 0) + { + if (off == last_off) + continue; + relocation_skip (&data->rel, off, + &WHERE (sec, NULL), skip_unref); + } + if (!check_loc_or_range_ref (&ctx, refs[i].cu, data, + &coverage, coverage_map, + off, refs[i].cu->address_size == 8, + &refs[i].ref.who, cat)) + retval = false; + last_off = off; + } + + if (retval) + { + relocation_skip_rest (data); + + /* We check that all CUs have the same address size when building + the CU chain. So just take the address size of the first CU in + chain. */ + coverage_find_holes (&coverage, 0, ctx.data->d_size, found_hole, + &((struct hole_info) + {sec, cat, cu_chain->address_size, + ctx.data->d_buf})); + + if (coverage_map) + coverage_map_find_holes (coverage_map, &coverage_map_found_hole, + &(struct coverage_map_hole_info) + {{sec, cat, 0, NULL}, + coverage_map->elf}); + } + + + coverage_free (&coverage); + coverage_map_free_XA (coverage_map); + + return retval; +} + +static GElf_Rela * +get_rel_or_rela (Elf_Data *data, int ndx, + GElf_Rela *dst, size_t type) +{ + if (type == SHT_RELA) + return gelf_getrela (data, ndx, dst); + else + { + assert (type == SHT_REL); + GElf_Rel rel_mem; + if (gelf_getrel (data, ndx, &rel_mem) == NULL) + return NULL; + dst->r_offset = rel_mem.r_offset; + dst->r_info = rel_mem.r_info; + dst->r_addend = 0; + return dst; + } +} + +static bool +read_rel (struct section_data *secdata, Elf_Data *reldata, bool elf_64) +{ + assert (secdata->rel.type == SHT_REL + || secdata->rel.type == SHT_RELA); + bool is_rela = secdata->rel.type == SHT_RELA; + + struct read_ctx ctx; + read_ctx_init (&ctx, secdata->file->dwarf, secdata->data); + + size_t entrysize + = elf_64 + ? (is_rela ? sizeof (Elf64_Rela) : sizeof (Elf64_Rel)) + : (is_rela ? sizeof (Elf32_Rela) : sizeof (Elf32_Rel)); + size_t count = reldata->d_size / entrysize; + + struct where parent = WHERE_SECDATA (secdata, NULL); + struct where where = WHERE (is_rela ? sec_rela : sec_rel, NULL); + where.ref = &parent; + + for (unsigned i = 0; i < count; ++i) + { + where_reset_1 (&where, i); + + REALLOC (&secdata->rel, rel); + struct relocation *cur = secdata->rel.rel + secdata->rel.size++; + WIPE (*cur); + + GElf_Rela rela_mem, *rela + = get_rel_or_rela (reldata, i, &rela_mem, secdata->rel.type); + if (rela == NULL) + { + wr_error (&where, ": couldn't read relocation.\n"); + skip: + cur->invalid = true; + continue; + } + + int cur_type = GELF_R_TYPE (rela->r_info); + if (cur_type == 0) /* No relocation. */ + { + wr_message (mc_impact_3 | mc_reloc | mc_acc_bloat, &where, + ": NONE relocation is superfluous.\n"); + goto skip; + } + + cur->offset = rela->r_offset; + cur->symndx = GELF_R_SYM (rela->r_info); + cur->type = cur_type; + + where_reset_2 (&where, cur->offset); + + Elf_Type type = ebl_reloc_simple_type (secdata->file->ebl, cur->type); + int width; + + switch (type) + { + case ELF_T_WORD: + case ELF_T_SWORD: + width = 4; + break; + + case ELF_T_XWORD: + case ELF_T_SXWORD: + width = 8; + break; + + case ELF_T_BYTE: + case ELF_T_HALF: + /* Technically legal, but never used. Better have dwarflint + flag them as erroneous, because it's more likely these + are a result of a bug than actually being used. */ + { + char buf[64]; + wr_error (&where, ": 8 or 16-bit relocation type %s.\n", + ebl_reloc_type_name (secdata->file->ebl, + cur->type, buf, sizeof (buf))); + goto skip; + } + + default: + { + char buf[64]; + wr_error (&where, ": invalid relocation %d (%s).\n", + cur->type, + ebl_reloc_type_name (secdata->file->ebl, + cur->type, buf, sizeof (buf))); + goto skip; + } + }; + + if (cur->offset + width >= secdata->data->d_size) + { + wr_error (&where, + ": relocation doesn't fall into relocated section.\n"); + goto skip; + } + + uint64_t value; + if (width == 4) + value = read_4ubyte_unaligned (secdata->file->dwarf, + secdata->data->d_buf + cur->offset); + else + { + assert (width == 8); + value = read_8ubyte_unaligned (secdata->file->dwarf, + secdata->data->d_buf + cur->offset); + } + + if (is_rela) + { + if (value != 0) + wr_message (mc_impact_2 | mc_reloc, &where, + ": SHR_RELA relocates a place with non-zero value (addend=%#" + PRIx64", value=%#"PRIx64").\n", rela->r_addend, value); + cur->addend = rela->r_addend; + } + else + cur->addend = value; + } + + /* Sort the reloc section so that the applicable addresses of + relocation entries are monotonously increasing. */ + int compare (const void *a, const void *b) + { + return ((struct relocation *)a)->offset + - ((struct relocation *)b)->offset; + } + + qsort (secdata->rel.rel, secdata->rel.size, + sizeof (*secdata->rel.rel), &compare); + return true; +} + +static bool +check_line_structural (struct section_data *data, + struct cu *cu_chain) +{ + struct read_ctx ctx; + read_ctx_init (&ctx, data->file->dwarf, data->data); + bool retval = true; + + struct addr_record line_tables; + WIPE (line_tables); + + while (!read_ctx_eof (&ctx)) + { + struct where where = WHERE_SECDATA (data, NULL); + uint64_t set_offset = read_ctx_get_offset (&ctx); + where_reset_1 (&where, set_offset); + addr_record_add (&line_tables, set_offset); + const unsigned char *set_begin = ctx.ptr; + + /* Size. */ + uint32_t size32; + uint64_t size; + bool dwarf_64; + if (!read_ctx_read_4ubyte (&ctx, &size32)) + { + wr_error (&where, ": can't read table length.\n"); + return false; + } + if (!read_size_extra (&ctx, size32, &size, &dwarf_64, &where)) + return false; + + struct read_ctx sub_ctx; + const unsigned char *set_end = ctx.ptr + size; + if (!read_ctx_init_sub (&sub_ctx, &ctx, set_begin, set_end)) + { + not_enough: + wr_error (&where, PRI_NOT_ENOUGH, "next unit"); + return false; + } + sub_ctx.ptr = ctx.ptr; + sub_ctx.begin = ctx.begin; + + { + /* Version. */ + uint16_t version; + if (!read_ctx_read_2ubyte (&sub_ctx, &version)) + { + wr_error (&where, ": can't read set version.\n"); + skip: + retval = false; + goto next; + } + if (!supported_version (version, 2, &where, 2, 3)) + goto skip; + + /* Header length. */ + uint64_t header_length; + if (!read_ctx_read_offset (&sub_ctx, dwarf_64, &header_length)) + { + wr_error (&where, ": can't read attribute value.\n"); + goto skip; + } + const unsigned char *program_start = sub_ctx.ptr + header_length; + + /* Minimum instruction length. */ + uint8_t minimum_i_length; + if (!read_ctx_read_ubyte (&sub_ctx, &minimum_i_length)) + { + wr_error (&where, ": can't read minimum instruction length.\n"); + goto skip; + } + + /* Default value of is_stmt. */ + uint8_t default_is_stmt; + if (!read_ctx_read_ubyte (&sub_ctx, &default_is_stmt)) + { + wr_error (&where, ": can't read default_is_stmt.\n"); + goto skip; + } + /* 7.21: The boolean values "true" and "false" used by the line + number information program are encoded as a single byte + containing the value 0 for "false," and a non-zero value for + "true." [But give a notice if it's not 0 or 1.] */ + if (default_is_stmt != 0 + && default_is_stmt != 1) + wr_message (mc_line | mc_impact_2 | mc_header, &where, + ": default_is_stmt should be 0 or 1, not %ud\n", + default_is_stmt); + + /* Line base. */ + int8_t line_base; + if (!read_ctx_read_ubyte (&sub_ctx, (uint8_t *)&line_base)) + { + wr_error (&where, ": can't read line_base.\n"); + goto skip; + } + + /* Line range. */ + uint8_t line_range; + if (!read_ctx_read_ubyte (&sub_ctx, &line_range)) + { + wr_error (&where, ": can't read line_range.\n"); + goto skip; + } + + /* Opcode base. */ + uint8_t opcode_base; + if (!read_ctx_read_ubyte (&sub_ctx, &opcode_base)) + { + wr_error (&where, ": can't read opcode_base.\n"); + goto skip; + } + + /* Standard opcode lengths. */ + if (opcode_base == 0) + { + wr_error (&where, ": opcode base set to 0.\n"); + opcode_base = 1; // so that in following, our -1s don't underrun + } + uint8_t std_opc_lengths[opcode_base - 1]; /* -1, opcodes go from 1. */ + for (unsigned i = 0; i < (unsigned)(opcode_base - 1); ++i) + if (!read_ctx_read_ubyte (&sub_ctx, std_opc_lengths + i)) + { + wr_error (&where, + ": can't read length of standard opcode #%d.\n", i); + goto skip; + } + + /* Include directories. */ + struct include_directory_t + { + const char *name; + bool used; + }; + struct include_directories_t + { + size_t size; + size_t alloc; + struct include_directory_t *dirs; + } include_directories; + WIPE (include_directories); + + while (!read_ctx_eof (&sub_ctx)) + { + const char *name = read_ctx_read_str (&sub_ctx); + if (name == NULL) + { + wr_error (&where, + ": can't read name of include directory #%zd.\n", + include_directories.size + 1); /* Numbered from 1. */ + goto skip; + } + if (*name == 0) + break; + + REALLOC (&include_directories, dirs); + include_directories.dirs[include_directories.size++] = + (struct include_directory_t){name, false}; + } + + /* File names. */ + struct file_t + { + const char *name; + uint64_t dir_idx; + bool used; + }; + struct files_t + { + size_t size; + size_t alloc; + struct file_t *files; + } files; + WIPE (files); + + /* Directory index. */ + bool read_directory_index (const char *name, uint64_t *ptr) + { + if (!checked_read_uleb128 (&sub_ctx, ptr, + &where, "directory index")) + return false; + if (*name == '/' && *ptr != 0) + wr_message (mc_impact_2 | mc_line | mc_header, &where, + ": file #%zd has absolute pathname, but refers to directory != 0.\n", + files.size + 1); + if (*ptr > include_directories.size) /* Not >=, dirs indexed from 1. */ + { + wr_message (mc_impact_4 | mc_line | mc_header, &where, + ": file #%zd refers to directory #%" PRId64 ", which wasn't defined.\n", + files.size + 1, *ptr); + /* Consumer might choke on that. */ + retval = false; + } + else if (*ptr != 0) + include_directories.dirs[*ptr - 1].used = true; + return true; + } + + while (1) + { + const char *name = read_ctx_read_str (&sub_ctx); + if (name == NULL) + { + wr_error (&where, + ": can't read name of file #%zd.\n", + files.size + 1); /* Numbered from 1. */ + goto skip; + } + if (*name == 0) + break; + + uint64_t dir_idx; + if (!read_directory_index (name, &dir_idx)) + goto skip; + + /* Time of last modification. */ + uint64_t timestamp; + if (!checked_read_uleb128 (&sub_ctx, ×tamp, + &where, "timestamp of file entry")) + goto skip; + + /* Size of the file. */ + uint64_t file_size; + if (!checked_read_uleb128 (&sub_ctx, &file_size, + &where, "file size of file entry")) + goto skip; + + REALLOC (&files, files); + files.files[files.size++] + = (struct file_t){name, dir_idx, false}; + } + + /* Skip the rest of the header. */ + if (sub_ctx.ptr > program_start) + { + wr_error (&where, + ": header claims that it has a size of %#" PRIx64 + ", but in fact it has a size of %#" PRIx64 ".\n", + header_length, sub_ctx.ptr - program_start + header_length); + /* Assume that the header lies, and what follows is in + fact line number program. */ + retval = false; + } + else if (sub_ctx.ptr < program_start) + { + if (!check_zero_padding (&sub_ctx, mc_line | mc_header, &where)) + wr_message_padding_n0 (mc_line | mc_header, &WHERE (sec_line, NULL), + read_ctx_get_offset (&sub_ctx), + program_start - sub_ctx.begin - 1); + sub_ctx.ptr = program_start; + } + + bool terminated = false; + bool first_file = true; + bool seen_opcode = false; + while (!read_ctx_eof (&sub_ctx)) + { + where_reset_2 (&where, read_ctx_get_offset (&sub_ctx)); + uint8_t opcode; + if (!read_ctx_read_ubyte (&sub_ctx, &opcode)) + { + wr_error (&where, ": can't read opcode.\n"); + goto skip; + } + + void use_file (uint64_t file_idx) + { + if (file_idx == 0 || file_idx > files.size) + { + wr_error (&where, + ": DW_LNS_set_file: invalid file index %" PRId64 ".\n", + file_idx); + retval = false; + } + else + files.files[file_idx - 1].used = true; + } + + unsigned operands = 0; + uint8_t extended = 0; + switch (opcode) + { + /* Extended opcodes. */ + case 0: + { + uint64_t skip_len; + if (!checked_read_uleb128 (&sub_ctx, &skip_len, &where, + "length of extended opcode")) + goto skip; + const unsigned char *next = sub_ctx.ptr + skip_len; + if (!read_ctx_read_ubyte (&sub_ctx, &extended)) + { + wr_error (&where, ": can't read extended opcode.\n"); + goto skip; + } + + bool handled = true; + switch (extended) + { + case DW_LNE_end_sequence: + terminated = true; + break; + + case DW_LNE_set_address: + { + uint64_t ctx_offset = read_ctx_get_offset (&sub_ctx); + uint64_t addr; + if (!read_ctx_read_offset (&sub_ctx, data->file->addr_64, &addr)) + { + wr_error (&where, ": can't read operand of DW_LNE_set_address.\n"); + goto skip; + } + + struct relocation *rel; + if ((rel = relocation_next (&data->rel, ctx_offset, + &where, skip_mismatched))) + relocate_one (&data->rel, rel, data->file->addr_64 ? 8 : 4, + &addr, &where, rel_address, NULL); + else if (data->file->ehdr.e_type == ET_REL) + wr_message (mc_impact_2 | mc_line | mc_reloc, &where, + PRI_LACK_RELOCATION, "DW_LNE_set_address"); + break; + } + + case DW_LNE_define_file: + { + const char *name; + if ((name = read_ctx_read_str (&sub_ctx)) == NULL) + { + wr_error (&where, + ": can't read filename operand of DW_LNE_define_file.\n"); + goto skip; + } + uint64_t dir_idx; + if (!read_directory_index (name, &dir_idx)) + goto skip; + REALLOC (&files, files); + files.files[files.size++] = + (struct file_t){name, dir_idx, false}; + operands = 2; /* Skip mtime & size of the file. */ + } + + /* See if we know about any other standard opcodes. */ + default: + handled = false; + switch (extended) + { +#define ONE_KNOWN_DW_LNE(NAME, CODE) case CODE: break; + ALL_KNOWN_DW_LNE +#undef ONE_KNOWN_DW_LNE + default: + /* No we don't, emit a warning. */ + wr_message (mc_impact_2 | mc_line, &where, + ": unknown extended opcode #%d.\n", extended); + }; + }; + + if (sub_ctx.ptr > next) + { + wr_error (&where, + ": opcode claims that it has a size of %#" PRIx64 + ", but in fact it has a size of %#" PRIx64 ".\n", + skip_len, skip_len + (next - sub_ctx.ptr)); + retval = false; + } + else if (sub_ctx.ptr < next) + { + if (handled + && !check_zero_padding (&sub_ctx, mc_line, &where)) + wr_message_padding_n0 (mc_line, &WHERE (sec_line, NULL), + read_ctx_get_offset (&sub_ctx), + next - sub_ctx.begin - 1); + sub_ctx.ptr = next; + } + break; + } + + /* Standard opcodes that need validation or have + non-ULEB operands. */ + case DW_LNS_fixed_advance_pc: + { + uint16_t a; + if (!read_ctx_read_2ubyte (&sub_ctx, &a)) + { + wr_error (&where, ": can't read operand of DW_LNS_fixed_advance_pc.\n"); + goto skip; + } + break; + } + + case DW_LNS_set_file: + { + uint64_t file_idx; + if (!checked_read_uleb128 (&sub_ctx, &file_idx, &where, + "DW_LNS_set_file operand")) + goto skip; + use_file (file_idx); + first_file = false; + } + break; + + case DW_LNS_set_isa: + // XXX is it possible to validate this? + operands = 1; + break; + + /* All the other opcodes. */ + default: + if (opcode < opcode_base) + operands = std_opc_lengths[opcode - 1]; + + switch (opcode) + { +#define ONE_KNOWN_DW_LNS(NAME, CODE) case CODE: break; + ALL_KNOWN_DW_LNS +#undef ONE_KNOWN_DW_LNS + + default: + if (opcode < opcode_base) + wr_message (mc_impact_2 | mc_line, &where, + ": unknown standard opcode #%d.\n", opcode); + }; + }; + + for (unsigned i = 0; i < operands; ++i) + { + uint64_t operand; + char buf[128]; + if (opcode != 0) + sprintf (buf, "operand #%d of DW_LNS_%s", + i, dwarf_locexpr_opcode_string (opcode)); + else + sprintf (buf, "operand #%d of extended opcode %d", + i, extended); + if (!checked_read_uleb128 (&sub_ctx, &operand, &where, buf)) + goto skip; + } + + if (first_file) + { + use_file (1); + first_file = false; + } + + if (opcode != 0 || extended != DW_LNE_end_sequence) + seen_opcode = true; + } + + for (size_t i = 0; i < include_directories.size; ++i) + if (!include_directories.dirs[i].used) + wr_message (mc_impact_3 | mc_acc_bloat | mc_line | mc_header, + &where, ": the include #%zd `%s' is not used.\n", + i + 1, include_directories.dirs[i].name); + + for (size_t i = 0; i < files.size; ++i) + if (!files.files[i].used) + wr_message (mc_impact_3 | mc_acc_bloat | mc_line | mc_header, + &where, ": the file #%zd `%s' is not used.\n", + i + 1, files.files[i].name); + + if (!seen_opcode) + wr_message (mc_line | mc_acc_bloat | mc_impact_3, &where, + ": empty line number program.\n"); + if (!terminated) + { + if (seen_opcode) + wr_error (&where, + ": sequence of opcodes not terminated with DW_LNE_end_sequence.\n"); + } + else if (sub_ctx.ptr != sub_ctx.end + && !check_zero_padding (&sub_ctx, mc_line, + &WHERE (sec_line, NULL))) + wr_message_padding_n0 (mc_line, &WHERE (sec_line, NULL), + /*begin*/read_ctx_get_offset (&sub_ctx), + /*end*/sub_ctx.end - sub_ctx.begin - 1); + } + + /* XXX overlaps in defined addresses are probably OK, one + instruction can be derived from several statements. But + certain flags in table should be consistent in that case, + namely is_stmt, basic_block, end_sequence, prologue_end, + epilogue_begin, isa. */ + + next: + if (!read_ctx_skip (&ctx, size)) + goto not_enough; + } + + if (retval) + { + relocation_skip_rest (data); + + for (struct cu *cu = cu_chain; cu != NULL; cu = cu->next) + for (size_t i = 0; i < cu->line_refs.size; ++i) + { + struct ref *ref = cu->line_refs.refs + i; + if (!addr_record_has_addr (&line_tables, ref->addr)) + wr_error (&ref->who, + ": unresolved reference to .debug_line table %#" PRIx64 ".\n", + ref->addr); + } + } + + return retval; +} diff --git a/src/dwarflint.h b/src/dwarflint.h new file mode 100644 index 00000000..13997a86 --- /dev/null +++ b/src/dwarflint.h @@ -0,0 +1,241 @@ +#ifndef DWARFLINT_HL_H +#define DWARFLINT_HL_H + +#include "../libdw/libdw.h" +#include "../libebl/libebl.h" + +#ifdef __cplusplus +extern "C" +{ +#else +# include <stdbool.h> +#endif + + /* Entry points for high-level checks. */ + + struct hl_ctx; + + /* Check that .debug_aranges and .debug_ranges match. */ + extern struct hl_ctx *hl_ctx_new (Dwarf *dwarf); + extern void hl_ctx_delete (struct hl_ctx *hlctx); + extern bool check_matching_ranges (struct hl_ctx *hlctx); + extern bool check_expected_trees (struct hl_ctx *hlctx); + + + /* Functions and data structures describing location in Dwarf. */ + + enum section_id + { + sec_invalid = 0, + rel_value, /* For relocations, this denotes that the + relocation is applied to taget value, not a + section offset. */ + rel_address, /* Same as above, but for addresses. */ + rel_exec, /* Some as above, but we expect EXEC bit. */ + + /* Debuginfo sections: */ + sec_info, + sec_abbrev, + sec_aranges, + sec_pubnames, + sec_pubtypes, + sec_str, + sec_line, + sec_loc, + sec_mac, + sec_ranges, + sec_locexpr, /* Not a section, but a portion of file that + contains a location expression. */ + /* Non-debuginfo sections: */ + sec_rel, + sec_rela, + }; + + enum where_formatting + { + wf_plain = 0, /* Default formatting for given section. */ + wf_cudie, + }; + + struct where + { + enum section_id section; + uint64_t addr1; // E.g. a CU offset. + uint64_t addr2; // E.g. a DIE address. + uint64_t addr3; // E.g. an attribute. + enum where_formatting formatting; + struct where *ref; // Related reference, e.g. an abbrev related to given DIE. + struct where *next; // For forming "caused-by" chains. + }; + +# define WHERE(SECTION, NEXT) \ + ((struct where) \ + {(SECTION), (uint64_t)-1, (uint64_t)-1, (uint64_t)-1, wf_plain, NULL, NEXT}) + + extern const char *where_fmt (const struct where *wh, char *ptr); + extern void where_fmt_chain (const struct where *wh, const char *severity); + extern void where_reset_1 (struct where *wh, uint64_t addr); + extern void where_reset_2 (struct where *wh, uint64_t addr); + extern void where_reset_3 (struct where *wh, uint64_t addr); + + + /* Functions and data structures for emitting various types of + messages. */ + +#define MESSAGE_CATEGORIES \ + /* Severity: */ \ + MC (impact_1, 0) /* no impact on the consumer */ \ + MC (impact_2, 1) /* still no impact, but suspicious or worth mentioning */ \ + MC (impact_3, 2) /* some impact */ \ + MC (impact_4, 3) /* high impact */ \ + \ + /* Accuracy: */ \ + MC (acc_bloat, 4) /* unnecessary constructs (e.g. unreferenced strings) */ \ + MC (acc_suboptimal, 5) /* suboptimal construct (e.g. lack of siblings) */ \ + \ + /* Various: */ \ + MC (error, 6) /* turn the message into an error */ \ + \ + /* Area: */ \ + MC (leb128, 7) /* ULEB/SLEB storage */ \ + MC (abbrevs, 8) /* abbreviations and abbreviation tables */ \ + MC (die_rel, 9) /* DIE relationship */ \ + MC (die_other, 10) /* other messages related to DIEs */ \ + MC (info, 11) /* messages related to .debug_info, but not particular DIEs */ \ + MC (strings, 12) /* string table */ \ + MC (aranges, 13) /* address ranges table */ \ + MC (elf, 14) /* ELF structure, e.g. missing optional sections */ \ + MC (pubtables, 15) /* table of public names/types */ \ + MC (pubtypes, 16) /* .debug_pubtypes presence */ \ + MC (loc, 17) /* messages related to .debug_loc */ \ + MC (ranges, 18) /* messages related to .debug_ranges */ \ + MC (line, 19) /* messages related to .debug_line */ \ + MC (reloc, 20) /* messages related to relocation handling */ \ + MC (header, 21) /* messages related to header portions in general */ \ + MC (other, 31) /* messages unrelated to any of the above */ + + enum message_category + { + mc_none = 0, + +#define MC(CAT, ID)\ + mc_##CAT = 1u << ID, + MESSAGE_CATEGORIES +#undef MC + }; + + extern void wr_error (const struct where *wh, const char *format, ...) + __attribute__ ((format (printf, 2, 3))); + + extern void wr_warning (const struct where *wh, const char *format, ...) + __attribute__ ((format (printf, 2, 3))); + + extern void wr_message (enum message_category category, const struct where *wh, + const char *format, ...) + __attribute__ ((format (printf, 3, 4))); + + extern void wr_format_padding_message (enum message_category category, + struct where *wh, + uint64_t start, uint64_t end, + char *kind); + + extern void wr_format_leb128_message (struct where *where, const char *what, + const char *purpose, + const unsigned char *begin, + const unsigned char *end); + + extern void wr_message_padding_0 (enum message_category category, + struct where *wh, + uint64_t start, uint64_t end); + + extern void wr_message_padding_n0 (enum message_category category, + struct where *wh, + uint64_t start, uint64_t end); + +# include "dwarflint-coverage.h" + + struct sec + { + Elf_Scn *scn; + GElf_Shdr shdr; + enum section_id id; + const char *name; + }; + + struct elf_file + { + Dwarf *dwarf; + Ebl *ebl; + GElf_Ehdr ehdr; /* Header of dwarf->elf. */ + bool addr_64; + + struct sec *sec; /* Array of sections. */ + size_t size; + size_t alloc; + }; + + struct section_coverage + { + struct sec *sec; + struct coverage cov; + bool hit; /* true if COV is not pristine. */ + bool warn; /* dwarflint should emit a warning if a coverage + appears in this section */ + }; + + struct coverage_map + { + struct elf_file *elf; + bool allow_overlap; + + struct section_coverage *scos; + size_t size; + size_t alloc; + }; + + void section_coverage_init (struct section_coverage *sco, + struct sec *sec, bool warn); + bool coverage_map_init (struct coverage_map *coverage_map, + struct elf_file *elf, + Elf64_Xword mask, + Elf64_Xword warn_mask, + bool allow_overlap); + void coverage_map_add (struct coverage_map *coverage_map, + uint64_t address, uint64_t length, + struct where *where, enum message_category cat); + bool coverage_map_find_holes (struct coverage_map *coverage_map, + bool (*cb) (uint64_t, uint64_t, + struct section_coverage *, void *), + void *user); + void coverage_map_free (struct coverage_map *coverage_map); + + + struct hole_info + { + enum section_id section; + enum message_category category; + unsigned align; + void *data; + }; + + /* DATA has to be a pointer to an instance of struct hole_info. + DATA->data has to point at d_buf of section in question. */ + bool found_hole (uint64_t begin, uint64_t end, void *data); + + struct coverage_map_hole_info + { + struct hole_info info; + struct elf_file *elf; + }; + + /* DATA has to be a pointer to an instance of struct hole_info. + DATA->info.data has to be NULL, it is used by the callback. */ + bool coverage_map_found_hole (uint64_t begin, uint64_t end, + struct section_coverage *sco, void *data); + + +#ifdef __cplusplus +} +#endif + +#endif/*DWARFLINT_HL_H*/ diff --git a/src/dwarfstrings.c b/src/dwarfstrings.c new file mode 100644 index 00000000..34c86d34 --- /dev/null +++ b/src/dwarfstrings.c @@ -0,0 +1,628 @@ +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "../libdw/known-dwarf.h" +#include "../libdw/dwarf.h" +#include "dwarfstrings.h" +#include <inttypes.h> +#include <stdio.h> +#include <libintl.h> + +const char * +dwarf_tag_string (unsigned int tag) +{ + static const char *const known_tags[] = + { + [DW_TAG_array_type] = "array_type", + [DW_TAG_class_type] = "class_type", + [DW_TAG_entry_point] = "entry_point", + [DW_TAG_enumeration_type] = "enumeration_type", + [DW_TAG_formal_parameter] = "formal_parameter", + [DW_TAG_imported_declaration] = "imported_declaration", + [DW_TAG_label] = "label", + [DW_TAG_lexical_block] = "lexical_block", + [DW_TAG_member] = "member", + [DW_TAG_pointer_type] = "pointer_type", + [DW_TAG_reference_type] = "reference_type", + [DW_TAG_compile_unit] = "compile_unit", + [DW_TAG_string_type] = "string_type", + [DW_TAG_structure_type] = "structure_type", + [DW_TAG_subroutine_type] = "subroutine_type", + [DW_TAG_typedef] = "typedef", + [DW_TAG_union_type] = "union_type", + [DW_TAG_unspecified_parameters] = "unspecified_parameters", + [DW_TAG_variant] = "variant", + [DW_TAG_common_block] = "common_block", + [DW_TAG_common_inclusion] = "common_inclusion", + [DW_TAG_inheritance] = "inheritance", + [DW_TAG_inlined_subroutine] = "inlined_subroutine", + [DW_TAG_module] = "module", + [DW_TAG_ptr_to_member_type] = "ptr_to_member_type", + [DW_TAG_set_type] = "set_type", + [DW_TAG_subrange_type] = "subrange_type", + [DW_TAG_with_stmt] = "with_stmt", + [DW_TAG_access_declaration] = "access_declaration", + [DW_TAG_base_type] = "base_type", + [DW_TAG_catch_block] = "catch_block", + [DW_TAG_const_type] = "const_type", + [DW_TAG_constant] = "constant", + [DW_TAG_enumerator] = "enumerator", + [DW_TAG_file_type] = "file_type", + [DW_TAG_friend] = "friend", + [DW_TAG_namelist] = "namelist", + [DW_TAG_namelist_item] = "namelist_item", + [DW_TAG_packed_type] = "packed_type", + [DW_TAG_subprogram] = "subprogram", + [DW_TAG_template_type_parameter] = "template_type_parameter", + [DW_TAG_template_value_parameter] = "template_value_parameter", + [DW_TAG_thrown_type] = "thrown_type", + [DW_TAG_try_block] = "try_block", + [DW_TAG_variant_part] = "variant_part", + [DW_TAG_variable] = "variable", + [DW_TAG_volatile_type] = "volatile_type", + [DW_TAG_dwarf_procedure] = "dwarf_procedure", + [DW_TAG_restrict_type] = "restrict_type", + [DW_TAG_interface_type] = "interface_type", + [DW_TAG_namespace] = "namespace", + [DW_TAG_imported_module] = "imported_module", + [DW_TAG_unspecified_type] = "unspecified_type", + [DW_TAG_partial_unit] = "partial_unit", + [DW_TAG_imported_unit] = "imported_unit", + [DW_TAG_mutable_type] = "mutable_type", + [DW_TAG_condition] = "condition", + [DW_TAG_shared_type] = "shared_type", + }; + const unsigned int nknown_tags = (sizeof (known_tags) + / sizeof (known_tags[0])); + static char buf[40]; + const char *result = NULL; + + if (likely (tag < nknown_tags)) + result = known_tags[tag]; + + if (unlikely (result == NULL)) + /* There are a few known extensions. */ + switch (tag) + { + case DW_TAG_MIPS_loop: + result = "MIPS_loop"; + break; + + case DW_TAG_format_label: + result = "format_label"; + break; + + case DW_TAG_function_template: + result = "function_template"; + break; + + case DW_TAG_class_template: + result = "class_template"; + break; + + default: + if (tag < DW_TAG_lo_user) + snprintf (buf, sizeof buf, gettext ("unknown tag %hx"), tag); + else + snprintf (buf, sizeof buf, gettext ("unknown user tag %hx"), tag); + result = buf; + break; + } + + return result; +} + + +const char * +dwarf_attr_string (unsigned int attrnum) +{ + static const char *const known_attrs[] = + { + [DW_AT_sibling] = "sibling", + [DW_AT_location] = "location", + [DW_AT_name] = "name", + [DW_AT_ordering] = "ordering", + [DW_AT_subscr_data] = "subscr_data", + [DW_AT_byte_size] = "byte_size", + [DW_AT_bit_offset] = "bit_offset", + [DW_AT_bit_size] = "bit_size", + [DW_AT_element_list] = "element_list", + [DW_AT_stmt_list] = "stmt_list", + [DW_AT_low_pc] = "low_pc", + [DW_AT_high_pc] = "high_pc", + [DW_AT_language] = "language", + [DW_AT_member] = "member", + [DW_AT_discr] = "discr", + [DW_AT_discr_value] = "discr_value", + [DW_AT_visibility] = "visibility", + [DW_AT_import] = "import", + [DW_AT_string_length] = "string_length", + [DW_AT_common_reference] = "common_reference", + [DW_AT_comp_dir] = "comp_dir", + [DW_AT_const_value] = "const_value", + [DW_AT_containing_type] = "containing_type", + [DW_AT_default_value] = "default_value", + [DW_AT_inline] = "inline", + [DW_AT_is_optional] = "is_optional", + [DW_AT_lower_bound] = "lower_bound", + [DW_AT_producer] = "producer", + [DW_AT_prototyped] = "prototyped", + [DW_AT_return_addr] = "return_addr", + [DW_AT_start_scope] = "start_scope", + [DW_AT_bit_stride] = "bit_stride", + [DW_AT_upper_bound] = "upper_bound", + [DW_AT_abstract_origin] = "abstract_origin", + [DW_AT_accessibility] = "accessibility", + [DW_AT_address_class] = "address_class", + [DW_AT_artificial] = "artificial", + [DW_AT_base_types] = "base_types", + [DW_AT_calling_convention] = "calling_convention", + [DW_AT_count] = "count", + [DW_AT_data_member_location] = "data_member_location", + [DW_AT_decl_column] = "decl_column", + [DW_AT_decl_file] = "decl_file", + [DW_AT_decl_line] = "decl_line", + [DW_AT_declaration] = "declaration", + [DW_AT_discr_list] = "discr_list", + [DW_AT_encoding] = "encoding", + [DW_AT_external] = "external", + [DW_AT_frame_base] = "frame_base", + [DW_AT_friend] = "friend", + [DW_AT_identifier_case] = "identifier_case", + [DW_AT_macro_info] = "macro_info", + [DW_AT_namelist_item] = "namelist_item", + [DW_AT_priority] = "priority", + [DW_AT_segment] = "segment", + [DW_AT_specification] = "specification", + [DW_AT_static_link] = "static_link", + [DW_AT_type] = "type", + [DW_AT_use_location] = "use_location", + [DW_AT_variable_parameter] = "variable_parameter", + [DW_AT_virtuality] = "virtuality", + [DW_AT_vtable_elem_location] = "vtable_elem_location", + [DW_AT_allocated] = "allocated", + [DW_AT_associated] = "associated", + [DW_AT_data_location] = "data_location", + [DW_AT_byte_stride] = "byte_stride", + [DW_AT_entry_pc] = "entry_pc", + [DW_AT_use_UTF8] = "use_UTF8", + [DW_AT_extension] = "extension", + [DW_AT_ranges] = "ranges", + [DW_AT_trampoline] = "trampoline", + [DW_AT_call_column] = "call_column", + [DW_AT_call_file] = "call_file", + [DW_AT_call_line] = "call_line", + [DW_AT_description] = "description", + [DW_AT_binary_scale] = "binary_scale", + [DW_AT_decimal_scale] = "decimal_scale", + [DW_AT_small] = "small", + [DW_AT_decimal_sign] = "decimal_sign", + [DW_AT_digit_count] = "digit_count", + [DW_AT_picture_string] = "picture_string", + [DW_AT_mutable] = "mutable", + [DW_AT_threads_scaled] = "threads_scaled", + [DW_AT_explicit] = "explicit", + [DW_AT_object_pointer] = "object_pointer", + [DW_AT_endianity] = "endianity", + [DW_AT_elemental] = "elemental", + [DW_AT_pure] = "pure", + [DW_AT_recursive] = "recursive", + }; + const unsigned int nknown_attrs = (sizeof (known_attrs) + / sizeof (known_attrs[0])); + static char buf[40]; + const char *result = NULL; + + if (likely (attrnum < nknown_attrs)) + result = known_attrs[attrnum]; + + if (unlikely (result == NULL)) + /* There are a few known extensions. */ + switch (attrnum) + { + case DW_AT_MIPS_fde: + result = "MIPS_fde"; + break; + + case DW_AT_MIPS_loop_begin: + result = "MIPS_loop_begin"; + break; + + case DW_AT_MIPS_tail_loop_begin: + result = "MIPS_tail_loop_begin"; + break; + + case DW_AT_MIPS_epilog_begin: + result = "MIPS_epilog_begin"; + break; + + case DW_AT_MIPS_loop_unroll_factor: + result = "MIPS_loop_unroll_factor"; + break; + + case DW_AT_MIPS_software_pipeline_depth: + result = "MIPS_software_pipeline_depth"; + break; + + case DW_AT_MIPS_linkage_name: + result = "MIPS_linkage_name"; + break; + + case DW_AT_MIPS_stride: + result = "MIPS_stride"; + break; + + case DW_AT_MIPS_abstract_name: + result = "MIPS_abstract_name"; + break; + + case DW_AT_MIPS_clone_origin: + result = "MIPS_clone_origin"; + break; + + case DW_AT_MIPS_has_inlines: + result = "MIPS_has_inlines"; + break; + + case DW_AT_MIPS_stride_byte: + result = "MIPS_stride_byte"; + break; + + case DW_AT_MIPS_stride_elem: + result = "MIPS_stride_elem"; + break; + + case DW_AT_MIPS_ptr_dopetype: + result = "MIPS_ptr_dopetype"; + break; + + case DW_AT_MIPS_allocatable_dopetype: + result = "MIPS_allocatable_dopetype"; + break; + + case DW_AT_MIPS_assumed_shape_dopetype: + result = "MIPS_assumed_shape_dopetype"; + break; + + case DW_AT_MIPS_assumed_size: + result = "MIPS_assumed_size"; + break; + + case DW_AT_sf_names: + result = "sf_names"; + break; + + case DW_AT_src_info: + result = "src_info"; + break; + + case DW_AT_mac_info: + result = "mac_info"; + break; + + case DW_AT_src_coords: + result = "src_coords"; + break; + + case DW_AT_body_begin: + result = "body_begin"; + break; + + case DW_AT_body_end: + result = "body_end"; + break; + + default: + if (attrnum < DW_AT_lo_user) + snprintf (buf, sizeof buf, gettext ("unknown attribute %hx"), + attrnum); + else + snprintf (buf, sizeof buf, gettext ("unknown user attribute %hx"), + attrnum); + result = buf; + break; + } + + return result; +} + + +const char * +dwarf_form_string (unsigned int form) +{ + static const char *const known_forms[] = + { + [DW_FORM_addr] = "addr", + [DW_FORM_block2] = "block2", + [DW_FORM_block4] = "block4", + [DW_FORM_data2] = "data2", + [DW_FORM_data4] = "data4", + [DW_FORM_data8] = "data8", + [DW_FORM_string] = "string", + [DW_FORM_block] = "block", + [DW_FORM_block1] = "block1", + [DW_FORM_data1] = "data1", + [DW_FORM_flag] = "flag", + [DW_FORM_sdata] = "sdata", + [DW_FORM_strp] = "strp", + [DW_FORM_udata] = "udata", + [DW_FORM_ref_addr] = "ref_addr", + [DW_FORM_ref1] = "ref1", + [DW_FORM_ref2] = "ref2", + [DW_FORM_ref4] = "ref4", + [DW_FORM_ref8] = "ref8", + [DW_FORM_ref_udata] = "ref_udata", + [DW_FORM_indirect] = "indirect" + }; + const unsigned int nknown_forms = (sizeof (known_forms) + / sizeof (known_forms[0])); + static char buf[40]; + const char *result = NULL; + + if (likely (form < nknown_forms)) + result = known_forms[form]; + + if (unlikely (result == NULL)) + { + snprintf (buf, sizeof buf, gettext ("unknown form %" PRIx64), + (uint64_t) form); + result = buf; + } + + return result; +} + + +const char * +dwarf_lang_string (unsigned int lang) +{ + static const char *const known[] = + { + [DW_LANG_C89] = "ISO C89", + [DW_LANG_C] = "C", + [DW_LANG_Ada83] = "Ada83", + [DW_LANG_C_plus_plus] = "C++", + [DW_LANG_Cobol74] = "Cobol74", + [DW_LANG_Cobol85] = "Cobol85", + [DW_LANG_Fortran77] = "Fortran77", + [DW_LANG_Fortran90] = "Fortran90", + [DW_LANG_Pascal83] = "Pascal83", + [DW_LANG_Modula2] = "Modula2", + [DW_LANG_Java] = "Java", + [DW_LANG_C99] = "ISO C99", + [DW_LANG_Ada95] = "Ada95", + [DW_LANG_Fortran95] = "Fortran95", + [DW_LANG_PL1] = "PL1", + [DW_LANG_Objc] = "Objective C", + [DW_LANG_ObjC_plus_plus] = "Objective C++", + [DW_LANG_UPC] = "UPC", + [DW_LANG_D] = "D", + }; + + if (likely (lang < sizeof (known) / sizeof (known[0]))) + return known[lang]; + else if (lang == DW_LANG_Mips_Assembler) + /* This language tag is used for assembler in general. */ + return "Assembler"; + + if (lang >= DW_LANG_lo_user && lang <= DW_LANG_hi_user) + { + static char buf[30]; + snprintf (buf, sizeof (buf), "lo_user+%u", lang - DW_LANG_lo_user); + return buf; + } + + return "???"; +} + + +const char * +dwarf_inline_string (unsigned int code) +{ + static const char *const known[] = + { + [DW_INL_not_inlined] = "not_inlined", + [DW_INL_inlined] = "inlined", + [DW_INL_declared_not_inlined] = "declared_not_inlined", + [DW_INL_declared_inlined] = "declared_inlined" + }; + + if (likely (code < sizeof (known) / sizeof (known[0]))) + return known[code]; + + return "???"; +} + + +const char * +dwarf_encoding_string (unsigned int code) +{ + static const char *const known[] = + { + [DW_ATE_void] = "void", + [DW_ATE_address] = "address", + [DW_ATE_boolean] = "boolean", + [DW_ATE_complex_float] = "complex_float", + [DW_ATE_float] = "float", + [DW_ATE_signed] = "signed", + [DW_ATE_signed_char] = "signed_char", + [DW_ATE_unsigned] = "unsigned", + [DW_ATE_unsigned_char] = "unsigned_char", + [DW_ATE_imaginary_float] = "imaginary_float", + [DW_ATE_packed_decimal] = "packed_decimal", + [DW_ATE_numeric_string] = "numeric_string", + [DW_ATE_edited] = "edited", + [DW_ATE_signed_fixed] = "signed_fixed", + [DW_ATE_unsigned_fixed] = "unsigned_fixed", + [DW_ATE_decimal_float] = "decimal_float", + }; + + if (likely (code < sizeof (known) / sizeof (known[0]))) + return known[code]; + + if (code >= DW_ATE_lo_user && code <= DW_ATE_hi_user) + { + static char buf[30]; + snprintf (buf, sizeof (buf), "lo_user+%u", code - DW_ATE_lo_user); + return buf; + } + + return "???"; +} + + +const char * +dwarf_access_string (unsigned int code) +{ + static const char *const known[] = + { + [DW_ACCESS_public] = "public", + [DW_ACCESS_protected] = "protected", + [DW_ACCESS_private] = "private" + }; + + if (likely (code < sizeof (known) / sizeof (known[0]))) + return known[code]; + + return "???"; +} + + +const char * +dwarf_visibility_string (unsigned int code) +{ + static const char *const known[] = + { + [DW_VIS_local] = "local", + [DW_VIS_exported] = "exported", + [DW_VIS_qualified] = "qualified" + }; + + if (likely (code < sizeof (known) / sizeof (known[0]))) + return known[code]; + + return "???"; +} + + +const char * +dwarf_virtuality_string (unsigned int code) +{ + static const char *const known[] = + { + [DW_VIRTUALITY_none] = "none", + [DW_VIRTUALITY_virtual] = "virtual", + [DW_VIRTUALITY_pure_virtual] = "pure_virtual" + }; + + if (likely (code < sizeof (known) / sizeof (known[0]))) + return known[code]; + + return "???"; +} + + +const char * +dwarf_identifier_case_string (unsigned int code) +{ + static const char *const known[] = + { + [DW_ID_case_sensitive] = "sensitive", + [DW_ID_up_case] = "up_case", + [DW_ID_down_case] = "down_case", + [DW_ID_case_insensitive] = "insensitive" + }; + + if (likely (code < sizeof (known) / sizeof (known[0]))) + return known[code]; + + return "???"; +} + + +const char * +dwarf_calling_convention_string (unsigned int code) +{ + static const char *const known[] = + { + [DW_CC_normal] = "normal", + [DW_CC_program] = "program", + [DW_CC_nocall] = "nocall", + }; + + if (likely (code < sizeof (known) / sizeof (known[0]))) + return known[code]; + + if (code >= DW_CC_lo_user && code <= DW_CC_hi_user) + { + static char buf[30]; + snprintf (buf, sizeof (buf), "lo_user+%u", code - DW_CC_lo_user); + return buf; + } + + return "???"; +} + + +const char * +dwarf_ordering_string (unsigned int code) +{ + static const char *const known[] = + { + [DW_ORD_row_major] = "row_major", + [DW_ORD_col_major] = "col_major" + }; + + if (likely (code < sizeof (known) / sizeof (known[0]))) + return known[code]; + + return "???"; +} + + +const char * +dwarf_discr_list_string (unsigned int code) +{ + static const char *const known[] = + { + [DW_DSC_label] = "label", + [DW_DSC_range] = "range" + }; + + if (likely (code < sizeof (known) / sizeof (known[0]))) + return known[code]; + + return "???"; +} + + +const char * +dwarf_locexpr_opcode_string (unsigned int code) +{ + static const char *const known[] = + { + /* Normally we can't affort building huge table of 64K entries, + most of them zero, just because there are a couple defined + values at the far end. In case of opcodes, it's OK. */ +#define ONE_KNOWN_DW_OP_DESC(NAME, CODE, DESC) ONE_KNOWN_DW_OP(NAME, CODE) +#define ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME, + ALL_KNOWN_DW_OP +#undef ONE_KNOWN_DW_OP +#undef ONE_KNOWN_DW_OP_DESC + }; + + const char *ret = NULL; + if (likely (code < sizeof (known) / sizeof (known[0]))) + ret = known[code]; + + if (ret == NULL) + { + static char buf[40]; + snprintf (buf, sizeof buf, gettext ("unknown opcode %" PRIx64), + (uint64_t) code); + ret = buf; + } + + return ret; +} diff --git a/src/dwarfstrings.h b/src/dwarfstrings.h new file mode 100644 index 00000000..77d63b39 --- /dev/null +++ b/src/dwarfstrings.h @@ -0,0 +1,41 @@ +#ifndef DWARFSTRINGS_H +#define DWARFSTRINGS_H 1 + +#ifdef __cplusplus +extern "C" +{ +#endif + +const char *dwarf_tag_string (unsigned int tag); + +const char *dwarf_attr_string (unsigned int attrnum); + +const char *dwarf_form_string (unsigned int form); + +const char *dwarf_lang_string (unsigned int lang); + +const char *dwarf_inline_string (unsigned int code); + +const char *dwarf_encoding_string (unsigned int code); + +const char *dwarf_access_string (unsigned int code); + +const char *dwarf_visibility_string (unsigned int code); + +const char *dwarf_virtuality_string (unsigned int code); + +const char *dwarf_identifier_case_string (unsigned int code); + +const char *dwarf_calling_convention_string (unsigned int code); + +const char *dwarf_ordering_string (unsigned int code); + +const char *dwarf_discr_list_string (unsigned int code); + +const char *dwarf_locexpr_opcode_string (unsigned int code); + +#ifdef __cplusplus +} +#endif + +#endif /* dwarfstrings.h */ diff --git a/src/expr_opcodes.h b/src/expr_opcodes.h new file mode 100644 index 00000000..b38f218a --- /dev/null +++ b/src/expr_opcodes.h @@ -0,0 +1,154 @@ +DEF_DW_OP (DW_OP_addr, DW_FORM_addr, 0) +DEF_DW_OP (DW_OP_deref, 0, 0) +DEF_DW_OP (DW_OP_xderef, 0, 0) +DEF_DW_OP (DW_OP_deref_size, DW_FORM_data1, 0) +DEF_DW_OP (DW_OP_xderef_size, DW_FORM_data1, 0) +DEF_DW_OP (DW_OP_const1u, DW_FORM_data1, 0) +DEF_DW_OP (DW_OP_const1s, DW_FORM_data1, 0) +DEF_DW_OP (DW_OP_const2u, DW_FORM_data2, 0) +DEF_DW_OP (DW_OP_const2s, DW_FORM_data2, 0) +DEF_DW_OP (DW_OP_const4u, DW_FORM_data4, 0) +DEF_DW_OP (DW_OP_const4s, DW_FORM_data4, 0) +DEF_DW_OP (DW_OP_const8u, DW_FORM_data8, 0) +DEF_DW_OP (DW_OP_const8s, DW_FORM_data8, 0) +DEF_DW_OP (DW_OP_constu, DW_FORM_udata, 0) +DEF_DW_OP (DW_OP_consts, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_dup, 0, 0) +DEF_DW_OP (DW_OP_drop, 0, 0) +DEF_DW_OP (DW_OP_over, 0, 0) +DEF_DW_OP (DW_OP_pick, DW_FORM_data1, 0) +DEF_DW_OP (DW_OP_swap, 0, 0) +DEF_DW_OP (DW_OP_rot, 0, 0) +DEF_DW_OP (DW_OP_abs, 0, 0) +DEF_DW_OP (DW_OP_and, 0, 0) +DEF_DW_OP (DW_OP_div, 0, 0) +DEF_DW_OP (DW_OP_minus, 0, 0) +DEF_DW_OP (DW_OP_mod, 0, 0) +DEF_DW_OP (DW_OP_mul, 0, 0) +DEF_DW_OP (DW_OP_neg, 0, 0) +DEF_DW_OP (DW_OP_not, 0, 0) +DEF_DW_OP (DW_OP_or, 0, 0) +DEF_DW_OP (DW_OP_plus, 0, 0) +DEF_DW_OP (DW_OP_plus_uconst, DW_FORM_udata, 0) +DEF_DW_OP (DW_OP_shl, 0, 0) +DEF_DW_OP (DW_OP_shr, 0, 0) +DEF_DW_OP (DW_OP_shra, 0, 0) +DEF_DW_OP (DW_OP_xor, 0, 0) +DEF_DW_OP (DW_OP_bra, DW_FORM_data2, 0) +DEF_DW_OP (DW_OP_eq, 0, 0) +DEF_DW_OP (DW_OP_ge, 0, 0) +DEF_DW_OP (DW_OP_gt, 0, 0) +DEF_DW_OP (DW_OP_le, 0, 0) +DEF_DW_OP (DW_OP_lt, 0, 0) +DEF_DW_OP (DW_OP_ne, 0, 0) +DEF_DW_OP (DW_OP_skip, DW_FORM_data2, 0) +DEF_DW_OP (DW_OP_lit0, 0, 0) +DEF_DW_OP (DW_OP_lit1, 0, 0) +DEF_DW_OP (DW_OP_lit2, 0, 0) +DEF_DW_OP (DW_OP_lit3, 0, 0) +DEF_DW_OP (DW_OP_lit4, 0, 0) +DEF_DW_OP (DW_OP_lit5, 0, 0) +DEF_DW_OP (DW_OP_lit6, 0, 0) +DEF_DW_OP (DW_OP_lit7, 0, 0) +DEF_DW_OP (DW_OP_lit8, 0, 0) +DEF_DW_OP (DW_OP_lit9, 0, 0) +DEF_DW_OP (DW_OP_lit10, 0, 0) +DEF_DW_OP (DW_OP_lit11, 0, 0) +DEF_DW_OP (DW_OP_lit12, 0, 0) +DEF_DW_OP (DW_OP_lit13, 0, 0) +DEF_DW_OP (DW_OP_lit14, 0, 0) +DEF_DW_OP (DW_OP_lit15, 0, 0) +DEF_DW_OP (DW_OP_lit16, 0, 0) +DEF_DW_OP (DW_OP_lit17, 0, 0) +DEF_DW_OP (DW_OP_lit18, 0, 0) +DEF_DW_OP (DW_OP_lit19, 0, 0) +DEF_DW_OP (DW_OP_lit20, 0, 0) +DEF_DW_OP (DW_OP_lit21, 0, 0) +DEF_DW_OP (DW_OP_lit22, 0, 0) +DEF_DW_OP (DW_OP_lit23, 0, 0) +DEF_DW_OP (DW_OP_lit24, 0, 0) +DEF_DW_OP (DW_OP_lit25, 0, 0) +DEF_DW_OP (DW_OP_lit26, 0, 0) +DEF_DW_OP (DW_OP_lit27, 0, 0) +DEF_DW_OP (DW_OP_lit28, 0, 0) +DEF_DW_OP (DW_OP_lit29, 0, 0) +DEF_DW_OP (DW_OP_lit30, 0, 0) +DEF_DW_OP (DW_OP_lit31, 0, 0) +DEF_DW_OP (DW_OP_reg0, 0, 0) +DEF_DW_OP (DW_OP_reg1, 0, 0) +DEF_DW_OP (DW_OP_reg2, 0, 0) +DEF_DW_OP (DW_OP_reg3, 0, 0) +DEF_DW_OP (DW_OP_reg4, 0, 0) +DEF_DW_OP (DW_OP_reg5, 0, 0) +DEF_DW_OP (DW_OP_reg6, 0, 0) +DEF_DW_OP (DW_OP_reg7, 0, 0) +DEF_DW_OP (DW_OP_reg8, 0, 0) +DEF_DW_OP (DW_OP_reg9, 0, 0) +DEF_DW_OP (DW_OP_reg10, 0, 0) +DEF_DW_OP (DW_OP_reg11, 0, 0) +DEF_DW_OP (DW_OP_reg12, 0, 0) +DEF_DW_OP (DW_OP_reg13, 0, 0) +DEF_DW_OP (DW_OP_reg14, 0, 0) +DEF_DW_OP (DW_OP_reg15, 0, 0) +DEF_DW_OP (DW_OP_reg16, 0, 0) +DEF_DW_OP (DW_OP_reg17, 0, 0) +DEF_DW_OP (DW_OP_reg18, 0, 0) +DEF_DW_OP (DW_OP_reg19, 0, 0) +DEF_DW_OP (DW_OP_reg20, 0, 0) +DEF_DW_OP (DW_OP_reg21, 0, 0) +DEF_DW_OP (DW_OP_reg22, 0, 0) +DEF_DW_OP (DW_OP_reg23, 0, 0) +DEF_DW_OP (DW_OP_reg24, 0, 0) +DEF_DW_OP (DW_OP_reg25, 0, 0) +DEF_DW_OP (DW_OP_reg26, 0, 0) +DEF_DW_OP (DW_OP_reg27, 0, 0) +DEF_DW_OP (DW_OP_reg28, 0, 0) +DEF_DW_OP (DW_OP_reg29, 0, 0) +DEF_DW_OP (DW_OP_reg30, 0, 0) +DEF_DW_OP (DW_OP_reg31, 0, 0) +DEF_DW_OP (DW_OP_breg0, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg1, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg2, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg3, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg4, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg5, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg6, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg7, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg8, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg9, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg10, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg11, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg12, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg13, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg14, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg15, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg16, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg17, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg18, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg19, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg20, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg21, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg22, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg23, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg24, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg25, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg26, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg27, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg28, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg29, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg30, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_breg31, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_regx, DW_FORM_udata, 0) +DEF_DW_OP (DW_OP_fbreg, DW_FORM_sdata, 0) +DEF_DW_OP (DW_OP_bregx, DW_FORM_udata, DW_FORM_sdata) +DEF_DW_OP (DW_OP_piece, DW_FORM_udata, 0) +DEF_DW_OP (DW_OP_nop, 0, 0) +DEF_DW_OP (DW_OP_push_object_address, 0, 0) +DEF_DW_OP (DW_OP_call2, DW_FORM_data2, 0) +DEF_DW_OP (DW_OP_call4, DW_FORM_data4, 0) +DEF_DW_OP (DW_OP_call_ref, DW_FORM_addr, 0) +DEF_DW_OP (DW_OP_form_tls_address, 0, 0) +DEF_DW_OP (DW_OP_GNU_push_tls_address, 0, 0) +DEF_DW_OP (DW_OP_call_frame_cfa, 0, 0) +DEF_DW_OP (DW_OP_bit_piece, DW_FORM_udata, DW_FORM_udata) +DEF_DW_OP (DW_OP_GNU_uninit, 0, 0) diff --git a/src/readelf.c b/src/readelf.c index 1c790650..e7390c80 100644 --- a/src/readelf.c +++ b/src/readelf.c @@ -57,6 +57,7 @@ #include "../libdw/libdwP.h" #include "../libdwfl/libdwflP.h" #include "../libdw/memory-access.h" +#include "dwarfstrings.h" /* Name and version of program. */ @@ -3043,590 +3044,6 @@ format_dwarf_addr (Dwfl_Module *dwflmod, return result; } -static const char * -dwarf_tag_string (unsigned int tag) -{ - static const char *const known_tags[] = - { - [DW_TAG_array_type] = "array_type", - [DW_TAG_class_type] = "class_type", - [DW_TAG_entry_point] = "entry_point", - [DW_TAG_enumeration_type] = "enumeration_type", - [DW_TAG_formal_parameter] = "formal_parameter", - [DW_TAG_imported_declaration] = "imported_declaration", - [DW_TAG_label] = "label", - [DW_TAG_lexical_block] = "lexical_block", - [DW_TAG_member] = "member", - [DW_TAG_pointer_type] = "pointer_type", - [DW_TAG_reference_type] = "reference_type", - [DW_TAG_compile_unit] = "compile_unit", - [DW_TAG_string_type] = "string_type", - [DW_TAG_structure_type] = "structure_type", - [DW_TAG_subroutine_type] = "subroutine_type", - [DW_TAG_typedef] = "typedef", - [DW_TAG_union_type] = "union_type", - [DW_TAG_unspecified_parameters] = "unspecified_parameters", - [DW_TAG_variant] = "variant", - [DW_TAG_common_block] = "common_block", - [DW_TAG_common_inclusion] = "common_inclusion", - [DW_TAG_inheritance] = "inheritance", - [DW_TAG_inlined_subroutine] = "inlined_subroutine", - [DW_TAG_module] = "module", - [DW_TAG_ptr_to_member_type] = "ptr_to_member_type", - [DW_TAG_set_type] = "set_type", - [DW_TAG_subrange_type] = "subrange_type", - [DW_TAG_with_stmt] = "with_stmt", - [DW_TAG_access_declaration] = "access_declaration", - [DW_TAG_base_type] = "base_type", - [DW_TAG_catch_block] = "catch_block", - [DW_TAG_const_type] = "const_type", - [DW_TAG_constant] = "constant", - [DW_TAG_enumerator] = "enumerator", - [DW_TAG_file_type] = "file_type", - [DW_TAG_friend] = "friend", - [DW_TAG_namelist] = "namelist", - [DW_TAG_namelist_item] = "namelist_item", - [DW_TAG_packed_type] = "packed_type", - [DW_TAG_subprogram] = "subprogram", - [DW_TAG_template_type_parameter] = "template_type_parameter", - [DW_TAG_template_value_parameter] = "template_value_parameter", - [DW_TAG_thrown_type] = "thrown_type", - [DW_TAG_try_block] = "try_block", - [DW_TAG_variant_part] = "variant_part", - [DW_TAG_variable] = "variable", - [DW_TAG_volatile_type] = "volatile_type", - [DW_TAG_dwarf_procedure] = "dwarf_procedure", - [DW_TAG_restrict_type] = "restrict_type", - [DW_TAG_interface_type] = "interface_type", - [DW_TAG_namespace] = "namespace", - [DW_TAG_imported_module] = "imported_module", - [DW_TAG_unspecified_type] = "unspecified_type", - [DW_TAG_partial_unit] = "partial_unit", - [DW_TAG_imported_unit] = "imported_unit", - [DW_TAG_mutable_type] = "mutable_type", - [DW_TAG_condition] = "condition", - [DW_TAG_shared_type] = "shared_type", - }; - const unsigned int nknown_tags = (sizeof (known_tags) - / sizeof (known_tags[0])); - static char buf[40]; - const char *result = NULL; - - if (likely (tag < nknown_tags)) - result = known_tags[tag]; - - if (unlikely (result == NULL)) - /* There are a few known extensions. */ - switch (tag) - { - case DW_TAG_MIPS_loop: - result = "MIPS_loop"; - break; - - case DW_TAG_format_label: - result = "format_label"; - break; - - case DW_TAG_function_template: - result = "function_template"; - break; - - case DW_TAG_class_template: - result = "class_template"; - break; - - default: - if (tag < DW_TAG_lo_user) - snprintf (buf, sizeof buf, gettext ("unknown tag %hx"), tag); - else - snprintf (buf, sizeof buf, gettext ("unknown user tag %hx"), tag); - result = buf; - break; - } - - return result; -} - - -static const char * -dwarf_attr_string (unsigned int attrnum) -{ - static const char *const known_attrs[] = - { - [DW_AT_sibling] = "sibling", - [DW_AT_location] = "location", - [DW_AT_name] = "name", - [DW_AT_ordering] = "ordering", - [DW_AT_subscr_data] = "subscr_data", - [DW_AT_byte_size] = "byte_size", - [DW_AT_bit_offset] = "bit_offset", - [DW_AT_bit_size] = "bit_size", - [DW_AT_element_list] = "element_list", - [DW_AT_stmt_list] = "stmt_list", - [DW_AT_low_pc] = "low_pc", - [DW_AT_high_pc] = "high_pc", - [DW_AT_language] = "language", - [DW_AT_member] = "member", - [DW_AT_discr] = "discr", - [DW_AT_discr_value] = "discr_value", - [DW_AT_visibility] = "visibility", - [DW_AT_import] = "import", - [DW_AT_string_length] = "string_length", - [DW_AT_common_reference] = "common_reference", - [DW_AT_comp_dir] = "comp_dir", - [DW_AT_const_value] = "const_value", - [DW_AT_containing_type] = "containing_type", - [DW_AT_default_value] = "default_value", - [DW_AT_inline] = "inline", - [DW_AT_is_optional] = "is_optional", - [DW_AT_lower_bound] = "lower_bound", - [DW_AT_producer] = "producer", - [DW_AT_prototyped] = "prototyped", - [DW_AT_return_addr] = "return_addr", - [DW_AT_start_scope] = "start_scope", - [DW_AT_bit_stride] = "bit_stride", - [DW_AT_upper_bound] = "upper_bound", - [DW_AT_abstract_origin] = "abstract_origin", - [DW_AT_accessibility] = "accessibility", - [DW_AT_address_class] = "address_class", - [DW_AT_artificial] = "artificial", - [DW_AT_base_types] = "base_types", - [DW_AT_calling_convention] = "calling_convention", - [DW_AT_count] = "count", - [DW_AT_data_member_location] = "data_member_location", - [DW_AT_decl_column] = "decl_column", - [DW_AT_decl_file] = "decl_file", - [DW_AT_decl_line] = "decl_line", - [DW_AT_declaration] = "declaration", - [DW_AT_discr_list] = "discr_list", - [DW_AT_encoding] = "encoding", - [DW_AT_external] = "external", - [DW_AT_frame_base] = "frame_base", - [DW_AT_friend] = "friend", - [DW_AT_identifier_case] = "identifier_case", - [DW_AT_macro_info] = "macro_info", - [DW_AT_namelist_item] = "namelist_item", - [DW_AT_priority] = "priority", - [DW_AT_segment] = "segment", - [DW_AT_specification] = "specification", - [DW_AT_static_link] = "static_link", - [DW_AT_type] = "type", - [DW_AT_use_location] = "use_location", - [DW_AT_variable_parameter] = "variable_parameter", - [DW_AT_virtuality] = "virtuality", - [DW_AT_vtable_elem_location] = "vtable_elem_location", - [DW_AT_allocated] = "allocated", - [DW_AT_associated] = "associated", - [DW_AT_data_location] = "data_location", - [DW_AT_byte_stride] = "byte_stride", - [DW_AT_entry_pc] = "entry_pc", - [DW_AT_use_UTF8] = "use_UTF8", - [DW_AT_extension] = "extension", - [DW_AT_ranges] = "ranges", - [DW_AT_trampoline] = "trampoline", - [DW_AT_call_column] = "call_column", - [DW_AT_call_file] = "call_file", - [DW_AT_call_line] = "call_line", - [DW_AT_description] = "description", - [DW_AT_binary_scale] = "binary_scale", - [DW_AT_decimal_scale] = "decimal_scale", - [DW_AT_small] = "small", - [DW_AT_decimal_sign] = "decimal_sign", - [DW_AT_digit_count] = "digit_count", - [DW_AT_picture_string] = "picture_string", - [DW_AT_mutable] = "mutable", - [DW_AT_threads_scaled] = "threads_scaled", - [DW_AT_explicit] = "explicit", - [DW_AT_object_pointer] = "object_pointer", - [DW_AT_endianity] = "endianity", - [DW_AT_elemental] = "elemental", - [DW_AT_pure] = "pure", - [DW_AT_recursive] = "recursive", - }; - const unsigned int nknown_attrs = (sizeof (known_attrs) - / sizeof (known_attrs[0])); - static char buf[40]; - const char *result = NULL; - - if (likely (attrnum < nknown_attrs)) - result = known_attrs[attrnum]; - - if (unlikely (result == NULL)) - /* There are a few known extensions. */ - switch (attrnum) - { - case DW_AT_MIPS_fde: - result = "MIPS_fde"; - break; - - case DW_AT_MIPS_loop_begin: - result = "MIPS_loop_begin"; - break; - - case DW_AT_MIPS_tail_loop_begin: - result = "MIPS_tail_loop_begin"; - break; - - case DW_AT_MIPS_epilog_begin: - result = "MIPS_epilog_begin"; - break; - - case DW_AT_MIPS_loop_unroll_factor: - result = "MIPS_loop_unroll_factor"; - break; - - case DW_AT_MIPS_software_pipeline_depth: - result = "MIPS_software_pipeline_depth"; - break; - - case DW_AT_MIPS_linkage_name: - result = "MIPS_linkage_name"; - break; - - case DW_AT_MIPS_stride: - result = "MIPS_stride"; - break; - - case DW_AT_MIPS_abstract_name: - result = "MIPS_abstract_name"; - break; - - case DW_AT_MIPS_clone_origin: - result = "MIPS_clone_origin"; - break; - - case DW_AT_MIPS_has_inlines: - result = "MIPS_has_inlines"; - break; - - case DW_AT_MIPS_stride_byte: - result = "MIPS_stride_byte"; - break; - - case DW_AT_MIPS_stride_elem: - result = "MIPS_stride_elem"; - break; - - case DW_AT_MIPS_ptr_dopetype: - result = "MIPS_ptr_dopetype"; - break; - - case DW_AT_MIPS_allocatable_dopetype: - result = "MIPS_allocatable_dopetype"; - break; - - case DW_AT_MIPS_assumed_shape_dopetype: - result = "MIPS_assumed_shape_dopetype"; - break; - - case DW_AT_MIPS_assumed_size: - result = "MIPS_assumed_size"; - break; - - case DW_AT_sf_names: - result = "sf_names"; - break; - - case DW_AT_src_info: - result = "src_info"; - break; - - case DW_AT_mac_info: - result = "mac_info"; - break; - - case DW_AT_src_coords: - result = "src_coords"; - break; - - case DW_AT_body_begin: - result = "body_begin"; - break; - - case DW_AT_body_end: - result = "body_end"; - break; - - default: - if (attrnum < DW_AT_lo_user) - snprintf (buf, sizeof buf, gettext ("unknown attribute %hx"), - attrnum); - else - snprintf (buf, sizeof buf, gettext ("unknown user attribute %hx"), - attrnum); - result = buf; - break; - } - - return result; -} - - -static const char * -dwarf_form_string (unsigned int form) -{ - static const char *const known_forms[] = - { - [DW_FORM_addr] = "addr", - [DW_FORM_block2] = "block2", - [DW_FORM_block4] = "block4", - [DW_FORM_data2] = "data2", - [DW_FORM_data4] = "data4", - [DW_FORM_data8] = "data8", - [DW_FORM_string] = "string", - [DW_FORM_block] = "block", - [DW_FORM_block1] = "block1", - [DW_FORM_data1] = "data1", - [DW_FORM_flag] = "flag", - [DW_FORM_sdata] = "sdata", - [DW_FORM_strp] = "strp", - [DW_FORM_udata] = "udata", - [DW_FORM_ref_addr] = "ref_addr", - [DW_FORM_ref1] = "ref1", - [DW_FORM_ref2] = "ref2", - [DW_FORM_ref4] = "ref4", - [DW_FORM_ref8] = "ref8", - [DW_FORM_ref_udata] = "ref_udata", - [DW_FORM_indirect] = "indirect" - }; - const unsigned int nknown_forms = (sizeof (known_forms) - / sizeof (known_forms[0])); - static char buf[40]; - const char *result = NULL; - - if (likely (form < nknown_forms)) - result = known_forms[form]; - - if (unlikely (result == NULL)) - snprintf (buf, sizeof buf, gettext ("unknown form %" PRIx64), - (uint64_t) form); - - return result; -} - - -static const char * -dwarf_lang_string (unsigned int lang) -{ - static const char *const known[] = - { - [DW_LANG_C89] = "ISO C89", - [DW_LANG_C] = "C", - [DW_LANG_Ada83] = "Ada83", - [DW_LANG_C_plus_plus] = "C++", - [DW_LANG_Cobol74] = "Cobol74", - [DW_LANG_Cobol85] = "Cobol85", - [DW_LANG_Fortran77] = "Fortran77", - [DW_LANG_Fortran90] = "Fortran90", - [DW_LANG_Pascal83] = "Pascal83", - [DW_LANG_Modula2] = "Modula2", - [DW_LANG_Java] = "Java", - [DW_LANG_C99] = "ISO C99", - [DW_LANG_Ada95] = "Ada95", - [DW_LANG_Fortran95] = "Fortran95", - [DW_LANG_PL1] = "PL1", - [DW_LANG_Objc] = "Objective C", - [DW_LANG_ObjC_plus_plus] = "Objective C++", - [DW_LANG_UPC] = "UPC", - [DW_LANG_D] = "D", - }; - - if (likely (lang < sizeof (known) / sizeof (known[0]))) - return known[lang]; - else if (lang == DW_LANG_Mips_Assembler) - /* This language tag is used for assembler in general. */ - return "Assembler"; - - if (lang >= DW_LANG_lo_user && lang <= DW_LANG_hi_user) - { - static char buf[30]; - snprintf (buf, sizeof (buf), "lo_user+%u", lang - DW_LANG_lo_user); - return buf; - } - - return "???"; -} - - -static const char * -dwarf_inline_string (unsigned int code) -{ - static const char *const known[] = - { - [DW_INL_not_inlined] = "not_inlined", - [DW_INL_inlined] = "inlined", - [DW_INL_declared_not_inlined] = "declared_not_inlined", - [DW_INL_declared_inlined] = "declared_inlined" - }; - - if (likely (code < sizeof (known) / sizeof (known[0]))) - return known[code]; - - return "???"; -} - - -static const char * -dwarf_encoding_string (unsigned int code) -{ - static const char *const known[] = - { - [DW_ATE_void] = "void", - [DW_ATE_address] = "address", - [DW_ATE_boolean] = "boolean", - [DW_ATE_complex_float] = "complex_float", - [DW_ATE_float] = "float", - [DW_ATE_signed] = "signed", - [DW_ATE_signed_char] = "signed_char", - [DW_ATE_unsigned] = "unsigned", - [DW_ATE_unsigned_char] = "unsigned_char", - [DW_ATE_imaginary_float] = "imaginary_float", - [DW_ATE_packed_decimal] = "packed_decimal", - [DW_ATE_numeric_string] = "numeric_string", - [DW_ATE_edited] = "edited", - [DW_ATE_signed_fixed] = "signed_fixed", - [DW_ATE_unsigned_fixed] = "unsigned_fixed", - [DW_ATE_decimal_float] = "decimal_float", - }; - - if (likely (code < sizeof (known) / sizeof (known[0]))) - return known[code]; - - if (code >= DW_ATE_lo_user && code <= DW_ATE_hi_user) - { - static char buf[30]; - snprintf (buf, sizeof (buf), "lo_user+%u", code - DW_ATE_lo_user); - return buf; - } - - return "???"; -} - - -static const char * -dwarf_access_string (unsigned int code) -{ - static const char *const known[] = - { - [DW_ACCESS_public] = "public", - [DW_ACCESS_protected] = "protected", - [DW_ACCESS_private] = "private" - }; - - if (likely (code < sizeof (known) / sizeof (known[0]))) - return known[code]; - - return "???"; -} - - -static const char * -dwarf_visibility_string (unsigned int code) -{ - static const char *const known[] = - { - [DW_VIS_local] = "local", - [DW_VIS_exported] = "exported", - [DW_VIS_qualified] = "qualified" - }; - - if (likely (code < sizeof (known) / sizeof (known[0]))) - return known[code]; - - return "???"; -} - - -static const char * -dwarf_virtuality_string (unsigned int code) -{ - static const char *const known[] = - { - [DW_VIRTUALITY_none] = "none", - [DW_VIRTUALITY_virtual] = "virtual", - [DW_VIRTUALITY_pure_virtual] = "pure_virtual" - }; - - if (likely (code < sizeof (known) / sizeof (known[0]))) - return known[code]; - - return "???"; -} - - -static const char * -dwarf_identifier_case_string (unsigned int code) -{ - static const char *const known[] = - { - [DW_ID_case_sensitive] = "sensitive", - [DW_ID_up_case] = "up_case", - [DW_ID_down_case] = "down_case", - [DW_ID_case_insensitive] = "insensitive" - }; - - if (likely (code < sizeof (known) / sizeof (known[0]))) - return known[code]; - - return "???"; -} - - -static const char * -dwarf_calling_convention_string (unsigned int code) -{ - static const char *const known[] = - { - [DW_CC_normal] = "normal", - [DW_CC_program] = "program", - [DW_CC_nocall] = "nocall", - }; - - if (likely (code < sizeof (known) / sizeof (known[0]))) - return known[code]; - - if (code >= DW_CC_lo_user && code <= DW_CC_hi_user) - { - static char buf[30]; - snprintf (buf, sizeof (buf), "lo_user+%u", code - DW_CC_lo_user); - return buf; - } - - return "???"; -} - - -static const char * -dwarf_ordering_string (unsigned int code) -{ - static const char *const known[] = - { - [DW_ORD_row_major] = "row_major", - [DW_ORD_col_major] = "col_major" - }; - - if (likely (code < sizeof (known) / sizeof (known[0]))) - return known[code]; - - return "???"; -} - - -static const char * -dwarf_discr_list_string (unsigned int code) -{ - static const char *const known[] = - { - [DW_DSC_label] = "label", - [DW_DSC_range] = "range" - }; - - if (likely (code < sizeof (known) / sizeof (known[0]))) - return known[code]; - - return "???"; -} - static void print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest, diff --git a/tests/ChangeLog b/tests/ChangeLog index e31dcbb4..39245f84 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,43 @@ +2009-03-25 Petr Machata <pmachata@redhat.com> + + * dwarf-attributes.cc: New file. + * Makefile.am (noinst_PROGRAMS): Add it. + (dwarf_attributes_SOURCES, dwarf_attributes_LDADD): New variables. + +2009-03-25 Roland McGrath <roland@redhat.com> + + * dwarf-print.cc (print_die, process_file): Take LIMIT argument. + Punt recursion at that depth. + (main): Grok first argument --depth=N to set it. + +2009-03-24 Roland McGrath <roland@redhat.com> + + * dwarf-print.cc: New file. + * Makefile.am (noinst_PROGRAMS): Add it. + (dwarf_print_SOURCES, dwarf_print_LDADD): New variables. + +2009-01-10 Roland McGrath <roland@redhat.com> + + * run-dwarfcmp-self.sh: Also run with -T. + + * run-dwarflint-self.sh: --no-debug -> -i + + * run-dwarfcmp-self.sh: Run both with and without -q. + +2009-01-10 Petr Machata <pmachata@redhat.com> + + * run-dwarflint-self.sh: Pass new option --no-debug to dwarflint. + +2009-01-10 Roland McGrath <roland@redhat.com> + + * run-dwarflint-self.sh: New file. + * Makefile.am (TESTS, EXTRA_DIST) Add it. + +2009-01-05 Roland McGrath <roland@redhat.com> + + * run-dwarfcmp-self.sh: New file. + * Makefile.am (TESTS, EXTRA_DIST) Add it. + 2009-05-07 Petr Machata <pmachata@redhat.com> * testfile51.bz2: New data file. diff --git a/tests/Makefile.am b/tests/Makefile.am index 956b7253..ba950088 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -60,7 +60,7 @@ noinst_PROGRAMS = arextract arsymtest newfile saridx scnnames sectiondump \ find-prologues funcretval allregs rdwrmmap \ dwfl-bug-addr-overflow arls dwfl-bug-fd-leak \ dwfl-addr-sect dwfl-bug-report early-offscn \ - dwfl-bug-getmodules dwarf-getmacros + dwfl-bug-getmodules dwarf-getmacros dwarf-print # get-ciefde asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \ asm-tst6 asm-tst7 asm-tst8 asm-tst9 @@ -86,7 +86,8 @@ TESTS = run-arextract.sh run-arsymtest.sh newfile test-nlist \ dwfl-bug-fd-leak dwfl-bug-report \ run-dwfl-bug-offline-rel.sh run-dwfl-addr-sect.sh \ run-disasm-x86.sh run-disasm-x86-64.sh \ - run-early-offscn.sh run-dwarf-getmacros.sh + run-early-offscn.sh run-dwarf-getmacros.sh \ + run-dwarfcmp-self.sh run-dwarflint-self.sh run-dwarf-attributes.sh # run-show-ciefde.sh if !STANDALONE @@ -120,6 +121,8 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh \ run-addrname-test.sh run-dwfl-bug-offline-rel.sh \ run-dwfl-addr-sect.sh run-early-offscn.sh \ run-dwarf-getmacros.sh \ + run-dwarfcmp-self.sh run-dwarflint-self.sh \ + run-dwarf-attributes.sh \ testfile15.bz2 testfile15.debug.bz2 \ testfile16.bz2 testfile16.debug.bz2 \ testfile17.bz2 testfile17.debug.bz2 \ @@ -184,6 +187,12 @@ libebl = ../libebl/libebl.a libeu = ../lib/libeu.a endif !STANDALONE +# XXX later the C++ stuff will be in libdw.so directly +libdwpp = ../libdw/libdwpp.a $(libdw) + +dwarf_print_SOURCES = dwarf-print.cc +dwarf_print_LDADD = $(libdwpp) $(libmudflap) -ldl + arextract_LDADD = $(libelf) $(libmudflap) arsymtest_LDADD = $(libelf) $(libmudflap) newfile_LDADD = $(libelf) $(libmudflap) diff --git a/tests/dwarf-print.cc b/tests/dwarf-print.cc new file mode 100644 index 00000000..8aa12ca9 --- /dev/null +++ b/tests/dwarf-print.cc @@ -0,0 +1,130 @@ +/* Test program for elfutils::dwarf basics. + Copyright (C) 2009 Red Hat, Inc. + This file is part of Red Hat elfutils. + + Red Hat elfutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by the + Free Software Foundation; version 2 of the License. + + Red Hat elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Red Hat elfutils; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. + + Red Hat elfutils is an included package of the Open Invention Network. + An included package of the Open Invention Network is a package for which + Open Invention Network licensees cross-license their patents. No patent + license is granted, either expressly or impliedly, by designation as an + included package. Should you wish to participate in the Open Invention + Network licensing program, please visit www.openinventionnetwork.com + <http://www.openinventionnetwork.com>. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <errno.h> +#include <error.h> +#include <fcntl.h> +#include <clocale> +#include <libintl.h> +#include <ostream> +#include <iomanip> + +#include "c++/dwarf" + +using namespace elfutils; +using namespace std; + +static Dwarf * +open_file (const char *fname) +{ + int fd = open (fname, O_RDONLY); + if (unlikely (fd == -1)) + error (2, errno, gettext ("cannot open '%s'"), fname); + Dwarf *dw = dwarf_begin (fd, DWARF_C_READ); + if (dw == NULL) + { + error (2, 0, + gettext ("cannot create DWARF descriptor for '%s': %s"), + fname, dwarf_errmsg (-1)); + } + return dw; +} + +static void +print_die (const dwarf::debug_info_entry &die, + unsigned int indent, unsigned int limit) +{ + string prefix (indent, ' '); + const string tag = dwarf::tags::name (die.tag ()); + + cout << prefix << "<" << tag << " offset=[" << die.offset () << "]"; + + for (dwarf::debug_info_entry::attributes::const_iterator i + = die.attributes ().begin (); i != die.attributes ().end (); ++i) + cout << " " << (*i).to_string (); + + if (die.has_children ()) + { + if (limit != 0 && indent >= limit) + { + cout << ">...\n"; + return; + } + + cout << ">\n"; + + for (dwarf::debug_info_entry::children::const_iterator i + = die.children ().begin (); i != die.children ().end (); ++i) + print_die (*i, indent + 1, limit); + + cout << prefix << "</" << tag << ">\n"; + } + else + cout << "/>\n"; +} + +static void +process_file (const char *file, unsigned int limit) +{ + dwarf dw (open_file (file)); + + cout << file << ":\n"; + + for (dwarf::compile_units::const_iterator i = dw.compile_units ().begin (); + i != dw.compile_units ().end (); + ++i) + print_die (*i, 1, limit); +} + +int +main (int argc, char *argv[]) +{ + /* Set locale. */ + (void) setlocale (LC_ALL, ""); + + /* Make sure the message catalog can be found. */ + (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR); + + /* Initialize the message catalog. */ + (void) textdomain (PACKAGE_TARNAME); + + cout << hex << setiosflags (ios::showbase); + + unsigned int depth = 0; + if (argc > 1 && sscanf (argv[1], "--depth=%u", &depth) == 1) + { + --argc; + ++argv; + } + + for (int i = 1; i < argc; ++i) + process_file (argv[i], depth); + + return 0; +} diff --git a/tests/run-dwarf-attributes.sh b/tests/run-dwarf-attributes.sh new file mode 100755 index 00000000..22c7289d --- /dev/null +++ b/tests/run-dwarf-attributes.sh @@ -0,0 +1,37 @@ +#! /bin/sh +# Copyright (C) 2009 Red Hat, Inc. +# This file is part of Red Hat elfutils. +# +# Red Hat elfutils is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by the +# Free Software Foundation; version 2 of the License. +# +# Red Hat elfutils is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Red Hat elfutils; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. +# +# Red Hat elfutils is an included package of the Open Invention Network. +# An included package of the Open Invention Network is a package for which +# Open Invention Network licensees cross-license their patents. No patent +# license is granted, either expressly or impliedly, by designation as an +# included package. Should you wish to participate in the Open Invention +# Network licensing program, please visit www.openinventionnetwork.com +# <http://www.openinventionnetwork.com>. + +. $srcdir/test-subr.sh + +testfiles testfile + +testrun_compare ./dwarf-print --depth=1 testfile <<\EOF +testfile: + <compile_unit offset=[0xb] stmt_list=0 high_pc=0x804845a low_pc=0x804842c name="m.c" comp_dir="/home/drepper/gnu/new-bu/build/ttt" producer="GNU C 2.96 20000731 (Red Hat Linux 7.0)" language=C89>... + <compile_unit offset=[0xca] stmt_list=0x4b high_pc=0x8048466 low_pc=0x804845c name="b.c" comp_dir="/home/drepper/gnu/new-bu/build/ttt" producer="GNU C 2.96 20000731 (Red Hat Linux 7.0)" language=C89>... + <compile_unit offset=[0x15fc] stmt_list=0x1e0 high_pc=0x8048472 low_pc=0x8048468 name="f.c" comp_dir="/home/drepper/gnu/new-bu/build/ttt" producer="GNU C 2.96 20000731 (Red Hat Linux 7.0)" language=C89>... +EOF + +exit 0 diff --git a/tests/run-dwarfcmp-self.sh b/tests/run-dwarfcmp-self.sh new file mode 100755 index 00000000..927083ed --- /dev/null +++ b/tests/run-dwarfcmp-self.sh @@ -0,0 +1,56 @@ +#! /bin/sh +# Copyright (C) 2009 Red Hat, Inc. +# This file is part of Red Hat elfutils. +# +# Red Hat elfutils is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by the +# Free Software Foundation; version 2 of the License. +# +# Red Hat elfutils is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Red Hat elfutils; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. +# +# Red Hat elfutils is an included package of the Open Invention Network. +# An included package of the Open Invention Network is a package for which +# Open Invention Network licensees cross-license their patents. No patent +# license is granted, either expressly or impliedly, by designation as an +# included package. Should you wish to participate in the Open Invention +# Network licensing program, please visit www.openinventionnetwork.com +# <http://www.openinventionnetwork.com>. + +. $srcdir/test-subr.sh + +status=0 +runtest() +{ + for file; do + if [ -f $file ]; then + { testrun ../src/dwarfcmp -q -i $file $file && + testrun ../src/dwarfcmp -i $file $file && + testrun ../src/dwarfcmp -T -q -i $file $file + } || + { echo "*** failure in $file"; status=1; } + fi + done +} + +runtest ../src/addr2line +runtest ../src/dwarfcmp +runtest ../src/elfcmp +runtest ../src/elflint +runtest ../src/findtextrel +runtest ../src/ld +runtest ../src/nm +runtest ../src/objdump +runtest ../src/readelf +runtest ../src/size +runtest ../src/strip +runtest ../src/unstrip +runtest ../*/*.so + +exit $status diff --git a/tests/run-dwarflint-self.sh b/tests/run-dwarflint-self.sh new file mode 100755 index 00000000..d1f6ca25 --- /dev/null +++ b/tests/run-dwarflint-self.sh @@ -0,0 +1,53 @@ +#! /bin/sh +# Copyright (C) 2009 Red Hat, Inc. +# This file is part of Red Hat elfutils. +# +# Red Hat elfutils is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by the +# Free Software Foundation; version 2 of the License. +# +# Red Hat elfutils is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Red Hat elfutils; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. +# +# Red Hat elfutils is an included package of the Open Invention Network. +# An included package of the Open Invention Network is a package for which +# Open Invention Network licensees cross-license their patents. No patent +# license is granted, either expressly or impliedly, by designation as an +# included package. Should you wish to participate in the Open Invention +# Network licensing program, please visit www.openinventionnetwork.com +# <http://www.openinventionnetwork.com>. + +. $srcdir/test-subr.sh + +status=0 +runtest() +{ + for file; do + if [ -f $file ]; then + testrun ../src/dwarflint -q -i --gnu $file || + { echo "*** failure in $file"; status=1; } + fi + done +} + +runtest ../src/addr2line +runtest ../src/dwarfcmp +runtest ../src/elfcmp +runtest ../src/elflint +runtest ../src/findtextrel +runtest ../src/ld +runtest ../src/nm +runtest ../src/objdump +runtest ../src/readelf +runtest ../src/size +runtest ../src/strip +runtest ../src/unstrip +runtest ../*/*.so + +exit $status |