[PATCH] strip don't mmap debug output file.

2019-06-18 Thread Mark Wielaard
Using ELF_C_WRITE_MMAP sometimes causes unexpected errors when disk
space is low. When writing out the file, the output file is first
extended so that it covers the whole file/mmap size. But it might
be that the file system allowed the extension as a sparse file. In
that case writing to the file through the mmap might still fail and
produce a SIGBUS if the disk is full. This is confusing to the user.

Using ELF_C_WRITE will produce "normal" errors when the file cannot
be written out. It also seems to use less memory because the debug
file is created from scratch. So the memory is first read into the
ELF data structure buffers, then written out as a whole. In this case
the mmap output buffer is just overhead.

Signed-off-by: Mark Wielaard 
---
 src/ChangeLog | 5 +
 src/strip.c   | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 580eea9..2cde63c 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2019-06-18  Mark Wielaard  
+
+   * strip.c (handle_elf): Use elf_begin ELF_C_WRITE, instead of
+   ELF_C_WRITE_MMAP.
+
 2019-05-10  Mark Wielaard  
 
* readelf.c (struct attrcb_args): Rename die to dies.
diff --git a/src/strip.c b/src/strip.c
index 4cd8750..48792a7 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -1097,7 +1097,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const 
char *fname,
   if (debug_fname != NULL)
 {
   /* Also create an ELF descriptor for the debug file */
-  debugelf = elf_begin (debug_fd, ELF_C_WRITE_MMAP, NULL);
+  debugelf = elf_begin (debug_fd, ELF_C_WRITE, NULL);
   if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0))
{
  error (0, 0, gettext ("cannot create new ehdr for file '%s': %s"),
-- 
1.8.3.1



[PATCH] libelf: Fix some 32bit offset/size issues that break updating 4G+ files.

2019-06-18 Thread Mark Wielaard
Hi,

I saw some curious crashes and broken ELF files in the Fedora builder
for really big packages (e.g. Qt5). It turned out that when libelf tries
to update or write out a 4GB+ files it used some 32bit offsets/types
where it should have been using 64bit offsets/types.

While searching for the bad coding pattern (using Elf64_Word as if it
was a 64bit type, it isn't!) I found that chromium probably is also
so big to hit this issue. There is even a somewhat similar fix!
https://chromium.googlesource.com/chromium/src/+/refs/heads/master/buildtools/third_party/eu-strip/fix-elf-size.patch

My apologies if you tried to upstream this and I missed it. But I think
the patch below is a more complete fix. If you could test it in your
setup that would be great.

The patch also contains a testcase. But since it is necessary to create
and process a 4GB+ file it is guarded by some checks to make sure the
machine has enough disk and memory. Do these checks look reasonable?
They probably prevent (make the test SKIP) on all buildbot CI workers.

Cheers,

Mark

-

Some years ago, in commit b1d0b0fc "libelf: Use int64_t for offsets in
libelf.h", we changed the public interface to use 64bit offsets/sizes.
This wasn't really a API change, before we relied on loff_t always
being 64bits on all platforms.

We didn't change the implementation to use the int64_t type though.
That was a little confusing, since the function definitions used a
different type, int64_t, from the function implementations, off_t.
Since we always build with _FILE_OFFSET_BITS=64 this should be fine.
But it was a bit sloppy and confusing.

Worse is that we got the translation of offset/sizes wrong in a
couple of places when translating to ELF types. In various places
we would use Elf32_Word or Elf64_Word. But both are 32bit (unsigned)
types! As is GElf_Word. Elf32_Off is 32bits and Elf64_Off is 64bits.
But we were not using those consistently.

This patch introduces comments for the usage of [G]Elf(32|64)Word in
libelf that are correct. And introduces Elf(32|64)_SizeWord in
elf32_updatenull.c where we want to make a difference between sizes
and offsets (the ELF variants are both unsigned, while int64_t/loff_t
is signed).

It also includes a new run-large-elf-file.sh test that creates a
large ELF files (one 64bit, little endian, rel and another big endian,
non-rel) and runs eu-strip, eu-elflint, eu-unstrip and eu-elfcmp.
Before this patch, that test case fails and creates corrupt ELF files.

The test is guarded by some checks that try to make sure there is
enough disk space and memory available on the machine. The test is
skipped otherwise.

Signed-off-by: Mark Wielaard 
---
 libelf/ChangeLog  | 32 ++
 libelf/common.h   |  2 +-
 libelf/elf32_newphdr.c|  3 ++
 libelf/elf32_updatefile.c |  8 ++--
 libelf/elf32_updatenull.c | 34 ---
 libelf/elf_begin.c| 15 ---
 libelf/elf_getaroff.c |  2 +-
 libelf/elf_getbase.c  |  4 +-
 libelf/elf_getdata_rawchunk.c |  2 +-
 libelf/elf_update.c   |  8 ++--
 libelf/libelfP.h  | 18 
 tests/ChangeLog   |  9 
 tests/Makefile.am |  2 +
 tests/addsections.c   | 44 +++
 tests/run-large-elf-file.sh   | 99 +++
 15 files changed, 230 insertions(+), 52 deletions(-)
 create mode 100755 tests/run-large-elf-file.sh

diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index 82e18eb..dde6c81 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,3 +1,35 @@
+2019-06-18  Mark Wielaard  
+
+   * common.h (allocate_elf): Use int64_t instead of off_t for offset.
+   * elf32_newphdr.c (newphdr): Document why Elf32/64_Word is correct.
+   * elf32_updatefile.c (fill): Use int64_t instead of off_t for pos.
+   (updatefile): Define last_offset, shdr_offset and scn_start as
+   int64_t instead of off_t.
+   * elf32_updatenull.c: Define Elf32_SizeWord and Elf64_SizeWord.
+   (updatenull_wrlock): Return int64_t instead of off_t. Define size,
+   sh_entsize, sh_align and sh_size as SizeWords. Define offset as
+   int64_t.  Cast data->d_off to SizeWord instead of GElf_Word. Drop
+   size GElf_Word cast. Cast offset to SizeWord instead of GElf_Word
+   when comparing with sh_size.
+   * elf_begin.c (get_shnum): Define offset as int64_t instead of
+   off_t. Document why use GElf_Word is correct.
+   (file_read_elf): Define offset as int64_t instead of off_t.
+   (__libelf_read_mmaped_file): Likewise.
+   (read_unmmaped_file): Likewise.
+   (read_file): Likewise.
+   * elf_getaroff.c (elf_getaroff): Return int64_t.
+   * elf_getbase.c (elf_getbase): Likewise.
+   * elf_getdata_rawchunk.c (elf_getdata_rawchunk): Define offset as
+   int64_t instead of off_t.
+   * elf_update.c (write_file): Return int64_t instead of off_t,
+   define size as int64_t instead