changeset 113:2d954d043296

core/elfutils: initial import, closes #1142
author David Demelier <markand@malikania.fr>
date Sat, 09 Mar 2019 17:03:57 +0100
parents 08181daf32da
children 96731cb1fcb0
files core/elfutils/elfutils.info core/elfutils/elfutils.sh core/elfutils/musl.patch
diffstat 3 files changed, 475 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/elfutils/elfutils.info	Sat Mar 09 17:03:57 2019 +0100
@@ -0,0 +1,29 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+PKGNAME=elfutils
+PKGVERSION=0.176
+PKGREVISION=1
+PKGLICENSE="GPLv2+ GPLv3+ LGPLv3+"
+PKGSUMMARY="utilities to inspect ELF files"
+PKGDOWNLOAD="https://sourceware.org/$PKGNAME/ftp/$PKGVERSION/$PKGNAME-$PKGVERSION.tar.bz2"
+PKGDEPENDS="lib/zlib lib/musl-fts lib/musl-obstack"
+PKGOPTIONS="NLS"
+
+if [ "$NLS" = "yes" ]; then
+	PKGDEPENDS="core/gettext $PKGDEPENDS"
+fi
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/elfutils/elfutils.sh	Sat Mar 09 17:03:57 2019 +0100
@@ -0,0 +1,55 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+: ${CHOST:=$(uname -m)-linux-musl}
+: ${CBUILD:=$(uname -m)-linux-musl}
+: ${CC:=gcc}
+: ${CFLAGS:=-O2 -Wno-error}
+: ${NLS:=yes}
+
+source ./elfutils.info
+
+set -ex
+
+if [ "$NLS" = "yes" ]; then
+	with_nls="--enable-nls"
+else
+	with_nls="--disable-nls"
+fi
+
+rm -rf $PKGNAME-$PKGVERSION
+tar xvaf $PKGNAME-$PKGVERSION.tar.bz2
+pushd $PKGNAME-$PKGVERSION
+
+# credits to void linux.
+# https://github.com/void-linux/void-packages/tree/master/srcpkgs/elfutils
+patch -p0 < ../musl.patch
+autoreconf -fi
+CC="$CC" \
+CFLAGS="$CFLAGS" \
+./configure \
+	--build=$CBUILD \
+	--host=$CHOST \
+	--prefix=/usr \
+	--program-prefix="eu-" \
+	${with_nls}
+make
+make install DESTDIR=$DESTDIR
+rm -f $DESTDIR/usr/lib/lib{asm,dw,ebl,elf}.a
+
+popd
+rm -rf $PKGNAME-$PKGVERSION
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/elfutils/musl.patch	Sat Mar 09 17:03:57 2019 +0100
@@ -0,0 +1,391 @@
+It looks like fregs.vregs[] is an array of double?
+Casting to Dwarf_Word instead of & 0xFFFFFFF should do.
+
+--- backends/aarch64_initreg.c	2015-11-27 14:36:29.000000000 +0100
++++ backends/aarch64_initreg.c	2016-08-09 03:47:25.428560159 +0200
+@@ -82,7 +83,7 @@
+ 
+   Dwarf_Word dwarf_fregs[32];
+   for (int r = 0; r < 32; r++)
+-    dwarf_fregs[r] = fregs.vregs[r] & 0xFFFFFFFF;
++    dwarf_fregs[r] = (Dwarf_Word)fregs.vregs[r];
+ 
+   if (! setfunc (64, 32, dwarf_fregs, arg))
+     return false;
+
+--- libcpu/i386_disasm.c	2015-08-21 14:22:37.000000000 +0200
++++ libcpu/i386_disasm.c	2015-11-20 06:30:59.250629957 +0100
+@@ -1,4 +1,4 @@
+-/* Disassembler for x86.
++/* Disassembler for x86.
+    Copyright (C) 2007, 2008, 2009, 2011 Red Hat, Inc.
+    This file is part of elfutils.
+    Written by Ulrich Drepper <drepper@redhat.com>, 2007.
+@@ -710,6 +710,7 @@
+ 
+ 		case 'm':
+ 		  /* Mnemonic.  */
++		  str = mnebuf;
+ 
+ 		  if (unlikely (instrtab[cnt].mnemonic == MNE_INVALID))
+ 		    {
+--- libelf/elf.h	2015-08-21 14:22:37.000000000 +0200
++++ libelf/elf.h	2015-11-20 04:54:33.948081321 +0100
+@@ -21,6 +21,17 @@
+ 
+ #include <features.h>
+ 
++#if !defined(__GLIBC__)
++/* C++ needs to know that types and declarations are C, not C++.  */
++#ifdef  __cplusplus
++# define __BEGIN_DECLS  extern "C" {
++# define __END_DECLS    }
++#else
++# define __BEGIN_DECLS
++# define __END_DECLS
++#endif
++#endif
++
+ __BEGIN_DECLS
+ 
+ /* Standard ELF types.  */
+--- lib/fixedsizehash.h.orig
++++ lib/fixedsizehash.h
+@@ -30,7 +30,6 @@
+ #include <errno.h>
+ #include <stdlib.h>
+ #include <string.h>
+-#include <sys/cdefs.h>
+ 
+ #include <system.h>
+ 
+--- configure.ac	2015-10-15 15:16:57.000000000 +0200
++++ configure.ac	2015-11-20 05:32:57.723901582 +0100
+@@ -305,6 +305,62 @@
+ fi
+ AC_SUBST([argp_LDADD])
+ 
++dnl Check if we have fts available from our libc
++AC_LINK_IFELSE(
++	[AC_LANG_PROGRAM(
++		[#if !defined(__x86_64__)
++		#undef  _FILE_OFFSET_BITS
++		#define _FILE_OFFSET_BITS 32
++		#endif
++		#include <fts.h>],
++		[FTS* fts = 0; return fts_close(fts); return 0;]
++		)],
++	[libc_has_fts="true"],
++	[libc_has_fts="false"]
++)
++
++dnl If our libc doesn't provide fts, then test for libfts
++if test "$libc_has_fts" = "false" ; then
++	AC_MSG_WARN("libc does not have fts")
++	AC_CHECK_LIB([fts], [fts_close], [have_fts="true"], [have_fts="false"])
++
++	if test "$have_fts" = "false"; then
++		AC_MSG_ERROR("no libfts found")
++	else
++		fts_LDADD="-lfts"
++	fi
++else
++	fts_LDADD=""
++fi
++AC_SUBST([fts_LDADD])
++
++dnl Check if we have obstack available from our libc
++AC_LINK_IFELSE(
++	[AC_LANG_PROGRAM(
++		[#include <obstack.h>],
++		[_obstack_begin(0, 0, 0, NULL, NULL); return 0;]
++		)],
++	[libc_has_obstack="true"],
++	[libc_has_obstack="false"]
++)
++
++dnl If our libc doesn't provide obstack, then test for libobstack
++if test "$libc_has_obstack" = "false" ; then
++	AC_MSG_WARN("libc does not have obstack")
++	AC_CHECK_LIB([obstack], [_obstack_begin], [have_obstack="true"], [have_obstack="false"])
++
++	if test "$have_obstack" = "false"; then
++		AC_MSG_ERROR("no libobstack found")
++	else
++		obstack_LDADD="-lobstack"
++	fi
++else
++	obstack_LDADD=""
++fi
++AC_SUBST([obstack_LDADD])
++
++dnl The directories with content.
++
+ dnl The directories with content.
+ 
+ dnl Documentation.
+--- src/Makefile.am.orig	2016-08-29 21:38:11.179295769 +0200
++++ src/Makefile.am	2016-08-03 18:19:47.000000000 +0200
+@@ -67,22 +67,22 @@
+ ar_no_Wstack_usage = yes
+ unstrip_no_Wstack_usage = yes
+ 
++readelf_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD) -ldl
++nm_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD) -ldl \
+-readelf_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(argp_LDADD) -ldl
+-nm_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(argp_LDADD) -ldl \
+ 	   $(demanglelib)
++size_LDADD = $(libelf) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD)
++strip_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD) -ldl
++elflint_LDADD  = $(libebl) $(libelf) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD) -ldl
++findtextrel_LDADD = $(libdw) $(libelf) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD)
++addr2line_LDADD = $(libdw) $(libelf) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD) $(demanglelib)
++elfcmp_LDADD = $(libebl) $(libelf) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD) -ldl
+-size_LDADD = $(libelf) $(libeu) $(argp_LDADD)
+-strip_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD) -ldl
+-elflint_LDADD  = $(libebl) $(libelf) $(libeu) $(argp_LDADD) -ldl
+-findtextrel_LDADD = $(libdw) $(libelf) $(libeu) $(argp_LDADD)
+-addr2line_LDADD = $(libdw) $(libelf) $(libeu) $(argp_LDADD) $(demanglelib)
+-elfcmp_LDADD = $(libebl) $(libelf) $(libeu) $(argp_LDADD) -ldl
+ objdump_LDADD  = $(libasm) $(libebl) $(libelf) $(libeu) $(argp_LDADD) -ldl
++ranlib_LDADD = libar.a $(libelf) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD)
++strings_LDADD = $(libelf) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD)
++ar_LDADD = libar.a $(libelf) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD)
++unstrip_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD) -ldl
++stack_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD) -ldl $(demanglelib)
++elfcompress_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD)
+-ranlib_LDADD = libar.a $(libelf) $(libeu) $(argp_LDADD)
+-strings_LDADD = $(libelf) $(libeu) $(argp_LDADD)
+-ar_LDADD = libar.a $(libelf) $(libeu) $(argp_LDADD)
+-unstrip_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD) -ldl
+-stack_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD) -ldl $(demanglelib)
+-elfcompress_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD)
+ 
+ installcheck-binPROGRAMS: $(bin_PROGRAMS)
+	bad=0; pid=$$$$; list="$(bin_PROGRAMS)"; for p in $$list; do \
+--- libdw/Makefile.am.orig	2017-08-05 01:41:04.103553705 +0000
++++ libdw/Makefile.am	2017-08-05 01:43:55.366543497 +0000
+@@ -105,7 +105,7 @@
+ libdw_so_LIBS = libdw_pic.a ../libdwelf/libdwelf_pic.a \
+ 	  ../libdwfl/libdwfl_pic.a ../libebl/libebl.a
+ libdw_so_DEPS = ../lib/libeu.a ../libelf/libelf.so
+-libdw_so_LDLIBS = $(libdw_so_DEPS) -ldl -lz $(argp_LDADD) $(zip_LIBS)
++libdw_so_LDLIBS = $(libdw_so_DEPS) -ldl -lz $(argp_LDADD) $(fts_LDADD) $(obstack_LDADD) $(zip_LIBS)
+ libdw_so_SOURCES =
+ libdw.so$(EXEEXT): $(srcdir)/libdw.map $(libdw_so_LIBS) $(libdw_so_DEPS)
+ # The rpath is necessary for libebl because its $ORIGIN use will
+--- src/arlib.h	2015-08-21 14:22:37.000000000 +0200
++++ src/arlib.h	2015-11-20 08:02:55.153199611 +0100
+@@ -29,6 +29,16 @@
+ #include <stdint.h>
+ #include <sys/types.h>
+ 
++#if !defined(ACCESSPERMS)
++# define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 0777 */
++#endif
++#if !defined(ALLPERMS)
++# define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* 07777 */
++#endif
++#if !defined(DEFFILEMODE)
++# define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)/* 0666*/
++#endif
++
+ 
+ /* State of -D/-U flags.  */
+ extern bool arlib_deterministic_output;
+--- src/elfcompress.c.orig	2016-04-02 12:51:26.903848894 +0200
++++ src/elfcompress.c	2016-04-02 12:55:15.076996338 +0200
+@@ -35,6 +35,14 @@
+ #include <gelf.h>
+ #include "system.h"
+ 
++#if !defined(FNM_EXTMATCH)
++# define FNM_EXTMATCH 0
++#endif
++
++#if !defined(ALLPERMS)
++# define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* 07777 */
++#endif
++
+ /* Name and version of program.  */
+ static void print_version (FILE *stream, struct argp_state *state);
+ ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
+--- src/strip.c.orig	2017-08-02 12:06:25.000000000 +0000
++++ src/strip.c	2017-08-05 01:58:01.523493062 +0000
+@@ -47,6 +47,14 @@
+ #include <system.h>
+ #include <printversion.h>
+ 
++#if !defined(FNM_EXTMATCH)
++# define FNM_EXTMATCH 0
++#endif
++
++#if !defined(ACCESSPERMS)
++#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO)
++#endif
++
+ typedef uint8_t GElf_Byte;
+ 
+ /* Name and version of program.  */
+--- src/readelf.c
++++ src/readelf.c
+@@ -4773,10 +4773,11 @@ listptr_base (struct listptr *p)
+   return cudie_base (&cu);
+ }
+ 
++static const char *listptr_name;
++
+ static int
+-compare_listptr (const void *a, const void *b, void *arg)
++compare_listptr (const void *a, const void *b)
+ {
+-  const char *name = arg;
+   struct listptr *p1 = (void *) a;
+   struct listptr *p2 = (void *) b;
+ 
+@@ -4792,21 +4793,21 @@ compare_listptr (const void *a, const void *b, void *arg)
+ 	  p1->warned = p2->warned = true;
+ 	  error (0, 0,
+ 		 gettext ("%s %#" PRIx64 " used with different address sizes"),
+-		 name, (uint64_t) p1->offset);
++		 listptr_name, (uint64_t) p1->offset);
+ 	}
+       if (p1->dwarf64 != p2->dwarf64)
+ 	{
+ 	  p1->warned = p2->warned = true;
+ 	  error (0, 0,
+ 		 gettext ("%s %#" PRIx64 " used with different offset sizes"),
+-		 name, (uint64_t) p1->offset);
++		 listptr_name, (uint64_t) p1->offset);
+ 	}
+       if (listptr_base (p1) != listptr_base (p2))
+ 	{
+ 	  p1->warned = p2->warned = true;
+ 	  error (0, 0,
+ 		 gettext ("%s %#" PRIx64 " used with different base addresses"),
+-		 name, (uint64_t) p1->offset);
++		 listptr_name, (uint64_t) p1->offset);
+ 	}
+       if (p1->attr != p2 ->attr)
+ 	{
+@@ -4814,7 +4815,7 @@ compare_listptr (const void *a, const void *b, void *arg)
+ 	  error (0, 0,
+ 		 gettext ("%s %#" PRIx64
+ 			  " used with different attribute %s and %s"),
+-		 name, (uint64_t) p1->offset, dwarf_attr_name (p2->attr),
++		 listptr_name, (uint64_t) p1->offset, dwarf_attr_name (p2->attr),
+ 		 dwarf_attr_name (p2->attr));
+ 	}
+     }
+@@ -4885,9 +4886,11 @@ notice_listptr (enum section_e section, struct listptr_table *table,
+ static void
+ sort_listptr (struct listptr_table *table, const char *name)
+ {
+-  if (table->n > 0)
+-    qsort_r (table->table, table->n, sizeof table->table[0],
+-	     &compare_listptr, (void *) name);
++  if (table->n > 0) {
++    listptr_name = name;
++    qsort (table->table, table->n, sizeof table->table[0],
++	     &compare_listptr);
++  }
+ }
+ 
+ static bool
+--- libdwfl/dwfl_error.c	2015-09-29 17:18:51.000000000 +0200
++++ libdwfl/dwfl_error.c	2015-11-20 05:11:29.889002058 +0100
+@@ -154,7 +154,16 @@
+   switch (error &~ 0xffff)
+     {
+     case OTHER_ERROR (ERRNO):
++#if defined(__GLIBC__)
+       return strerror_r (error & 0xffff, "bad", 0);
++#else
++      {
++        static __thread char buf[128] = "";
++        if (0 == strerror_r(error & 0xffff, buf, sizeof(buf)))
++          return buf;
++      }
++      return "strerror_r() failed";
++#endif
+     case OTHER_ERROR (LIBELF):
+       return elf_errmsg (error & 0xffff);
+     case OTHER_ERROR (LIBDW):
+--- src/unstrip.c.orig	2017-04-27 14:26:26.000000000 +0000
++++ src/unstrip.c	2017-05-05 15:51:33.515154220 +0000
+@@ -56,6 +56,15 @@
+ # define _(str) gettext (str)
+ #endif
+ 
++#ifndef strndupa
++#define strndupa(s, n) \
++       (__extension__ ({const char *__in = (s);                    \
++                        size_t __len = strnlen (__in, (n)) + 1;    \
++                        char *__out = (char *) alloca (__len);     \
++                        __out[__len-1] = '\0';                     \
++                        (char *) memcpy (__out, __in, __len-1);}))
++#endif
++
+ /* Name and version of program.  */
+ ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
+ 
+--- /dev/null	2019-03-09 09:00:22.527999555 +0100
++++ src/error.h	2019-03-09 16:37:44.828481636 +0100
+@@ -0,0 +1,27 @@
++#ifndef _ERROR_H_
++#define _ERROR_H_
++
++#include <stdarg.h>
++#include <stdio.h>
++#include <stdlib.h>
++#include <string.h>
++#include <errno.h>
++
++static unsigned int error_message_count = 0;
++
++static inline void error(int status, int errnum, const char* format, ...)
++{
++	va_list ap;
++	fprintf(stderr, "%s: ", program_invocation_name);
++	va_start(ap, format);
++	vfprintf(stderr, format, ap);
++	va_end(ap);
++	if (errnum)
++		fprintf(stderr, ": %s", strerror(errnum));
++	fprintf(stderr, "\n");
++	error_message_count++;
++	if (status)
++		exit(status);
++}
++
++#endif	/* _ERROR_H_ */
+--- /dev/null	2019-03-09 09:00:22.527999555 +0100
++++ lib/error.h	2019-03-09 16:37:46.732481531 +0100
+@@ -0,0 +1,27 @@
++#ifndef _ERROR_H_
++#define _ERROR_H_
++
++#include <stdarg.h>
++#include <stdio.h>
++#include <stdlib.h>
++#include <string.h>
++#include <errno.h>
++
++static unsigned int error_message_count = 0;
++
++static inline void error(int status, int errnum, const char* format, ...)
++{
++	va_list ap;
++	fprintf(stderr, "%s: ", program_invocation_name);
++	va_start(ap, format);
++	vfprintf(stderr, format, ap);
++	va_end(ap);
++	if (errnum)
++		fprintf(stderr, ": %s", strerror(errnum));
++	fprintf(stderr, "\n");
++	error_message_count++;
++	if (status)
++		exit(status);
++}
++
++#endif	/* _ERROR_H_ */