Typo in IsLegalToCallImmediateAddr?

It seems that the || should be && here?

/// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
/// to immediate address.
bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
  if (Is64Bit)
    return false;
  return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
}

For example, if you are doing ELF PIC (i.e. for a shared library), it
is not valid to use a call immediate.

- David

I think that on 32 bits it is legal, just not profitable.

I tried compiling two .c files without -fPIC:

Rafael,

I believe your example is not related to IsLegalToCallImmediateAddr.

This is an example of calling to an immediate address:

typedef int (*funcptr)(void);

int main() {
  funcptr foo = (funcptr)0x100;
  foo();
}

If IsLegalToCallImmedateAddr is true, this generates a call to
absolute address 0x100:

call 0x100

This requires a relocation of the value 0x100 - PC.
(NOTE: this is NOT the same as: "foo: call foo+0x100", which requires
no relocation)

This can't be done correctly in PIC mode. If you do this in a shared
library, the relocation is ignored and you get a jump to module_start
+ 0x100 instead of absolute 0x100.

- pdox

Rafael,

I believe your example is not related to IsLegalToCallImmediateAddr.

This is an example of calling to an immediate address:

typedef int (*funcptr)(void);

int main() {
funcptr foo = (funcptr)0x100;
foo();
}

If IsLegalToCallImmedateAddr is true, this generates a call to
absolute address 0x100:

call 0x100

If so there is a bug, but I cannot reproduce it. I am getting:

   0: b8 00 01 00 00 mov $0x100,%eax
   5: ff e0 jmp *%eax

I have tried 142624.

- pdox

Cheers,
Rafael

Rafael,

Use this bitcode:

define i32 @main() nounwind {
entry:
  %call = tail call i32 inttoptr (i64 256 to i32 ()*)() nounwind
  ret i32 0
}

And this command:

$ llc -mtriple "i686-linux-gnu" test.ll -o test.s -filetype=asm
-relocation-model=pic

- pdox

And this command:

$ llc -mtriple "i686-linux-gnu" test.ll -o test.s -filetype=asm
-relocation-model=pic

I can reproduce it now. Sorry, I was using a test returning void and we
don't have tail call of immediate.

My impression in that the right fix would be to just remove the
"isTargetELF()". It would make the function correct for ELF and not less
correct for other formats, since it is already broken for a format with
no calls to immediate.

- pdox

Cheers,
Rafael

Rafael,

I believe MachO can't represent this relocation, even in non-PIC mode.
On my Mac, I tried compiling "call 256". I got:

in section __TEXT,__text reloc 0: R_ABS reloc but no absolute symbol
at target address

I believe the correct thing to do is:

isTargetELF() && TM.getRelocationModel() == Reloc::Static

This will do the right thing on ELF, and the right thing on other
formats. This may have been the original intent.

- pdox

2011/10/21 David Meyer <pdox@google.com>> Rafael,

I believe MachO can't represent this relocation, even in non-PIC mode.
On my Mac, I tried compiling "call 256". I got:

I think PIC is just the default (the kernel being non-PIC for
example), but I am not sure.

in section __TEXT,__text reloc 0: R_ABS reloc but no absolute symbol
at target address

I believe the correct thing to do is:

isTargetELF() && TM.getRelocationModel() == Reloc::Static

This will do the right thing on ELF, and the right thing on other
formats. This may have been the original intent.

Could be, echristo, bigcheese, would this be correct for Mach-O and COFF?

- pdox

Cheers,
Rafael

Could be, echristo, bigcheese, would this be correct for Mach-O and COFF?

bigcheese noted on IRC that the test crashes the COFF emitter. For some reason I am always getting

  movl $256, %eax ## imm = 0x100
  calll *%eax

on darwin already, so I guess you are right, the correct would be

  isTargetELF() && TM.getRelocationModel() == Reloc::Static;

Please include a test with the commit :slight_smile:

Cheers,
Rafael

IIRC, we never use Static on Darwin targets.

-Eli

Eli,

Hm. There's a test in (CodeGen/X86/call-imm.ll) which uses darwin with
relocation model static. It expects to use call-to-immediate.

Is this in error? Should I disable this check?

- pdox

IIRC the kernel uses relocation model as static.

-eric

Thought a bit more. There's also -mdynamic-no-pic. Not typically used these days, but is still there AFAIK.

-Jim

Remaining questions...

Is "call 1234" legal in X86-32 MachO Static? How about X86-32 COFF static?

Is the legality with dynamic-no-pic the same as with static?

- pdox