libdwfl: Correctly handle corefile non-contiguous segments
It is possible for segments of different shared libaries to be interleaved
in memory such that the segments of one library are located in between
non-contiguous segments of another library.
For example, this can be seen with firefox on RHEL 7.9 where multiple
shared libraries could be mapped in between ld-2.17.so segments:
[...]
7f0972082000-
7f09720a4000 00000000 139264 /usr/lib64/ld-2.17.so
7f09720a4000-
7f09720a5000 00000000 4096 /memfd:mozilla-ipc (deleted)
7f09720a5000-
7f09720a7000 00000000 8192 /memfd:mozilla-ipc (deleted)
7f09720a7000-
7f09720a9000 00000000 8192 /memfd:mozilla-ipc (deleted)
7f0972134000-
7f0972136000 00000000 8192 /usr/lib64/firefox/libmozwayland.so
7f0972136000-
7f0972137000 00002000 4096 /usr/lib64/firefox/libmozwayland.so
7f0972137000-
7f0972138000 00003000 4096 /usr/lib64/firefox/libmozwayland.so
7f0972138000-
7f0972139000 00003000 4096 /usr/lib64/firefox/libmozwayland.so
7f097213a000-
7f0972147000 00000000 53248 /usr/lib64/firefox/libmozsqlite3.so
7f0972147000-
7f097221e000 0000d000 880640 /usr/lib64/firefox/libmozsqlite3.so
7f097221e000-
7f0972248000 000e4000 172032 /usr/lib64/firefox/libmozsqlite3.so
7f0972248000-
7f0972249000 0010e000 4096 /usr/lib64/firefox/libmozsqlite3.so
7f0972249000-
7f097224c000 0010e000 12288 /usr/lib64/firefox/libmozsqlite3.so
7f097224c000-
7f0972250000 00111000 16384 /usr/lib64/firefox/libmozsqlite3.so
7f0972250000-
7f0972253000 00000000 12288 /usr/lib64/firefox/liblgpllibs.so
[...]
7f09722a3000-
7f09722a4000 00021000 4096 /usr/lib64/ld-2.17.so
7f09722a4000-
7f09722a5000 00022000 4096 /usr/lib64/ld-2.17.so
dwfl_segment_report_module did not account for the possibility of
interleaving non-contiguous segments, resulting in premature closure
of modules as well as failing to report modules.
Fix this by removing segment skipping in dwfl_segment_report_module.
When dwfl_segment_report_module reported a module, it would return
the index of the segment immediately following the end address of the
current module. Since there's a chance that other modules might fall
within this address range, dwfl_segment_report_module instead returns
the index of the next segment.
This patch also fixes premature module closure that can occur in
dwfl_segment_report_module when interleaving non-contiguous segments
are found. Previously modules with start and end addresses that overlap
with the current segment would have their build-ids compared with the
current segment's build-id. If there was a mismatch, that module would
be closed. Avoid closing modules in this case when mismatching build-ids
correspond to distinct modules.
https://sourceware.org/bugzilla/show_bug.cgi?id=30975
Signed-off-by: Aaron Merey <amerey@redhat.com>