Re: [PATCH] elfcompress: Don't rewrite file if no section data needs to be updated.

2018-07-24 Thread Mark Wielaard
On Mon, Jul 23, 2018 at 06:36:37PM +0200, Igor Gnatenko wrote:
> Acked-by: Igor Gnatenko 

Thanks. Pushed to master.


Re: [PATCH] elfcompress: Don't rewrite file if no section data needs to be updated.

2018-07-24 Thread Mark Wielaard
On Tue, Jul 24, 2018 at 09:57:28AM +0200, Mark Wielaard wrote:
> On Mon, Jul 23, 2018 at 06:36:37PM +0200, Igor Gnatenko wrote:
> > Acked-by: Igor Gnatenko 
> 
> Thanks. Pushed to master.

BTW. For those at home just reading the mailinglist and wondering why
they only see one part of the converstation. The elfutils-devel
mailinglist is open for all, but does reject HTML emails. But since I
was on CC I still got those, replying text-only.


Re: [PATCH] backends: add abi_cfi and register_info callbacks for RISC-V

2018-07-24 Thread Andreas Schwab
How about this, does it look reasonable?  It doesn't handle SET6/SUB6
yet, and eu-readelf -wframe doesn't agree with readelf -wf, so this is
only a RFC.

Andreas.

diff --git a/backends/riscv_symbol.c b/backends/riscv_symbol.c
index dce8e3586b..866be8f093 100644
--- a/backends/riscv_symbol.c
+++ b/backends/riscv_symbol.c
@@ -44,10 +44,27 @@ riscv_reloc_simple_type (Ebl *ebl __attribute__ ((unused)), 
int type)
 {
   switch (type)
 {
+case R_RISCV_SET8:
+  return ELF_T_BYTE;
+case R_RISCV_SET16:
+  return ELF_T_HALF;
 case R_RISCV_32:
+case R_RISCV_SET32:
   return ELF_T_WORD;
 case R_RISCV_64:
   return ELF_T_XWORD;
+case R_RISCV_ADD16:
+  return ELF_T_ADD_HALF;
+case R_RISCV_SUB16:
+  return ELF_T_SUB_HALF;
+case R_RISCV_ADD32:
+  return ELF_T_ADD_WORD;
+case R_RISCV_SUB32:
+  return ELF_T_SUB_WORD;
+case R_RISCV_ADD64:
+  return ELF_T_ADD_XWORD;
+case R_RISCV_SUB64:
+  return ELF_T_SUB_XWORD;
 default:
   return ELF_T_NUM;
 }
diff --git a/libdwfl/relocate.c b/libdwfl/relocate.c
index 9afcdebecc..1c4a1bade2 100644
--- a/libdwfl/relocate.c
+++ b/libdwfl/relocate.c
@@ -379,9 +379,18 @@ relocate (Dwfl_Module * const mod,
 DO_TYPE (WORD, Word); DO_TYPE (SWORD, Sword);  \
 DO_TYPE (XWORD, Xword); DO_TYPE (SXWORD, Sxword)
 size_t size;
+bool is_add_sub = false;
 switch (type)
   {
 #define DO_TYPE(NAME, Name)\
+   case ELF_T_ADD_##NAME:  \
+   case ELF_T_SUB_##NAME:  \
+ if (addend == NULL)   \
+   /* These do not make sense with SHT_REL.  */ \
+   return DWFL_E_BADRELTYPE;   \
+ is_add_sub = true;\
+ size = sizeof (GElf_##Name);  \
+ break;\
case ELF_T_##NAME:  \
  size = sizeof (GElf_##Name);  \
break
@@ -417,9 +426,23 @@ relocate (Dwfl_Module * const mod,
   {
/* For the addend form, we have the value already.  */
value += *addend;
+   if (is_add_sub)
+ {
+   Elf_Data *d = gelf_xlatetom (relocated, &tmpdata, &rdata,
+ehdr->e_ident[EI_DATA]);
+   if (d == NULL)
+ return DWFL_E_LIBELF;
+   assert (d == &tmpdata);
+ }
switch (type)
  {
 #define DO_TYPE(NAME, Name)\
+   case ELF_T_ADD_##NAME:  \
+ tmpbuf.Name += value; \
+ break;\
+   case ELF_T_SUB_##NAME:  \
+ tmpbuf.Name -= value; \
+ break;\
case ELF_T_##NAME:  \
  tmpbuf.Name = value;  \
break
diff --git a/libelf/gelf_fsize.c b/libelf/gelf_fsize.c
index 0c509265cb..faa6d6fd4b 100644
--- a/libelf/gelf_fsize.c
+++ b/libelf/gelf_fsize.c
@@ -52,6 +52,18 @@ const size_t __libelf_type_sizes[EV_NUM - 1][ELFCLASSNUM - 
1][ELF_T_NUM] =
   [ELF_T_SWORD]= ELFW2(LIBELFBITS, FSZ_SWORD),   \
   [ELF_T_XWORD]= ELFW2(LIBELFBITS, FSZ_XWORD),   \
   [ELF_T_SXWORD]   = ELFW2(LIBELFBITS, FSZ_SXWORD),  \
+  [ELF_T_ADD_BYTE] = 1,  \
+  [ELF_T_ADD_HALF] = ELFW2(LIBELFBITS, FSZ_HALF),\
+  [ELF_T_ADD_WORD] = ELFW2(LIBELFBITS, FSZ_WORD),\
+  [ELF_T_ADD_SWORD]= ELFW2(LIBELFBITS, FSZ_SWORD), 
  \
+  [ELF_T_ADD_XWORD]= ELFW2(LIBELFBITS, FSZ_XWORD), 
  \
+  [ELF_T_ADD_SXWORD] = ELFW2(LIBELFBITS, FSZ_SXWORD),\
+  [ELF_T_SUB_BYTE] = 1,  \
+  [ELF_T_SUB_HALF] = ELFW2(LIBELFBITS, FSZ_HALF),\
+  [ELF_T_SUB_WORD] = ELFW2(LIBELFBITS, FSZ_WORD),\
+  [ELF_T_SUB_SWORD]= ELFW2(LIBELFBITS, FSZ_SWORD), 
  \
+  [ELF_T_SUB_XWORD]= ELFW2(LIBELFBITS, FSZ_XWORD), 
  \
+  [ELF_T_SUB_SXWORD] = ELFW2(LIBELFBITS, FSZ_SXWORD),\
   [ELF_T_EHDR] = sizeof (ElfW2(LIBELFBITS, Ext_Ehdr)),   \
   [ELF_T_SHDR] = sizeof (ElfW2(LIBELFBITS, Ext_Shdr)),   \
   [ELF_T_SYM]  = sizeof (ElfW2(LIBELFBITS, Ext_Sym)),\
diff --git a/libelf/libelf.h b/libelf/libelf.h
index 547c0f5081..38eeea31ab 100644
--- a/libelf/libelf.h
+++ b/libelf/libelf.h
@@ -117,6 +117,18 @@ typedef enum
   ELF_T_GNUHASH,   /* GNU-style hash section.  */
   ELF_T_AUXV,  /* Elf32_auxv_t, Elf64_auxv_t, ... */
   ELF_T_CHDR,  /* Com

[PATCH] unstrip: Also check sh_size in compare_unalloc_sections.

2018-07-24 Thread Mark Wielaard
compare_unalloc_sections only checked sh_flags and the section names.
This would cause stripped/debug section mismatches when there were
multiple sections with the same name and flags. Fix this by also checking
the size of the section matches.

Add a testcase that has two ".group" sections created on i386 with the
gcc annobin plugin.

Signed-off-by: Mark Wielaard 
---
 src/ChangeLog  |   4 +++
 src/unstrip.c  |   6 
 tests/ChangeLog|   6 
 tests/Makefile.am  |   1 +
 tests/run-annobingroup.sh  |  55 +
 tests/testfile-annobingroup-i386.o.bz2 | Bin 0 -> 1387 bytes
 6 files changed, 72 insertions(+)
 create mode 100644 tests/testfile-annobingroup-i386.o.bz2

diff --git a/src/ChangeLog b/src/ChangeLog
index 37e7b62..eee144d 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2018-07-24  Mark Wielaard  
+
+   * unstrip.c (compare_unalloc_sections): Also compare sh_size.
+
 2018-07-21  Mark Wielaard  
 
* unstrip.c (adjust_all_relocs): Skip SHT_GROUP sections.
diff --git a/src/unstrip.c b/src/unstrip.c
index cb1f7dc..ec46c95 100644
--- a/src/unstrip.c
+++ b/src/unstrip.c
@@ -709,6 +709,12 @@ compare_unalloc_sections (const GElf_Shdr *shdr1, const 
GElf_Shdr *shdr2,
   if (shdr1->sh_flags > shdr2->sh_flags)
 return 1;
 
+  /* Sizes should be the same.  */
+  if (shdr1->sh_size < shdr2->sh_size)
+return -1;
+  if (shdr1->sh_size > shdr2->sh_size)
+return 1;
+
   /* Sort by name as last resort.  */
   return strcmp (name1, name2);
 }
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 45844b1..2a20ffa 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,9 @@
+2018-07-24  Mark Wielaard  
+
+   * run-annobingroup.sh: Add testfile-annobingroup-i386.o tests.
+   * testfile-annobingroup-i386.o.bz2: New test file.
+   * Makefile.am (EXTRA_DIST): Add testfile-annobingroup-i386.o.bz2.
+
 2018-07-21  Mark Wielaard  
 
* run-annobingroup.sh: New test.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b7a0e17..2946083 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -195,6 +195,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
 run-strip-test12.sh \
 run-strip-nothing.sh run-strip-remove-keep.sh run-strip-g.sh \
 run-annobingroup.sh testfile-annobingroup.o.bz2 \
+testfile-annobingroup-i386.o.bz2 \
 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-annobingroup.sh b/tests/run-annobingroup.sh
index 5f08b35..700df32 100755
--- a/tests/run-annobingroup.sh
+++ b/tests/run-annobingroup.sh
@@ -65,4 +65,59 @@ EOF
 
 testrun ${abs_top_builddir}/src/elfcmp testfile-annobingroup.o remerged.elf
 
+# echo "void * __attribute__((cold)) foo (void) { return foo; }"
+#  > testfile-annobingroup-i386.c
+# gcc -fpic -g -O2 -fplugin=annobin -c testfile-annobingroup-i386.c
+testfiles testfile-annobingroup-i386.o
+
+testrun_compare ${abs_top_builddir}/src/readelf -g 
testfile-annobingroup-i386.o << EOF
+
+Section group [ 1] '.group' with signature '.text.unlikely.group' contains 3 
entries:
+  [ 8] .gnu.build.attributes..text.unlikely
+  [ 9] .rel.gnu.build.attributes..text.unlikely
+  [10] .text.unlikely
+
+COMDAT section group [ 2] '.group' with signature '__x86.get_pc_thunk.ax' 
contains 1 entry:
+  [13] .text.__x86.get_pc_thunk.ax
+EOF
+
+testrun ${abs_top_builddir}/src/strip -o stripped.elf -f debugfile.elf 
testfile-annobingroup-i386.o
+
+testrun_compare ${abs_top_builddir}/src/readelf -g stripped.elf << EOF
+
+Section group [ 1] '.group' with signature '.text.unlikely.group' contains 3 
entries:
+  [ 8] .gnu.build.attributes..text.unlikely
+  [ 9] .rel.gnu.build.attributes..text.unlikely
+  [10] .text.unlikely
+
+COMDAT section group [ 2] '.group' with signature '__x86.get_pc_thunk.ax' 
contains 1 entry:
+  [13] .text.__x86.get_pc_thunk.ax
+EOF
+
+testrun_compare ${abs_top_builddir}/src/readelf -g debugfile.elf << EOF
+
+Section group [ 1] '.group' with signature '.text.unlikely.group' contains 3 
entries:
+  [ 8] .gnu.build.attributes..text.unlikely
+  [ 9] .rel.gnu.build.attributes..text.unlikely
+  [10] .text.unlikely
+
+COMDAT section group [ 2] '.group' with signature '__x86.get_pc_thunk.ax' 
contains 1 entry:
+  [13] .text.__x86.get_pc_thunk.ax
+EOF
+
+testrun ${abs_top_builddir}/src/unstrip -o remerged.elf stripped.elf 
debugfile.elf
+
+testrun_compare ${abs_top_builddir}/src/readelf -g remerged.elf << EOF
+
+Section group [ 1] '.group' with signature '.text.unlikely.group' contains 3 
entries:
+  [ 8] .gnu.build.attributes..text.unlikely
+  [ 9] .rel.gnu.build.attributes..text.unlikely
+  [10] .text.unlikely
+
+COMDAT section group [ 2] '.group' with signature '__x86.get_pc