public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [PATCH 10/10] libdwfl: Document core memory and remote memory ELF shdrs reading.
Date: Thu, 13 Sep 2018 23:02:00 -0000	[thread overview]
Message-ID: <1536879700-30439-11-git-send-email-mark@klomp.org> (raw)
In-Reply-To: <1536879700-30439-1-git-send-email-mark@klomp.org>

There are two places, dwfl_segment_report_module and elf_from_remote_memory
in libdwfl where we use the Ehdr e_shnum directly. Document why this is fine.
Getting the shdrs in those two places is really just a nice bonus and if there
are more than 0xff00 then it is unlikely we will get them all anyway.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libdwfl/ChangeLog                    | 6 ++++++
 libdwfl/dwfl_segment_report_module.c | 6 ++++++
 libdwfl/elf-from-memory.c            | 6 ++++++
 3 files changed, 18 insertions(+)

diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 15d7511..5e9b986 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,9 @@
+2018-09-13  Mark Wielaard  <mark@klomp.org>
+
+	* dwfl_segment_report_module.c (dwfl_segment_report_module):
+	Document why we use e_shnum directly.
+	* elf-from-memory.c (elf_from_remote_memory): Likewise.
+
 2018-07-17  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* linux-pid-attach.c: Include sys/uio.h only on linux.
diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c
index 207a257..36e5c82 100644
--- a/libdwfl/dwfl_segment_report_module.c
+++ b/libdwfl/dwfl_segment_report_module.c
@@ -367,6 +367,11 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
       phentsize = ehdr.e32.e_phentsize;
       if (phentsize != sizeof (Elf32_Phdr))
 	return finish ();
+      /* NOTE if the number of sections is > 0xff00 then e_shnum
+	 is zero and the actual number would come from the section
+	 zero sh_size field. We ignore this here because getting shdrs
+	 is just a nice bonus (see below in consider_phdr PT_LOAD
+	 where we trim the last segment).  */
       shdrs_end = ehdr.e32.e_shoff + ehdr.e32.e_shnum * ehdr.e32.e_shentsize;
       break;
 
@@ -380,6 +385,7 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
       phentsize = ehdr.e64.e_phentsize;
       if (phentsize != sizeof (Elf64_Phdr))
 	return finish ();
+      /* See the NOTE above for shdrs_end and ehdr.e32.e_shnum.  */
       shdrs_end = ehdr.e64.e_shoff + ehdr.e64.e_shnum * ehdr.e64.e_shentsize;
       break;
 
diff --git a/libdwfl/elf-from-memory.c b/libdwfl/elf-from-memory.c
index 12a0a1b..c54c1b9 100644
--- a/libdwfl/elf-from-memory.c
+++ b/libdwfl/elf-from-memory.c
@@ -139,6 +139,11 @@ elf_from_remote_memory (GElf_Addr ehdr_vma,
       phentsize = ehdr.e32.e_phentsize;
       if (phentsize != sizeof (Elf32_Phdr) || phnum == 0)
 	goto bad_elf;
+      /* NOTE if the number of sections is > 0xff00 then e_shnum
+	 is zero and the actual number would come from the section
+	 zero sh_size field. We ignore this here because getting shdrs
+	 is just a nice bonus (see below where we trim the last phdrs
+	 PT_LOAD segment).  */
       shdrs_end = ehdr.e32.e_shoff + ehdr.e32.e_shnum * ehdr.e32.e_shentsize;
       break;
 
@@ -151,6 +156,7 @@ elf_from_remote_memory (GElf_Addr ehdr_vma,
       phentsize = ehdr.e64.e_phentsize;
       if (phentsize != sizeof (Elf64_Phdr) || phnum == 0)
 	goto bad_elf;
+      /* See the NOTE above for shdrs_end and ehdr.e32.e_shnum.  */
       shdrs_end = ehdr.e64.e_shoff + ehdr.e64.e_shnum * ehdr.e64.e_shentsize;
       break;
 
-- 
1.8.3.1

  parent reply	other threads:[~2018-09-13 23:02 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-13 23:01 Fix various issues with ELF files containing many sections Mark Wielaard
2018-09-13 23:02 ` [PATCH 04/10] elfcmp: Get, check and shdrstrndx for section names Mark Wielaard
2018-09-13 23:02 ` [PATCH 01/10] backends: Always use elf_getshdrstrndx in check_special_symbol Mark Wielaard
2018-09-13 23:02 ` [PATCH 07/10] libdw: dwarf_begin_elf should use elf_getshdrstrndx to get section names Mark Wielaard
2018-09-13 23:02 ` [PATCH 09/10] readelf: Use elf_getshdrnum in print_shdr and print_phdr Mark Wielaard
2018-09-13 23:02 ` [PATCH 08/10] strip,unstrip: Use and set shdrstrndx consistently Mark Wielaard
2018-09-13 23:02 ` Mark Wielaard [this message]
2018-09-13 23:02 ` [PATCH 05/10] libelf: Fix shnum and section zero handling Mark Wielaard
2018-09-13 23:02 ` [PATCH 03/10] libebl: Use elf_getshdrstrndx in ebl_section_strip_p Mark Wielaard
2018-09-13 23:02 ` [PATCH 06/10] elflint: Use shnum and shstrndx instead of ehdr field directly Mark Wielaard
2018-09-13 23:02 ` [PATCH 02/10] backends: Use elf_getshdrstrndx to find .odp section in ppc64_init Mark Wielaard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1536879700-30439-11-git-send-email-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).