From mboxrd@z Thu Jan 1 00:00:00 1970 Content-Type: multipart/mixed; boundary="===============5588696523258132052==" MIME-Version: 1.0 From: Mark Wielaard To: elfutils-devel@lists.fedorahosted.org Subject: [PATCH 15/17] libdwfl: Don't allocate phdrs and dyn on stack in link_map. Date: Sat, 23 May 2015 23:10:28 +0200 Message-ID: <1432415430-13488-16-git-send-email-mjw@redhat.com> In-Reply-To: 1432415430-13488-1-git-send-email-mjw@redhat.com --===============5588696523258132052== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Signed-off-by: Mark Wielaard --- libdwfl/ChangeLog | 5 +++++ libdwfl/link_map.c | 32 +++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog index 772de3e..90fc0f3 100644 --- a/libdwfl/ChangeLog +++ b/libdwfl/ChangeLog @@ -1,5 +1,10 @@ 2015-05-22 Mark Wielaard = + * link_map.c (dwfl_link_map_report): Allocate phdrs and dyn with + malloc instead of stack allocating. Call free when done with data. + +2015-05-22 Mark Wielaard + * dwfl_segment_report_module.c (dwfl_segment_report_module): Allocate phdrs with malloc, not on stack. free in finish. Allocate dyn with malloc, not on stack, free after use. diff --git a/libdwfl/link_map.c b/libdwfl/link_map.c index eaf43b5..8236901 100644 --- a/libdwfl/link_map.c +++ b/libdwfl/link_map.c @@ -1,5 +1,5 @@ /* Report modules by examining dynamic linker data structures. - Copyright (C) 2008-2014 Red Hat, Inc. + Copyright (C) 2008-2015 Red Hat, Inc. This file is part of elfutils. = This file is free software; you can redistribute it and/or modify @@ -856,18 +856,24 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, s= ize_t auxv_size, } if (in_ok) { - union + typedef union { Elf32_Phdr p32; Elf64_Phdr p64; char data[phnum * phent]; - } buf; + } data_buf; + data_buf *buf =3D malloc (sizeof (data_buf)); + if (buf =3D=3D NULL) + { + __libdwfl_seterrno (DWFL_E_NOMEM); + return false; + } Elf_Data out =3D { .d_type =3D ELF_T_PHDR, .d_version =3D EV_CURRENT, .d_size =3D phnum * phent, - .d_buf =3D &buf + .d_buf =3D buf }; in.d_size =3D out.d_size; if (likely ((elfclass =3D=3D ELFCLASS32 @@ -879,7 +885,7 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, siz= e_t auxv_size, { Elf32_Phdr p32[phnum]; Elf64_Phdr p64[phnum]; - } *u =3D (void *) &buf; + } *u =3D (void *) buf; if (elfclass =3D=3D ELFCLASS32) { for (size_t i =3D 0; i < phnum; ++i) @@ -900,6 +906,7 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, siz= e_t auxv_size, = (*memory_callback) (dwfl, -1, &in.d_buf, &in.d_size, 0, 0, memory_callback_arg); + free (buf); } else /* We could not read the executable's phdrs from the @@ -943,18 +950,24 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, s= ize_t auxv_size, if ((*memory_callback) (dwfl, dyn_segndx, &in.d_buf, &in.d_size, dyn_vaddr, dyn_filesz, memory_callback_arg)) { - union + typedef union { Elf32_Dyn d32; Elf64_Dyn d64; char data[dyn_filesz]; - } buf; + } data_buf; + data_buf *buf =3D malloc (sizeof (data_buf)); + if (buf =3D=3D NULL) + { + __libdwfl_seterrno (DWFL_E_NOMEM); + return false; + } Elf_Data out =3D { .d_type =3D ELF_T_DYN, .d_version =3D EV_CURRENT, .d_size =3D dyn_filesz, - .d_buf =3D &buf + .d_buf =3D buf }; in.d_size =3D out.d_size; if (likely ((elfclass =3D=3D ELFCLASS32 @@ -966,7 +979,7 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, siz= e_t auxv_size, { Elf32_Dyn d32[dyn_filesz / sizeof (Elf32_Dyn)]; Elf64_Dyn d64[dyn_filesz / sizeof (Elf64_Dyn)]; - } *u =3D (void *) &buf; + } *u =3D (void *) buf; if (elfclass =3D=3D ELFCLASS32) { size_t n =3D dyn_filesz / sizeof (Elf32_Dyn); @@ -991,6 +1004,7 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, si= ze_t auxv_size, = (*memory_callback) (dwfl, -1, &in.d_buf, &in.d_size, 0, 0, memory_callback_arg); + free (buf); } } } -- = 1.8.3.1 --===============5588696523258132052==--