Bug 1278676

Summary: rpmbuild does not work with optflags in f23
Product: [Fedora] Fedora Reporter: fujiwara <tfujiwar>
Component: rpmAssignee: Packaging Maintenance Team <packaging-team-maint>
Status: CLOSED NOTABUG QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 23CC: i18n-bugs, jzeleny, lkardos, mmraka, novyjindrich, packaging-team-maint, pknirsch, shawn.p.huang, tagoh, tfujiwar, yeti
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-01-22 11:47:16 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description fujiwara 2015-11-06 07:06:38 UTC
If I try to build with customized optflags, rpmbuild causes a build error.

% rpmbuild -bb --define "optflags -g -Wall" ibus-anthy.spec 
...
checking for suffix of executables... 
checking whether we are cross compiling... configure: error: in `/root/rpmbuild/BUILD/ibus-anthy-1.5.7':
configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details


I read configure about cross compiling:

------ conftest.c -------
# view configure
#include <stdio.h>
int
main ()
{
FILE *f = fopen ("conftest.out", "w");
 return ferror (f) || fclose (f) != 0;

  ;
  return 0;
}
--------------------------


When I gave the optflags, configure calls:
# gcc -o conftest -specs=/usr/lib/rpm/redhat/redhat-hardened-ld conftest.c
/usr/bin/ld: /tmp/ccmHJ9B6.o: relocation R_X86_64_32 against `.rodata'
can not be used when making a shared object; recompile with -fPIC
/tmp/ccmHJ9B6.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status

When I build the module without optflags, configure is succeeded.
# gcc -o conftest -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 \
  -specs=/usr/lib/rpm/redhat/redhat-hardened-ld conftest.c

In /usr/lib/rpm/redhat/macros
%_hardening_cflags      -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1
%_hardening_ldflags     -specs=/usr/lib/rpm/redhat/redhat-hardened-ld


So if I run rpmbuild with optflags and __global_ldflags, the build is succeeded:

# rpmbuild -bb --define "optflags -g -Wall" \
  --define "__global_ldflags -Wl,-z,relro" ibus-anthy.spec 

I'm not sure if it's the expected way.
Probably it's better to have a new variable likes --define "ldflags foo" ?
While I know "optflags -g -Wall -fPIC" also works.

Comment 1 Ľuboš Kardoš 2016-01-21 14:02:54 UTC
The errors are from a configure script. rpmbuild doesn't care about optflags. They are just passed to a configure script. A configure script is part of a package and not part of rpmbuild. So I am reassigning this to ibus-anthy package.

Comment 2 fujiwara 2016-01-22 08:09:32 UTC
I think the package side has not changed anything.
This problem does not happen in Fedora 22 or older.
This problem happens in Fedora 23 or later.

If configure.ac includes "AC_PROG_CC", this problem happens:

- The tarball only includes configure.ac
% zcat ../SOURCES/a-1.5.8.tar.gz | tar tfv -
drwxr-xr-x root/root         0 2016-01-22 16:22 a-1.5.8/
-rw-r--r-- root/root      1207 2016-01-22 16:21 a-1.5.8/configure.ac

- configure.ac has only "AC_PROG_CC" and "AC_INIT".
% cat ../BUILD/a-1.5.8/configure.ac 
AC_INIT([ibus-anthy], [ibus_anthy_version], [https://github.com/ibus/ibus-anthy/issues],[ibus-anthy])
AC_PROG_CC

- a.spec runs only "autoconf" and "%configure".
Name:           a
Version:        1.5.8

%prep
%setup -q

%build
autoreconf
%configure



Then this problem always happen:
% rpmbuild -bb --define "optflags -g" a.spec 
checking whether we are cross compiling... configure: error: in `/root/rpmbuild/BUILD/a-1.5.8':
configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details
error: Bad exit status from /var/tmp/rpm-tmp.969HDg (%build)


Why rpmbuild runs LDFLAGS='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld' forcibly?

The default CFLAGS is no problem:
CFLAGS='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic'

But If I change optflags, the problem happens.

I have no idea to fix this in each package side.

Comment 3 Ľuboš Kardoš 2016-01-22 11:47:16 UTC
Yes, the root cause of this error is that you have '-specs=/usr/lib/rpm/redhat/redhat-hardened-ld' in your LDFLAGS and you don't have its counterpart '-specs=/usr/lib/rpm/redhat/redhat-hardened-cc1' in CFLAGS because you completely rewrote optflags with '--define "optflags -g"'. 

Maybe it would be better just to append to optflags instead of rewriting them. You can do this with '--define "optflags `rpm -E %{optflags}` -g"'. If you really want to rewrite optflags then you have to modify accordingly also %{__global_ldflags}. But I don't recommend completely rewrote optflags because then you will create a package which is against fedora rules.

> Why rpmbuild runs LDFLAGS='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld' forcibly?

Hardening all fedora packages [1] is a change which was announced, discussed and approved. Rpm team doesn't initiated nor performed this change. I am not entitled to revert this change. You can discuss your problem with owner of this change [1].

Because this is not a bug in rpm. I am closing this as not a bug.

[1] https://fedoraproject.org//wiki/Changes/Harden_All_Packages

Comment 4 fujiwara 2016-01-25 02:29:45 UTC
(In reply to Ľuboš Kardoš from comment #3)
> You can do this with '--define "optflags `rpm -E %{optflags}` -g"'. If

The suggestion does not help my case since the default optflags includes "-O2".


> > Why rpmbuild runs LDFLAGS='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld' forcibly?
> 
> Hardening all fedora packages [1] is a change which was announced, discussed
> and approved. Rpm team doesn't initiated nor performed this change. I am not
> entitled to revert this change. You can discuss your problem with owner of
> this change [1].

OK, thanks.

> 
> Because this is not a bug in rpm. I am closing this as not a bug.
> 
> [1] https://fedoraproject.org//wiki/Changes/Harden_All_Packages

Comment 5 Ľuboš Kardoš 2016-01-25 08:08:24 UTC
(In reply to fujiwara from comment #4)
> (In reply to Ľuboš Kardoš from comment #3)
> > You can do this with '--define "optflags `rpm -E %{optflags}` -g"'. If
> 
> The suggestion does not help my case since the default optflags includes
> "-O2".

That's not problem, you can override it with another option -O. From gdb man:

If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.

Comment 6 fujiwara 2016-01-25 09:16:43 UTC
(In reply to Ľuboš Kardoš from comment #5)
> (In reply to fujiwara from comment #4)
> > (In reply to Ľuboš Kardoš from comment #3)
> > > You can do this with '--define "optflags `rpm -E %{optflags}` -g"'. If
> > 
> > The suggestion does not help my case since the default optflags includes
> > "-O2".
> 
> That's not problem, you can override it with another option -O. From gdb man:
> 
> If you use multiple -O options, with or without level numbers, the last such
> option is the one that is effective.

Thank you for the suggestion.

Probably I think it would be great to archive this way in Fedora wiki.

Comment 7 fujiwara 2016-02-04 09:54:59 UTC
(In reply to Ľuboš Kardoš from comment #5)
> (In reply to fujiwara from comment #4)
> > (In reply to Ľuboš Kardoš from comment #3)
> > > You can do this with '--define "optflags `rpm -E %{optflags}` -g"'. If
> > 
> > The suggestion does not help my case since the default optflags includes
> > "-O2".
> 
> That's not problem, you can override it with another option -O. From gdb man:
> 
> If you use multiple -O options, with or without level numbers, the last such
> option is the one that is effective.

I think -O is still optimized. I need no -O.

Comment 8 Michael Mráka 2016-02-04 10:04:32 UTC
> I think -O is still optimized. I need no -O.

Use -O0 (aka -O<zero>).

Comment 9 Ľuboš Kardoš 2016-02-04 10:10:00 UTC
If adding '-O0' (-O followed by zero) doesn't disable optimization then there is a bug in gcc.

I mean try 'rpmbuild  --define "optflags `rpm -E %{optflags}` -g -O0" .....'

Comment 10 fujiwara 2016-02-04 10:22:58 UTC
Thanks for the comments.
I have never used -O0, sorry.