Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programs – Dynamic Linking and Loading

Similar presentations


Presentation on theme: "Programs – Dynamic Linking and Loading"— Presentation transcript:

1 Programs – Dynamic Linking and Loading
CS/COE 0449 (term 2174) Jarrett Billingsley

2 Class announcements Here are your exams! I know, right?
Project 2 will come out soon! Probably next Tuesday, or possibly sooner if things are set up. Have to coordinate with Dr. Misurda This is the "password project" ;) Labs will resume this Friday/Monday/Tuesday. 2/9/2017 CS/COE term 2174

3 Question time 2/9/2017 CS/COE term 2174

4 Dynamic Linking 2/7/2017 CS/COE term 2174

5 Dynamic linking There are two main downsides of static linking: it can make programs bigger, and it can embed bugs into your executables. Many programs need printf. Why duplicate it? Dynamic linking leaves the puzzle to be completed when the program is run. This is effectively another linking step, but done at "load-time." When you run the program, it is linked with the proper shared object files, then executed. When the program is run, the loader looks for the right shared object(s) to link in. libc.so 2/7/2017 CS/COE term 2174

6 Pros and cons... Dynamic linking...
Lets us reuse common code between programs, like the C standard library (libc). Lets us fix bugs and security holes in that common code, and every program that uses it is now "fixed" too. but... It imposes extra overhead both when starting the program and during program execution – indirect calls. Fixing bugs can break programs. Shared libraries can have multiple versions and programs can link to a specific one or to any one. Managing this is hard. Windows has no proper shared library versioning system and has to deal with this crap with something called WinSxS. 2/7/2017 CS/COE term 2174

7 What's a library? 2/9/2017 CS/COE term 2174

8 WELL WHAT IS IT A library is a collection of compiled code available for executables to reuse. Although it's compiled, it's not an executable itself. It's an incomplete part of a whole... Wait, doesn't that sound familiar? These .a files (archives) are just a bunch of object files glued together. Libraries are a collection of object files. Well, this is one kind of a library. This is a statically linked library. libc.a 2/9/2017 CS/COE term 2174

9 Dynamically Linked Libraries
With a dynamically linked library, the linker leaves a piece of out the puzzle. The missing piece is called a shared object on Unix. Shared objects are again, very much like object files. They are often compiled in a different way to allow them to be loaded into memory in a more flexible way. Linking to the shared object is deferred from link-time to load time. Essentially, we perform the final step of linking right before the program starts to run! But if the linking fails, the program will not run. libc.so 2/9/2017 CS/COE term 2174

10 Dynamically Loaded Libraries
With a dyamically loaded library, the application code decides what shared objects to load, and when! This happens after the program starts running. This is often used to load optional functionality, usually called plugins. But... how do we call functions that the compiler didn't even know existed? Program MPEG Decoder H.264 Decoder 2/9/2017 CS/COE term 2174

11 Function pointers 2/9/2017 CS/COE term 2174

12 return type parameter types
Point to anything! We can have pointers to data anywhere in memory. Why not to functions, too? A function pointer is gee, I dunno. what do you think In C, it looks... terrifying. int (*fp)(int); // wHAT return type parameter types This is actually a variable declaration. It makes a variable fp that points to a function that takes an int and returns an int. 2/9/2017 CS/COE term 2174

13 Typedef is your friend int(**(*fp)(const char*))(float);
What is this? int(**(*fp)(const char*))(float); asdgafsdgflajflkhj THIS IS A REAL TYPE. It's a pointer to a function which takes a const char*, and returns an array of function pointers, each of which takes a float and returns an int. Typedef is nice. typedef int(*MATH_FUNC)(float); MATH_FUNC* (*fp)(const char*); 2/9/2017 CS/COE term 2174

14 Why function pointers??? They're great!
You can dynamically load functions from libraries and call brand-new functions that didn't even exist when your program started. Object-oriented programming is really just a pretty way to hide function pointers behind method names. So, you can implement real OOP in C... in a very, very ugly way. You can pass functions as arguments to other functions. Whaaaaaaaa Let's look at the qsort standard library function! This isn't quite as handy as first-class functions but it's a taste of what more advanced programming languages can give you. 2/9/2017 CS/COE term 2174


Download ppt "Programs – Dynamic Linking and Loading"

Similar presentations


Ads by Google