Use -fPIC when compiling lua

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Use -fPIC when compiling lua

Aki Tuomi
Please modify your Makefile for lua to use -fPIC. This is required to link
static LUA library to any other stuff that wants to build position
independent code.

Kind regards,
Aki Tuomi

>From 27d18f6aa78de0257f1e1764fc48ad45fab83573 Mon Sep 17 00:00:00 2001
From: Aki Tuomi <[hidden email]>
Date: Tue, 16 Dec 2014 09:49:43 +0200
Subject: [PATCH] Use fPIC

---
 src/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/Makefile b/src/Makefile
index 7b4b2b7..a5ec275 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -7,7 +7,7 @@
 PLAT= none

 CC= gcc
-CFLAGS= -O2 -Wall -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS)
+CFLAGS= -O2 -Wall -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS) -fPIC
 LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS)
 LIBS= -lm $(SYSLIBS) $(MYLIBS)

--
1.9.1


Reply | Threaded
Open this post in threaded view
|

Re: Use -fPIC when compiling lua

Rob Kendrick-2
On Tue, Dec 16, 2014 at 09:54:34AM +0200, Aki Tuomi wrote:
> Please modify your Makefile for lua to use -fPIC. This is required to link
> static LUA library to any other stuff that wants to build position
> independent code.

The shipped build system does not build shared libraries; the
recommendation is always to statically link.  It's left up to packagers
to change the build system to produce shared libraries and such if they
wish.

I suspect the recommendation in your case will be to simply add the Lua
sources to your own project.

B.

Reply | Threaded
Open this post in threaded view
|

Re: Use -fPIC when compiling lua

Aki Tuomi
On Tue, Dec 16, 2014 at 09:21:43AM +0000, Rob Kendrick wrote:

> On Tue, Dec 16, 2014 at 09:54:34AM +0200, Aki Tuomi wrote:
> > Please modify your Makefile for lua to use -fPIC. This is required to link
> > static LUA library to any other stuff that wants to build position
> > independent code.
>
> The shipped build system does not build shared libraries; the
> recommendation is always to statically link.  It's left up to packagers
> to change the build system to produce shared libraries and such if they
> wish.
>
> I suspect the recommendation in your case will be to simply add the Lua
> sources to your own project.
>
> B.
>

This is required for static libaries as well, when linking them to a PIC
binary.

Aki

Reply | Threaded
Open this post in threaded view
|

Re: Use -fPIC when compiling lua

Luiz Henrique de Figueiredo
In reply to this post by Aki Tuomi
> -CFLAGS= -O2 -Wall -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS)
> +CFLAGS= -O2 -Wall -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS) -fPIC

The Makefile supports variables MYCFLAGS, MYLDFLAGS, MYLIBS, MYOBJS.
In your case, just do "make linux MYCFLAGS=-fPIC".

Reply | Threaded
Open this post in threaded view
|

Re: Use -fPIC when compiling lua

Ryan
I would recommend this either be the default or noted on the download/build instructions page.  I know you can simply add MYCFLAGS but people trying to build for a 64-bit software (like I was a couple weeks ago) might not know about this.  Just adding the line "If you are trying to build for 64-bit compatible software you will need to specify that by adding "MYCFLAGS=-fPIC" on the end of the build script.

On Tue, Dec 16, 2014 at 5:21 AM, Luiz Henrique de Figueiredo <[hidden email]> wrote:
> -CFLAGS= -O2 -Wall -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS)
> +CFLAGS= -O2 -Wall -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS) -fPIC

The Makefile supports variables MYCFLAGS, MYLDFLAGS, MYLIBS, MYOBJS.
In your case, just do "make linux MYCFLAGS=-fPIC".

Reply | Threaded
Open this post in threaded view
|

Re: Use -fPIC when compiling lua

Rob Kendrick-2
On Tue, Dec 16, 2014 at 05:01:11PM -0500, iggyvolz . wrote:
> I would recommend this either be the default or noted on the download/build
> instructions page.  I know you can simply add MYCFLAGS but people trying to
> build for a 64-bit software (like I was a couple weeks ago) might not know
> about this.  Just adding the line "If you are trying to build for 64-bit
> compatible software you will need to specify that by adding
> "MYCFLAGS=-fPIC" on the end of the build script.

While -fPIC is almost always want you want when building and linking
position-independant code, it is also almost never the right thing to
issue when you don't.

So no, this shouldn't be the default.  People who know they need it are
in a position to add it.

B.

Reply | Threaded
Open this post in threaded view
|

Re: Use -fPIC when compiling lua

Jay Carlson

Eye roll. -fPIC is always safe on SVR4 descendents.

If you want to have a flame war about CFLAGS, I suggest over declaring reentrancy for libc.

On Dec 16, 2014 6:49 PM, "Rob Kendrick" <[hidden email]> wrote:
On Tue, Dec 16, 2014 at 05:01:11PM -0500, iggyvolz . wrote:
> I would recommend this either be the default or noted on the download/build
> instructions page.  I know you can simply add MYCFLAGS but people trying to
> build for a 64-bit software (like I was a couple weeks ago) might not know
> about this.  Just adding the line "If you are trying to build for 64-bit
> compatible software you will need to specify that by adding
> "MYCFLAGS=-fPIC" on the end of the build script.

While -fPIC is almost always want you want when building and linking
position-independant code, it is also almost never the right thing to
issue when you don't.

So no, this shouldn't be the default.  People who know they need it are
in a position to add it.

B.

Reply | Threaded
Open this post in threaded view
|

Re: Use -fPIC when compiling lua

Rob Kendrick-2
On Wed, Dec 17, 2014 at 01:06:47AM -0500, Jay Carlson wrote:
> Eye roll. -fPIC is always safe on SVR4 descendents.

This is a very small proportion of operating systems out there.

B.

Reply | Threaded
Open this post in threaded view
|

Re: Use -fPIC when compiling lua

Daniel Silverstone
In reply to this post by Jay Carlson
On Wed, Dec 17, 2014 at 01:06:47 -0500, Jay Carlson wrote:
> Eye roll. -fPIC is always safe on SVR4 descendents.

It's also almost always slower than non PIC code on all but x86_32.  Unless
you're building a shared library or a position independent executable, don't
use -fPIC.

Also, for reference, -fpic and -fPIC have different meanings on at least three
architectures so be careful with which you mean and when.

D.

--
Daniel Silverstone                         http://www.digital-scurf.org/
PGP mail accepted and encouraged.            Key Id: 3CCE BABE 206C 3B69

Reply | Threaded
Open this post in threaded view
|

Re: Use -fPIC when compiling lua

Jay Carlson
On Dec 17, 2014, at 5:28 AM, Daniel Silverstone <[hidden email]> wrote:

> On Wed, Dec 17, 2014 at 01:06:47 -0500, Jay Carlson wrote:
>> Eye roll. -fPIC is always safe on SVR4 descendents.
>
> It's also almost always slower than non PIC code on all but x86_32.  Unless
> you're building a shared library or a position independent executable, don't
> use -fPIC.

In God we trust. All else bring math.

> Also, for reference, -fpic and -fPIC have different meanings on at least three
> architectures so be careful with which you mean and when.

Yes, I am aware[1]. Saying things like "use -fpic unless you overflow the GOT; then use -fPIC" leads to head-shaking at the build engineer[2] and then terrible ideas like "shared libraries are hard, link statically."

Especially with modern Lua minimizing external symbols, I don't see a problem with taking Sun's position: "life is short, use -fPIC" and SPARC is probably hit the worst in object code by big-GOT. (Embedded MIPS is hit the worst by everything else.)

Jay

[1]: http://lua-users.org/lists/lua-l/2012-03/msg00389.html
[2]: Build engineer? We're talking about what, you me and Enrico?
Reply | Threaded
Open this post in threaded view
|

Re: Use -fPIC when compiling lua

Ricardo Ramos Massaro
On Fri, Dec 19, 2014 at 12:32 AM, Jay Carlson <[hidden email]> wrote:
On Dec 17, 2014, at 5:28 AM, Daniel Silverstone <[hidden email]> wrote:
> It's also almost always slower than non PIC code on all but x86_32.  Unless
> you're building a shared library or a position independent executable, don't
> use -fPIC.

In God we trust. All else bring math.

 I got curious, so I did a little experiment (Lua 5.2.3, Linux/x86_64). For whatever it's worth:

**** without -fPIC
time ./lua -e 'for i=0,1e9 do x=i+1 end'
real 0m33.197s
user 0m33.124s
sys 0m0.036s


**** with -fPIC
time ./lua -e 'for i=0,1e9 do x=i+1 end'
real 0m36.279s
user 0m36.173s
sys 0m0.068s

So -fPIC makes this little test about 10% slower on my machine.

-Ricardo