public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] elflint: Add gnuld check when a NOBITS section falls inside a segment.
@ 2015-08-11 22:16 Mark Wielaard
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Wielaard @ 2015-08-11 22:16 UTC (permalink / raw)
  To: elfutils-devel

[-- Attachment #1: Type: text/plain, Size: 2715 bytes --]

gnuld has a really bad bug where it can place a NOBITS section inside
a PT_LOAD segment. Normally that would not work. But it also makes sure
that the contents of the file is all zeros. So in practice it is actually
a PROGBITS section with all zero data. Except that other tools will think
there is an unused gap in the ELF file after the NOBITS section.

Recognize and check this pattern in elflint when --gnu is given.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
---
 src/ChangeLog |  5 +++++
 src/elflint.c | 34 ++++++++++++++++++++++++++++++++--
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index a0c32fa..5be1075 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2015-08-11  Mark Wielaard  <mjw@redhat.com>
+
+	* elflint.c (check_sections): When gnuld and a NOBITS section falls
+	inside a segment make sure any ELF file contents is zero.
+
 2015-07-29  Mark Wielaard  <mjw@redhat.com>
 
 	* unstrip.c (sections_flags_match): New function.
diff --git a/src/elflint.c b/src/elflint.c
index a916886..0d5f34d 100644
--- a/src/elflint.c
+++ b/src/elflint.c
@@ -3978,9 +3978,39 @@ section [%2zu] '%s' not fully contained in segment of program header entry %d\n"
 		  {
 		    if (shdr->sh_offset < phdr->p_offset + phdr->p_filesz
 			&& !is_debuginfo)
-		      ERROR (gettext ("\
+		      {
+			if (!gnuld)
+			  ERROR (gettext ("\
 section [%2zu] '%s' has type NOBITS but is read from the file in segment of program header entry %d\n"),
-			 cnt, section_name (ebl, cnt), pcnt);
+				 cnt, section_name (ebl, cnt), pcnt);
+			else
+			  {
+			    /* This is truly horrible. GNU ld might put a
+			       NOBITS section in the middle of a PT_LOAD
+			       segment, assuming the next gap in the file
+			       actually consists of zero bits...
+			       So it really is like a PROGBITS section
+			       where the data is all zeros.  Check those
+			       zero bytes are really there.  */
+			    bool bad;
+			    Elf_Data *databits;
+			    databits = elf_getdata_rawchunk (ebl->elf,
+							     shdr->sh_offset,
+							     shdr->sh_size,
+							     ELF_T_BYTE);
+			    bad = (databits == NULL
+				   || databits->d_size != shdr->sh_size);
+			    for (size_t idx = 0;
+				 idx < databits->d_size && ! bad;
+				 idx++)
+			      bad = ((char *) databits->d_buf)[idx] != 0;
+
+			    if (bad)
+			      ERROR (gettext ("\
+section [%2zu] '%s' has type NOBITS but is read from the file in segment of program header entry %d and file contents is non-zero\n"),
+				     cnt, section_name (ebl, cnt), pcnt);
+			  }
+		      }
 		  }
 		else
 		  {
-- 
2.4.3


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] elflint: Add gnuld check when a NOBITS section falls inside a segment.
@ 2015-08-17 13:36 Mark Wielaard
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Wielaard @ 2015-08-17 13:36 UTC (permalink / raw)
  To: elfutils-devel

[-- Attachment #1: Type: text/plain, Size: 583 bytes --]

On Wed, 2015-08-12 at 00:16 +0200, Mark Wielaard wrote:
> gnuld has a really bad bug where it can place a NOBITS section inside
> a PT_LOAD segment. Normally that would not work. But it also makes sure
> that the contents of the file is all zeros. So in practice it is actually
> a PROGBITS section with all zero data. Except that other tools will think
> there is an unused gap in the ELF file after the NOBITS section.
> 
> Recognize and check this pattern in elflint when --gnu is given.

Updated https://fedorahosted.org/elfutils/wiki/ElflintGNU and pushed to
master.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-08-17 13:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-11 22:16 [PATCH] elflint: Add gnuld check when a NOBITS section falls inside a segment Mark Wielaard
2015-08-17 13:36 Mark Wielaard

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).