public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Peng Yu <pengyu.ut@gmail.com>
To: libc-help <libc-help@sourceware.org>
Subject: minimal loader program in C
Date: Thu, 29 Apr 2021 08:58:47 -0500	[thread overview]
Message-ID: <CABrM6wmH3QJj=SOM5Rd9zmTHZY3mad=pzv6e0qr87Oy+n31r2g@mail.gmail.com> (raw)
In-Reply-To: <CABrM6wnFdprziCNdj7PYzd7S1XAxw0jNT6oEBJew6+2PhLg45w@mail.gmail.com>

I see the following system calls are used when a program is run. I'd
like to understand how this process is done in detail. Is there a
minimal loader C program that just covers the basics of loading this
simple program?

$ cat main.c
int main() { return 0; }
$ gcc main.c
$ strace ./a.out
execve("./a.out", ["./a.out"], 0x7ffe586d5e60 /* 50 vars */) = 0
brk(NULL)                               = 0x562f27c1c000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=116056, ...}) = 0
mmap(NULL, 116056, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f71ab750000
close(3)                                = 0
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@n\2\0\0\0\0\0"...,
832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1839792, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0x7f71ab74e000
mmap(NULL, 1852680, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f71ab589000
mprotect(0x7f71ab5ae000, 1662976, PROT_NONE) = 0
mmap(0x7f71ab5ae000, 1355776, PROT_READ|PROT_EXEC,
MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x25000) = 0x7f71ab5ae000
mmap(0x7f71ab6f9000, 303104, PROT_READ,
MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x170000) = 0x7f71ab6f9000
mmap(0x7f71ab744000, 24576, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1ba000) = 0x7f71ab744000
mmap(0x7f71ab74a000, 13576, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f71ab74a000
close(3)                                = 0
arch_prctl(ARCH_SET_FS, 0x7f71ab74f540) = 0
mprotect(0x7f71ab744000, 12288, PROT_READ) = 0
mprotect(0x562f2789d000, 4096, PROT_READ) = 0
mprotect(0x7f71ab797000, 4096, PROT_READ) = 0
munmap(0x7f71ab750000, 116056)          = 0
exit_group(0)                           = ?
+++ exited with 0 +++

--
Regards,
Peng

       reply	other threads:[~2021-04-29 13:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CABrM6wnFdprziCNdj7PYzd7S1XAxw0jNT6oEBJew6+2PhLg45w@mail.gmail.com>
2021-04-29 13:58 ` Peng Yu [this message]
2021-05-02 22:46   ` Peng Yu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABrM6wmH3QJj=SOM5Rd9zmTHZY3mad=pzv6e0qr87Oy+n31r2g@mail.gmail.com' \
    --to=pengyu.ut@gmail.com \
    --cc=libc-help@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).