Skip to content
0:00 / 0:00

#linker#loader#dynamiclinking

CppCon 2018: Matt Godbolt “The Bits Between the Bits: How We Get to main()”

CppCon

189K subscribers • 103 videos on Hallway • Conference channel

YouTube
  • Recommended by 1 person: 1 in engineering communities.
  • Presented by Matt Godbolt, creator of Compiler Explorer
  • Linked in the descriptionCppCon/CppCon2018

Advanced. Deep dive for people who know the field.

207K views7 years ago1 recommendation

Systems programmingProgramming languagesCompilers and interpretersC++Operating systems

Matt Godbolt explains how the linker, loader, operating system, and C/C++ runtimes work together to set up and run a program's main function.

CppCon.org — Presentation Slides, PDFs, Source Code and other presenter materials are available at: github.com/CppCon/CppCon2018 — When you run your C++ code, have you ever considered how the linker, loader, operating system, C and C++ runtime all work so hard to get everything set up for you to start running your code in main()? In this Linux-focused talk, Matt will talk about how the linker stitches together your code and how that fits in with dynamic linking. He'll touch on debugging issues with the loader, and how ODR violations can manifest themselves. Then he'll take a look at what's going on behind the scenes to get the C runtime up, and then the C++ runtime, along with all the global object constructors - showing more reasons why you shouldn't be using them! By the end of the talk you should have an understanding of how a bundle of object files are brought together by the linker, along with the relevant runtimes, and then loaded and executed by the operating system. — Matt Godbolt, Coinbase Senior Software Engineer Matt Godbolt is the creator of the Compiler Explorer website. He is passionate about writing efficient code. He has previously worked at a trading firm, on mobile apps at Google, run his own C++ tools company and spent more than a decade making console games. When he's not hacking on Compiler Explorer, Matt enjoys writing emulators for old 8-bit computer hardware. — Videos Filmed & Edited by Bash Films: BashFilms.com Work at Hudson River Trading (HRT): tinyurl.com/safxfctf --- Videos Filmed & Edited by Bash Films: BashFilms.com

Why it's here

1 person recommends this

What people say. Recommended as an excellent explanation of how linkers, loaders, and runtimes prepare a program to run.

u/mttdin r/ReverseEngineering • Recommends

“The Bits Between the Bits: How We Get to main()” - CppCon 2018: Matt Godbolt

u/DeeBoFour20in r/C_Programming • Mentions

On “What Happens Before the Main Function is Called ?”

Here's a talk about the subject I watched a while back by Matt Godbolt (author of godbolt.org): [video

u/susmatthewin r/C_Programming • Mentions

confusing thing from libc watching a Godbolt talk (link to this video -- code at 16:50), he presented a snippet from libc that did some math on arrays of function pointers and confused me. it looked like: typedef void (fp*)(some arguments); extern fp array_start[]; extern fp array_end[]; int aFunction (args) { const size_t size= array_end - array_start; for (use size here) } `` how does the math of 'size' work here? is it just the addresses of the arrays of function pointers? I fired up gcc and it compiles but I can't make sense of the results. When I change the sizes of the arrays the values change but it's not what I would expect.

u/ste_3d_venin r/C_Programming • Mentions

On “What does the 'main' function actually do?”

Matt Godbolt gave a talk about this at CppCon. While it is oriented to C++, the startup of the two languages is very similar. It might be a good resource link to this video

u/DeadLink404in r/C_Programming • Mentions

On “const char* memory leak?”

If they are constant the compiler can treat them like \#define constantly and just insert the value wherever it is used. Which can result in instructions of the form "load the next {1,2,4} bytes into register Y" but compilers can get more creative than that. If they aren't constant they go in .data and .bss where bss stands for block started by symbol but really means zero initialized data. To save space the .bss segment is tiny in the executable but the OS is told how many pages of zeros you need and gives it to you. If you are using c++ where globals can have constructors I highly recommend you watch godbolt explain what happens before main.

u/DeadLink404in r/C_Programming • Mentions

On “[deleted by user]”

> if you asked me how a struct is represented in memory or in assembly code I wouldn't know how to answer that. Structs are just sizeof(struct xyz) size chunks of memory. The compiler knows that the 8 bytes at offset 14 is a long long, or whatever it may be. In the assembly you'll see something like this when accessing a member: mov rax, $ptr mov eax, dword ptr rax + 8 ptr is here a pointer to a struct. The second line loads a 4 byte (dword) value from offset 8 of that pointer into register eax. And that's about as complicated as it gets in assembly. You can explore what you code gets turned into by a compiler on godbolt.org The creator of that website also has a nice tal…

u/Fine-Ad9168in r/C_Programming • Mentions

On “Why does this program run and terminate in segfault instead of catching it as a compile time error?”

This one by Godbolt [video is on C++ but seems really good. Godbolt always gives a good talk.