develooper Front page | perl.perl5.porters | Postings from May 2012

Configure misdetects strlcpy et. al. with gcc -flto

Thread Next
From:
Lukas Mai
Date:
May 14, 2012 23:42
Subject:
Configure misdetects strlcpy et. al. with gcc -flto
Message ID:
20120515084152.2498609f@web.de
Hi,

while trying to build perl-5.16.0-RC1 I ran into this problem:

...
cgcc -fstack-protector -L/usr/local/lib -O2 -flto -o miniperl \
    perlmini.o opmini.o miniperlmain.o   gv.o toke.o perly.o pad.o regcomp.o dump.o util.o mg.o reentr.o mro.o keywords.o hv.o av.o run.o pp_hot.o sv.o pp.o scope.o pp_ctl.o pp_sys.o doop.o doio.o regexec.o utf8.o taint.o deb.o universal.o globals.o perlio.o perlapi.o numeric.o mathoms.o locale.o pp_pack.o pp_sort.o   -lnsl -ldl -lm -lcrypt -lutil -lc 
/tmp/ccn4nr6G.ltrans6.ltrans.o: In function `Perl_sv_usepvn_flags':
ccn4nr6G.ltrans6.o:(.text+0x19c5): undefined reference to `malloc_size'
ccn4nr6G.ltrans6.o:(.text+0x1a8b): undefined reference to `malloc_size'
/tmp/ccn4nr6G.ltrans9.ltrans.o: In function `Perl_find_script':
ccn4nr6G.ltrans9.o:(.text+0x453f): undefined reference to `strlcpy'
/tmp/ccn4nr6G.ltrans9.ltrans.o: In function `Perl_more_bodies':
ccn4nr6G.ltrans9.o:(.text+0x73b7): undefined reference to `malloc_good_size'
/tmp/ccn4nr6G.ltrans25.ltrans.o: In function `Perl_sv_grow':
ccn4nr6G.ltrans25.o:(.text+0x172b): undefined reference to `malloc_size'
ccn4nr6G.ltrans25.o:(.text+0x1760): undefined reference to `malloc_size'
/tmp/ccn4nr6G.ltrans4.ltrans.o: In function `Perl_av_extend':
ccn4nr6G.ltrans4.o:(.text+0x721): undefined reference to `malloc_size'
/tmp/ccn4nr6G.ltrans28.ltrans.o: In function `Perl_magic_set':
ccn4nr6G.ltrans28.o:(.text+0x1ca3): undefined reference to `setproctitle'
ccn4nr6G.ltrans28.o:(.text+0x2659): undefined reference to `setrgid'
ccn4nr6G.ltrans28.o:(.text+0x26d8): undefined reference to `setruid'
collect2: ld returned 1 exit status
make: *** [miniperl] Fehler 1

This is caused by Configure thinking I have malloc_size, strlcpy, etc. when I
do not.

I was able to fix this problem with the following patch:

--- perl-5.16.0-RC1-orig/Configure	2012-04-25 02:18:30.000000000 +0200
+++ perl-5.16.0-RC1/Configure	2012-05-15 08:11:20.000000000 +0200
@@ -7782,13 +7782,13 @@
 		if $contains $tlook $tf >/dev/null 2>&1; then
 			tval=true;
 		elif $test "$mistrustnm" = compile -o "$mistrustnm" = run; then
-			echo "$extern_C void *$1$tdc; void *(*(p()))$tdc { return &$1; } int main() { if(p()) return(0); else return(1); }"> try.c;
+			echo "$extern_C void *$1$tdc; void *(*(p()))$tdc { return &$1; } int main() { if(p() && p() != (void *)main) return(0); else return(1); }"> try.c;
 			$cc -o try $optimize $ccflags $ldflags try.c >/dev/null 2>&1 $libs && tval=true;
 			$test "$mistrustnm" = run -a -x try && { $run ./try$_exe >/dev/null 2>&1 || tval=false; };
 			$rm_try;
 		fi;
 	else
-		echo "$extern_C void *$1$tdc; void *(*(p()))$tdc { return &$1; } int main() { if(p()) return(0); else return(1); }"> try.c;
+		echo "$extern_C void *$1$tdc; void *(*(p()))$tdc { return &$1; } int main() { if(p() && p() != (void *)main) return(0); else return(1); }"> try.c;
 		$cc -o try $optimize $ccflags $ldflags try.c $libs >/dev/null 2>&1 && tval=true;
 		$rm_try;
 	fi;


And this is what happened (I think):

Configure thinks I have these symbols because I'm building with -O2 -flto.
"lto" stands for "link-time optimization" and lets gcc apply optimizations in
the linking phase.
The configure test effectively checks whether &foo is NULL. gcc knows that the
address of a symbol can't be NULL, so the test is optimized out.
But that was the only reference to 'p' in the program, so dead code elimination
removes it (and with it the only reference to 'foo').
The program is now effectively 'int main() { return 0; }', so linking succeeds.
End result: Configure thinks the symbols strlcpy, malloc_size, etc exist.

My patch makes it actually check the address so gcc can't get rid of it
completely, thus Configure gets the linker errors it expects.

Comments?

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About