Download presentation
Presentation is loading. Please wait.
1
Programs – Loading and Running an Executable
CS/COE 0449 (term 2174) Jarrett Billingsley
2
Class announcements Project 2!!!!
Due Wednesday, March 1st by midnight. Fixed some slides. Check your ! Laid out the dates for the quizzes and projects. Check the schedule! Next quiz is in 2 weeks... 2/14/2017 CS/COE term 2174
3
Question time 2/14/2017 CS/COE term 2174
4
Function pointers??? 2/14/2017 CS/COE term 2174
5
Okay, why are they useful?
Dynamically loaded libraries are one reason, but... They're not THAT common. (Though you'll have a lab to get comfortable with em.) But being able to pass functions as arguments to other functions... Now THAT is cool. Let's have a look at a small example using the qsort function from stdlib.h. qsort takes a pointer to a comparator function which must follow a set of rules to make qsort work properly. This brings up a couple small things to talk about... 2/14/2017 CS/COE term 2174
6
const void*? const char* s = "hello"; s = "goodbye"; // fine!
A void* is a "universal pointer." It can point to anything. An int* points at ints, a FILE* points at files... A void* can be assigned any other type of pointer. You can't read from a void* without casting (next slide!). For any type T, a const T* is a read-only pointer to a T. You can read the data that it points to, but you can't write it. You can however change what the pointer points to. const char* s = "hello"; s = "goodbye"; // fine! s[3] = 'x'; // error! 2/14/2017 CS/COE term 2174
7
Pointer casts float f = 3.567; int* p = (int*)&f; // points to f...
A cast looks like (type)value. You've used them before to do stuff like: double fstop = (double)numerator / denominator; Since all pointers are memory addresses, they're all the same size. You can cast one pointer type to another. float f = 3.567; int* p = (int*)&f; // points to f... printf("%x\n", *p); // interprets f as an int! The computer does not care what bits represent. We do! If you have a void*, you must cast it to another type if you want to use the data that it points to. 2/14/2017 CS/COE term 2174
8
Executable files 2/14/2017 CS/COE term 2174
9
What the heck is in an executable file?
We looked inside an object file, which has more or less all the same pieces that an executable file (or shared library) has: Machine code ("text") Data Symbol table(s) OS-specific info objdump can inspect executables as well! This is because both object files and executables use almost exactly the same format. Well, on most flavors of UNIX anyway... 2/14/2017 CS/COE term 2174
10
Executable formats Most flavors of UNIX use ELF (executable and linkable format) executables have no extension; shared libraries are .so files objects are .o files; statically linked libraries are .a files ...with the exception of MacOS, which uses Mach-O shared libraries are .dylib files; otherwise the same as UNIX! Finally, Windows uses PE (portable executable) executables are .exe files; shared libraries are .dll files No standard object/static library format – MS compilers use their own format (based on COFF); gcc actually uses ELF. Each of these formats is broadly similar, but each operating system works differently and so can require different information. 2/14/2017 CS/COE term 2174
11
Load shared objects and copy into RAM Copy code/data into RAM
The final countdown The last step is loading. The loader is the part of the OS that, uh, loads the executable. It sets up everything so that we can start running it! Load shared objects and copy into RAM yes Open executable file Copy code/data into RAM Dynamic libs? Set up OS resources no Start a new process! 2/14/2017 CS/COE term 2174
12
Processes 2/14/2017 CS/COE term 2174
13
All my children OS Kernel Code Memory Code Memory
To manage running programs, the OS uses processes. A process is the in-memory representation of the program, all its data, its resources (like open files), and so on. The OS's main responsibility is controlling processes' access to resources. The OS wants everyone to play nicely! And if they don't... it's lights out for the process. ps and pstree let you see what processes are running! Code Memory Process 1 Code Memory Process 2 Ugh, I'm dead. HEY! NO TOUCHING! OS Kernel 2/14/2017 CS/COE term 2174
14
Address Spaces Each process gets its own address space: a part of memory that it alone is allowed to access. This prevents malfunctioning or malicious programs from affecting other programs or the OS itself! Older CPU architectures did this with memory segmentation. This is the origin of the term "segmentation fault" or "segfault"! On every memory access, the CPU would check if the current process was allowed to access that part of memory. Modern architectures instead use virtual memory! 2/14/2017 CS/COE term 2174
15
Clearly this is impossible. But the programs believe it's true.
Virtual memory Virtual memory is an illusion: the OS makes every process believe that it's the only program running on the entire computer! Physical Memory Code Memory Process 1 0x8000 0xFFFF Process 2 ... Process 2's memory Process 1's memory 0x0066_7FFF 0x0066_0000 0x0040_FFFF Clearly this is impossible. But the programs believe it's true. 0x0040_8000 2/14/2017 CS/COE term 2174
16
One process's address space
So what's a (virtual) address space look like? Kinda like this, usually. The code (or the text segment) usually resides at the bottom of memory (the lowest addresses). Not usually at address 0, though. Then comes the global data segment. Both the code and globals are more or less copied directly from the executable file. We already saw how the stack and heap grow towards each other! But how did they get there, and how do they get bigger...? Stack Heap Globals Code 2/14/2017 CS/COE term 2174
17
Shattered memories Physical memory is a scarce resource. Therefore, the OS manages it. It does this by splitting up memory into pages: manageable chunks that can be allocated to processes. Usually these days, each page is 4KiB. The OS maintains a page table which keeps track of which parts of memory belong to which process. The page table is actually way more complex than this, and handled by the CPU hardware... But that's CS1550/1541 stuff! Page Physical Address Owning Process ... 10 0000A000 <OS> 9 8 bash 7 6 5 ls 4 ps 3 exifview 2 1 2/14/2017 CS/COE term 2174
18
Thanks! Someone else can use it.
Honey, I blew up the heap This is how programs can dynamically change the size of the stack and heap! When you use malloc... Your program might ask the OS for more pages to be allocated onto its heap. When you use free... Your program might give those pages back. Those mights are in there because how memory allocation works is... complicated. Stack Heap Gimme a page! OK! I'm done with this. OS Kernel Thanks! Someone else can use it. 2/14/2017 CS/COE term 2174
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.