platform/upstream/elfutils.git
6 years agolibelf: Sync elf.h from glibc
Andreas Schwab [Thu, 19 Apr 2018 15:47:47 +0000 (17:47 +0200)]
libelf: Sync elf.h from glibc

Signed-off-by: Andreas Schwab <schwab@suse.de>
6 years agotests: Test readelf --debug-dump=[decoded]line with min_inst_len > 1.
Mark Wielaard [Mon, 16 Apr 2018 09:40:22 +0000 (11:40 +0200)]
tests: Test readelf --debug-dump=[decoded]line with min_inst_len > 1.

This testcase tests the fix for the readelf --debug-dump=line bug fixed
in commit 40b1f18 "readelf: Fix advance_pc to use op_addr_advance, not
just op_advance."

This tests both the --debug-dump=line (readelf) implementation and the
--debug-dump=decodedline (libdw) implementation. The first fails without
the bug fix. The second was already correct.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Fix advance_pc to use op_addr_advance, not just op_advance.
Mark Wielaard [Mon, 16 Apr 2018 08:25:35 +0000 (10:25 +0200)]
readelf: Fix advance_pc to use op_addr_advance, not just op_advance.

Found by John Mellor-Crummey.

The reason this bug wasn't found earlier is because gcc fixes
minimum_instr_len and max_ops_per_instr to 1 for all architectures
(in theory max_ops_per_instr could be overridden, but I didn't find
any architecture that does). And op_index always seems zero (it looks
like it is really only for special VLWI architectures).

So in all cases I saw it means that:

op_addr_advance = minimum_instr_len * ((op_index + op_advance)
                                       / max_ops_per_instr)
                = 1 * ((0 + op_advance) / 1)
                = op_advance

Completely masking the bug.

The libdw dwarf_getsrclines.c implementation does get this right.
Because it doesn't care about the data representation and so does
the calculation directly.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Only lookup files and show errors in attr_callback when not silent.
Mark Wielaard [Fri, 13 Apr 2018 12:56:48 +0000 (14:56 +0200)]
readelf: Only lookup files and show errors in attr_callback when not silent.

We recently added a couple more sanity checks and show an error when
attributes that represent files cannot be resolved. We should only do
this when the attr_callback should print the attribute information.
When not (for example when not showing the .debug_info section, but
only collecting information about the CUs containing pointers into
other sections) we don't need to lookup the file names, nor print
the errors/warnings.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoaarch64: Add default cfi rule to restore SP from CFA address.
Mark Wielaard [Wed, 11 Apr 2018 08:37:45 +0000 (10:37 +0200)]
aarch64: Add default cfi rule to restore SP from CFA address.

The CFA is set by default to the stack pointer of the previous frame.
So that is also how we can always restore the SP. This default aarch64
CFI rule is necessary on Fedora 28 with GCC8 to make the run-deleted.sh
and run-backtrace-dwarf.sh testcases work.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdwfl: Handle unwind frame when the return address register isn't set.
Mark Wielaard [Tue, 10 Apr 2018 14:13:34 +0000 (16:13 +0200)]
libdwfl: Handle unwind frame when the return address register isn't set.

When we have unwound the frame and then cannot set the return address
we wouldn't set any error. That meant that a dwfl_thread_getframes ()
call could end in an error, but without any dwfl_errno set, producing
the "no error" error message.

If we cannot set the return address at the end of unwinding the frame
that means that either the return address register is bogus (error),
or that the return address is undefined (end of the call stack).

This fixes the run-backtrace-native-biarch.sh testcase for me on an
i386 on x86_64 setup with gcc 7.2.1 and glibc 2.17.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Restructure address range reading for .debug_loc and .debug_ranges.
Mark Wielaard [Mon, 2 Apr 2018 21:08:40 +0000 (23:08 +0200)]
libdw: Restructure address range reading for .debug_loc and .debug_ranges.

This caches the CU base address, makes error checking slight more relaxed
and restructures the code so it will be easier to add different forms
of ranges. Adds a new test for the new CU base address code.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Make sure to initialize Dwarf_CU addr_base and str_off_base.
Mark Wielaard [Wed, 4 Apr 2018 14:43:38 +0000 (16:43 +0200)]
libdw: Make sure to initialize Dwarf_CU addr_base and str_off_base.

The __libdw_cu_addr_base () and __libdw_cu_str_off_base () functions
rely on these fields being initialized to -1 when the values have not
yet been set up. Shows up as a valgrind warning.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Add support for reading DW_FORM_strx[1234] in .debug_str_offsets.
Mark Wielaard [Wed, 14 Mar 2018 21:45:56 +0000 (22:45 +0100)]
libdw: Add support for reading DW_FORM_strx[1234] in .debug_str_offsets.

Recognize the new .debug_str_offsets section. The CU will now hold a new
str_off_base offset in that section for that CU. dwarf_form_string will
decode DW_FORM_strx[1234] and return strings using that str_off_base
from the .debug_addr.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Add support for reading DW_FORM_addrx[1234] in .debug_addr.
Mark Wielaard [Fri, 24 Nov 2017 10:18:39 +0000 (11:18 +0100)]
libdw: Add support for reading DW_FORM_addrx[1234] in .debug_addr.

Recognize the new .debug_addr section. The CU will now hold a new
address base offset in that section for that CU. dwarf_form_addr will
decode DW_FORM_addrx[1234] and return addresses using that address
base from the .debug_addr. A new internal function read_3ubyte_unaligned
will try to read a 24-bit value depending on endianness of the underlying
file.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Extend documentation to explain relative file paths and comp_dir.
Mark Wielaard [Thu, 29 Mar 2018 12:09:12 +0000 (14:09 +0200)]
libdw: Extend documentation to explain relative file paths and comp_dir.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Break sysv[64] symbol hash bucket chain loops.
Mark Wielaard [Wed, 28 Mar 2018 19:27:48 +0000 (21:27 +0200)]
readelf: Break sysv[64] symbol hash bucket chain loops.

The bucket chain should not contain loops. If it does we should mark the
hash bucket chain as invalid. This is easily checked by noticing when we
have seen more than the number of chain elements. Which equals the max
number as symbols in the table.

https://sourceware.org/bugzilla/show_bug.cgi?id=23011

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdwfl: Use process_vm_readv when available.
Mark Wielaard [Sat, 17 Mar 2018 23:24:08 +0000 (00:24 +0100)]
libdwfl: Use process_vm_readv when available.

If possible use process_vm_readv to read 4K blocks instead of fetching
each word individually with ptrace. For unwinding this often means we
only have to do one process_vm_readv of the stack instead of dozens of
ptrace calls. There is one 4K cache per process, cleared whenever a
thread is detached.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Print dwarf_dieoffset as %PRIx64, not %zx.
Mark Wielaard [Tue, 27 Mar 2018 14:22:16 +0000 (16:22 +0200)]
readelf: Print dwarf_dieoffset as %PRIx64, not %zx.

On 32bit architectures size_t is not 64bit...

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Report error when decl_file or call_file attribute is invalid.
Mark Wielaard [Tue, 20 Mar 2018 12:29:25 +0000 (13:29 +0100)]
readelf: Report error when decl_file or call_file attribute is invalid.

Report an error for why the DW_AT_decl_file or DW_AT_call_file cannot
be resolved to a file name. This is likely invalid DWARF, a missing
DW_AT_stmt_list attribute on the CU or a missing .debug_line section.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibebl: Handle SYMTAB_SHNDX in ebl_dynamic_tag_name.
Mark Wielaard [Fri, 16 Mar 2018 18:42:17 +0000 (19:42 +0100)]
libebl: Handle SYMTAB_SHNDX in ebl_dynamic_tag_name.

SYMTAB_SHNDX was introduced when elf.h was imported, but not yet handled
in ebl_dynamic_tag_name. Handle it and add an eu_static_assert to make
sure stdtags always contains DT_NUM entries.

https://sourceware.org/bugzilla/show_bug.cgi?id=22976

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Add new DWARF5 Dwarf expression operations.
Mark Wielaard [Wed, 15 Nov 2017 11:13:38 +0000 (12:13 +0100)]
libdw: Add new DWARF5 Dwarf expression operations.

DW_OP_implicit_pointer, DW_OP_entry_value, DW_OP_const_type,
DW_OP_regval_type, DW_OP_deref_type, DW_OP_xderef_type,
DW_OP_convert and OP_reinterpret are implemented like their
pre-DWARF5 GNU variants.

DW_OP_xderef_type is implemented as a (non-CU relative) variant of
DW_OP_deref_type.

DW_OP_addrx and DW_OP_constx are recognized but not interpreted yet.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdwfl: Use realpath (name, NULL) instead of canonicalize_file_name (name).
Mark Wielaard [Mon, 5 Mar 2018 13:23:14 +0000 (14:23 +0100)]
libdwfl: Use realpath (name, NULL) instead of canonicalize_file_name (name).

Some systems apparently don't provide canonicalize_file_name. Since
canonicalize_file_name (path) is defined as realpath(path, NULL) just
use realpath everywhere.

https://sourceware.org/bugzilla/show_bug.cgi?id=21009

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Add some support for locviews.
Mark Wielaard [Wed, 28 Feb 2018 23:14:52 +0000 (00:14 +0100)]
readelf: Add some support for locviews.

This adds minimal support for locviews as output by GCC8.
It changes readelf to keep track of loclistptrs from DW_AT_GNU_locviews
and prints the locview pairs for those. Since there is no terminator
we have to keep track of where the next loclist entry starts.
The --debug-dump=loc output looks as follows:

 CU [   714] base: +0x0000000000003020 <elf_hash>
 [    b4] view pair 1, 2
 [    b6] range 4, 7f
          +0x0000000000003024 <elf_hash+0x4>..
          +0x000000000000309e <elf_hash+0x7e>
           [ 0] reg5
 [    d9] view pair 3, 1
          view pair 1, 2
 [    dd] range 4, 4c
          +0x0000000000003024 <elf_hash+0x4>..
          +0x000000000000306b <elf_hash+0x4b>
           [ 0] reg5
          range 4c, 7f
          +0x000000000000306c <elf_hash+0x4c>..
          +0x000000000000309e <elf_hash+0x7e>
           [ 0] reg2

Note that in the above output the view pairs correspond to the ranges
immediately following in the loc list. This is how GCC8 currently
outputs the locview pairs and ranges, but this is not guaranteed and
you'll need to look at the location and GNU_locviews attributes of the
DIE to know which really match up together. We might want to adjust the
output to make this more clear.

This does not yet add an locview accessor to libdw. It just recognizes
the DW_AT_GNU_locviews attribute as a loclistptr when encoded as a
sec_offset form.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoelflint: Make sure we can read a whole element when iterating over group.
Mark Wielaard [Mon, 26 Feb 2018 21:53:10 +0000 (22:53 +0100)]
elflint: Make sure we can read a whole element when iterating over group.

Change the for loop so that we can always read a full element.

https://sourceware.org/bugzilla/show_bug.cgi?id=22892

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibebl: Allow SHT_NOTE as relocation target type.
Mark Wielaard [Wed, 21 Feb 2018 15:31:44 +0000 (16:31 +0100)]
libebl: Allow SHT_NOTE as relocation target type.

eu-elflint uses ebl_check_reloc_target_type to determine whether a section
is a valid relocation target. In Fedora rawhide there are new ELF notes
(annobin) which have relocations against them in ET_REL files. eu-elflint
currently flags these as invalid. It looks like that is not correct.
I cannot find any reason an SHT_NOTE section cannot have relocations
against it. So this patch allows them.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Add support for DWARF5 DW_FORM_data16.
Mark Wielaard [Thu, 28 Dec 2017 16:44:06 +0000 (17:44 +0100)]
libdw: Add support for DWARF5 DW_FORM_data16.

The DWARF5 spec says DW_FORM_data16 is constant class (128bit value).
But we treat it as if it is block class. So to use a attribute that is
encoded as DW_FORM_data16 use dwarf_formblock, not dwarf_form[us]data.

We cannot use dwarf_form[us]data since they return a Dwarf_Word/Sword,
which are only 64bits.

This does mean we don't try to convert the value but just return it as
a block of 16 raw bytes.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Handle DWARF5 DW_FORM_implicit_const. Add dwarf_getabbrevattr_data.
Mark Wielaard [Sun, 26 Nov 2017 16:51:11 +0000 (17:51 +0100)]
libdw: Handle DWARF5 DW_FORM_implicit_const. Add dwarf_getabbrevattr_data.

Handle the new DW_FORM_implicit_const. The value of this form is embedded
in the abbrev data (as sleb128) and not in the info DIE data. This also
adds a new function dwarf_getabbrevattr_data which allows getting any
data/value associated with a form. eu-readelf will use this new function
to show the DW_FORM_implicit_const value.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Recognize new DWARF5 attribute forms.
Mark Wielaard [Sun, 26 Nov 2017 16:40:56 +0000 (17:40 +0100)]
libdw: Recognize new DWARF5 attribute forms.

This just makes sure we know how the new forms are encoded.
It doesn't yet handle them in the dwarf_form* functions.
But it does make it possible to skip them when reading DWARF5.

DW_FORM_implicit_const has zero size (the value is in the abbrev,
not in the info). DW_FORM_addrx[1234], DW_FORM_strx[1234],
DW_FORM_ref_sup[48] and DW_FORM_data16 have constant size.
DW_FORM_strp_sup and DW_FORM_line_strp are offset size.
DW_FORM_addrx, DW_FORM_strx, DW_FORM_loclistx and DW_FORM_rnglistx
encode a uleb128.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Add new dwarf_get_units function to iterate over all units.
Mark Wielaard [Tue, 30 Jan 2018 11:05:57 +0000 (12:05 +0100)]
libdw: Add new dwarf_get_units function to iterate over all units.

The dwarf_nextcu and dwarf_next_unit functions provide information to
construct the offset to construct the associated CU DIE using dwarf_offdie
or dwarf_offdie_types.  This requires the user to know beforehand where
to DIE data is stored (in the .debug_info or .debug_types section).
For type units one also needs to use the type offset to create the actual
type DIE. In DWARF5 DIEs can come from even more data locations. And there
are also skeleton units which require the user to find the associated
split compile unit DIE (which would come from a different file).

The new dwarf_get_units function simplifies iterating over the units in
a DWARF file. It doesn't require the user to know where the DIE data is
stored, it will automagically iterate over all know data sources (sections)
returning the Dwarf_CU and the associated Dwarf_Die if requested. If the
user requests to know the associated "subdie" it will also be resolved.

This implementation returns the correct subdie for type units. A future
version will also handle skeleton units and return the associated skeleton
DIE and split unit DIE.

readelf has been adapted to use the new iterator and print the new DWARF5
unit header information (which it gets through dwarf_cu_die). The new
interface hides which section exactly to iterate on (by design). readelf
works around that by "cheating". It sets up a Dwarf_CU so that it gets
the data from the right section, using the (normally) internal data
structure.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Parse new DWARF5 units and CU DIEs.
Mark Wielaard [Mon, 29 Jan 2018 14:59:05 +0000 (15:59 +0100)]
libdw: Parse new DWARF5 units and CU DIEs.

Parse DWARF5 units, add the unit_type to the Dwarf_CU and generalize some
code calculating the header length and getting at the first DIE of a unit.
Unit headers can have different sizes depending on the unit type.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibelf: Sync elf.h from glibc.
Mark Wielaard [Fri, 16 Feb 2018 23:23:19 +0000 (00:23 +0100)]
libelf: Sync elf.h from glibc.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agotests: Accept any core if no core with the "correct" pid can be found.
Mark Wielaard [Fri, 16 Feb 2018 19:34:25 +0000 (20:34 +0100)]
tests: Accept any core if no core with the "correct" pid can be found.

In some containers our view of pids is confused. We see the container
pid namespace, but the core is generated using the host pid namespace.
Since tests are run in a new fresh directory any core here is most like
is ours.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoInclude sys/ptrace.h as early as possible.
Mark Wielaard [Thu, 15 Feb 2018 13:44:18 +0000 (14:44 +0100)]
Include sys/ptrace.h as early as possible.

On some systems, at least on Fedora 27 ppc64le with glibc 2.26-24 and
kernel 4.14.18-300, including sys/ptrace.h late (after signal.h or
sys/wait.h for example) will cause issues and produce errors like:

In file included from /usr/include/asm/sigcontext.h:12:0,
                 from /usr/include/bits/sigcontext.h:30,
                 from /usr/include/signal.h:287,
                 from /usr/include/sys/wait.h:36,
                 from linux-pid-attach.c:38:
/usr/include/sys/ptrace.h:73:3: error: expected identifier before numeric constant
   PTRACE_GETREGS = 12,
   ^

Swapping the include order fixes these issues.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Add dwarf_die_addr_die function.
Mark Wielaard [Thu, 25 Jan 2018 00:13:10 +0000 (01:13 +0100)]
libdw: Add dwarf_die_addr_die function.

Currently storing a lot of Dwarf_Dies might be inefficient since it
costs a lot of memory since the sizeof (Dwarf_Die) == 32 bytes on 64bit
arches. You can try storing just the Dwarf_Off from dwarf_dieoffset.
Which is just 8 bytes. But then you have to keep track of whether to
call dwarf_dieoffset, if the Dwarf_Die came from the main .debug_info,
or call dwarf_dieoffset_types, if it came from .debug_types. And you'll
have to keep track of whether it came from the main Dwarf or the alt
Dwarf (dwz multi file). With DWARF5 or GNU DebugFission split-dwarf
you will also need to know which split Dwarf file the original DIE
came from.

A Dwarf_Die consists of an addr pointer where the actual DIE data
comes from, a CU pointer that provides context (and has a pointer
to the Dwarf file the Die is associated with) and a (cached)
Dwarf_Abbrev pointer that is initialized when the Dwarf_Die is
first read and describes how to interpret the DIE data.

libdw already keeps track of the data pointers (sections) of a
Dwarf file and given an offset it can already reconstruct the
other Dwarf_Die fields. So this patch introduces dwarf_die_addr_die.
Given a Dwarf_Die addr dwarf_die_addr_die returns a (reconstructed)
Dwarf_Die, or NULL if the given addr didn't come from a valid
Dwarf_Die. In particular it will make sure that the correct Dwarf_CU
pointer is set for the Dwarf_Die, the Dwarf_Abbrev pointer will not
be set up yet (it will only be once the Dwarf_Die is used to read
attributes, children or siblings).

This functions can be used to keep a reference to a Dwarf_Die which
you want to refer to later. The addr, and the result of this function,
is only valid while the associated Dwarf is valid.

Since libdw already had to lookup the Dwarf_CU given an offset, this
function is as efficient as dwarf_dieoffset (or dwarf_dieoffset_types)
without having to know the original origin of the Dwarf_Die. It will
search both the .debug_info and .debug_types data sections from both
the main Dwarf or the alt Dwarf file. Once split dwarf support is added
it will also look in any split dwarf .dwo (or the .dwp) file.

The only limitation, compared to using a Dwarf_Off and dwarf_dieoffset,
is that it only works during runtime while the main Dwarf object is
valid (till dwarf_end has been called on it).

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoUse fallthrough attribute.
Joshua Watt [Fri, 9 Feb 2018 16:27:18 +0000 (10:27 -0600)]
Use fallthrough attribute.

Use __attribute__ ((fallthrough)) to indicate switch case fall through
instead of a comment. This ensures that the fallthrough warning is not
triggered even if the file is pre-processed (hence stripping the
comments) before it is compiled.

The actual fallback implementation is hidden behind a FALLBACK macro in
case the compiler doesn't support it.

Finally, the -Wimplict-fallthrough warning was upgraded to only allow
the attribute to satisfy it; a comment alone is no longer sufficient.

Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
6 years agolibdw: Resolve alt file on first use.
Mark Wielaard [Fri, 19 Jan 2018 22:59:21 +0000 (23:59 +0100)]
libdw: Resolve alt file on first use.

Add a new alt_fd field to the Dwarf struct. This tracks whether we tried
to open the alt file ourselves. This is used in dwarf_getalt to see if
we should try to find and open the alt file ourselves (if the user hasn't
called dwarf_setalt yet). dwarf_formref_die and dwarf_formstring now call
dwarf_getalt instead of accessing the alt_dwarf Dwarf field directly.

For applications using libdwfl nothing changes (dwfl will find, set and
clean up the alt file). For programs that set the alt file themselves
already through other means, nothing changes. But for applications that
don't create the Dwarf through libdwfl and don't set the alt file already
libdw will now try to find and set it on first access. If found the
application will now not get errors for missing alt files.

Add a simple testcase based on the existing allfcts test which already
tries to set the alt file, but is too simplistic to find it in some
subdir (relative to the main debug file).

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agotests: Check symtabshdr instead of symtabndx in elfstrmerge.c.
Mark Wielaard [Thu, 25 Jan 2018 13:37:17 +0000 (14:37 +0100)]
tests: Check symtabshdr instead of symtabndx in elfstrmerge.c.

Some gcc omptimization levels (-Og in particular) didn't see that when
symtabndx != 0, then symtabshdr was certain to be initialized. Change
the symtabndx == 0 check to symtabshdr == NULL and initialize symtabshdr
to work around that.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Fix crash on reading loc data or range data with bad/no CUs.
Mark Wielaard [Thu, 25 Jan 2018 12:24:19 +0000 (13:24 +0100)]
readelf: Fix crash on reading loc data or range data with bad/no CUs.

In print_debug_ranges_section and print_debug_loc_section we try to
get the associated CU through skip_listptr_hole for the first data data.
If no CU at all can be found (because the .debug_info section was bogus)
this would keep the Dwarf_CU uninitialized causing a crash later on
when it was compared to the last_cu and used because it was unequal.
Fix this by explicitly initializing cu to last_cu (which is NULL on
first use).

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: dwarf_formsdata should return a signed value
Petr Machata [Sun, 14 Jan 2018 02:22:05 +0000 (03:22 +0100)]
libdw: dwarf_formsdata should return a signed value

The function dwarf_formsdata is used for decoding signed values, but
except for the variable-length DW_FORM_sdata, it uses unsigned
primitives to decode the value. This is not a problem for 64-bit values,
but the smaller values come decoded wrong. Fix by changing to signed
primitives for decoding the fixed-length forms.

Add a test case that uses dwarf_aggregate_size to determine an array
size whose lower bound is -1, encoded using DW_FORM_data1, and upper
bound 255 with DW_FORM_data2. When the -1 is decoded wrongly, it comes
back as 255, and the array size is 1. The correct array size should be
257.

Signed-off-by: Petr Machata <pmachata@gmail.com>
6 years agolibdw: Reduce size of struct Dwarf_Abbrev.
Mark Wielaard [Tue, 26 Dec 2017 15:49:46 +0000 (16:49 +0100)]
libdw: Reduce size of struct Dwarf_Abbrev.

If we don't cache the attrcnt and use bitfields for the has_children and
code we can reduce the size of struct Dwarf Abbrev from 32 to 24 bytes on
64bit architectures and from 28 to 20 bytes on 32bit arches.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: New get_uleb128_unchecked to use with already checked Dwarf_Abbrev.
Mark Wielaard [Tue, 26 Dec 2017 10:52:06 +0000 (11:52 +0100)]
libdw: New get_uleb128_unchecked to use with already checked Dwarf_Abbrev.

When creating a Dwarf_Abbrev in dwarf_getabbrev (__libdw_getabbrev) we
already check it is fully readable from the .debug_abbrev section. So
whenever we reread it later using the attrp pointer we don't have to
check it again. Introduce get_uleb128_unchecked to use for ulebs we
know are safe to read directly.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Use dwarf_form_name for printing (unknown) forms.
Mark Wielaard [Mon, 1 Jan 2018 21:39:24 +0000 (22:39 +0100)]
readelf: Use dwarf_form_name for printing (unknown) forms.

Use dwarf_form_name consistently to print forms. attr_callback was
using its own string conversion, print_debug_macro_section was using
dwarf_form_string instead of dwarf_form_name.

The difference between dwarf_form_string and dwarf_form_name is that
the first returns NULL if the form is unknown and the second returns
an hexadecimal string representation in that case.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agotests: Try to use coredumpctl to extract core files.
Mark Wielaard [Sat, 23 Dec 2017 22:16:24 +0000 (23:16 +0100)]
tests: Try to use coredumpctl to extract core files.

If systemd-coredump is installed we have to use coredumpctl to extract
the core file to test. Unfortunately systemd-coredump/coredumpctl seem
to be somewhat fragile if multiple core dumps are generated/extracted
at the same time. So use a lock file to only run one core dump test at
a time (under make -j).

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Format offset as DIE index (hex).
Mark Wielaard [Thu, 28 Dec 2017 23:46:19 +0000 (00:46 +0100)]
readelf: Format offset as DIE index (hex).

That makes it so much easier to find the actual DIE offset in the output.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Check there is .debug_info/types section data in __libdw_offdie.
Mark Wielaard [Thu, 28 Dec 2017 23:34:14 +0000 (00:34 +0100)]
libdw: Check there is .debug_info/types section data in __libdw_offdie.

If a Dwarf_Die was requested from an offset into a section data that
didn't exist we would crash. Crashing is bad even if given bad input.
Just return an error in that case.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoDon't overflow in __libdw_in_section and __libdw_offset_in_section.
Ulf Hermann [Tue, 9 May 2017 16:28:33 +0000 (18:28 +0200)]
Don't overflow in __libdw_in_section and __libdw_offset_in_section.

This exposes a bug in dwarf_formstring as detected by the dwarf-getmacros
test before we made sure to use the correct sec_idx for the CU.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Add explicit section index to struct Dwarf_CU.
Mark Wielaard [Wed, 20 Dec 2017 15:50:57 +0000 (16:50 +0100)]
libdw: Add explicit section index to struct Dwarf_CU.

The DIE (attribute) data might come from either the main .debug_info
section or for DWARFv4 from a separate .debug_types section. Or in
case of the fake_loc_cu from the .debug_loc section and in the case
of macros from the .debug_macinfo or .debug_macro section.

We didn't handle the last two "fake" CU cases correctly when sanity
checking offsets in __libdw_read_address and __libdw_read_offset.

Add an explicit sec_idx field to struct Dwarf_CU that is always set
to the actual section that the data came from.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Try to continue after encountering bogus ELF Note data.
Mark Wielaard [Mon, 18 Dec 2017 11:39:13 +0000 (12:39 +0100)]
readelf: Try to continue after encountering bogus ELF Note data.

There might be other sections or segments with ELF Notes that are
readable. Also adjust error message depending on whether or not we
actually got any data for the ELF Note segment or section.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: dwarf_aggregate_size should not peel the given DIE.
Mark Wielaard [Mon, 11 Dec 2017 22:58:34 +0000 (23:58 +0100)]
libdw: dwarf_aggregate_size should not peel the given DIE.

Reserve memory for a new DIE first. The caller might not care, but it
isn't really nice to change the DIE the caller gave us.

See also https://sourceware.org/bugzilla/show_bug.cgi?id=22546#c5

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: dwarf_aggregate_size() works with multi-dimensional arrays
Dima Kogan [Fri, 8 Dec 2017 09:45:10 +0000 (01:45 -0800)]
libdw: dwarf_aggregate_size() works with multi-dimensional arrays

If we have a multidimensional array of dimensions (a,b,c) the number of elements
should be a*b*c, but prior to this patch dwarf_aggregate_size() would report
a+b+c instead.

This patch fixes the bug and adds a test that demonstrates the bug (the test
fails without the functional part of this patch).

Fixes: https://sourceware.org/bugzilla/show_bug.cgi?id=22546

Signed-off-by: Dima Kogan <dima@secretsauce.net>
6 years agoreadelf: Handle DW_OP_call2 and DW_OP_call4 correctly.
Mark Wielaard [Thu, 7 Dec 2017 15:31:54 +0000 (16:31 +0100)]
readelf: Handle DW_OP_call2 and DW_OP_call4 correctly.

DW_OP_call2 and DW_OP_call4 didn't correctly advance the data pointer.
This caused print_ops to produce garbage operands. Also format the
arguments as DIE offsets. That makes it easier to follow the call to the
actual dwarf_procedure DIE.

Testcase from https://sourceware.org/bugzilla/show_bug.cgi?id=22532

The testcase only checks the eu-readelf output is correct for the
byte_size attribute. But it might be interesting to write a full
expression parser to check the actual sizes.

 [    3e]    structure_type
             name                 (strp) "pck__rec"
             byte_size            (exprloc)
              [   0] push_object_address
              [   1] deref_size 1
              [   3] call4 [    95]
              [   8] plus_uconst 7
              [  10] const1s -4
              [  12] and

 [    95]    dwarf_procedure
             location             (exprloc)
              [   0] dup
              [   1] lit1
              [   2] ne
              [   3] bra 10
              [   6] lit4
              [   7] skip 31
              [  10] dup
              [  11] lit4
              [  12] ne
              [  13] bra 20
              [  16] lit0
              [  17] skip 31
              [  20] dup
              [  21] lit3
              [  22] eq
              [  23] bra 30
              [  26] lit0
              [  27] skip 31
              [  30] lit4
              [  31] swap
              [  32] drop

The "answer" depends on the Discr value (first byte at object address),
and is rounded up to 4 or 8 bytes.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Hook up -g, --section-groups to display the section groups.
Mark Wielaard [Wed, 29 Nov 2017 23:10:58 +0000 (00:10 +0100)]
readelf: Hook up -g, --section-groups to display the section groups.

It was already possible to display the section groups using -a, but
the argp options didn't yet have an -g, --section-groups entry to
just display the section groups.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Print CU, base address and unresolved .debug_loc entries.
Mark Wielaard [Wed, 29 Nov 2017 15:37:50 +0000 (16:37 +0100)]
readelf: Print CU, base address and unresolved .debug_loc entries.

Also adjust the formatting for the resolved addresses to print them
on separate lines so they nicely line up even when the addresses are
resolved to symbol+offset names. And print the operands starting on
a new line.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Print CU, base address and unresolved .debug_range entries.
Mark Wielaard [Wed, 29 Nov 2017 15:27:33 +0000 (16:27 +0100)]
readelf: Print CU, base address and unresolved .debug_range entries.

Also adjust the formatting for the resolved addresses to print them
on separate lines so they nicely line up even when the addresses are
resolved to symbol+offset names.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Print actual file for decl_file and call_file attributes.
Mark Wielaard [Wed, 29 Nov 2017 15:06:36 +0000 (16:06 +0100)]
readelf: Print actual file for decl_file and call_file attributes.

When we see a DW_AT_decl_file or DW_AT_call_file attribute print the
actual file name. The current interface gives us a full (absolute) patch,
but we only want to show the file name for now to not clutter the output
too much. This helps a lot when trying to determine where something was
declared if you are just looking at the DIE tree. Otherwise you'll have
to cross match the number by hand with the corresponding line table entry.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Print abbrev code for DIE with --debug-dump=info.
Mark Wielaard [Wed, 29 Nov 2017 14:58:40 +0000 (15:58 +0100)]
readelf: Print abbrev code for DIE with --debug-dump=info.

If there is anything wrong with a DIE it is useful to know what the abbrev
code was so you can lookup the abbrev description.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Adjust print_ops formatting.
Mark Wielaard [Wed, 29 Nov 2017 14:43:26 +0000 (15:43 +0100)]
readelf: Adjust print_ops formatting.

Use only 2 spaces for index (there are never 10000, the most seen in the
wild is 64). Adjust re-indenting after GNU_entry_value.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdwfl: Don't dereference possibly unaligned auxv entry pointer from core.
Mark Wielaard [Mon, 20 Nov 2017 13:11:02 +0000 (14:11 +0100)]
libdwfl: Don't dereference possibly unaligned auxv entry pointer from core.

The notes in a core file that contain the auxv entries might not be
naturally aligned. The code already tried to account for that, but
the GCC 8 undefined behaviour sanitizer found we were till dereferencing
the actual auxv entry pointer directly. Fix this by calculating all pointers
by hand and not use an array of auxv entries trick.

This makes make distcheck (which enables sanitize-undefined by default)
pass again using GCC8.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Print attribute name and form in error message.
Mark Wielaard [Tue, 21 Nov 2017 10:13:00 +0000 (11:13 +0100)]
readelf: Print attribute name and form in error message.

Now an error getting the attribute value will not only print the DIE offset
and the reason, but also the attribute name and form. e.g.

  DIE [b] cannot get attribute 'ranges' (sec_offset) value: .debug_ranges
  section missing

Also we don't abort, but try to print the other attributes of the DIE
anyway. It might just be one attribute whose value cannot be resolved.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Print DIE offset in attribute reading error messages.
Mark Wielaard [Tue, 3 Oct 2017 14:55:25 +0000 (16:55 +0200)]
readelf: Print DIE offset in attribute reading error messages.

When processing large files it is useful to know the DIE offset if
printing of attributes fails (especially when redirecting the output).
With this change the error message looks like:

  eu-readelf: DIE [2aeb8ef1] cannot get attribute value: invalid DWARF

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agotests: Fix cfi_debug => cfi_debug_bias typo in varlocs assert.
Mark Wielaard [Thu, 16 Nov 2017 12:20:11 +0000 (13:20 +0100)]
tests: Fix cfi_debug => cfi_debug_bias typo in varlocs assert.

We want to check whether the bias is zero, not whether we have dwarf
debug_frame cfi.

This triggered on a ppc64/ppc64le self-check since it has both
debug_frame and eh_frame cfi (other arches often only have eh_frame).

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agotests: Add varlocs-self and exprlocs-self tests.
Mark Wielaard [Wed, 8 Nov 2017 10:25:33 +0000 (11:25 +0100)]
tests: Add varlocs-self and exprlocs-self tests.

Make sure the testcases (library functions they use) don't crash,
triggers self-check/asserts or leaks memory under valgrind. This
also helps making sure newer DWARF constructs are handled (when
building with -gdwarf-5).

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Handle DW_OP_GNU_variable_value.
Mark Wielaard [Thu, 2 Nov 2017 15:24:41 +0000 (16:24 +0100)]
libdw: Handle DW_OP_GNU_variable_value.

Handle DW_OP_GNU_variable_value in dwarf_getlocation[_attr,_die].
DW_OP_GNU_variable_value takes one argument a DIE reference that
describes a value given by a location of const_value attribute.

To test handling of the new operand the varlocs test is adapted
to print out all DIEs and attributes with expressions or location
lists (the original varlocs test only prints out variables and
arguments of function DIEs).

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Update acceptable forms and attributes for dwarf_getlocation.
Mark Wielaard [Thu, 2 Nov 2017 15:24:17 +0000 (16:24 +0100)]
libdw: Update acceptable forms and attributes for dwarf_getlocation.

dwarf_getlocation has to know which attributes can contain a DWARF
expression or location list because the form alone might be ambiguous.

Since DWARF4 there is DW_FORM_exprloc so always accept that. But for older
DWARF or location lists we cannot just check for DW_FORM_sec_offset since
that could be a reference to diffent kinds of sections (based on attribute).

Update the attribute list based on the latest DWARF5 encodings table.
Note that DW_AT_call_origin wasn't added because that seems to be a
typo in the DWARF5 spec. http://dwarfstd.org/ShowIssue.php?issue=171103.1

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoreadelf: Handle DW_OP_GNU_variable_value.
Mark Wielaard [Thu, 2 Nov 2017 15:23:24 +0000 (16:23 +0100)]
readelf: Handle DW_OP_GNU_variable_value.

Also format both DW_OP_call_ref and DW_OP_GNU_variable_value argument
as a normal DIE reference.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoconfig: files under /usr/lib/sysctl.d (_sysctldir) aren't %config.
Mark Wielaard [Thu, 2 Nov 2017 15:53:10 +0000 (16:53 +0100)]
config: files under /usr/lib/sysctl.d (_sysctldir) aren't %config.

Admin can place the real config file under /etc/sysctl.d as override.
https://bugzilla.redhat.com/show_bug.cgi?id=1506660

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agobackends: Ignore GCC8 -Wpacked-not-aligned for m68k_corenote.c.
Mark Wielaard [Tue, 24 Oct 2017 12:23:30 +0000 (14:23 +0200)]
backends: Ignore GCC8 -Wpacked-not-aligned for m68k_corenote.c.

The GCC8 -Wpacked-not-aligned warns if a structure field with explicit
padding in a packed structure will be misaligned. m68k prstatus core
notes are described by a packed structure which has such aligned structure
fields.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolib: Remove md5 and sha1 implementations.
Mark Wielaard [Mon, 16 Oct 2017 14:16:12 +0000 (16:16 +0200)]
lib: Remove md5 and sha1 implementations.

Only the testcase md5-sha1-test used them. So also remove that testcase.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdwfl: When the kernel is found, but not the modules warn, don't fail.
Mark Wielaard [Mon, 16 Oct 2017 11:25:33 +0000 (13:25 +0200)]
libdwfl: When the kernel is found, but not the modules warn, don't fail.

For -k the argp parser could fail to find the modules after the kernel
itself was already found. Calling failure at this point closes the Dwfl,
which isn't necessary. Just warn about the missing modules and continue
with the kernel only Dwfl.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibelf: Don't error out when sanity checking e_shoff if scncnt is zero.
Mark Wielaard [Wed, 4 Oct 2017 18:52:53 +0000 (20:52 +0200)]
libelf: Don't error out when sanity checking e_shoff if scncnt is zero.

We won't use the e_shoff value in that case because we will set
elf->state.elf[64|32].scns.cnt to zero to indicate not to read
any section header data from the file.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibelf: Add ELF_E_INVALID_ELF error value.
Mark Wielaard [Wed, 4 Oct 2017 14:51:15 +0000 (16:51 +0200)]
libelf: Add ELF_E_INVALID_ELF error value.

Add ELF_E_INVALID_ELF which is set when the ELF file data is bad.
This is different from ELF_E_INVALID_FILE which is set when the file
could not be read.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Define LIBDW_CIE_ID and use it in dwarf_cfi_cie_p.
Mark Wielaard [Tue, 3 Oct 2017 21:08:01 +0000 (23:08 +0200)]
libdw: Define LIBDW_CIE_ID and use it in dwarf_cfi_cie_p.

dwarf_cfi_cie_p used DW_CIE_ID_64 from dwarf.h which meant you had
to include dwarf.h just to use the dwarf_cfi_cie_p macros. Just define
the constant as LIBDW_CIE_ID in libdw.h to keep it independent.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoMake sure packed structs follow the gcc memory layout
Ulf Hermann [Fri, 18 Aug 2017 12:05:10 +0000 (14:05 +0200)]
Make sure packed structs follow the gcc memory layout

gcc defaults to using struct layouts that follow the native conventions,
even if __attribute__((packed)) is given. In order to get the layout we
expect, we need to tell gcc to always use the gcc struct layout, at
least for packed structs. To do this, we can use the gcc_struct
attribute.

This is important, not only for porting to windows, but also potentially
for other platforms, as the bugs resulting from struct layout
differences are rather subtle and hard to find.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
6 years agoar: Check whether ar header values fit.
Mark Wielaard [Sun, 10 Sep 2017 22:12:31 +0000 (00:12 +0200)]
ar: Check whether ar header values fit.

When compiling with -O3 gcc finds an interesting error:

src/ar.c: In function ‘do_oper_insert’:
src/ar.c:1077:56: error: ‘%-*ld’ directive output may be truncated writing between 6 and 10 bytes into a region of size 7 [-Werror=format-truncation=]
   snprintf (tmpbuf, sizeof (tmpbuf), ofmt ? "%-*lo" : "%-*ld", bufsize, val);
                                                        ^~~~~
The problem is that the ar header values have to fit in a limited
(not zero terminated) string. We should check the snprintf return
value to see if the values are representable.

Also make ar valgrind and ubsan clean and add a minimal sanity test.

Reported-by: Matthias Klose <doko@ubuntu.com>
Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoREADME: Add basic build instructions.
Mark Wielaard [Tue, 19 Sep 2017 14:05:49 +0000 (16:05 +0200)]
README: Add basic build instructions.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoInternationalized messages should not contain the '\v' escape sequence.
Mark Wielaard [Fri, 1 Sep 2017 10:42:45 +0000 (12:42 +0200)]
Internationalized messages should not contain the '\v' escape sequence.

Replace horizontal tab '\v' with double line feed '\n\n' in doc strings.
Regenerate .po files.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoCast pid_t to long long when printing
Ulf Hermann [Fri, 18 Aug 2017 15:20:02 +0000 (17:20 +0200)]
Cast pid_t to long long when printing

We don't know sizeof(pid_t) as it's not specified in any standard. In
order to still print it, we cast to long long, the largest integer type
we can easily print.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoOn non-linux systems, don't use native signal numbers
Ulf Hermann [Fri, 18 Aug 2017 15:03:50 +0000 (17:03 +0200)]
On non-linux systems, don't use native signal numbers

We assume core files from linux systems, so we should use the linux
version of the signals when reading them. Other OS might have different
signal numbers.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
6 years agoDetect if symbol versioning is supported
Ulf Hermann [Fri, 18 Aug 2017 13:50:42 +0000 (15:50 +0200)]
Detect if symbol versioning is supported

If not, throw an error unless symbol versioning was explicitly
disabled.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
6 years agoDrop -rdynamic from deleted-lib.so link step
Ulf Hermann [Fri, 18 Aug 2017 13:17:38 +0000 (15:17 +0200)]
Drop -rdynamic from deleted-lib.so link step

-rdynamic is meant for ELF objects that need to export internal
symbols to libraries they link to, but deleted-lib.so does not link to
anything else and doesn't have any internal symbols.

Note that the "deleted" test program does link to deleted-lib.so, but
deleted-lib.so being a shared object, will automatically export the
(non-hidden) "libfunc" symbol anyway.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
6 years agoCheck if gcc complains about __attribute__ (visibility(..))
Ulf Hermann [Fri, 18 Aug 2017 11:06:36 +0000 (13:06 +0200)]
Check if gcc complains about __attribute__ (visibility(..))

If so, define attribute_hidden to be empty. Also, use attribute_hidden
in all places where we hide symbols. If this attribute is missing, it
simply means that we cannot hide private symbols in the binary using
attributes. This disables some optimizations and may increase the risk
of symbol name clashes with other libraries, but is not fatal.

However, we still employ linker version scripts to explicitly define
the exported symbols. This serves much of the same purpose. Also, as
all our symbols are prefixed with the library name, and "__" for
private ones, the chance of clashes is low anyway.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
6 years agoCheck for -z,defs, -z,relro, -fPIC, -fPIE before using them
Ulf Hermann [Fri, 18 Aug 2017 10:41:11 +0000 (12:41 +0200)]
Check for -z,defs, -z,relro, -fPIC, -fPIE before using them

Those flags are not available on all platforms, and omitting them when
not available will not cause any harm. In particular:

-z,defs disallows undefined symbols in object files. This option is
unsupported if the target binary format enforces the same condition
already. Furthermore it is only a compile time sanity check. When it is
omitted, the same binary is produced.

-z,relro instructs the loader to mark sections read-only after loading
the library, where possible. This is a hardening mechanism. If it is
unavailable, the functionality of the code is not affected in any way.

-fPIC instructs the compiler to produce position independent code. While
this is preferable to relocatable code, relocatable code also works and
may even be faster. Relocatable code might just be loaded into memory
multiple times for different processes.

-fPIE is the same thing as -fPIC for executables rather than shared
libraries.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
6 years agolibelf: Sync elf.h from glibc.
Mark Wielaard [Tue, 15 Aug 2017 20:43:01 +0000 (22:43 +0200)]
libelf: Sync elf.h from glibc.

Add new powerpc note descriptors.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agotests: robustify run-strip-nothing.sh against unstripped libc_nonshared.a
Dmitry V. Levin [Tue, 8 Aug 2017 16:06:16 +0000 (19:06 +0300)]
tests: robustify run-strip-nothing.sh against unstripped libc_nonshared.a

When glibc's libc_nonshared.a contains objects with debug info,
this debug info is leaked into every output file produced by gcc.
Change run-strip-nothing.sh to use "gcc -s" instead of plain "gcc"
for producing objects without debug info.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
6 years agoPrepare for 0.170. elfutils-0.170
Mark Wielaard [Wed, 2 Aug 2017 16:30:07 +0000 (18:30 +0200)]
Prepare for 0.170.

Set version to 0.170. Update po/*.po files.
Add some more user visible changes to NEWS.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agobackends: sparc GOTDATA_OP[_HIX22|LOX10] can be used in ET_REL files.
Mark Wielaard [Thu, 27 Jul 2017 21:07:00 +0000 (23:07 +0200)]
backends: sparc GOTDATA_OP[_HIX22|LOX10] can be used in ET_REL files.

Tested on new GNU Compile Farm server gcc202.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Add DW_MACRO constants and DW_MACRO_GNU compatibility defines.
Mark Wielaard [Wed, 26 Jul 2017 21:02:18 +0000 (23:02 +0200)]
libdw: Add DW_MACRO constants and DW_MACRO_GNU compatibility defines.

Accept version 5 .debug_macro format, which is identical to the GNU
version 4 format. No real support yet for the new supplementary object
file (sup) and indirect string references (strx).  GCC doesn't generate
them yet. readelf does recognize them, but doesn't try to decode them.
dwarf_getmacros currently rejects the new formats.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Handle DWARF5 immutable, packed and shared in dwarf_peel_type.
Mark Wielaard [Wed, 26 Jul 2017 20:54:59 +0000 (22:54 +0200)]
libdw: Handle DWARF5 immutable, packed and shared in dwarf_peel_type.

Also update the documentation to explain that any type alias or modifier
that doesn't modify, change the structural layout or the way to access
the underlying type is peeled. Explicitly mention pointer and reference
types as examples of modifiers that don't obey that rule and so aren't
peeled.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoHandle DWARF5 defaulted member function encodings.
Mark Wielaard [Wed, 26 Jul 2017 16:32:27 +0000 (18:32 +0200)]
Handle DWARF5 defaulted member function encodings.

Add DW_DEFAULTED_no, DW_DEFAULTED_in_class and DW_DEFAULTED_out_of_class
to dwarf.h.

Print value (no, in_class or out_of_class) of DW_AT_defaulted in readelf.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: DWARF5 Add DW_CC_pass_by_reference and DW_CC_pass_by_reference.
Mark Wielaard [Wed, 26 Jul 2017 16:15:20 +0000 (18:15 +0200)]
libdw: DWARF5 Add DW_CC_pass_by_reference and DW_CC_pass_by_reference.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Add dwarf_default_lower_bound.
Mark Wielaard [Wed, 26 Jul 2017 16:06:21 +0000 (18:06 +0200)]
libdw: Add dwarf_default_lower_bound.

Add dwarf_default_lower_bound to get the default lower bound for a language
when not given as attribute for an subrange type. Implementation extracted
from dwarf_aggregate_size.

Add a test to check all known language codes are handled.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: DWARF5 Add new DW_LANG codes and default lower array bound.
Mark Wielaard [Wed, 26 Jul 2017 15:44:29 +0000 (17:44 +0200)]
libdw: DWARF5 Add new DW_LANG codes and default lower array bound.

Add DW_LANG_OpenCL, DW_LANG_Modula3, DW_LANG_C_plus_plus_03,
DW_LANG_OCaml, DW_LANG_Rust, DW_LANG_Swift, DW_LANG_Julia,
DW_LANG_Dylan, DW_LANG_RenderScript and DW_LANG_BLISS to dwarf.h.

Update default language array lower bounds in dwarf_aggregate_size.c.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Add new DWARF5 character encodings.
Mark Wielaard [Wed, 26 Jul 2017 15:38:22 +0000 (17:38 +0200)]
libdw: Add new DWARF5 character encodings.

Add DW_ATE_UCS and DW_ATE_ASCII for Fortran 2003 string kinds ASCII
(ISO/IEC 646:1991) and ISO_10646 (UCS-4 in ISO/IEC 10646:2000).

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Add new DWARF5 tag constants.
Mark Wielaard [Wed, 26 Jul 2017 15:06:07 +0000 (17:06 +0200)]
libdw: Add new DWARF5 tag constants.

Add DW_TAG_coarray_type, DW_TAG_generic_subrange, DW_TAG_dynamic_type,
DW_TAG_call_site, DW_TAG_call_site_parameter, DW_TAG_skeleton_unit,
DW_TAG_immutable_type.

Just the constants, no further interpretion yet.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdw: Add DWARF5 attributes.
Mark Wielaard [Tue, 25 Jul 2017 13:29:10 +0000 (15:29 +0200)]
libdw: Add DWARF5 attributes.

Add new DWARF5 attribute constant names to the attributes enum.
Also add reserved comments between non-consecutive (reserved) numbers.

Remove DW_AT_subscr_data, DW_AT_element_list and DW_AT_member from the
enum list and turn them into compatibility defines because they are
not part of DWARF2+. That way code that directly references them still
compiles but they won't show up in known-dwarf.h.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agolibdwfl: Move nested functions in parse_opt to file scope.
Yunlian Jiang [Wed, 26 Jul 2017 23:25:43 +0000 (16:25 -0700)]
libdwfl: Move nested functions in parse_opt to file scope.

* Move nested function 'failure' to file scope to compile with clang.
* Move nested function 'fail' to file scope to compile with clang.

Signed-off-by: Yunlian Jiang <yunlian@google.com>
6 years agolibdw: Add dwarf_line_file.
Mark Wielaard [Fri, 21 Jul 2017 19:23:07 +0000 (21:23 +0200)]
libdw: Add dwarf_line_file.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoppc64: Add HTM SPRs support to readelf
Gustavo Romero [Thu, 20 Jul 2017 21:49:02 +0000 (17:49 -0400)]
ppc64: Add HTM SPRs support to readelf

Since POWER8, PowerPC 64 supports Hardware Transactional Memory, which has
three special purpose registers associated to it: tfhar, tfiar, and texasr.
This commit add HTM SPRs set as known note type so it's possible to use
'readelf --notes' to inspect the HTM SPRs in a coredump file generated in
such a machines.

Signed-off-by: Gustavo Romero <gromero@linux.vnet.ibm.com>
6 years agostrip: Deal with ARM data marker symbols pointing to debug sections.
Mark Wielaard [Thu, 20 Jul 2017 20:34:29 +0000 (22:34 +0200)]
strip: Deal with ARM data marker symbols pointing to debug sections.

ARM data marker symbols "$d" indicate the start of a sequence of data
items in a section. For data only sections no data marker symbol is
necessary, but may be put pointing to the start of the section.
binutils however has a bug which places a data marker symbol somewhere
inside the section (at least for .debug_frame).
https://sourceware.org/bugzilla/show_bug.cgi?id=21809

When strip finds a symbol pointing to a debug section that would be
put into the .debug file then it will copy over the whole symbol table.
This isn't necessary because the symbol is redundant.

Add an ebl hook to recognize data marker symbols with implementations
for arm and aarch64. Use it in strip to strip such symbols from the
symbol table if they point to a debug section.

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agobackends: Don't depend on linux/bpf.h to compile bpf disassembler.
Mark Wielaard [Tue, 18 Jul 2017 12:12:36 +0000 (14:12 +0200)]
backends: Don't depend on linux/bpf.h to compile bpf disassembler.

We only need a few constants and one structure definition from linux/bpf.
Just define those in a local lib/bpf.h file. This makes sure the bpf
disassembler is always build and included even when elfutils is build
on older GNU/Linux systems (and even on other platforms).

Signed-off-by: Mark Wielaard <mark@klomp.org>
6 years agoWrite to /dev/null rather than /dev/zero
Ulf Hermann [Thu, 4 May 2017 15:27:47 +0000 (17:27 +0200)]
Write to /dev/null rather than /dev/zero

/dev/zero is meant for reading zeroes. /dev/null is for writing into
nirvana.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
6 years agoUnify linking of libasm, libelf, libdw, backends
Ulf Hermann [Fri, 17 Feb 2017 15:11:28 +0000 (16:11 +0100)]
Unify linking of libasm, libelf, libdw, backends

Link them all with -z,defs,-z,relro,--no-undefined, provide complete
dependencies for the link steps, and add libeu.a to each one. libeu.a
contains useful library functionality that each of them might use. The
linker will strip unneeded symbols, so linking it in won't hurt even if
none of the functions are used.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
7 years agobackends: Swap sys/ptrace.h and asm/ptrace.h include order on s390.
Mark Wielaard [Mon, 17 Jul 2017 15:26:25 +0000 (17:26 +0200)]
backends: Swap sys/ptrace.h and asm/ptrace.h include order on s390.

glibc 2.26 changed the sys/ptrace.h header so that it cannot be included
after asm/ptrace.h. We still need to include the kernel asm/ptrace.h for
the ptrace_area definition. Including it after sys/ptrace.h works against
both old and new glibc.

Signed-off-by: Mark Wielaard <mark@klomp.org>
7 years agostrip: Add --keep-section=SECTION and --remove-section=SECTION.
Mark Wielaard [Fri, 14 Jul 2017 15:09:40 +0000 (17:09 +0200)]
strip: Add --keep-section=SECTION and --remove-section=SECTION.

Adds two new output options:

  --keep-section=SECTION Keep the named section.  SECTION is an extended
                         wildcard pattern.  May be given more than once.

  --remove-section=SECTION   Remove the named section.  SECTION is an
                         extended wildcard pattern.  May be given more than
                         once.  Only non-allocated sections can be removed.

The --remove-section was already partially implemented, but only for the
.comment section. The short option -R is to be compatible with binutils.

The new testcase makes sure that various combinations of kept/removed
sections pull the correct dependencies into the output and/or debug files.

https://bugzilla.redhat.com/show_bug.cgi?id=1465997

Signed-off-by: Mark Wielaard <mark@klomp.org>