0

i want read ebp address to ebg_reg, and to offset +4 +8, archive argc and argv. when i compiler on macOS use gcc version 13.0.0.

gcc -c -fno-builtin -nostdlib -fno-stack-protector entry.c malloc.c stdio.c string.c printf.c

compiler failed:

entry.c:51:9: error: invalid operand for instruction
    asm("movl %%ebp, %0 \n":"=r"(ebp_reg));
        ^
<inline asm>:1:13: note: instantiated into assembly here
        movl %ebp, %rax 
void test(void) 
{

    int ret;
    int argc = 0;
    char **argv;
    char *ebp_reg = 0;
    asm("movl %%ebp, %0 \n":"=r"(ebp_reg));
    argc = *(int *)(ebp_reg + 4);
    argv = (char **)(ebp_reg + 8);
    
}
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
刘松洪
  • 1
  • 1
  • Pointers are 64-bit in x86-64, so GCC picked 64-bit RAX for `char *ebp_reg`. Since this is 64-bit code, that's also not where argc and argv will be; they were passed in registers, not on the stack. A debug build would spill them somewhere if you declared them as args, but not to any standardized location. – Peter Cordes Dec 22 '21 at 03:08
  • Also, you must be using clang; GCC 13 doesn't exist yet, but MacOS installs clang as `gcc` in your PATH. – Peter Cordes Dec 22 '21 at 03:10
  • Related for when you fix this surface-level problem and get to deeper problems: [Clang : getting a function's arguments through inline assembly](https://stackoverflow.com/q/63929201). Also [How to pass function parameters into inline assembly blocks without assigning them to register variables in c++](https://stackoverflow.com/q/58901121) – Peter Cordes Dec 22 '21 at 03:19
  • Almost a duplicate of [movl on x64 with GCC inline](https://stackoverflow.com/q/22433289), but really you should be copying the whole frame pointer, RBP. Or just making 32-bit code with `-m32`, which you can't do on modern MacOS. – Peter Cordes Dec 22 '21 at 03:24
  • i can't fully understand your commit(beyond capability for me). thank you Peter Cordes – 刘松洪 Dec 22 '21 at 06:14
  • Your on x86-64 MacOS, so `gcc` defaults to `-m64`. Your code as written only works for `-m32`, a 32-bit build. If you don't understand x86-64 assembly and the integer registers, don't learn it via *inline* asm; that's really hard to use and requires understanding asm as well as how compilers think to be able to correctly describe your asm template to the compiler. e.g. that both operands to `mov` have to be the same size. – Peter Cordes Dec 22 '21 at 06:22
  • -m32 solve my problem, thank a lot! Peter Cordes – 刘松洪 Dec 22 '21 at 10:17
  • That's why I closed it as a duplicate of [How to compile gcc 32bit app on 64bit osx](https://stackoverflow.com/q/20059636) before you even commented. :/ – Peter Cordes Dec 22 '21 at 20:54
  • because of apple can not support 32 bit arch about 2018 。 macOS High Sierra 10.13.4。 is it right? – 刘松洪 Dec 23 '21 at 07:16
  • when i compiler myself minicrt.a: step 1 gcc -m32 -c -fno-builtin -nostdlib -fno-stack-protector entry.c malloc.c stdio.c string.c printf.c and ar -rs minicrt.a malloc.o printf.o stdio.o string.o step 2 gcc -m32 -c -ggdb -fno-builtin -nostdlib -fno-stack-protector test.c step 3 ld -static -e mini_crt_entry entry.o test.o minicrt.a -o test ld: warning: ignoring file minicrt.a, building for free standing-i386 but attempting to link with file built for macOS-i386 error : ld: symbol(s) not found for architecture i386 i real konw your describle can't do on modern MacOS. – 刘松洪 Dec 23 '21 at 07:19
  • IIRC, MacOS didn't fully remove 32-bit support until 10.15 Catalina. But I don't use a Mac myself. From your error message, ld is complaining about `minicrt.a` having a different target name, `macOS-i386` instead of `free standing-i386`. Have you tried not using `ar`, and just linking all the loose `.o` files? Also, don't you need an `ld` option to tell it to link a 32-bit executable instead of the default 64-bit? You should probably ask a separate question, if there isn't a duplicate already about that error message. – Peter Cordes Dec 23 '21 at 07:20
  • em, i try tell ld 32-bit .but it tell me the same error. i think the problem is macOS-i386 is different standing-i386. but i try gcc -arch macOS-i386 -c -ggdb -fno-builtin -nostdlib -fno-stack-protector test.c it tell clang: error: invalid arch name '-arch macOS-i386'. it is other question. i try ask a separate question. my english expression is not very good, but you still answer patiently。 thank again peter – 刘松洪 Dec 23 '21 at 07:49

0 Answers0