Programs – Dynamic Linking and Loading

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
C Programming - Lecture 5
Compiling and Linking. Compiling is quite the same as creating an executable file! Instead, creating an executable is a multistage process divided into.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
Linking and Loading Linker collects procedures and links them together object modules into one executable program. Why isn't everything written as just.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
Functions, Scope & File IO C++ Lecture 4 Bhaskar Bhattacharya.
Programs – Preprocessing, Compilation and Linking
CS/COE 0449 (term 2174) Jarrett Billingsley
Programs – Calling Conventions
Programs, Instructions, and Registers
CS/COE 0449 (term 2174) Jarrett Billingsley
EGR 2261 Unit 11 Pointers and Dynamic Variables
Copyright © Jim Fawcett Spring 2017
Computer Organization and Design Pointers, Arrays and Strings in C
CS/COE 0447 (term 2181) Jarrett Billingsley
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
CS/COE 0449 (term 2174) Jarrett Billingsley
A bit of C programming Lecture 3 Uli Raich.
Lesson One – Creating a thread
Programs – Loading and Running an Executable
CS/COE 1541 (term 2174) Jarrett Billingsley
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Pointers in C.
CSE 374 Programming Concepts & Tools
Chapter 5 Conclusion CIS 61.
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
CS/COE 0449 (term 2174) Jarrett Billingsley
Programs – Dynamic Linking and Loading
Pseudo-ops, Debugging, etc.
CSC 253 Lecture 8.
C – Structs, Unions, Enums, Typedefs
Functions Inputs Output
C – Scope, Lifetime, and the Stack
Object Oriented Programming COP3330 / CGS5409
CS/COE 0449 Jarrett Billingsley
CSC 253 Lecture 8.
CS/COE 0449 Jarrett Billingsley
Programming in C Pointer Basics.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Reference Parameters.
Programming in C Pointer Basics.
Programs – Preprocessing, Compilation and Linking
Mastering Memory Modes
Programs – Loading and Running an Executable
Data Structures & Algorithms
C++ Templates An Introduction to Generic Programming.
C Programming - Lecture 5
point when a program element is bound to a characteristic or property
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
Programming in C Pointer Basics.
CISC101 Reminders Assignment 3 due today.
EECE.2160 ECE Application Programming
ENERGY 211 / CME 211 Lecture 29 December 3, 2008.
Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, 2009
Presentation transcript:

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

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 0449 term 2174

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

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

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 0449 term 2174

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 0449 term 2174

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

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 0449 term 2174

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 0449 term 2174

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 0449 term 2174

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

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 0449 term 2174

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 0449 term 2174

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 0449 term 2174