[PATCH 1/1] support cross compilation

2021-05-26 Thread Petr Ovtchenkov
Possible problem with out-of-tree build expected
---
 Makefile.am  |  2 +-
 configure.ac |  6 ++
 lib-host/Makefile.am | 47 
 libcpu/Makefile.am   | 11 +++
 4 files changed, 65 insertions(+), 1 deletion(-)
 create mode 100644 lib-host/Makefile.am

diff --git a/Makefile.am b/Makefile.am
index 9c47afa9..b93839ec 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -26,7 +26,7 @@ AM_MAKEFLAGS = --no-print-directory
 
 pkginclude_HEADERS = version.h
 
-SUBDIRS = config lib libelf libcpu backends libebl libdwelf libdwfl libdw \
+SUBDIRS = config lib lib-host libelf libcpu backends libebl libdwelf libdwfl 
libdw \
  libasm debuginfod src po doc tests
 
 EXTRA_DIST = elfutils.spec GPG-KEY NOTES CONTRIBUTING \
diff --git a/configure.ac b/configure.ac
index b348a717..432b961a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -88,6 +88,7 @@ AS_IF([test "$use_locks" = yes],
 AH_TEMPLATE([USE_LOCKS], [Defined if libraries should be thread-safe.])
 
 AC_PROG_CC
+AX_PROG_CC_FOR_BUILD
 AC_PROG_RANLIB
 AC_PROG_YACC
 AM_PROG_LEX
@@ -96,6 +97,8 @@ m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
 AC_CHECK_TOOL([READELF], [readelf])
 AC_CHECK_TOOL([NM], [nm])
 
+AM_CONDITIONAL([CROSS], [test "x$cross_compiling" = xyes])
+
 # We use -std=gnu99 but have explicit checks for some language constructs
 # and GNU extensions since some compilers claim GNU99 support, but don't
 # really support all language extensions. In particular we need
@@ -593,6 +596,9 @@ AC_CONFIG_FILES([doc/Makefile])
 dnl Support library.
 AC_CONFIG_FILES([lib/Makefile])
 
+dnl Support library.
+AC_CONFIG_FILES([lib-host/Makefile])
+
 dnl ELF library.
 AC_CONFIG_FILES([libelf/Makefile])
 
diff --git a/lib-host/Makefile.am b/lib-host/Makefile.am
new file mode 100644
index ..9316c8ed
--- /dev/null
+++ b/lib-host/Makefile.am
@@ -0,0 +1,47 @@
+## Process this file with automake to create Makefile.in
+##
+## Copyright (C) 1996-2011 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 .
+##
+if CROSS
+
+include $(top_srcdir)/config/eu.am
+AM_CFLAGS += $(fpic_CFLAGS)
+AM_CPPFLAGS +=  -D_GNU_SOURCE -I$(srcdir)/../lib -I$(srcdir)/../libelf
+
+noinst_LIBRARIES = libeu.a
+
+$(libeu_a_OBJECTS):CC=${CC_FOR_BUILD}
+
+libeu_a_SOURCES = ../lib/xstrdup.c ../lib/xstrndup.c ../lib/xmalloc.c 
../lib/next_prime.c \
+ ../lib/crc32.c ../lib/crc32_file.c \
+ ../lib/color.c ../lib/printversion.c
+
+if !GPROF
+xmalloc_CFLAGS = -ffunction-sections
+endif
+endif
diff --git a/libcpu/Makefile.am b/libcpu/Makefile.am
index 43844ecf..5086f8be 100644
--- a/libcpu/Makefile.am
+++ b/libcpu/Makefile.am
@@ -27,6 +27,13 @@
 ## the GNU Lesser General Public License along with this program.  If
 ## not, see .
 ##
+
+if CROSS
+i386_gendis_LINK = ${CC_FOR_BUILD} ${LDFLAGS} -o $@
+
+$(i386_gendis_OBJECTS):CC=${CC_FOR_BUILD}
+endif
+
 include $(top_srcdir)/config/eu.am
 AM_CPPFLAGS += -I$(srcdir)/../libelf -I$(srcdir)/../libebl \
-I$(srcdir)/../libdw -I$(srcdir)/../libasm
@@ -79,7 +86,11 @@ endif
 
 i386_lex_no_Werror = yes
 
+if CROSS
+libeu = ../lib-host/libeu.a
+else
 libeu = ../lib/libeu.a
+endif
 
 i386_lex_CFLAGS = -Wno-unused-label -Wno-unused-function -Wno-sign-compare \
  -Wno-implicit-fallthrough
-- 
2.29.2.1.g08c1adf7b9



Re: [PATCH 1/1] support cross compilation

2021-05-26 Thread Petr Ovtchenkov
On Wed, 26 May 2021 10:09:31 -0300
Érico Nogueira  wrote:

> Hi! Are you sure this is necessary? In Void Linux, we cross compile
> elfutils for arm and aarch64 without any issue, and I have built it a
> few times for powerpc as well.
> 

Hello!

Yes, I am sure. I do not know about you process (check, that you really
cross, not run via qemu or like). Build process _run_ i386_gendis to
generate headers:


if MAINTAINER_MODE
noinst_HEADERS += memory-access.h i386_parse.h i386_data.h

noinst_PROGRAMS = i386_gendis$(EXEEXT)

$(srcdir)/%_dis.h: %_defs i386_gendis$(EXEEXT)
$(AM_V_GEN)./i386_gendis$(EXEEXT) $< > $@T<
$(AM_V_at)mv -f $@T $@

else

$(srcdir)/%_dis.h:
@echo '*** missing $@; configure with --enable-maintainer-mode'
@false

endif


> > +if CROSS
> > +i386_gendis_LINK = ${CC_FOR_BUILD} ${LDFLAGS} -o $@
> > +
> > +$(i386_gendis_OBJECTS): CC=${CC_FOR_BUILD}
> > +endif  
>
> Isn't this hardcoding an assumption that the build machine is x86?

I think no. But this question is for original author:

  commit 3cbdd387c752999255aea91600b5cfdefbeac7d0
  Author: Ulrich Drepper 
  Date:   Wed Jan 2 17:44:39 2008 +

  propagate from branch 'com.redhat.elfutils.disasm' (head
  d15b4eb794e81e477f9896fe82a74cb5ecf4514c) to branch
  'com.redhat.elfutils' (head
  eaacbf01f8cc89d043ec6eca9b5e35cb5c4cde06)

;)



Re: [PATCH 1/1] support cross compilation

2021-05-26 Thread Petr Ovtchenkov
On Wed, 26 May 2021 12:41:51 -0300
Érico Nogueira  wrote:

> 
> Yes, we really cross.

It doesn't matter because

> We always use the release tarballs, which already have the %_dis.h
> files. This explains why we haven't hit any issues.


> Anyway, couldn't you (re)use the distribution tarball generation stuff
> for cross setups from git master?

I can. But I haven't problems with building it "directly from VCS" too,
as you see. Nevertheless there are opinion that code from VCS
could be smoothly building without procedures behind scene.

> Otherwise this would add a
> dependency on autoconf-archive for anyone generating the configure
> script locally...

Well, anyone who generate configure locally has it. Otherwise it is a
big pain ;).

> I think a final version of this patch should
> document the autoconf-archive requirement, if it's merged.

May be addition of m4/ax_prog_cc_for_build.m4 make sense?

Best regards,

--

  - ptr


[PATCH 0/2] cross-build support, updated

2022-05-06 Thread Petr Ovtchenkov
Cross-build support updated for 59158656f3b:

  - build libeu.a for build host arch (in lib-host);
  - build i386_gendis for build host arch;
  - use i386_gendis for generating;
  - avoid _FORTIFY_SOURCE for helper programs on build host,
this may not correspond to target _FORTIFY_SOURCE level.

Tricks in use only when cross-build detected.

See also https://sourceware.org/bugzilla/show_bug.cgi?id=28891

Possible problem (not critical, IMHO): not tested for
out-of-tree build.


Petr Ovtchenkov (2):
  support cross compilation
  drop _FORTIFY_SOURCE for host build

 Makefile.am  |  2 +-
 configure.ac |  6 ++
 lib-host/Makefile.am | 48 
 libcpu/Makefile.am   | 12 +++
 4 files changed, 67 insertions(+), 1 deletion(-)
 create mode 100644 lib-host/Makefile.am

-- 
2.35.1.1.ge5a6898900



[PATCH 1/2] support cross compilation

2022-05-06 Thread Petr Ovtchenkov
Possible problem with out-of-tree build expected
---
 Makefile.am  |  2 +-
 configure.ac |  6 ++
 lib-host/Makefile.am | 47 
 libcpu/Makefile.am   | 11 +++
 4 files changed, 65 insertions(+), 1 deletion(-)
 create mode 100644 lib-host/Makefile.am

diff --git a/Makefile.am b/Makefile.am
index 8643312a..a9c6c1d9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -26,7 +26,7 @@ AM_MAKEFLAGS = --no-print-directory
 
 pkginclude_HEADERS = version.h
 
-SUBDIRS = config lib libelf libcpu backends libebl libdwelf libdwfl libdw \
+SUBDIRS = config lib lib-host libelf libcpu backends libebl libdwelf libdwfl 
libdw \
  libasm debuginfod src po doc tests
 
 EXTRA_DIST = elfutils.spec GPG-KEY NOTES CONTRIBUTING \
diff --git a/configure.ac b/configure.ac
index 11d1cf82..d0ad4678 100644
--- a/configure.ac
+++ b/configure.ac
@@ -90,6 +90,7 @@ AH_TEMPLATE([USE_LOCKS], [Defined if libraries should be 
thread-safe.])
 
 AC_PROG_CC_C99
 AC_PROG_CXX
+AX_PROG_CC_FOR_BUILD
 AC_PROG_RANLIB
 AC_PROG_YACC
 AM_PROG_LEX
@@ -98,6 +99,8 @@ m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
 AC_CHECK_TOOL([READELF], [readelf])
 AC_CHECK_TOOL([NM], [nm])
 
+AM_CONDITIONAL([CROSS], [test "x$cross_compiling" = xyes])
+
 AC_CACHE_CHECK([whether gcc supports __attribute__((visibility()))],
ac_cv_visibility, [dnl
 save_CFLAGS="$CFLAGS"
@@ -608,6 +611,9 @@ AC_CONFIG_FILES([doc/Makefile])
 dnl Support library.
 AC_CONFIG_FILES([lib/Makefile])
 
+dnl Support library.
+AC_CONFIG_FILES([lib-host/Makefile])
+
 dnl ELF library.
 AC_CONFIG_FILES([libelf/Makefile])
 
diff --git a/lib-host/Makefile.am b/lib-host/Makefile.am
new file mode 100644
index ..9316c8ed
--- /dev/null
+++ b/lib-host/Makefile.am
@@ -0,0 +1,47 @@
+## Process this file with automake to create Makefile.in
+##
+## Copyright (C) 1996-2011 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 .
+##
+if CROSS
+
+include $(top_srcdir)/config/eu.am
+AM_CFLAGS += $(fpic_CFLAGS)
+AM_CPPFLAGS +=  -D_GNU_SOURCE -I$(srcdir)/../lib -I$(srcdir)/../libelf
+
+noinst_LIBRARIES = libeu.a
+
+$(libeu_a_OBJECTS):CC=${CC_FOR_BUILD}
+
+libeu_a_SOURCES = ../lib/xstrdup.c ../lib/xstrndup.c ../lib/xmalloc.c 
../lib/next_prime.c \
+ ../lib/crc32.c ../lib/crc32_file.c \
+ ../lib/color.c ../lib/printversion.c
+
+if !GPROF
+xmalloc_CFLAGS = -ffunction-sections
+endif
+endif
diff --git a/libcpu/Makefile.am b/libcpu/Makefile.am
index 57d0a164..8da743d4 100644
--- a/libcpu/Makefile.am
+++ b/libcpu/Makefile.am
@@ -27,6 +27,13 @@
 ## the GNU Lesser General Public License along with this program.  If
 ## not, see .
 ##
+
+if CROSS
+i386_gendis_LINK = ${CC_FOR_BUILD} ${LDFLAGS} -o $@
+
+$(i386_gendis_OBJECTS):CC=${CC_FOR_BUILD}
+endif
+
 include $(top_srcdir)/config/eu.am
 AM_CPPFLAGS += -I$(srcdir)/../libelf -I$(srcdir)/../libebl \
-I$(srcdir)/../libdw -I$(srcdir)/../libasm
@@ -87,7 +94,11 @@ endif
 
 i386_lex_no_Werror = yes
 
+if CROSS
+libeu = ../lib-host/libeu.a
+else
 libeu = ../lib/libeu.a
+endif
 
 i386_lex_CFLAGS = -Wno-unused-label -Wno-unused-function -Wno-sign-compare \
  -Wno-implicit-fallthrough
-- 
2.35.1.1.ge5a6898900



[PATCH 2/2] drop _FORTIFY_SOURCE for host build

2022-05-06 Thread Petr Ovtchenkov
Check for _FORTIFY_SOURCE level was made for target, not for build
host. This level may not satisfy to libc on host. Fortification for
build tools looks insignificant, so just drop -D_FORTIFY_SOURCE=...
definition for host compiler.
---
 lib-host/Makefile.am | 1 +
 libcpu/Makefile.am   | 1 +
 2 files changed, 2 insertions(+)

diff --git a/lib-host/Makefile.am b/lib-host/Makefile.am
index 9316c8ed..9b6ea77d 100644
--- a/lib-host/Makefile.am
+++ b/lib-host/Makefile.am
@@ -36,6 +36,7 @@ AM_CPPFLAGS +=  -D_GNU_SOURCE -I$(srcdir)/../lib 
-I$(srcdir)/../libelf
 noinst_LIBRARIES = libeu.a
 
 $(libeu_a_OBJECTS):CC=${CC_FOR_BUILD}
+$(libeu_a_OBJECTS):CFLAGS=${CPPFLAGS} $(fpic_CFLAGS)
 
 libeu_a_SOURCES = ../lib/xstrdup.c ../lib/xstrndup.c ../lib/xmalloc.c 
../lib/next_prime.c \
  ../lib/crc32.c ../lib/crc32_file.c \
diff --git a/libcpu/Makefile.am b/libcpu/Makefile.am
index 8da743d4..9a1b4a8e 100644
--- a/libcpu/Makefile.am
+++ b/libcpu/Makefile.am
@@ -32,6 +32,7 @@ if CROSS
 i386_gendis_LINK = ${CC_FOR_BUILD} ${LDFLAGS} -o $@
 
 $(i386_gendis_OBJECTS):CC=${CC_FOR_BUILD}
+$(i386_gendis_OBJECTS):CFLAGS=$(CPPFLAGS)
 endif
 
 include $(top_srcdir)/config/eu.am
-- 
2.35.1.1.ge5a6898900



_FORTIFY_SOURCE=3 fail for gcc 12/glibc 2.35

2022-05-06 Thread Petr Ovtchenkov
Hi,

_FORTIFY_SOURCE=3 fail to compile with gcc 12/glibc 2.35.

Used:
  (GCC) 12.0.1 20220504 (prerelease)
  glibc ec5b79aac768 (release/2.35/master)


In file included from /x86_64-unknown-linux-gnu/usr/include/features.h:490,
 from .../x86_64-unknown-linux-gnu/usr/include/assert.h:35,
 from elf_getarsym.c:34:
In function ‘pread’,
inlined from ‘pread_retry’ at ../lib/system.h:188:21,
inlined from ‘read_number_entries’ at elf_getarsym.c:63:21,
inlined from ‘elf_getarsym’ at elf_getarsym.c:172:11:
/x86_64-unknown-linux-gnu/usr/include/bits/unistd.h:74:10: error: 
‘__pread_alias’ specified size between 9223372036854775813 and 
18446744073709551615 exceeds maximum object size 9223372036854775807 
[-Werror=stringop-overflow=]
   74 |   return __glibc_fortify (pread, __nbytes, sizeof (char),
  |  ^~~
elf_getarsym.c: In function ‘elf_getarsym’:
elf_getarsym.c:56:5: note: destination object allocated here
   56 |   } u;
  | ^
/x86_64-unknown-linux-gnu/usr/include/bits/unistd.h:50:16: note: in a call 
to function ‘__pread_alias’ declared with attribute ‘access (write_only, 2, 3)’
   50 | extern ssize_t __REDIRECT (__pread_alias,
  |^~


In file included from /x86_64-unknown-linux-gnu/usr/include/features.h:490,
 from /x86_64-unknown-linux-gnu/usr/include/assert.h:35,
 from elf_getarsym.c:34:
In function ‘pread’,
inlined from ‘pread_retry’ at ../lib/system.h:188:21,
inlined from ‘read_number_entries’ at elf_getarsym.c:63:21,
inlined from ‘elf_getarsym’ at elf_getarsym.c:172:11:
.../x86_64-unknown-linux-gnu/usr/include/bits/unistd.h:74:10: error: 
‘__pread_alias’ specified size between 9223372036854775813 and 
18446744073709551615 exceeds maximum object size 9223372036854775807 
[-Werror=stringop-overflow=]
   74 |   return __glibc_fortify (pread, __nbytes, sizeof (char),
  |  ^~~
elf_getarsym.c: In function ‘elf_getarsym’:
elf_getarsym.c:56:5: note: destination object allocated here
   56 |   } u;
  | ^
.../x86_64-unknown-linux-gnu/usr/include/bits/unistd.h:50:16: note: in a call 
to function ‘__pread_alias’ declared with attribute ‘access (write_only, 2, 3)’
   50 | extern ssize_t __REDIRECT (__pread_alias,


Lower level (_FORTIFY_SOURCE=2) compiled fine.

Tricks similar to suggested in 
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91397
not help: warning disappear only when I set wrong condition

  if (w >= sizeof(u))
__builtin_unreachable();

(should be w > sizeof(u)).

--

   - ptr