Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gcc -flto=auto : function ‘uftrace___cyg_profile_func_exit’ redeclared as variable #1343

Open
vitlav opened this issue Sep 11, 2021 · 11 comments

Comments

@vitlav
Copy link

vitlav commented Sep 11, 2021

With -flto=auto optflag I got

  LINK     libmcount/libmcount.so
/tmp/.private/lav/RPM/BUILD/uftrace-0.10/libmcount/plthook.c:93:1: error: function ‘uftrace___cyg_profile_func_exit’ redeclared as variable
   93 | ALIAS_DECL(__cyg_profile_func_exit);
      | ^
/tmp/.private/lav/RPM/BUILD/uftrace-0.10/libmcount/mcount.c:1994:1: note: previously declared here
 1994 | UFTRACE_ALIAS(__cyg_profile_func_exit);
      | ^
/tmp/.private/lav/RPM/BUILD/uftrace-0.10/libmcount/plthook.c:92:1: error: function ‘uftrace___cyg_profile_func_enter’ redeclared as variable
   92 | ALIAS_DECL(__cyg_profile_func_enter);
      | ^
/tmp/.private/lav/RPM/BUILD/uftrace-0.10/libmcount/mcount.c:1988:1: note: previously declared here
 1988 | UFTRACE_ALIAS(__cyg_profile_func_enter);

pltook.c:
/* use weak reference for non-defined (arch-dependent) symbols */
#define ALIAS_DECL(_sym)  extern __weak void (*uftrace_##_sym)(void);

mcount.c:
#define UFTRACE_ALIAS(_func) void uftrace_##_func(void*, void*) __alias(_func)

@namhyung
Copy link
Owner

Thanks for the report, I'll take a look.

@honggyukim
Copy link
Collaborator

Hmm.. The error is also shown only with -flto and it looks tricky anyway.

Is there any special reason to use -flto=auto or -flto option when building uftrace?

@vitlav
Copy link
Author

vitlav commented Sep 16, 2021

Hmm.. The error is also shown only with -flto and it looks tricky anyway.

Is there any special reason to use -flto=auto or -flto option when building uftrace?

That redeclation looks like a hidden bug for me.
There is no special reason to use LTO exclude the reason all vendors enable it by default:
https://fedoraproject.org/wiki/LTOByDefault
https://wiki.ubuntu.com/ToolChain/LTO
https://en.opensuse.org/openSUSE:LTO

@honggyukim
Copy link
Collaborator

Hmm.. we will think more about it. Thanks for reporting the issue.

@FantasqueX
Copy link

Same issue here when trying to package uftrace for ArchLinux. Temporarily disable LTO to solve the problem.

@amadio
Copy link

amadio commented Sep 16, 2022

Related bug in Gentoo Linux: https://bugs.gentoo.org/858503

@namhyung
Copy link
Owner

I'm not sure but does the below help?

diff --git a/libmcount/plthook.c b/libmcount/plthook.c
index 5b5620bd6a..aa54bbc364 100644
--- a/libmcount/plthook.c
+++ b/libmcount/plthook.c
@@ -81,13 +81,14 @@ static void resolve_pltgot(struct plthook_data *pd, int idx)
 
 /* use weak reference for non-defined (arch-dependent) symbols */
 #define ALIAS_DECL(_sym) extern __weak void (*uftrace_##_sym)(void);
+#define ALIAS_DECL2(_sym) extern __weak void (*uftrace_##_sym)(void *, void *);
 
 ALIAS_DECL(mcount);
 ALIAS_DECL(_mcount);
 ALIAS_DECL(__fentry__);
 ALIAS_DECL(__gnu_mcount_nc);
-ALIAS_DECL(__cyg_profile_func_enter);
-ALIAS_DECL(__cyg_profile_func_exit);
+ALIAS_DECL2(__cyg_profile_func_enter);
+ALIAS_DECL2(__cyg_profile_func_exit);
 
 #define SKIP_SYM(func)                                                                             \
        {                                                                                          \

@amadio
Copy link

amadio commented Sep 20, 2022

Unfortunately, no. I still get a compile error:

/tmp/portage/dev-util/uftrace-0.12/work/uftrace-0.12/libmcount/plthook.c:93:1: error: function ‘uftrace___cyg_profile_func_exit’ redeclared as variable
   93 | ALIAS_DECL2(__cyg_profile_func_exit);
      | ^
/tmp/portage/dev-util/uftrace-0.12/work/uftrace-0.12/libmcount/mcount.c:2004:1: note: previously declared here
 2004 | UFTRACE_ALIAS(__cyg_profile_func_exit);
      | ^
/tmp/portage/dev-util/uftrace-0.12/work/uftrace-0.12/libmcount/plthook.c:92:1: error: function ‘uftrace___cyg_profile_func_enter’ redeclared as variable
   92 | ALIAS_DECL2(__cyg_profile_func_enter);
      | ^
/tmp/portage/dev-util/uftrace-0.12/work/uftrace-0.12/libmcount/mcount.c:1998:1: note: previously declared here
 1998 | UFTRACE_ALIAS(__cyg_profile_func_enter);
      | ^
lto1: fatal error: errors during merging of translation units
compilation terminated.
lto-wrapper: fatal error: x86_64-pc-linux-gnu-gcc returned 1 exit status
compilation terminated.

If you want to reproduce this locally, try using

CFLAGS="-O2 -flto -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing"

when building.

@namhyung
Copy link
Owner

Thanks, I was able to reproduce it locally with your help. :)

This patch fixes the redeclaration error.

diff --git a/libmcount/plthook.c b/libmcount/plthook.c
index 5b5620bd6a..d9f1e8dd6a 100644
--- a/libmcount/plthook.c
+++ b/libmcount/plthook.c
@@ -80,14 +80,15 @@ static void resolve_pltgot(struct plthook_data *pd, int idx)
 }
 
 /* use weak reference for non-defined (arch-dependent) symbols */
-#define ALIAS_DECL(_sym) extern __weak void (*uftrace_##_sym)(void);
+#define ALIAS_DECL(_sym) extern __weak void uftrace_##_sym(void);
+#define ALIAS_DECL2(_sym) extern __weak void uftrace_##_sym(void *, void *);
 
 ALIAS_DECL(mcount);
 ALIAS_DECL(_mcount);
 ALIAS_DECL(__fentry__);
 ALIAS_DECL(__gnu_mcount_nc);
-ALIAS_DECL(__cyg_profile_func_enter);
-ALIAS_DECL(__cyg_profile_func_exit);
+ALIAS_DECL2(__cyg_profile_func_enter);
+ALIAS_DECL2(__cyg_profile_func_exit);
 
 #define SKIP_SYM(func)                                                                             \
        {                                                                                          \

But it reveals another problem like below. :-(

$ make
  LINK     libmcount/libmcount.so
lto-wrapper: warning: using serial compilation of 3 LTRANS jobs
lto-wrapper: note: see the ‘-flto’ option documentation for more information
/usr/bin/ld: /tmp/ccKRh5HD.ltrans0.ltrans.o: in function `mcount_rstack_restore':
/home/namhyung/project/uftrace/libmcount/misc.c:128: undefined reference to `plthook_return'
/usr/bin/ld: /tmp/ccKRh5HD.ltrans0.ltrans.o: in function `mcount_rstack_reset':
/home/namhyung/project/uftrace/libmcount/misc.c:207: undefined reference to `plthook_return'
/usr/bin/ld: /tmp/ccKRh5HD.ltrans0.ltrans.o: in function `mcount_startup.part.0':
/home/namhyung/project/uftrace/libmcount/mcount.c:1843: undefined reference to `mcount_return'
/usr/bin/ld: /tmp/ccKRh5HD.ltrans2.ltrans.o: in function `mcount_setup_trampoline':
/home/namhyung/project/uftrace/arch/x86_64/mcount-dynamic.c:79: undefined reference to `__dentry__'
/usr/bin/ld: /home/namhyung/project/uftrace/arch/x86_64/mcount-dynamic.c:59: undefined reference to `__xray_entry'
/usr/bin/ld: /home/namhyung/project/uftrace/arch/x86_64/mcount-dynamic.c:64: undefined reference to `__xray_exit'
collect2: error: ld returned 1 exit status
make: *** [Makefile:261: /home/namhyung/project/uftrace/libmcount/libmcount.so] Error 1

@amadio
Copy link

amadio commented Feb 8, 2023

I tried with 0.13.1 and this bug is still there.

@namhyung
Copy link
Owner

namhyung commented Feb 9, 2023

I'm still not sure if LTO supports partial linking (ld -r).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants