]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
strip: Deal with ARM data marker symbols pointing to debug sections.
authorMark Wielaard <mark@klomp.org>
Thu, 20 Jul 2017 20:34:29 +0000 (22:34 +0200)
committerMark Wielaard <mark@klomp.org>
Mon, 24 Jul 2017 10:20:01 +0000 (12:20 +0200)
ARM data marker symbols "$d" indicate the start of a sequence of data
items in a section. For data only sections no data marker symbol is
necessary, but may be put pointing to the start of the section.
binutils however has a bug which places a data marker symbol somewhere
inside the section (at least for .debug_frame).
https://sourceware.org/bugzilla/show_bug.cgi?id=21809

When strip finds a symbol pointing to a debug section that would be
put into the .debug file then it will copy over the whole symbol table.
This isn't necessary because the symbol is redundant.

Add an ebl hook to recognize data marker symbols with implementations
for arm and aarch64. Use it in strip to strip such symbols from the
symbol table if they point to a debug section.

Signed-off-by: Mark Wielaard <mark@klomp.org>
16 files changed:
backends/ChangeLog
backends/aarch64_init.c
backends/aarch64_symbol.c
backends/arm_init.c
backends/arm_symbol.c
libebl/ChangeLog
libebl/Makefile.am
libebl/ebl-hooks.h
libebl/ebl_data_marker_symbol.c [new file with mode: 0644]
libebl/eblopenbackend.c
libebl/libebl.h
src/ChangeLog
src/strip.c
tests/ChangeLog
tests/Makefile.am
tests/run-strip-g.sh [new file with mode: 0755]

index eb7e25f1a1fe6ba2fa4b5efd468416e5f113ffb9..88b9764ab57f5ef35c7dc650ccd9a73cf77cad7d 100644 (file)
@@ -1,3 +1,10 @@
+2017-08-20  Mark Wielaard  <mark@klomp.org>
+
+       * aarch64_init.c (aarch64_init): Hook data_marker_symbol.
+       * aarch64_symbol.c (aarch64_data_marker_symbol): New function.
+       * arm_init.c (arm_init): Hook data_marker_symbol.
+       * arm_symbol.c (aarch64_data_marker_symbol): New function.
+
 2017-07-18  Mark Wielaard  <mark@klomp.org>
 
        * Makefile.am (cpu_bpf): Always define.
index 08664943d08cb4a12176f6f6a5254c6144308820..fad923faa6112d0ac386c5a1ca1a5aaa09b27d44 100644 (file)
@@ -1,5 +1,5 @@
 /* Initialization of AArch64 specific backend library.
-   Copyright (C) 2013 Red Hat, Inc.
+   Copyright (C) 2013, 2017 Red Hat, Inc.
    This file is part of elfutils.
 
    This file is free software; you can redistribute it and/or modify
@@ -56,6 +56,7 @@ aarch64_init (Elf *elf __attribute__ ((unused)),
   HOOK (eh, reloc_simple_type);
   HOOK (eh, return_value_location);
   HOOK (eh, check_special_symbol);
+  HOOK (eh, data_marker_symbol);
   HOOK (eh, abi_cfi);
 
   /* X0-X30 (31 regs) + SP + 1 Reserved + ELR, 30 Reserved regs (34-43)
index 76999e4b1c9749ff93c6a90fe7ad3e59db54aa50..da3382e9601081eba861224f04cd8fde4cd573d6 100644 (file)
@@ -1,5 +1,5 @@
 /* AArch64 specific symbolic name handling.
-   Copyright (C) 2013, 2015 Red Hat, Inc.
+   Copyright (C) 2013, 2015, 2017 Red Hat, Inc.
    This file is part of elfutils.
 
    This file is free software; you can redistribute it and/or modify
@@ -90,3 +90,15 @@ aarch64_check_special_symbol (Elf *elf, GElf_Ehdr *ehdr, const GElf_Sym *sym,
 
   return false;
 }
+
+/* A data mapping symbol is a symbol with "$d" name or "$d.<any...>" name,
+   STT_NOTYPE, STB_LOCAL and st_size of zero. The indicate the stat of a
+   sequence of data items.  */
+bool
+aarch64_data_marker_symbol (const GElf_Sym *sym, const char *sname)
+{
+  return (sym != NULL && sname != NULL
+         && sym->st_size == 0 && GELF_ST_BIND (sym->st_info) == STB_LOCAL
+         && GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
+         && (strcmp (sname, "$d") == 0 || strncmp (sname, "$d.", 3) == 0));
+}
index caadac65c9d176cd10e105c4a255d4f02ebfb9bd..f2b1b11ea5bbff33c2aea5c7405d48fd11bb3d5e 100644 (file)
@@ -1,5 +1,5 @@
 /* Initialization of Arm specific backend library.
-   Copyright (C) 2002, 2005, 2009, 2013, 2014, 2015 Red Hat, Inc.
+   Copyright (C) 2002, 2005, 2009, 2013, 2014, 2015, 2017 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2002.
 
@@ -64,6 +64,7 @@ arm_init (Elf *elf __attribute__ ((unused)),
   HOOK (eh, abi_cfi);
   HOOK (eh, check_reloc_target_type);
   HOOK (eh, symbol_type_name);
+  HOOK (eh, data_marker_symbol);
 
   /* We only unwind the core integer registers.  */
   eh->frame_nregs = 16;
index da4a50a718982054dc3a0181aa36c5b56c1514b9..3edda7247ef1ff62aa3588e0369b8c64b4f92104 100644 (file)
@@ -1,5 +1,5 @@
 /* Arm specific symbolic name handling.
-   Copyright (C) 2002-2009, 2014, 2015 Red Hat, Inc.
+   Copyright (C) 2002-2009, 2014, 2015, 2017 Red Hat, Inc.
    This file is part of elfutils.
 
    This file is free software; you can redistribute it and/or modify
@@ -32,6 +32,7 @@
 
 #include <elf.h>
 #include <stddef.h>
+#include <string.h>
 
 #define BACKEND                arm_
 #include "libebl_CPU.h"
@@ -142,3 +143,15 @@ arm_symbol_type_name (int type,
     }
   return NULL;
 }
+
+/* A data mapping symbol is a symbol with "$d" name or "$d.<any...>" name,
+ *    STT_NOTYPE, STB_LOCAL and st_size of zero. The indicate the stat of a
+ *       sequence of data items.  */
+bool
+arm_data_marker_symbol (const GElf_Sym *sym, const char *sname)
+{
+  return (sym != NULL && sname != NULL
+          && sym->st_size == 0 && GELF_ST_BIND (sym->st_info) == STB_LOCAL
+          && GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
+          && (strcmp (sname, "$d") == 0 || strncmp (sname, "$d.", 3) == 0));
+}
index 506915bade1934d679f797a8a39185bd86674ec4..f44744968aba5f9c137259e1a6f1759538b73646 100644 (file)
@@ -1,3 +1,12 @@
+2017-07-20  Mark Wielaard  <mark@klomp.org>
+
+       * Makefile.am (gen_SOURCES): Add ebl_data_marker_symbol.c.
+       * ebl-hooks.h (data_marker_symbol): New hook.
+       * ebl_data_marker_symbol.c: New file.
+       * eblopenbackend.c (default_data_marker_symbol): New function.
+       (fill_defaults): Add default_data_marker_symbol.
+       * libebl.h (ebl_data_marker_symbol): New function.
+
 2017-04-20  Ulf Hermann  <ulf.hermann@qt.io>
 
        * libebl.h: Use __pure_attribute__.
index 6f945eb8824f4a70d05751838386c22b4cdd4a49..2491df80903a1b828e960e6f1e99707e35fd7dbd 100644 (file)
@@ -1,6 +1,6 @@
 ## Process this file with automake to create Makefile.in
 ##
-## Copyright (C) 2000-2010, 2013, 2016 Red Hat, Inc.
+## Copyright (C) 2000-2010, 2013, 2016, 2017 Red Hat, Inc.
 ## This file is part of elfutils.
 ##
 ## This file is free software; you can redistribute it and/or modify
@@ -53,7 +53,8 @@ gen_SOURCES = eblopenbackend.c eblclosebackend.c \
              eblsysvhashentrysize.c eblauxvinfo.c eblcheckobjattr.c \
              ebl_check_special_section.c ebl_syscall_abi.c eblabicfi.c \
              eblstother.c eblinitreg.c ebldwarftoregno.c eblnormalizepc.c \
-             eblunwind.c eblresolvesym.c eblcheckreloctargettype.c
+             eblunwind.c eblresolvesym.c eblcheckreloctargettype.c \
+             ebl_data_marker_symbol.c
 
 libebl_a_SOURCES = $(gen_SOURCES)
 
index b7253748204993fc6715748901bbc2e7c93283b6..f3a0e64a2785a78f38bbf35c5d9ea8764dc4f573 100644 (file)
@@ -1,5 +1,5 @@
 /* Backend hook signatures internal interface for libebl.
-   Copyright (C) 2000-2011, 2013, 2014, 2016 Red Hat, Inc.
+   Copyright (C) 2000-2011, 2013, 2014, 2016, 2017 Red Hat, Inc.
    This file is part of elfutils.
 
    This file is free software; you can redistribute it and/or modify
@@ -121,6 +121,9 @@ bool EBLHOOK(relative_reloc_p) (int);
 bool EBLHOOK(check_special_symbol) (Elf *, GElf_Ehdr *, const GElf_Sym *,
                              const char *, const GElf_Shdr *);
 
+/* Check if this is a data marker symbol.  e.g. '$d' symbols for ARM.  */
+bool EBLHOOK(data_marker_symbol) (const GElf_Sym *sym, const char *sname);
+
 /* Check whether only valid bits are set on the st_other symbol flag.
    Standard ST_VISIBILITY have already been masked off.  */
 bool EBLHOOK(check_st_other_bits) (unsigned char st_other);
diff --git a/libebl/ebl_data_marker_symbol.c b/libebl/ebl_data_marker_symbol.c
new file mode 100644 (file)
index 0000000..922d720
--- /dev/null
@@ -0,0 +1,44 @@
+/* Check whether a symbol is a special data marker.
+   Copyright (C) 2017 Red Hat, Inc.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+     * the GNU Lesser General Public License as published by the Free
+       Software Foundation; either version 3 of the License, or (at
+       your option) any later version
+
+   or
+
+     * the GNU General Public License as published by the Free
+       Software Foundation; either version 2 of the License, or (at
+       your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see <http://www.gnu.org/licenses/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <libeblP.h>
+
+
+bool
+ebl_data_marker_symbol (Ebl *ebl, const GElf_Sym *sym, const char *sname)
+{
+  if (ebl == NULL)
+    return false;
+
+  return ebl->data_marker_symbol (sym, sname);
+}
index f3a65cfa2e9da8a7e3c75c839061dee6503f8c74..1f814776594426281d76c41f3d4bef7d61f63a9a 100644 (file)
@@ -1,5 +1,5 @@
 /* Generate ELF backend handle.
-   Copyright (C) 2000-2016 Red Hat, Inc.
+   Copyright (C) 2000-2017 Red Hat, Inc.
    This file is part of elfutils.
 
    This file is free software; you can redistribute it and/or modify
@@ -184,6 +184,7 @@ static bool default_check_special_symbol (Elf *elf, GElf_Ehdr *ehdr,
                                          const GElf_Sym *sym,
                                          const char *name,
                                          const GElf_Shdr *destshdr);
+static bool default_data_marker_symbol (const GElf_Sym *sym, const char *sname);
 static bool default_check_st_other_bits (unsigned char st_other);
 static bool default_check_special_section (Ebl *, int,
                                           const GElf_Shdr *, const char *);
@@ -235,6 +236,7 @@ fill_defaults (Ebl *result)
   result->none_reloc_p = default_none_reloc_p;
   result->relative_reloc_p = default_relative_reloc_p;
   result->check_special_symbol = default_check_special_symbol;
+  result->data_marker_symbol = default_data_marker_symbol;
   result->check_st_other_bits = default_check_st_other_bits;
   result->bss_plt_p = default_bss_plt_p;
   result->return_value_location = default_return_value_location;
@@ -671,6 +673,13 @@ default_check_special_symbol (Elf *elf __attribute__ ((unused)),
   return false;
 }
 
+static bool
+default_data_marker_symbol (const GElf_Sym *sym __attribute__ ((unused)),
+                           const char *sname __attribute__ ((unused)))
+{
+  return false;
+}
+
 static bool
 default_check_st_other_bits (unsigned char st_other __attribute__ ((unused)))
 {
index 87896e4acbd26ac4425796e29992f98abc5cc75f..882bdb99d414c310cef595ee8c16ddf3d7080e82 100644 (file)
@@ -1,5 +1,5 @@
 /* Interface for libebl.
-   Copyright (C) 2000-2010, 2013, 2014, 2015, 2016 Red Hat, Inc.
+   Copyright (C) 2000-2010, 2013, 2014, 2015, 2016, 2017 Red Hat, Inc.
    This file is part of elfutils.
 
    This file is free software; you can redistribute it and/or modify
@@ -156,6 +156,10 @@ extern bool ebl_check_special_symbol (Ebl *ebl, GElf_Ehdr *ehdr,
                                      const GElf_Sym *sym, const char *name,
                                      const GElf_Shdr *destshdr);
 
+/* Check if this is a data marker symbol.  e.g. '$d' symbols for ARM.  */
+extern bool ebl_data_marker_symbol (Ebl *ebl, const GElf_Sym *sym,
+                                   const char *sname);
+
 /* Check whether only valid bits are set on the st_other symbol flag.  */
 extern bool ebl_check_st_other_bits (Ebl *ebl, unsigned char st_other);
 
index 0b60fc75d441a2e3e544032d2e3abd18ad48a1a7..f92f10d732012f30adc2832ae0b0490e7df00712 100644 (file)
@@ -1,3 +1,8 @@
+2017-07-20  Mark Wielaard  <mark@klomp.org>
+
+       * strip.c (handle_elf): Deal with data marker symbols pointing to
+       debug sections (they can be removed).
+
 2017-07-14  Mark Wielaard  <mark@klomp.org>
 
        * strip (OPT_KEEP_SECTION): New define.
index 4a35ea1d64df3e5a46b5eb773d2c837776aed957..773ed548a73b383cb0328697cb41d56e9306f432 100644 (file)
@@ -1,5 +1,5 @@
 /* Discard section not used at runtime from object files.
-   Copyright (C) 2000-2012, 2014, 2015, 2016 Red Hat, Inc.
+   Copyright (C) 2000-2012, 2014, 2015, 2016, 2017 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
 
@@ -960,8 +960,19 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
                      if (shdr_info[scnidx].idx == 0)
                        /* This symbol table has a real symbol in
                           a discarded section.  So preserve the
-                          original table in the debug file.  */
-                       shdr_info[cnt].debug_data = symdata;
+                          original table in the debug file.  Unless
+                          it is a redundant data marker to a debug
+                          (data only) section.  */
+                       if (! (ebl_section_strip_p (ebl, ehdr,
+                                                   &shdr_info[scnidx].shdr,
+                                                   shdr_info[scnidx].name,
+                                                   remove_comment,
+                                                   remove_debug)
+                              && ebl_data_marker_symbol (ebl, sym,
+                                       elf_strptr (elf,
+                                                   shdr_info[cnt].shdr.sh_link,
+                                                   sym->st_name))))
+                         shdr_info[cnt].debug_data = symdata;
                    }
                }
 
@@ -1293,7 +1304,10 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
        shdr_info[cnt].shdr.sh_name = dwelf_strent_off (shdr_info[cnt].se);
 
        /* Update the section header from the input file.  Some fields
-          might be section indeces which now have to be adjusted.  */
+          might be section indeces which now have to be adjusted.  Keep
+          the index to the "current" sh_link in case we need it to lookup
+          symbol table names.  */
+       size_t sh_link = shdr_info[cnt].shdr.sh_link;
        if (shdr_info[cnt].shdr.sh_link != 0)
          shdr_info[cnt].shdr.sh_link =
            shdr_info[shdr_info[cnt].shdr.sh_link].idx;
@@ -1492,13 +1506,17 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
                      /* The symbol points to a section that is discarded
                         but isn't preserved in the debug file. Check that
                         this is a section or group signature symbol
-                        for a section which has been removed.  */
+                        for a section which has been removed.  Or a special
+                        data marker symbol to a debug section.  */
                      {
                        elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION
                                    || ((shdr_info[sidx].shdr.sh_type
                                         == SHT_GROUP)
                                        && (shdr_info[sidx].shdr.sh_info
-                                           == inner)));
+                                           == inner))
+                                   || ebl_data_marker_symbol (ebl, sym,
+                                               elf_strptr (elf, sh_link,
+                                                           sym->st_name)));
                      }
                  }
 
index 194d019fe54d4336e448c9aeafe8fe78266c8334..6c70d02079b9128556d6fc7a8459fa03312e0d72 100644 (file)
@@ -1,3 +1,9 @@
+2017-07-20  Mark Wielaard  <mark@klomp.org>
+
+       * run-strip-g.sh: New test.
+       * Makefile.am (TESTS): Add run-strip-g.sh.
+       (EXTRA_DIST): Likewise.
+
 2017-07-18  Mark Wielaard  <mark@klomp.org>
 
        * Makefile.am (TESTS): Always add run-disasm-bpf.sh if HAVE_LIBASM.
index 368e92635dcf0810855b47f74c037cc28c194d01..407c0511f14862a13430783e2cb9d723f48f6e57 100644 (file)
@@ -81,7 +81,7 @@ TESTS = run-arextract.sh run-arsymtest.sh newfile test-nlist \
        run-strip-test3.sh run-strip-test4.sh run-strip-test5.sh \
        run-strip-test6.sh run-strip-test7.sh run-strip-test8.sh \
        run-strip-test9.sh run-strip-test10.sh run-strip-test11.sh \
-       run-strip-nothing.sh \
+       run-strip-nothing.sh run-strip-g.sh \
        run-strip-groups.sh run-strip-reloc.sh run-strip-strmerge.sh \
        run-strip-nobitsalign.sh run-strip-remove-keep.sh \
        run-unstrip-test.sh run-unstrip-test2.sh run-unstrip-test3.sh \
@@ -173,7 +173,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh \
             run-strip-test4.sh run-strip-test5.sh run-strip-test6.sh \
             run-strip-test7.sh run-strip-test8.sh run-strip-groups.sh \
             run-strip-test9.sh run-strip-test10.sh run-strip-test11.sh \
-            run-strip-nothing.sh run-strip-remove-keep.sh \
+            run-strip-nothing.sh run-strip-remove-keep.sh run-strip-g.sh \
             run-strip-strmerge.sh run-strip-nobitsalign.sh \
             testfile-nobitsalign.bz2 testfile-nobitsalign.strip.bz2 \
             run-strip-reloc.sh hello_i386.ko.bz2 hello_x86_64.ko.bz2 \
diff --git a/tests/run-strip-g.sh b/tests/run-strip-g.sh
new file mode 100755 (executable)
index 0000000..1303819
--- /dev/null
@@ -0,0 +1,102 @@
+#! /bin/sh
+# Copyright (C) 2017 Red Hat, Inc.
+# This file is part of elfutils.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# elfutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+. $srcdir/test-subr.sh
+
+# When stripping just the debug sections/symbols we keep the symtab
+# in the main ELF file. There should be no symbols pointing into the
+# debug sections and so there should not be a copy in the debug file
+# except for a NOBITS one.
+
+tempfiles a.out strip.out debug.out readelf.out
+
+echo Create debug a.out.
+echo "int main() { return 1; }" | gcc -g -xc -
+
+echo strip -g to file with debug file
+testrun ${abs_top_builddir}/src/strip -g -o strip.out -f debug.out ||
+  { echo "*** failed to strip -g -o strip.out -f debug.out a.out"; exit -1; }
+
+status=0
+testrun ${abs_top_builddir}/src/readelf -S strip.out > readelf.out
+grep SYMTAB readelf.out || status=$?
+echo $status
+if test $status -ne 0; then
+  echo no symtab found in strip.out
+  exit 1
+fi
+
+status=0
+testrun ${abs_top_builddir}/src/readelf -S debug.out > readelf.out
+grep SYMTAB readelf.out || status=$?
+echo $status
+if test $status -ne 1; then
+  echo symtab found in debug.out
+  exit 1
+fi
+
+# arm (with data marker in .debug_frame). See tests/run-addrcfi.sh
+testfiles testfilearm
+
+echo arm strip -g to file with debug file
+testrun ${abs_top_builddir}/src/strip -g -o strip.out -f debug.out testfilearm ||
+  { echo "*** failed to strip -g -o strip.out -f debug.out a.out"; exit -1; }
+
+status=0
+testrun ${abs_top_builddir}/src/readelf -S strip.out > readelf.out
+grep SYMTAB readelf.out || status=$?
+echo $status
+if test $status -ne 0; then
+  echo no symtab found in strip.out
+  exit 1
+fi
+
+status=0
+testrun ${abs_top_builddir}/src/readelf -S debug.out > readelf.out
+grep SYMTAB readelf.out || status=$?
+echo $status
+if test $status -ne 1; then
+  echo symtab found in debug.out
+  exit 1
+fi
+
+# aarch64 (with data marker in .debug_frame). See tests/run-addrcfi.sh
+testfiles testfileaarch64
+
+echo aarch64 strip -g to file with debug file
+testrun ${abs_top_builddir}/src/strip -g -o strip.out -f debug.out testfileaarch64 ||
+  { echo "*** failed to strip -g -o strip.out -f debug.out a.out"; exit -1; }
+
+status=0
+testrun ${abs_top_builddir}/src/readelf -S strip.out > readelf.out
+grep SYMTAB readelf.out || status=$?
+echo $status
+if test $status -ne 0; then
+  echo no symtab found in strip.out
+  exit 1
+fi
+
+status=0
+testrun ${abs_top_builddir}/src/readelf -S debug.out > readelf.out
+grep SYMTAB readelf.out || status=$?
+echo $status
+if test $status -ne 1; then
+  echo symtab found in debug.out
+  exit 1
+fi
+
+exit 0