0

If I understand clang assumes that the stack segment for x86 is flat (has 0 base). E.g., when compiling using the following command line:

clang -cc1 -S -mllvm --x86-asm-syntax=intel -o - -triple i986-unknown-unknown -mrelocation-model static xxx.c

xxx.c:

void f()
{
    int a = 5;
    int *ap = &a;
    int b = *ap;
}

the following assembly is produced:

f:
    sub     ESP, 12
    lea     EAX, DWORD PTR [ESP + 8]
    mov     DWORD PTR [ESP + 8], 5
    mov     DWORD PTR [ESP + 4], EAX
    mov     EAX, DWORD PTR [ESP + 4]
    mov     EAX, DWORD PTR [EAX]
    mov     DWORD PTR [ESP], EAX
    add     ESP, 12
    ret

This may only be correct if the stack is flat, because EAX contains the offset from SS base.

Is it possible to compile the C code for SS with an arbitrary base?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Dmitry K.
  • 1,145
  • 1
  • 9
  • 16
  • 1
    It might be useful to describe what you need this for, as I'm not sure what you are trying to achieve. – fuz Jan 18 '15 at 14:28

1 Answers1

1

When executing in protected mode, segment registers do not contain offsets you can use. Rather, each segment is independent of the others. As far as I know, no operating system in use today uses an ABI where pointers contain segment information and as you can take the address of any local variable (placed on the stack) this would be necessary. I don't think clang (or gcc for that matter) can compile code for such a model.

fuz
  • 88,405
  • 25
  • 200
  • 352