7068422
2007-07-24  Jakub Jelinek  <jakub@redhat.com>
7068422
7068422
	* elf.c (get_program_header_size): Adjacent loadable .note*
7068422
	sections need just one PT_NOTE segment.
7068422
	(_bfd_elf_map_sections_to_segments): Likewise.
7068422
7068422
--- bfd/elf.c	18 Jul 2007 11:23:37 -0000	1.399
7068422
+++ bfd/elf.c	24 Jul 2007 08:24:11 -0000
7068422
@@ -3696,6 +3696,19 @@ get_program_header_size (bfd *abfd, stru
7068422
 	{
7068422
 	  /* We need a PT_NOTE segment.  */
7068422
 	  ++segs;
7068422
+	  /* Try to create just one PT_NOTE segment
7068422
+	     for all adjacent loadable .note* sections.
7068422
+	     gABI requires that within a PT_NOTE segment
7068422
+	     (and also inside of each SHT_NOTE section)
7068422
+	     each note is padded to a multiple of 4 size,
7068422
+	     so we check whether the sections are correctly
7068422
+	     aligned.  */
7068422
+	  if (s->alignment_power == 2)
7068422
+	    while (s->next != NULL
7068422
+		   && s->next->alignment_power == 2
7068422
+		   && (s->next->flags & SEC_LOAD) != 0
7068422
+		   && CONST_STRNEQ (s->next->name, ".note"))
7068422
+	      s = s->next;
7068422
 	}
7068422
     }
7068422
 
7068422
@@ -4070,25 +4083,44 @@ _bfd_elf_map_sections_to_segments (bfd *
7068422
 	  pm = &m->next;
7068422
 	}
7068422
 
7068422
-      /* For each loadable .note section, add a PT_NOTE segment.  We don't
7068422
-	 use bfd_get_section_by_name, because if we link together
7068422
-	 nonloadable .note sections and loadable .note sections, we will
7068422
-	 generate two .note sections in the output file.  FIXME: Using
7068422
-	 names for section types is bogus anyhow.  */
7068422
+      /* For each batch of consecutive loadable .note sections,
7068422
+	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
7068422
+	 because if we link together nonloadable .note sections and
7068422
+	 loadable .note sections, we will generate two .note sections
7068422
+	 in the output file.  FIXME: Using names for section types is
7068422
+	 bogus anyhow.  */
7068422
       for (s = abfd->sections; s != NULL; s = s->next)
7068422
 	{
7068422
 	  if ((s->flags & SEC_LOAD) != 0
7068422
 	      && CONST_STRNEQ (s->name, ".note"))
7068422
 	    {
7068422
+	      asection *s2;
7068422
+	      unsigned count = 1;
7068422
 	      amt = sizeof (struct elf_segment_map);
7068422
+	      if (s->alignment_power == 2)
7068422
+		for (s2 = s; s2->next != NULL; s2 = s2->next)
7068422
+		  if (s2->next->alignment_power == 2
7068422
+		      && (s2->next->flags & SEC_LOAD) != 0
7068422
+		      && CONST_STRNEQ (s2->next->name, ".note")
7068422
+		      && align_power (s2->vma + s2->size, 2) == s2->next->vma)
7068422
+		    count++;
7068422
+		  else
7068422
+		    break;
7068422
+	      amt += (count - 1) * sizeof (asection *);
7068422
 	      m = bfd_zalloc (abfd, amt);
7068422
 	      if (m == NULL)
7068422
 		goto error_return;
7068422
 	      m->next = NULL;
7068422
 	      m->p_type = PT_NOTE;
7068422
-	      m->count = 1;
7068422
-	      m->sections[0] = s;
7068422
-
7068422
+	      m->count = count;
7068422
+	      while (count > 1)
7068422
+		{
7068422
+		  m->sections[m->count - count--] = s;
7068422
+		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
7068422
+		  s = s->next;
7068422
+		}
7068422
+	      m->sections[m->count - 1] = s;
7068422
+	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
7068422
 	      *pm = m;
7068422
 	      pm = &m->next;
7068422
 	    }