This is the mail archive of the libc-hacker@sourceware.org mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Find .note.ABI-tag notes even when multiple notes are in one PT_NOTE segment


Hi!

All other places in glibc that handle notes already handle multiple
notes in PT_NOTE segment (which ELF gABI allows), just .note.ABI-tag
code in ld.so and ldconfig insisted it is the only note present.

2007-06-29  Jakub Jelinek  <jakub@redhat.com>

	* elf/dl-load.c (open_verify): Find .note.ABI-tag notes even
	in PT_NOTE segments with multiple notes.
	* elf/readelflib.c (process_elf_file): Likewise.

--- libc/elf/dl-load.c.jj	2007-06-29 10:19:54.000000000 +0200
+++ libc/elf/dl-load.c	2007-06-29 10:45:28.000000000 +0200
@@ -1634,7 +1634,7 @@ open_verify (const char *name, struct fi
     {
       ElfW(Ehdr) *ehdr;
       ElfW(Phdr) *phdr, *ph;
-      ElfW(Word) *abi_note, abi_note_buf[8];
+      ElfW(Word) *abi_note;
       unsigned int osversion;
       size_t maplength;
 
@@ -1751,20 +1751,37 @@ open_verify (const char *name, struct fi
 
       /* Check .note.ABI-tag if present.  */
       for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
-	if (ph->p_type == PT_NOTE && ph->p_filesz == 32 && ph->p_align >= 4)
+	if (ph->p_type == PT_NOTE && ph->p_filesz >= 32 && ph->p_align >= 4)
 	  {
-	    if (ph->p_offset + 32 <= (size_t) fbp->len)
+	    ElfW(Addr) size = ph->p_filesz;
+
+	    if (ph->p_offset + size <= (size_t) fbp->len)
 	      abi_note = (void *) (fbp->buf + ph->p_offset);
 	    else
 	      {
+		abi_note = alloca (size);
 		__lseek (fd, ph->p_offset, SEEK_SET);
-		if (__libc_read (fd, (void *) abi_note_buf, 32) != 32)
+		if (__libc_read (fd, (void *) abi_note, size) != size)
 		  goto read_error;
+	      }
 
-		abi_note = abi_note_buf;
+	    while (memcmp (abi_note, &expected_note, sizeof (expected_note)))
+	      {
+#define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word)))
+		ElfW(Addr) note_size = 3 * sizeof (ElfW(Word))
+				       + ROUND (abi_note[0])
+				       + ROUND (abi_note[1]);
+
+		if (size - 32 < note_size)
+		  {
+		    size = 0;
+		    break;
+		  }
+		size -= note_size;
+		abi_note = (void *) abi_note + note_size;
 	      }
 
-	    if (memcmp (abi_note, &expected_note, sizeof (expected_note)))
+	    if (size == 0)
 	      continue;
 
 	    osversion = (abi_note[5] & 0xff) * 65536
--- libc/elf/readelflib.c.jj	2005-12-14 11:05:56.000000000 +0100
+++ libc/elf/readelflib.c	2007-06-29 10:46:00.000000000 +0200
@@ -1,4 +1,4 @@
-/* Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+/* Copyright (C) 1999, 2000, 2001, 2002, 2007 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Andreas Jaeger <aj@suse.de>, 1999 and
 		  Jakub Jelinek <jakub@redhat.com>, 1999.
@@ -127,16 +127,37 @@ process_elf_file (const char *file_name,
 	  break;
 
 	case PT_NOTE:
-	  if (!*osversion && segment->p_filesz == 32 && segment->p_align >= 4)
+	  if (!*osversion && segment->p_filesz >= 32 && segment->p_align >= 4)
 	    {
 	      ElfW(Word) *abi_note = (ElfW(Word) *) (file_contents
 						     + segment->p_offset);
-	      if (abi_note [0] == 4 && abi_note [1] == 16 && abi_note [2] == 1
-		  && memcmp (abi_note + 3, "GNU", 4) == 0)
-		*osversion = (abi_note [4] << 24) |
-			     ((abi_note [5] & 0xff) << 16) |
-			     ((abi_note [6] & 0xff) << 8) |
-			     (abi_note [7] & 0xff);
+	      ElfW(Addr) size = segment->p_filesz;
+
+	      while (abi_note [0] != 4 || abi_note [1] != 16
+		     || abi_note [2] != 1
+		     || memcmp (abi_note + 3, "GNU", 4) != 0)
+		{
+#define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word)))
+		  ElfW(Addr) note_size = 3 * sizeof (ElfW(Word))
+					 + ROUND (abi_note[0])
+					 + ROUND (abi_note[1]);
+
+		  if (size - 32 < note_size || note_size == 0)
+		    {
+		      size = 0;
+		      break;
+		    }
+		  size -= note_size;
+		  abi_note = (void *) abi_note + note_size;
+		}
+
+	      if (size == 0)
+		break;
+
+	      *osversion = (abi_note [4] << 24) |
+			   ((abi_note [5] & 0xff) << 16) |
+			   ((abi_note [6] & 0xff) << 8) |
+			   (abi_note [7] & 0xff);
 	    }
 	  break;
 

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]