everything you ever wanted to know about “hello, world”*€¦ · compiler runtime support file...
TRANSCRIPT
![Page 1: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/1.jpg)
Approved for public release; distribution is unlimited. This research is sponsored by the Defense Advanced Research Projects Agency (DARPA) and the Air Force Research Laboratory (AFRL), under contract FA8750-10-C-0237. The views, opinions, and/or findings contained in this article/presentation are those of the author(s)/presenter(s) and should not be interpreted as representing the official views or policies of the Department of Defense or the U.S. Government.
Everything you ever wanted to know about “hello, world”*
(*but were afraid to ask)
Brooks Davis SRI International
June 10, 2016 BSDCan 2016
![Page 2: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/2.jpg)
K&R: The C Programming Language
2
#include<stdio.h>main(){printf("hello,world\n");}
![Page 3: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/3.jpg)
K&R: The C Programming Language
3
#include<stdio.h>voidmain(void){printf("hello,world\n");}
![Page 4: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/4.jpg)
Today’s version
4
intmain(void){constcharhello[]="HelloWorld!";printf("%s%d\n",hello,123);return(0);}
![Page 5: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/5.jpg)
Minimal C version
5
voidmain(void){constchar*hello[]=“hello,world\n”;write(1,hello,sizeof(hello));exit(0);}
![Page 6: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/6.jpg)
Minimal (MIPS) assembly version
6
.text.global__start.ent__start__start:li$a0,1dla$a1,helloli$a2,12li$v0,4syscall#write(1,"hello,world\n",13)li$a0,0li$v0,1syscall#exit(0).end__start.datahello:.ascii"hello,world\n"
![Page 7: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/7.jpg)
Size comparison
• Assembly
• Compiles to 9 instructions
• Stripped binary less than 1K
• Mostly ELF headers, MIPS ABI bits
• Minimal C
• Stripped binary over 550K!
• Mostly malloc() and localization
7
![Page 8: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/8.jpg)
Program linkage
$cc-static-ohelloworldhelloworld.o
$ld-EB-melf64btsmip_fbsd-Bstatic\-ohelloworld/usr/lib/crt1.o\/usr/lib/crti.o/usr/lib/crtbeginT.o\-L/usr/libhelloworld.o\--start-group-lgcc-lgcc_eh–lc--end-group\/usr/lib/crtend.o/usr/lib/crtn.o
8
![Page 9: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/9.jpg)
Compiler runtime support File Purpose
crt1.o Contains __start() function which initializes process environment and calls main().
crti.o Entry points for old style _init() and _fini() functions.
crtbegin.ocrtbeginS.ocrtbeginT.o
Declares .ctor and .dtor constructor and destructor sections. Declares functions to call constructors and destructors.
crtend.o NULL terminates .ctor and .dtor sections.
crtn.o Trailers for _init() and _fini() functions.
9
Built in gnu/lib/csu and lib/csu/ARCH.
![Page 10: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/10.jpg)
Code and images online
12
https://people.freebsd.org/~brooks/talks/bsdcan2016-helloworld or
http://bit.ly/helloworld-talk
![Page 11: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/11.jpg)
execve()
13
![Page 12: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/12.jpg)
exec_copyin_args()
14
Allocate memory
Copy in program
path
![Page 13: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/13.jpg)
sys_execve()
15
![Page 14: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/14.jpg)
kern_execve()
16
namei()Resolve
path
exec_check_permissions()Check that the file has the right
permissions and open it.
exec_map_first_page()Map the header into kernel
memory.
![Page 15: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/15.jpg)
exec_elf64_imgact()
17
![Page 16: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/16.jpg)
exec_new_vmspace()
18
pmap_remove_pages()vm_map_remove()
Evict all page mappings from the address space
vm_map_stack()Map a stack into the
addres space
Stack
![Page 17: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/17.jpg)
exec_elf64_imgact()
19
Stack
elf_load_section()Map .textsection into
memory
.text
elf_load_section()Map .datasection into memory and create bss
.data bss
![Page 18: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/18.jpg)
kern_execve()
20
exec_copyout_strings()elf64_freebsd_fixup()Copy argv, envp, etc to the stack and adjust stack pointer.
exec_setregs()Set initial register context to
entry __start().
Stack .text .data bss
![Page 19: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/19.jpg)
sys_execve()
21
Stack .text .data bss
![Page 20: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/20.jpg)
Returning to userspace
• Stack is mapped into address space
• Program is mapped into address space
• Strings, argv, envp, signal handler, etc are on the top of the stack
• Register state is set up to call __start()
22
Stack .text .data bss
![Page 21: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/21.jpg)
SCO i386 ABI stack
23
rtld path
argc
argv[]
environ[]
sigcode
canary
pagesizes array
ELF auxargs
arg and env
strings
ps_strings
SP
__start(char**ap,…){...
argc=*(long*)ap;argv=ap+1;env=ap+2+argc;...
![Page 22: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/22.jpg)
__start()
24
Most cycles spent in malloc()
![Page 23: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/23.jpg)
__start()1/2
25
void__start(char**ap){intargc;char**argv,**env;argc=*(long*)ap;argv=ap+1;env=ap+2+argc;…
![Page 24: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/24.jpg)
__start()2/2
26
…handle_argv(argc,argv,env);_init_tls();handle_static_init(argc,argv,env);exit(main(argc,argv,env));}
Set environ and __progname variables.
![Page 25: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/25.jpg)
_init_tls()
27
Most cycles spent in malloc()
![Page 26: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/26.jpg)
Elf_Addr*sp;sp=(Elf_Addr*)environ;while(*sp++!=0);aux=(Elf_Auxinfo*)sp;
_init_tls()
• Find the ELF auxargs vector
• Use that to find the program headers
• Use those to find the PT_TLS section (initial values)
• Call __libc_allocate_tls() (as _rtld_allocate_tls())
• Allocates space
• Copies initial values
• Set the TLS pointer
28
Uses JEMalloc, but JEMalloc uses TLS!
![Page 27: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/27.jpg)
__start()2/2
29
…handle_argv(argc,argv,env);_init_tls();handle_static_init(argc,argv,env);exit(main(argc,argv,env));}
Calls constructors and registers destructors. Four types supported:
• .pre_init_array section• _init() function • .ctors section (via _init()) • .init_array section
![Page 28: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/28.jpg)
main()
30
![Page 29: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/29.jpg)
vfprintf()
31
![Page 30: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/30.jpg)
__get_locale()
32
![Page 31: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/31.jpg)
vfprintf()
33
![Page 32: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/32.jpg)
__vfprintf()
34
![Page 33: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/33.jpg)
__vfprintf()
35
(“%s”, hello) (“ %d”, 123)
(“\n”) Look up decimal point string.
![Page 34: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/34.jpg)
__sprint()
36
New-line character found.
![Page 35: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/35.jpg)
__flush()
37
The actual call to write()
![Page 36: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/36.jpg)
38
HelloWorld!123
![Page 37: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/37.jpg)
__start()
39
![Page 38: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/38.jpg)
exit()
40
Call destructors registered with atexit()
Flush any unflushed FILEs
Call _exit()
![Page 39: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/39.jpg)
Dynamic binary
41
_rtld_relocate_nonplt_self()Rtld relocates itself __start()
Stack .text .data bss rtld libc
Load and relocate libc
![Page 40: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/40.jpg)
__start()
42
![Page 41: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/41.jpg)
printf()
43
![Page 42: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/42.jpg)
_mips_rtld_bind()
44
![Page 43: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/43.jpg)
printf()
45
![Page 44: Everything you ever wanted to know about “hello, world”*€¦ · Compiler runtime support File Purpose crt1.o Contains __start() function which initializes process environment](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06db127e708231d41a1345/html5/thumbnails/44.jpg)
Feedback re quested
• Was the talk interesting and/or helpful?
• What didn’t make sense?
• What would you like have learned more (or less) about?
• http://bit.ly/bsdcan16-helloworld
46