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