[SOLVED] Re: statically linked PIEs without GOT

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxx>



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux