https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124027

--- Comment #7 from Pali Rohár <pali at kernel dot org> ---
Created attachment 64284
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=64284&action=edit
PE-COFF i386 -fpic support

In the attached change is my initial implementation of -fpic for Windows
PE-COFF i386 format. For the simple C code:

  extern int a;
  int func(void) { return a; }

It generates following asm code with -fpic option:

          .file   "test.c"
          .text
          .globl  _func
          .def    _func;  .scl    2;      .type   32;     .endef
  _func:
          pushl   %ebp
          movl    %esp, %ebp
          call    LPR0
  L3:
          leal    [_func-L3](%eax), %eax
          leal    [_a-_func](%eax), %eax
          movl    (%eax), %eax
          popl    %ebp
          ret
  LPR0:
          movl    (%esp), %eax
          ret

It uses the logic which I described in the issue description, just instead of
"." (current location) it generates new label, which references and then
beginning of the function is used as a fixed point for calculating all relative
differences.

When assembling this asm code, it produces COFF object file:

  test.o:     file format pe-i386

  Disassembly of section .text:

  00000000 <_func>:
     0:   55                      push   %ebp
     1:   89 e5                   mov    %esp,%ebp
     3:   e8 0d 00 00 00          call   15 <_func+0x15>
     8:   8d 40 f8                lea    -0x8(%eax),%eax
     b:   8d 80 11 00 00 00       lea    0x11(%eax),%eax
                          d: DISP32       _a
    11:   8b 00                   mov    (%eax),%eax
    13:   5d                      pop    %ebp
    14:   c3                      ret
    15:   8b 04 24                mov    (%esp),%eax
    18:   c3                      ret
    19:   90                      nop
    1a:   90                      nop
    1b:   90                      nop

And as can be seen, only relative DISP32 relocation is there which is
completely resolved and eliminated by GNU linker at the link time. So the final
code in DLL/EXE is fully position independent and does not need any runtime
relocation patching.


But there is a problem with this my change. When compiling with -O2, the
generated code is wrong:

          .file   "test.c"
          .text
          .p2align 4
          .globl  _func
          .def    _func;  .scl    2;      .type   32;     .endef
  _func:
  L2:
          call    LPR0
          movl    [_a-L2](%eax), %eax
          ret
  LPR0:
          movl    (%esp), %eax
          ret

It correctly optimized the block:

          leal    [_func-L3](%eax), %eax
          leal    [_a-_func](%eax), %eax
          movl    (%eax), %eax

Into the one instruction:

          movl    [_a-L2](%eax), %eax

Which is nice. But for some reasons the -O2 optimizer moves the L2 label and
break the code. The label needs to be immediately after the "call LPR0" as it
is used for relative offset address calculation (LPR0 returns EIP which refers
to the location _after_ the "call" instruction, not before).

In my change I used "emit_label_after (curloc_label, insn);" in
ix86_init_pic_reg() to explicitly put the L2 label after the insn (call LPR0),
but -O2 ignored it.

Any idea how to take the gen_rtx_LABEL_REF of the location immediately after
that LPR0 to fix this issue?

Reply via email to