Skip to contentSkip to conversation

Support copy relocations in .data.rel.ro

PATCH In review Kyle Russell 2 participants 5 messages Sep 28, 2018 – Oct 12, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Support copy relocations in .data.rel.ro

binutils-2.28 (17026142ef35b62ac88bfe517b4160614902cb28) adds support for copying read-only dynamic symbols into .data.rel.ro instead of .bss since .bss is technically writable. This causes prelink to error out on any binary containing COPY relocations in .data.rel.ro.

Read-only variables defined in shared libraries should be copied directly into the space allocated for them in .data.rel.ro by the linker.

To achieve this, we determine whether either of the two sections containing copy relocations is .data.rel.ro. If so, we relocate the symbol memory directly into the existing section instead of constructing a new .(s)dynbss section once prelink_build_conflicts() returns.

Fixes cxx1.sh, cxx2.sh, and cxx3.sh on Fedora 28 (which uses binutils-2.29).

---
 src/conflict.c | 51 +++++++++++++++++++++++++++++++++++++-------------
 src/undo.c     |  9 +++++++++
 2 files changed, 47 insertions(+), 13 deletions(-)
src/conflict.c +38 -13
index 9ae2ddb..5613ace 100644
@@ -450,7 +450,7 @@ get_relocated_mem (struct prelink_info *info, DSO *dso, GElf_Addr addr,
 int
 prelink_build_conflicts (struct prelink_info *info)
 {
-  int i, ndeps = info->ent->ndepends + 1;
+  int i, reset_dynbss = 0, reset_sdynbss = 0, ndeps = info->ent->ndepends + 1;
   struct prelink_entry *ent;
   int ret = 0;
   DSO *dso;
@@ -675,6 +675,11 @@ prelink_build_conflicts (struct prelink_info *info)
 		     dso->filename);
 	      goto error_out;
 	    }
+
+	  name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[bss1].sh_name);
+	  if (strcmp(name, ".data.rel.ro") == 0)
+	    reset_sdynbss = 1;
+
 	  firstbss2 = i;
 	  info->sdynbss_size = cr.rela[i - 1].r_offset - cr.rela[0].r_offset;
 	  info->sdynbss_size += cr.rela[i - 1].r_addend;
@@ -702,6 +707,10 @@ prelink_build_conflicts (struct prelink_info *info)
 	    }
 	}
 
+      name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[bss2].sh_name);
+      if (strcmp(name, ".data.rel.ro") == 0)
+        reset_dynbss = 1;
+
       info->dynbss_size = cr.rela[cr.count - 1].r_offset
 			  - cr.rela[firstbss2].r_offset;
       info->dynbss_size += cr.rela[cr.count - 1].r_addend;
@@ -719,9 +728,9 @@ prelink_build_conflicts (struct prelink_info *info)
 	  && strcmp (name = strptr (dso, dso->ehdr.e_shstrndx,
 				    dso->shdr[bss1].sh_name),
 		     ".dynbss") != 0
-	  && strcmp (name, ".sdynbss") != 0)
+	  && strcmp (name, ".sdynbss") != 0 && strcmp (name, ".data.rel.ro") != 0)
 	{
-	  error (0, 0, "%s: COPY relocations don't point into .bss or .sbss section",
+	  error (0, 0, "%s: COPY relocations don't point into .bss, .sbss, or .data.rel.ro sections",
 		 dso->filename);
 	  goto error_out;
 	}
@@ -730,9 +739,9 @@ prelink_build_conflicts (struct prelink_info *info)
 	  && strcmp (name = strptr (dso, dso->ehdr.e_shstrndx,
 				    dso->shdr[bss2].sh_name),
 		     ".dynbss") != 0
-	  && strcmp (name, ".sdynbss") != 0)
+	  && strcmp (name, ".sdynbss") != 0 && strcmp (name, ".data.rel.ro") != 0)
 	{
-	  error (0, 0, "%s: COPY relocations don't point into .bss or .sbss section",
+	  error (0, 0, "%s: COPY relocations don't point into .bss, .sbss, or .data.rel.ro section",
 		 dso->filename);
 	  goto error_out;
 	}
@@ -768,16 +777,21 @@ prelink_build_conflicts (struct prelink_info *info)
 	      }
 
 	  assert (j < ndeps);
+	  GElf_Addr symaddr = s->u.ent->base + s->value;
+	  char *buf;
+
 	  if (i < firstbss2)
-	    j = get_relocated_mem (info, ndso, s->u.ent->base + s->value,
-				   info->sdynbss + cr.rela[i].r_offset
-				   - info->sdynbss_base, cr.rela[i].r_addend,
-				   cr.rela[i].r_offset);
+	    if (reset_sdynbss)
+	      buf = get_data(dso, cr.rela[i].r_offset, NULL, NULL);
+	    else
+	      buf = info->sdynbss + cr.rela[i].r_offset - info->sdynbss_base;
 	  else
-	    j = get_relocated_mem (info, ndso, s->u.ent->base + s->value,
-				   info->dynbss + cr.rela[i].r_offset
-				   - info->dynbss_base, cr.rela[i].r_addend,
-				   cr.rela[i].r_offset);
+	    if (reset_dynbss)
+	      buf = get_data(dso, cr.rela[i].r_offset, NULL, NULL);
+	    else
+	      buf = info->dynbss + cr.rela[i].r_offset - info->dynbss_base;
+
+	  j = get_relocated_mem (info, ndso, symaddr, buf, cr.rela[i].r_addend, cr.rela[i].r_offset);
 
 	  switch (j)
 	    {
@@ -815,6 +829,17 @@ prelink_build_conflicts (struct prelink_info *info)
     if (info->dsos[i])
       close_dso (info->dsos[i]);
 
+  if (reset_sdynbss)
+    {
+      free (info->sdynbss);
+      info->sdynbss = NULL;
+    }
+
+  if (reset_dynbss)
+    {
+      free (info->dynbss);
+      info->dynbss = NULL;
+    }
   info->dsos = NULL;
   free (cr.rela);
   return ret;
src/undo.c +9 -0
index 8a55bf2..4c38dab 100644
@@ -633,6 +633,15 @@ prelink_undo (DSO *dso)
 	      d->d_buf = NULL;
 	      dso->shdr[i].sh_type = SHT_NOBITS;
 	    }
+	  else if (dso->shdr[i].sh_type == SHT_PROGBITS
+		   && strcmp (name, ".data.rel.ro") == 0)
+	    {
+	      scn = dso->scn[i];
+	      d = elf_getdata (scn, NULL);
+	      assert (d != NULL && elf_getdata (scn, d) == NULL);
+	      assert (d->d_size == dso->shdr[i].sh_size);
+	      assert (memset(d->d_buf, 0, d->d_size) == d->d_buf);
+	    }
 	  else if (dso->shdr[i].sh_type == SHT_RELA
 		   && shdr[i].sh_type == SHT_REL)
 	    {
Signature · 1 line
2.17.1
#1 lore raw

Hey Mark,

Do you think this approach is reasonable? If so, I have another patch I'd like to propose that would enable us to better catch error scenarios (like the last two patches address) that we might encounter during do_image_prelink. We just happened to detect these last two issues even though image_prelink didn't fail the build, so I'd like to provide an option to enable a little more assertive error path, if desired.

Thanks,
Kyle

Quoted 166 lines · Kyle Russell

binutils-2.28 (17026142ef35b62ac88bfe517b4160614902cb28) adds support for copying read-only dynamic symbols into .data.rel.ro instead of .bss since .bss is technically writable. This causes prelink to error out on any binary containing COPY relocations in .data.rel.ro.

Read-only variables defined in shared libraries should be copied directly into the space allocated for them in .data.rel.ro by the linker.

To achieve this, we determine whether either of the two sections containing copy relocations is .data.rel.ro. If so, we relocate the symbol memory directly into the existing section instead of constructing a new .(s)dynbss section once prelink_build_conflicts() returns.

Fixes cxx1.sh, cxx2.sh, and cxx3.sh on Fedora 28 (which uses binutils-2.29).

---
 src/conflict.c | 51 +++++++++++++++++++++++++++++++++++++-------------
 src/undo.c     |  9 +++++++++
 2 files changed, 47 insertions(+), 13 deletions(-)
src/conflict.c +7 -1
index 9ae2ddb..5613ace 100644
@@ -450,7 +450,7 @@ get_relocated_mem (struct prelink_info *info, DSO
 *dso, GElf_Addr addr,
 int
 prelink_build_conflicts (struct prelink_info *info)
 {
-  int i, ndeps = info->ent->ndepends + 1;
+  int i, reset_dynbss = 0, reset_sdynbss = 0, ndeps = info->ent->ndepends
+ 1;
   struct prelink_entry *ent;
   int ret = 0;
   DSO *dso;
@@ -675,6 +675,11 @@ prelink_build_conflicts (struct prelink_info *info)
                     dso->filename);
              goto error_out;
            }
+
+         name = strptr (dso, dso->ehdr.e_shstrndx,
 dso->shdr[bss1].sh_name);
+         if (strcmp(name, ".data.rel.ro") == 0)
+           reset_sdynbss = 1;
+
          firstbss2 = i;
          info->sdynbss_size = cr.rela[i - 1].r_offset -
cr.rela[0].r_offset;
          info->sdynbss_size += cr.rela[i - 1].r_addend;
src/conflict.c +6 -2
@@ -702,6 +707,10 @@ prelink_build_conflicts (struct prelink_info *info)
            }
        }
 
+      name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[bss2].sh_name);
+      if (strcmp(name, ".data.rel.ro") == 0)
+        reset_dynbss = 1;
+
       info->dynbss_size = cr.rela[cr.count - 1].r_offset
                          - cr.rela[firstbss2].r_offset;
       info->dynbss_size += cr.rela[cr.count - 1].r_addend;
@@ -719,9 +728,9 @@ prelink_build_conflicts (struct prelink_info *info)
          && strcmp (name = strptr (dso, dso->ehdr.e_shstrndx,
                                    dso->shdr[bss1].sh_name),
                     ".dynbss") != 0
-         && strcmp (name, ".sdynbss") != 0)
+         && strcmp (name, ".sdynbss") != 0 && strcmp (name, ".data.rel.ro")
 != 0)
        {
-         error (0, 0, "%s: COPY relocations don't point into .bss or
 .sbss section",
+         error (0, 0, "%s: COPY relocations don't point into .bss, .sbss,
 or .data.rel.ro sections",
                 dso->filename);
          goto error_out;
        }
src/conflict.c +2 -2
@@ -730,9 +739,9 @@ prelink_build_conflicts (struct prelink_info *info)
          && strcmp (name = strptr (dso, dso->ehdr.e_shstrndx,
                                    dso->shdr[bss2].sh_name),
                     ".dynbss") != 0
-         && strcmp (name, ".sdynbss") != 0)
+         && strcmp (name, ".sdynbss") != 0 && strcmp (name, ".data.rel.ro")
 != 0)
        {
-         error (0, 0, "%s: COPY relocations don't point into .bss or
 .sbss section",
+         error (0, 0, "%s: COPY relocations don't point into .bss, .sbss,
 or .data.rel.ro section",
                 dso->filename);
          goto error_out;
        }
src/conflict.c +13 -8
@@ -768,16 +777,21 @@ prelink_build_conflicts (struct prelink_info *info)
              }
 
          assert (j < ndeps);
+         GElf_Addr symaddr = s->u.ent->base + s->value;
+         char *buf;
+
          if (i < firstbss2)
-           j = get_relocated_mem (info, ndso, s->u.ent->base + s->value,
-                                  info->sdynbss + cr.rela[i].r_offset
-                                  - info->sdynbss_base,
 cr.rela[i].r_addend,
-                                  cr.rela[i].r_offset);
+           if (reset_sdynbss)
+             buf = get_data(dso, cr.rela[i].r_offset, NULL, NULL);
+           else
+             buf = info->sdynbss + cr.rela[i].r_offset -
 info->sdynbss_base;
          else
-           j = get_relocated_mem (info, ndso, s->u.ent->base + s->value,
-                                  info->dynbss + cr.rela[i].r_offset
-                                  - info->dynbss_base,
 cr.rela[i].r_addend,
-                                  cr.rela[i].r_offset);
+           if (reset_dynbss)
+             buf = get_data(dso, cr.rela[i].r_offset, NULL, NULL);
+           else
+             buf = info->dynbss + cr.rela[i].r_offset - info->dynbss_base;
+
+         j = get_relocated_mem (info, ndso, symaddr, buf,

cr.rela[i].r_addend, cr.rela[i].r_offset);

          switch (j)
            {
src/conflict.c +11 -0
@@ -815,6 +829,17 @@ prelink_build_conflicts (struct prelink_info *info)
     if (info->dsos[i])
       close_dso (info->dsos[i]);
 
+  if (reset_sdynbss)
+    {
+      free (info->sdynbss);
+      info->sdynbss = NULL;
+    }
+
+  if (reset_dynbss)
+    {
+      free (info->dynbss);
+      info->dynbss = NULL;
+    }
   info->dsos = NULL;
   free (cr.rela);
   return ret;
src/undo.c +9 -1
index 8a55bf2..4c38dab 100644
@@ -633,6 +633,15 @@ prelink_undo (DSO *dso)
              d->d_buf = NULL;
              dso->shdr[i].sh_type = SHT_NOBITS;
            }
+         else if (dso->shdr[i].sh_type == SHT_PROGBITS
+                  && strcmp (name, ".data.rel.ro") == 0)
+           {
+             scn = dso->scn[i];
+             d = elf_getdata (scn, NULL);
+             assert (d != NULL && elf_getdata (scn, d) == NULL);
+             assert (d->d_size == dso->shdr[i].sh_size);
+             assert (memset(d->d_buf, 0, d->d_size) == d->d_buf);
+           }
          else if (dso->shdr[i].sh_type == SHT_RELA
                   && shdr[i].sh_type == SHT_REL)
            {
--

2.17.1

#2 lore raw
Quoted 8 lines · Kyle Russell

Hey Mark,

Do you think this approach is reasonable?  If so, I have another patch I'd like to propose that would enable us to better catch error scenarios (like the last two patches address) that we might encounter during do_image_prelink.  We just happened to detect these last two issues even though image_prelink didn't fail the build, so I'd like to provide an option to enable a little more assertive error path, if desired.

I'm getting caught back up on my prelink 'TODO' set. The approach seems reasonable to me. However, I appear to have lost the reference to the original patch email.

I'm going to try to reapply from this email (or the list archive), but I may need you to resend it. I'll let you [and others know] once I get these things merged.

Thanks!
--Mark

Quoted 168 lines

Thanks,
Kyle

On Fri, Sep 28, 2018 at 10:57 AM Kyle Russell <[email protected] <mailto:[email protected]>> wrote:

    binutils-2.28 (17026142ef35b62ac88bfe517b4160614902cb28) adds support
    for copying read-only dynamic symbols into .data.rel.ro <http://data.rel.ro>
    instead of .bss
    since .bss is technically writable.  This causes prelink to error out on
    any binary containing COPY relocations in .data.rel.ro <http://data.rel.ro>.

    Read-only variables defined in shared libraries should be copied directly
    into the space allocated for them in .data.rel.ro <http://data.rel.ro> by
    the linker.

    To achieve this, we determine whether either of the two sections
    containing copy relocations is .data.rel.ro <http://data.rel.ro>.  If so, we
    relocate the
    symbol memory directly into the existing section instead of constructing
    a new .(s)dynbss section once prelink_build_conflicts() returns.

    Fixes cxx1.sh, cxx2.sh, and cxx3.sh on Fedora 28 (which uses
    binutils-2.29).
    ---
     src/conflict.c | 51 +++++++++++++++++++++++++++++++++++++-------------
     src/undo.c     |  9 +++++++++
     2 files changed, 47 insertions(+), 13 deletions(-)

    diff --git a/src/conflict.c b/src/conflict.c
    index 9ae2ddb..5613ace 100644
    --- a/src/conflict.c
    +++ b/src/conflict.c
    @@ -450,7 +450,7 @@ get_relocated_mem (struct prelink_info *info, DSO *dso,
    GElf_Addr addr,
     int
     prelink_build_conflicts (struct prelink_info *info)
     {
    -  int i, ndeps = info->ent->ndepends + 1;
    +  int i, reset_dynbss = 0, reset_sdynbss = 0, ndeps = info->ent->ndepends + 1;
       struct prelink_entry *ent;
       int ret = 0;
       DSO *dso;
    @@ -675,6 +675,11 @@ prelink_build_conflicts (struct prelink_info *info)
                         dso->filename);
                  goto error_out;
                }
    +
    +         name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[bss1].sh_name);
    +         if (strcmp(name, ".data.rel.ro <http://data.rel.ro>") == 0)
    +           reset_sdynbss = 1;
    +
              firstbss2 = i;
              info->sdynbss_size = cr.rela[i - 1].r_offset - cr.rela[0].r_offset;
              info->sdynbss_size += cr.rela[i - 1].r_addend;
    @@ -702,6 +707,10 @@ prelink_build_conflicts (struct prelink_info *info)
                }
            }

    +      name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[bss2].sh_name);
    +      if (strcmp(name, ".data.rel.ro <http://data.rel.ro>") == 0)
    +        reset_dynbss = 1;
    +
           info->dynbss_size = cr.rela[cr.count - 1].r_offset
                              - cr.rela[firstbss2].r_offset;
           info->dynbss_size += cr.rela[cr.count - 1].r_addend;
    @@ -719,9 +728,9 @@ prelink_build_conflicts (struct prelink_info *info)
              && strcmp (name = strptr (dso, dso->ehdr.e_shstrndx,
                                        dso->shdr[bss1].sh_name),
                         ".dynbss") != 0
    -         && strcmp (name, ".sdynbss") != 0)
    +         && strcmp (name, ".sdynbss") != 0 && strcmp (name, ".data.rel.ro
    <http://data.rel.ro>") != 0)
            {
    -         error (0, 0, "%s: COPY relocations don't point into .bss or .sbss
    section",
    +         error (0, 0, "%s: COPY relocations don't point into .bss, .sbss,
    or .data.rel.ro <http://data.rel.ro> sections",
                     dso->filename);
              goto error_out;
            }
    @@ -730,9 +739,9 @@ prelink_build_conflicts (struct prelink_info *info)
              && strcmp (name = strptr (dso, dso->ehdr.e_shstrndx,
                                        dso->shdr[bss2].sh_name),
                         ".dynbss") != 0
    -         && strcmp (name, ".sdynbss") != 0)
    +         && strcmp (name, ".sdynbss") != 0 && strcmp (name, ".data.rel.ro
    <http://data.rel.ro>") != 0)
            {
    -         error (0, 0, "%s: COPY relocations don't point into .bss or .sbss
    section",
    +         error (0, 0, "%s: COPY relocations don't point into .bss, .sbss,
    or .data.rel.ro <http://data.rel.ro> section",
                     dso->filename);
              goto error_out;
            }
    @@ -768,16 +777,21 @@ prelink_build_conflicts (struct prelink_info *info)
                  }

              assert (j < ndeps);
    +         GElf_Addr symaddr = s->u.ent->base + s->value;
    +         char *buf;
    +
              if (i < firstbss2)
    -           j = get_relocated_mem (info, ndso, s->u.ent->base + s->value,
    -                                  info->sdynbss + cr.rela[i].r_offset
    -                                  - info->sdynbss_base, cr.rela[i].r_addend,
    -                                  cr.rela[i].r_offset);
    +           if (reset_sdynbss)
    +             buf = get_data(dso, cr.rela[i].r_offset, NULL, NULL);
    +           else
    +             buf = info->sdynbss + cr.rela[i].r_offset - info->sdynbss_base;
              else
    -           j = get_relocated_mem (info, ndso, s->u.ent->base + s->value,
    -                                  info->dynbss + cr.rela[i].r_offset
    -                                  - info->dynbss_base, cr.rela[i].r_addend,
    -                                  cr.rela[i].r_offset);
    +           if (reset_dynbss)
    +             buf = get_data(dso, cr.rela[i].r_offset, NULL, NULL);
    +           else
    +             buf = info->dynbss + cr.rela[i].r_offset - info->dynbss_base;
    +
    +         j = get_relocated_mem (info, ndso, symaddr, buf,
    cr.rela[i].r_addend, cr.rela[i].r_offset);

              switch (j)
                {
    @@ -815,6 +829,17 @@ prelink_build_conflicts (struct prelink_info *info)
         if (info->dsos[i])
           close_dso (info->dsos[i]);

    +  if (reset_sdynbss)
    +    {
    +      free (info->sdynbss);
    +      info->sdynbss = NULL;
    +    }
    +
    +  if (reset_dynbss)
    +    {
    +      free (info->dynbss);
    +      info->dynbss = NULL;
    +    }
       info->dsos = NULL;
       free (cr.rela);
       return ret;
    diff --git a/src/undo.c b/src/undo.c
    index 8a55bf2..4c38dab 100644
    --- a/src/undo.c
    +++ b/src/undo.c
    @@ -633,6 +633,15 @@ prelink_undo (DSO *dso)
                  d->d_buf = NULL;
                  dso->shdr[i].sh_type = SHT_NOBITS;
                }
    +         else if (dso->shdr[i].sh_type == SHT_PROGBITS
    +                  && strcmp (name, ".data.rel.ro <http://data.rel.ro>") == 0)
    +           {
    +             scn = dso->scn[i];
    +             d = elf_getdata (scn, NULL);
    +             assert (d != NULL && elf_getdata (scn, d) == NULL);
    +             assert (d->d_size == dso->shdr[i].sh_size);
    +             assert (memset(d->d_buf, 0, d->d_size) == d->d_buf);
    +           }
              else if (dso->shdr[i].sh_type == SHT_RELA
                       && shdr[i].sh_type == SHT_REL)
                {
    -- 
    2.17.1
#3 lore raw

Do you want me to just resent my last two patches? I don't mind if that would be easier, and I'll remember to add the Signed-off-by. :)

Quoted 233 lines · Mark Hatle
Quoted 3 lines · Kyle Russell

Hey Mark,

Do you think this approach is reasonable? If so, I have another patch

I'd like

Quoted 1 line

to propose that would enable us to better catch error scenarios (like

the last

Quoted 1 line

two patches address) that we might encounter during do_image_prelink.

We just

Quoted 1 line

happened to detect these last two issues even though image_prelink

didn't fail

Quoted 1 line

the build, so I'd like to provide an option to enable a little more

assertive

Quoted 1 line

error path, if desired.

I'm getting caught back up on my prelink 'TODO' set. The approach seems reasonable to me. However, I appear to have lost the reference to the original
patch email.

I'm going to try to reapply from this email (or the list archive), but I may
need you to resend it. I'll let you [and others know] once I get these things
merged.

Thanks!
--Mark

Quoted 8 lines

Thanks,
Kyle

On Fri, Sep 28, 2018 at 10:57 AM Kyle Russell <[email protected] <mailto:[email protected]>> wrote:

binutils-2.28 (17026142ef35b62ac88bfe517b4160614902cb28) adds support for copying read-only dynamic symbols into .data.rel.ro <

http://data.rel.ro>

Quoted 2 lines

instead of .bss
since .bss is technically writable. This causes prelink to error

out on

Quoted 1 line

any binary containing COPY relocations in .data.rel.ro <

http://data.rel.ro>.

Quoted 2 lines

Read-only variables defined in shared libraries should be copied

directly

Quoted 1 line

into the space allocated for them in .data.rel.ro <

http://data.rel.ro> by

Quoted 4 lines

the linker.

To achieve this, we determine whether either of the two sections containing copy relocations is .data.rel.ro <http://data.rel.ro>.

If so, we

Quoted 2 lines

relocate the
symbol memory directly into the existing section instead of

constructing

Quoted 6 lines
    a new .(s)dynbss section once prelink_build_conflicts() returns.

    Fixes cxx1.sh, cxx2.sh, and cxx3.sh on Fedora 28 (which uses
    binutils-2.29).
    ---
     src/conflict.c | 51

+++++++++++++++++++++++++++++++++++++-------------

Quoted 8 lines
     src/undo.c     |  9 +++++++++
     2 files changed, 47 insertions(+), 13 deletions(-)
    diff --git a/src/conflict.c b/src/conflict.c
    index 9ae2ddb..5613ace 100644
    --- a/src/conflict.c
    +++ b/src/conflict.c
    @@ -450,7 +450,7 @@ get_relocated_mem (struct prelink_info *info,

DSO *dso,

Quoted 6 lines

GElf_Addr addr,
int
prelink_build_conflicts (struct prelink_info *info) {
- int i, ndeps = info->ent->ndepends + 1;
+ int i, reset_dynbss = 0, reset_sdynbss = 0, ndeps =

info->ent->ndepends + 1;

Quoted 4 lines
       struct prelink_entry *ent;
       int ret = 0;
       DSO *dso;
    @@ -675,6 +675,11 @@ prelink_build_conflicts (struct prelink_info

*info)

Quoted 5 lines
                         dso->filename);
                  goto error_out;
                }
    +
    +         name = strptr (dso, dso->ehdr.e_shstrndx,

dso->shdr[bss1].sh_name);

Quoted 1 line

+ if (strcmp(name, ".data.rel.ro <http://data.rel.ro>") ==

0)

Quoted 4 lines
    +           reset_sdynbss = 1;
    +
              firstbss2 = i;
              info->sdynbss_size = cr.rela[i - 1].r_offset -

cr.rela[0].r_offset;

Quoted 2 lines
              info->sdynbss_size += cr.rela[i - 1].r_addend;
    @@ -702,6 +707,10 @@ prelink_build_conflicts (struct prelink_info

*info)

Quoted 4 lines
                }
            }

    +      name = strptr (dso, dso->ehdr.e_shstrndx,

dso->shdr[bss2].sh_name);

Quoted 7 lines
    +      if (strcmp(name, ".data.rel.ro <http://data.rel.ro>") == 0)
    +        reset_dynbss = 1;
    +
           info->dynbss_size = cr.rela[cr.count - 1].r_offset
                              - cr.rela[firstbss2].r_offset;
           info->dynbss_size += cr.rela[cr.count - 1].r_addend;
    @@ -719,9 +728,9 @@ prelink_build_conflicts (struct prelink_info

*info)

Quoted 5 lines
              && strcmp (name = strptr (dso, dso->ehdr.e_shstrndx,
                                        dso->shdr[bss1].sh_name),
                         ".dynbss") != 0
    -         && strcmp (name, ".sdynbss") != 0)
    +         && strcmp (name, ".sdynbss") != 0 && strcmp (name, ".

data.rel.ro

Quoted 3 lines
    <http://data.rel.ro>") != 0)
            {
    -         error (0, 0, "%s: COPY relocations don't point into .bss

or .sbss

Quoted 2 lines

section",
+ error (0, 0, "%s: COPY relocations don't point into .bss,

.sbss,

Quoted 5 lines
    or .data.rel.ro <http://data.rel.ro> sections",
                     dso->filename);
              goto error_out;
            }
    @@ -730,9 +739,9 @@ prelink_build_conflicts (struct prelink_info

*info)

Quoted 5 lines
              && strcmp (name = strptr (dso, dso->ehdr.e_shstrndx,
                                        dso->shdr[bss2].sh_name),
                         ".dynbss") != 0
    -         && strcmp (name, ".sdynbss") != 0)
    +         && strcmp (name, ".sdynbss") != 0 && strcmp (name, ".

data.rel.ro

Quoted 3 lines
    <http://data.rel.ro>") != 0)
            {
    -         error (0, 0, "%s: COPY relocations don't point into .bss

or .sbss

Quoted 2 lines

section",
+ error (0, 0, "%s: COPY relocations don't point into .bss,

.sbss,

Quoted 5 lines
    or .data.rel.ro <http://data.rel.ro> section",
                     dso->filename);
              goto error_out;
            }
    @@ -768,16 +777,21 @@ prelink_build_conflicts (struct prelink_info

*info)

Quoted 8 lines

}

assert (j < ndeps);
+ GElf_Addr symaddr = s->u.ent->base + s->value;
+ char *buf;
+
if (i < firstbss2)
- j = get_relocated_mem (info, ndso, s->u.ent->base +

s->value,

Quoted 1 line

- info->sdynbss +

cr.rela[i].r_offset

Quoted 1 line

- - info->sdynbss_base,

cr.rela[i].r_addend,

Quoted 5 lines

- cr.rela[i].r_offset);
+ if (reset_sdynbss)
+ buf = get_data(dso, cr.rela[i].r_offset, NULL, NULL);
+ else
+ buf = info->sdynbss + cr.rela[i].r_offset -

info->sdynbss_base;

Quoted 2 lines

else
- j = get_relocated_mem (info, ndso, s->u.ent->base +

s->value,

Quoted 2 lines

- info->dynbss + cr.rela[i].r_offset
- - info->dynbss_base,

cr.rela[i].r_addend,

Quoted 5 lines

- cr.rela[i].r_offset);
+ if (reset_dynbss)
+ buf = get_data(dso, cr.rela[i].r_offset, NULL, NULL);
+ else
+ buf = info->dynbss + cr.rela[i].r_offset -

info->dynbss_base;

Quoted 7 lines
    +
    +         j = get_relocated_mem (info, ndso, symaddr, buf,
    cr.rela[i].r_addend, cr.rela[i].r_offset);

              switch (j)
                {
    @@ -815,6 +829,17 @@ prelink_build_conflicts (struct prelink_info

*info)

Quoted 19 lines
         if (info->dsos[i])
           close_dso (info->dsos[i]);

    +  if (reset_sdynbss)
    +    {
    +      free (info->sdynbss);
    +      info->sdynbss = NULL;
    +    }
    +
    +  if (reset_dynbss)
    +    {
    +      free (info->dynbss);
    +      info->dynbss = NULL;
    +    }
       info->dsos = NULL;
       free (cr.rela);
       return ret;
    diff --git a/src/undo.c b/src/undo.c
    index 8a55bf2..4c38dab 100644
Quoted 8 lines
    --- a/src/undo.c
    +++ b/src/undo.c
    @@ -633,6 +633,15 @@ prelink_undo (DSO *dso)
                  d->d_buf = NULL;
                  dso->shdr[i].sh_type = SHT_NOBITS;
                }
    +         else if (dso->shdr[i].sh_type == SHT_PROGBITS
    +                  && strcmp (name, ".data.rel.ro <

http://data.rel.ro>") == 0)

Quoted 13 lines

+ {
+ scn = dso->scn[i];
+ d = elf_getdata (scn, NULL);
+ assert (d != NULL && elf_getdata (scn, d) == NULL);
+ assert (d->d_size == dso->shdr[i].sh_size);
+ assert (memset(d->d_buf, 0, d->d_size) == d->d_buf);
+ }
else if (dso->shdr[i].sh_type == SHT_RELA
&& shdr[i].sh_type == SHT_REL)
{
--
2.17.1

#4 lore raw
Quoted 2 lines · Kyle Russell

Do you want me to just resent my last two patches?  I don't mind if that would be easier, and I'll remember to add the Signed-off-by. :)

I was able to pull them from the mailing list archives and have them applied to my local tree. I have a few other patches and then I'll be pushing to the staging tree.

--Mark

Quoted 214 lines

On Fri, Oct 12, 2018 at 10:00 AM Mark Hatle <[email protected] <mailto:[email protected]>> wrote:

    On 10/4/18 9:12 AM, Kyle Russell wrote:
    > Hey Mark,
    >
    > Do you think this approach is reasonable?  If so, I have another patch I'd
    like
    > to propose that would enable us to better catch error scenarios (like the last
    > two patches address) that we might encounter during do_image_prelink.  We just
    > happened to detect these last two issues even though image_prelink didn't fail
    > the build, so I'd like to provide an option to enable a little more assertive
    > error path, if desired.

    I'm getting caught back up on my prelink 'TODO' set.  The approach seems
    reasonable to me.  However, I appear to have lost the reference to the original
    patch email.

    I'm going to try to reapply from this email (or the list archive), but I may
    need you to resend it.  I'll let you [and others know] once I get these things
    merged.

    Thanks!
    --Mark

    > Thanks,
    > Kyle
    >
    > On Fri, Sep 28, 2018 at 10:57 AM Kyle Russell <[email protected]
    <mailto:[email protected]>
    > <mailto:[email protected] <mailto:[email protected]>>> wrote:
    >
    >     binutils-2.28 (17026142ef35b62ac88bfe517b4160614902cb28) adds support
    >     for copying read-only dynamic symbols into .data.rel.ro
    <http://data.rel.ro> <http://data.rel.ro>
    >     instead of .bss
    >     since .bss is technically writable.  This causes prelink to error out on
    >     any binary containing COPY relocations in .data.rel.ro
    <http://data.rel.ro> <http://data.rel.ro>.
    >
    >     Read-only variables defined in shared libraries should be copied directly
    >     into the space allocated for them in .data.rel.ro <http://data.rel.ro>
    <http://data.rel.ro> by
    >     the linker.
    >
    >     To achieve this, we determine whether either of the two sections
    >     containing copy relocations is .data.rel.ro <http://data.rel.ro>
    <http://data.rel.ro>.  If so, we
    >     relocate the
    >     symbol memory directly into the existing section instead of constructing
    >     a new .(s)dynbss section once prelink_build_conflicts() returns.
    >
    >     Fixes cxx1.sh, cxx2.sh, and cxx3.sh on Fedora 28 (which uses
    >     binutils-2.29).
    >     ---
    >      src/conflict.c | 51 +++++++++++++++++++++++++++++++++++++-------------
    >      src/undo.c     |  9 +++++++++
    >      2 files changed, 47 insertions(+), 13 deletions(-)
    >
    >     diff --git a/src/conflict.c b/src/conflict.c
    >     index 9ae2ddb..5613ace 100644
    >     --- a/src/conflict.c
    >     +++ b/src/conflict.c
    >     @@ -450,7 +450,7 @@ get_relocated_mem (struct prelink_info *info, DSO
    *dso,
    >     GElf_Addr addr,
    >      int
    >      prelink_build_conflicts (struct prelink_info *info)
    >      {
    >     -  int i, ndeps = info->ent->ndepends + 1;
    >     +  int i, reset_dynbss = 0, reset_sdynbss = 0, ndeps =
    info->ent->ndepends + 1;
    >        struct prelink_entry *ent;
    >        int ret = 0;
    >        DSO *dso;
    >     @@ -675,6 +675,11 @@ prelink_build_conflicts (struct prelink_info *info)
    >                          dso->filename);
    >                   goto error_out;
    >                 }
    >     +
    >     +         name = strptr (dso, dso->ehdr.e_shstrndx,
    dso->shdr[bss1].sh_name);
    >     +         if (strcmp(name, ".data.rel.ro <http://data.rel.ro>
    <http://data.rel.ro>") == 0)
    >     +           reset_sdynbss = 1;
    >     +
    >               firstbss2 = i;
    >               info->sdynbss_size = cr.rela[i - 1].r_offset -
    cr.rela[0].r_offset;
    >               info->sdynbss_size += cr.rela[i - 1].r_addend;
    >     @@ -702,6 +707,10 @@ prelink_build_conflicts (struct prelink_info *info)
    >                 }
    >             }
    >
    >     +      name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[bss2].sh_name);
    >     +      if (strcmp(name, ".data.rel.ro <http://data.rel.ro>
    <http://data.rel.ro>") == 0)
    >     +        reset_dynbss = 1;
    >     +
    >            info->dynbss_size = cr.rela[cr.count - 1].r_offset
    >                               - cr.rela[firstbss2].r_offset;
    >            info->dynbss_size += cr.rela[cr.count - 1].r_addend;
    >     @@ -719,9 +728,9 @@ prelink_build_conflicts (struct prelink_info *info)
    >               && strcmp (name = strptr (dso, dso->ehdr.e_shstrndx,
    >                                         dso->shdr[bss1].sh_name),
    >                          ".dynbss") != 0
    >     -         && strcmp (name, ".sdynbss") != 0)
    >     +         && strcmp (name, ".sdynbss") != 0 && strcmp (name,
    ".data.rel.ro <http://data.rel.ro>
    >     <http://data.rel.ro>") != 0)
    >             {
    >     -         error (0, 0, "%s: COPY relocations don't point into .bss or
    .sbss
    >     section",
    >     +         error (0, 0, "%s: COPY relocations don't point into .bss, .sbss,
    >     or .data.rel.ro <http://data.rel.ro> <http://data.rel.ro> sections",
    >                      dso->filename);
    >               goto error_out;
    >             }
    >     @@ -730,9 +739,9 @@ prelink_build_conflicts (struct prelink_info *info)
    >               && strcmp (name = strptr (dso, dso->ehdr.e_shstrndx,
    >                                         dso->shdr[bss2].sh_name),
    >                          ".dynbss") != 0
    >     -         && strcmp (name, ".sdynbss") != 0)
    >     +         && strcmp (name, ".sdynbss") != 0 && strcmp (name,
    ".data.rel.ro <http://data.rel.ro>
    >     <http://data.rel.ro>") != 0)
    >             {
    >     -         error (0, 0, "%s: COPY relocations don't point into .bss or
    .sbss
    >     section",
    >     +         error (0, 0, "%s: COPY relocations don't point into .bss, .sbss,
    >     or .data.rel.ro <http://data.rel.ro> <http://data.rel.ro> section",
    >                      dso->filename);
    >               goto error_out;
    >             }
    >     @@ -768,16 +777,21 @@ prelink_build_conflicts (struct prelink_info *info)
    >                   }
    >
    >               assert (j < ndeps);
    >     +         GElf_Addr symaddr = s->u.ent->base + s->value;
    >     +         char *buf;
    >     +
    >               if (i < firstbss2)
    >     -           j = get_relocated_mem (info, ndso, s->u.ent->base + s->value,
    >     -                                  info->sdynbss + cr.rela[i].r_offset
    >     -                                  - info->sdynbss_base,
    cr.rela[i].r_addend,
    >     -                                  cr.rela[i].r_offset);
    >     +           if (reset_sdynbss)
    >     +             buf = get_data(dso, cr.rela[i].r_offset, NULL, NULL);
    >     +           else
    >     +             buf = info->sdynbss + cr.rela[i].r_offset -
    info->sdynbss_base;
    >               else
    >     -           j = get_relocated_mem (info, ndso, s->u.ent->base + s->value,
    >     -                                  info->dynbss + cr.rela[i].r_offset
    >     -                                  - info->dynbss_base,
    cr.rela[i].r_addend,
    >     -                                  cr.rela[i].r_offset);
    >     +           if (reset_dynbss)
    >     +             buf = get_data(dso, cr.rela[i].r_offset, NULL, NULL);
    >     +           else
    >     +             buf = info->dynbss + cr.rela[i].r_offset -
    info->dynbss_base;
    >     +
    >     +         j = get_relocated_mem (info, ndso, symaddr, buf,
    >     cr.rela[i].r_addend, cr.rela[i].r_offset);
    >
    >               switch (j)
    >                 {
    >     @@ -815,6 +829,17 @@ prelink_build_conflicts (struct prelink_info *info)
    >          if (info->dsos[i])
    >            close_dso (info->dsos[i]);
    >
    >     +  if (reset_sdynbss)
    >     +    {
    >     +      free (info->sdynbss);
    >     +      info->sdynbss = NULL;
    >     +    }
    >     +
    >     +  if (reset_dynbss)
    >     +    {
    >     +      free (info->dynbss);
    >     +      info->dynbss = NULL;
    >     +    }
    >        info->dsos = NULL;
    >        free (cr.rela);
    >        return ret;
    >     diff --git a/src/undo.c b/src/undo.c
    >     index 8a55bf2..4c38dab 100644
    >     --- a/src/undo.c
    >     +++ b/src/undo.c
    >     @@ -633,6 +633,15 @@ prelink_undo (DSO *dso)
    >                   d->d_buf = NULL;
    >                   dso->shdr[i].sh_type = SHT_NOBITS;
    >                 }
    >     +         else if (dso->shdr[i].sh_type == SHT_PROGBITS
    >     +                  && strcmp (name, ".data.rel.ro <http://data.rel.ro>
    <http://data.rel.ro>") == 0)
    >     +           {
    >     +             scn = dso->scn[i];
    >     +             d = elf_getdata (scn, NULL);
    >     +             assert (d != NULL && elf_getdata (scn, d) == NULL);
    >     +             assert (d->d_size == dso->shdr[i].sh_size);
    >     +             assert (memset(d->d_buf, 0, d->d_size) == d->d_buf);
    >     +           }
    >               else if (dso->shdr[i].sh_type == SHT_RELA
    >                        && shdr[i].sh_type == SHT_REL)
    >                 {
    >     --
    >     2.17.1
    >