2

I am trying to learn more about strace. I use strace to understand the calls a c program makes. The c program is:

void
_start ()
{
  for (;;)
    {
    }
}

The program gets compiled with

clang -nostdlib littletest.c

The system calls are traced with

strace ./a.out

The expected output is. Source at 3:55

execve("./a.out", ["./a.out"], 0x7ffdf74a7720 /* 42 vars */) = 0 

The actual output is

execve("./a.out", ["./a.out"], 0x7ffdf74a7720 /* 42 vars */) = 0
brk(NULL)                               = 0x55a722f48000
arch_prctl(0x3001 /* ARCH_??? */, 0x7fff7444a540) = -1 EINVAL (Invalid argument)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f85ea9b2000
arch_prctl(ARCH_SET_FS, 0x7f85ea9b2b00) = 0
set_tid_address(0x7f85ea9b2dd0)         = 10888
set_robust_list(0x7f85ea9b2de0, 24)     = 0
rseq(0x7f85ea9b34a0, 0x20, 0, 0x53053053) = 0
mprotect(0x55a722e3c000, 4096, PROT_READ) = 0

Why there are more than one system-call?

Edit1:

clang -nostdlib -static littletest.c

Produces the expected output. Thank you @ilkkachu

Edit2:

clang --version

clang version 13.0.1

Edit3:
@Stephen Kitt gets the expected output without adding "-static". His clang version is also 13.0.1.

  • `ld.so.preload` points at the dynamic linker, and there's other memory juggling there too that doesn't seem out of place for it. On the system I tried, clang compiled that into a static executable, which didn't do those calls, but gcc made a dynamic one that did. Might depend on the compiler version too. Try with `clang -static -nostdlib littletest.c` to see if it changes? – ilkkachu Jul 12 '22 at 11:54
  • adding -static produces the output I expected. Thank you. – Koronis Neilos Jul 12 '22 at 11:57
  • That’s intriguing, I’m using clang 13.0.1 as well and I get the expected result without using `-static` (but the binary produced *is* static). – Stephen Kitt Jul 12 '22 at 12:03
  • 2
    To be honest I do not understand why I have to add -static if I link against nothing. Also I can not find documentation for -static on "https://clang.llvm.org/docs/ClangCommandLineReference.html" – Koronis Neilos Jul 12 '22 at 12:09
  • @KoronisNeilos, it's mentioned there, under "Linker flags", near the end. But that page seems to missing actual descriptions of a lot of flags, not just that one... Then again, if they nicked the names of the command line options from GCC, we could guess it does pretty much the same thing as there?? (That's the angle I got to it from anyway...) – ilkkachu Jul 12 '22 at 16:05

0 Answers0