7c0489
commit 58e8f5fd2ba47b6dc47fd4d0a35e4175c7c87aaa
7c0489
Author: Andreas Schwab <schwab@suse.de>
7c0489
Date:   Wed Oct 9 17:46:47 2019 +0200
7c0489
7c0489
    ldconfig: handle .dynstr located in separate segment (bug 25087)
7c0489
    
7c0489
    To determine the load offset of the DT_STRTAB section search for the
7c0489
    segment containing it, instead of using the load offset of the first
7c0489
    segment.
7c0489
7c0489
diff --git a/elf/readelflib.c b/elf/readelflib.c
7c0489
index 5a1e2dc2dfa36599..8774e779f5abbfbb 100644
7c0489
--- a/elf/readelflib.c
7c0489
+++ b/elf/readelflib.c
7c0489
@@ -45,7 +45,6 @@ process_elf_file (const char *file_name, const char *lib, int *flag,
7c0489
 {
7c0489
   int i;
7c0489
   unsigned int j;
7c0489
-  ElfW(Addr) loadaddr;
7c0489
   unsigned int dynamic_addr;
7c0489
   size_t dynamic_size;
7c0489
   char *program_interpreter;
7c0489
@@ -87,7 +86,6 @@ process_elf_file (const char *file_name, const char *lib, int *flag,
7c0489
      libc5/libc6.  */
7c0489
   *flag = FLAG_ELF;
7c0489
 
7c0489
-  loadaddr = -1;
7c0489
   dynamic_addr = 0;
7c0489
   dynamic_size = 0;
7c0489
   program_interpreter = NULL;
7c0489
@@ -98,11 +96,6 @@ process_elf_file (const char *file_name, const char *lib, int *flag,
7c0489
 
7c0489
       switch (segment->p_type)
7c0489
 	{
7c0489
-	case PT_LOAD:
7c0489
-	  if (loadaddr == (ElfW(Addr)) -1)
7c0489
-	    loadaddr = segment->p_vaddr - segment->p_offset;
7c0489
-	  break;
7c0489
-
7c0489
 	case PT_DYNAMIC:
7c0489
 	  if (dynamic_addr)
7c0489
 	    error (0, 0, _("more than one dynamic segment\n"));
7c0489
@@ -176,11 +169,6 @@ process_elf_file (const char *file_name, const char *lib, int *flag,
7c0489
 	}
7c0489
 
7c0489
     }
7c0489
-  if (loadaddr == (ElfW(Addr)) -1)
7c0489
-    {
7c0489
-      /* Very strange. */
7c0489
-      loadaddr = 0;
7c0489
-    }
7c0489
 
7c0489
   /* Now we can read the dynamic sections.  */
7c0489
   if (dynamic_size == 0)
7c0489
@@ -197,7 +185,29 @@ process_elf_file (const char *file_name, const char *lib, int *flag,
7c0489
       check_ptr (dyn_entry);
7c0489
       if (dyn_entry->d_tag == DT_STRTAB)
7c0489
 	{
7c0489
-	  dynamic_strings = (char *) (file_contents + dyn_entry->d_un.d_val - loadaddr);
7c0489
+	  /* Find the file offset of the segment containing the dynamic
7c0489
+	     string table.  */
7c0489
+	  ElfW(Off) loadoff = -1;
7c0489
+	  for (i = 0, segment = elf_pheader;
7c0489
+	       i < elf_header->e_phnum; i++, segment++)
7c0489
+	    {
7c0489
+	      if (segment->p_type == PT_LOAD
7c0489
+		  && dyn_entry->d_un.d_val >= segment->p_vaddr
7c0489
+		  && (dyn_entry->d_un.d_val - segment->p_vaddr
7c0489
+		      < segment->p_filesz))
7c0489
+		{
7c0489
+		  loadoff = segment->p_vaddr - segment->p_offset;
7c0489
+		  break;
7c0489
+		}
7c0489
+	    }
7c0489
+	  if (loadoff == (ElfW(Off)) -1)
7c0489
+	    {
7c0489
+	      /* Very strange. */
7c0489
+	      loadoff = 0;
7c0489
+	    }
7c0489
+
7c0489
+	  dynamic_strings = (char *) (file_contents + dyn_entry->d_un.d_val
7c0489
+				      - loadoff);
7c0489
 	  check_ptr (dynamic_strings);
7c0489
 	  break;
7c0489
 	}