This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
[SOLVED] Re: statically linked PIEs without GOT
- From: Morten Shearman Kirkegaard <moki at fabletech dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 12 Jul 2012 21:09:11 +0200
- Subject: [SOLVED] Re: statically linked PIEs without GOT
- References: <1342015572.7031.15.camel@pacific>
On Wed, 2012-07-11 at 16:06 +0200, Morten Shearman Kirkegaard wrote:
> The code generated by GCC with -fpic uses the Global Offset Table to
> resolve external symbols. Since my program will be statically linked, I
> would like to avoid this, to simplify loading. All symbols will be
> resolvable at link time.
I have solved the problem, by specifying visibility=hidden for the
external symbol.
> ----- example source code
> extern int var;
>
> int get_var(void)
> {
> return var;
> }
> ----- end
----- example source code
extern __attribute__((visibility("hidden"))) int var;
int get_var(void)
{
return var;
}
----- end
> ----- what I currently get
> get_var:
> pushl %ebp
> movl %esp, %ebp
> call __x86.get_pc_thunk.cx
> addl $_GLOBAL_OFFSET_TABLE_, %ecx
> movl var@GOT(%ecx), %eax
> movl (%eax), %eax
> popl %ebp
> ret
>
> Offset Info Type Sym.Value Sym. Name
> 00000004 00000b02 R_386_PC32 00000000 __x86.get_pc_thunk.cx
> 0000000a 00000c0a R_386_GOTPC 00000000 _GLOBAL_OFFSET_TABLE_
> 00000010 00000d03 R_386_GOT32 00000000 var
> ----- end
----- what I have now
get_var:
pushl %ebp
movl %esp, %ebp
call __x86.get_pc_thunk.cx
addl $_GLOBAL_OFFSET_TABLE_, %ecx
movl var@GOTOFF(%ecx), %eax
popl %ebp
ret
Offset Info Type Sym.Value Sym. Name
00000004 00000b02 R_386_PC32 00000000 __x86.get_pc_thunk.cx
0000000a 00000c0a R_386_GOTPC 00000000 _GLOBAL_OFFSET_TABLE_
00000010 00000d09 R_386_GOTOFF 00000000 var
----- end
Now, all relocations can be handled at link time, which allows the code
to be fully position independent.
Kind regards,
Morten
--
Morten Shearman Kirkegaard <moki@fabletech.com>