libdwfl: Make dwfl_segment_report_module aware of maximum Elf size
Commit Message
At the end of dwfl_segment_report_module we might try to read in
the whole contents described by a core file. To do this we first
allocate a zeroed block of memory that is as big as possible. The
core file however may describe much more loaded data than is actually
available in the Elf image. So pass the maximum size so we can
limit the amount of memory we reserve.
Signed-off-by: Mark Wielaard <mark@klomp.org>
---
libdwfl/ChangeLog | 8 ++++++++
libdwfl/core-file.c | 1 +
libdwfl/dwfl_segment_report_module.c | 5 +++--
libdwfl/libdwflP.h | 1 +
4 files changed, 13 insertions(+), 2 deletions(-)
@@ -1,3 +1,11 @@
+2021-12-17 Mark Wielaard <mark@klomp.org>
+
+ * libdwflP.h (dwfl_segment_report_module): Add maxread argument.
+ * core-file.c (dwfl_core_file_report): Pass elf->maximum_size to
+ dwfl_segment_report_module.
+ * dwfl_segment_report_module.c (dwfl_segment_report_module): Add
+ maxread argument. Check file_trimmed_end against maxread.
+
2021-12-08 Mark Wielaard <mark@klomp.org>
* dwfl_segment_report_module.c (dwfl_segment_report_module): Add
@@ -559,6 +559,7 @@ dwfl_core_file_report (Dwfl *dwfl, Elf *elf, const char *executable)
int seg = dwfl_segment_report_module (dwfl, ndx, NULL,
&dwfl_elf_phdr_memory_callback, elf,
core_file_read_eagerly, elf,
+ elf->maximum_size,
note_file, note_file_size,
&r_debug_info);
if (unlikely (seg < 0))
@@ -294,6 +294,7 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
void *memory_callback_arg,
Dwfl_Module_Callback *read_eagerly,
void *read_eagerly_arg,
+ size_t maxread,
const void *note_file, size_t note_file_size,
const struct r_debug_info *r_debug_info)
{
@@ -908,8 +909,8 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
/* The caller wants to read the whole file in right now, but hasn't
done it for us. Fill in a local image of the virtual file. */
- if (file_trimmed_end > SIZE_MAX)
- goto out;
+ if (file_trimmed_end > maxread)
+ file_trimmed_end = maxread;
void *contents = calloc (1, file_trimmed_end);
if (unlikely (contents == NULL))
@@ -698,6 +698,7 @@ extern int dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
void *memory_callback_arg,
Dwfl_Module_Callback *read_eagerly,
void *read_eagerly_arg,
+ size_t maxread,
const void *note_file,
size_t note_file_size,
const struct r_debug_info *r_debug_info);