public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* statically linked PIEs without GOT
@ 2012-07-11 14:06 Morten Shearman Kirkegaard
  2012-07-11 14:37 ` Ian Lance Taylor
  2012-07-12 19:09 ` [SOLVED] " Morten Shearman Kirkegaard
  0 siblings, 2 replies; 4+ messages in thread
From: Morten Shearman Kirkegaard @ 2012-07-11 14:06 UTC (permalink / raw)
  To: gcc-help

Dear GCC Experts,

I have a somewhat strange request. I would like to generate statically
linked position independent executables for I386 and AMD64. The source
language is C, I use ELF for the object files, and link them using GNU
ld with a custom linker script.

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.

Is there an option to avoid using a GOT?


----- example source code
extern 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 would like
get_var:
        pushl   %ebp
        movl    %esp, %ebp
        call    __x86.get_pc_thunk.cx
        movl    var-.(%ecx), %eax
        popl    %ebp
        ret

 Offset     Info    Type            Sym.Value  Sym. Name
00000004  00000702 R_386_PC32        00000000   __x86.get_pc_thunk.cx
0000000a  00000802 R_386_PC32        00000000   var
----- end


Kind regards,
Morten

-- 
Morten Shearman Kirkegaard <moki@fabletech.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: statically linked PIEs without GOT
  2012-07-11 14:06 statically linked PIEs without GOT Morten Shearman Kirkegaard
@ 2012-07-11 14:37 ` Ian Lance Taylor
  2012-07-11 17:22   ` Morten Shearman Kirkegaard
  2012-07-12 19:09 ` [SOLVED] " Morten Shearman Kirkegaard
  1 sibling, 1 reply; 4+ messages in thread
From: Ian Lance Taylor @ 2012-07-11 14:37 UTC (permalink / raw)
  To: Morten Shearman Kirkegaard; +Cc: gcc-help

On Wed, Jul 11, 2012 at 7:06 AM, Morten Shearman Kirkegaard
<moki@fabletech.com> wrote:
>
> I have a somewhat strange request. I would like to generate statically
> linked position independent executables for I386 and AMD64. The source
> language is C, I use ELF for the object files, and link them using GNU
> ld with a custom linker script.
>
> 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.
>
> Is there an option to avoid using a GOT?

Such a thing could be supported in principle, by including a runtime
relocation routine in the executable itself.  However, I'm not aware
of any current support for this.

Ian

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: statically linked PIEs without GOT
  2012-07-11 14:37 ` Ian Lance Taylor
@ 2012-07-11 17:22   ` Morten Shearman Kirkegaard
  0 siblings, 0 replies; 4+ messages in thread
From: Morten Shearman Kirkegaard @ 2012-07-11 17:22 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

Hi Ian,

Thanks for your reply.

On Wed, 2012-07-11 at 07:36 -0700, Ian Lance Taylor wrote:
> On Wed, Jul 11, 2012 at 7:06 AM, Morten Shearman Kirkegaard
> <moki@fabletech.com> wrote:
...
> > Is there an option to avoid using a GOT?
> 
> Such a thing could be supported in principle, by including a runtime
> relocation routine in the executable itself.  However, I'm not aware
> of any current support for this.

Okay. I will see if I can find an alternative way of doing it.

Kind regards,
Morten

-- 
Morten Shearman Kirkegaard <moki@fabletech.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [SOLVED] Re: statically linked PIEs without GOT
  2012-07-11 14:06 statically linked PIEs without GOT Morten Shearman Kirkegaard
  2012-07-11 14:37 ` Ian Lance Taylor
@ 2012-07-12 19:09 ` Morten Shearman Kirkegaard
  1 sibling, 0 replies; 4+ messages in thread
From: Morten Shearman Kirkegaard @ 2012-07-12 19:09 UTC (permalink / raw)
  To: gcc-help

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>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-07-12 19:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-11 14:06 statically linked PIEs without GOT Morten Shearman Kirkegaard
2012-07-11 14:37 ` Ian Lance Taylor
2012-07-11 17:22   ` Morten Shearman Kirkegaard
2012-07-12 19:09 ` [SOLVED] " Morten Shearman Kirkegaard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).