Static and Dynamic Memory Allocation Memory allocation is an aspect of a more general action in software operation known as binding Static allocation performed by compiler, linker, or loader • Sizes of data structures must be known a priori Dynamic allocation provides flexibility • Memory allocation actions constitute an overhead during operation Operating Systems, by Dhananjay Dhamdhere 1 1 Mr. D. J. Patel, AITS, Rajkot LINKERS Mr. D. J. Patel, AITS, Rajkot Execution of Programs A has to be transformed before it can be executed Many of these transformations perform memory bindings • Accordingly, an address is called compiled address, linked address, etc Operating Systems, by Dhananjay Dhamdhere 3 3 Mr. D. J. Patel, AITS, Rajkot Why Linkers ?? Modularity Program can be written as a collection of smaller source files, rather than one monolithic mass. Can build libraries of common functions e.g., Math library, standard C library Efficiency Time: Change one source file, compile, and then relink. No need to recompile other source files. Space: Libraries of common functions can be aggregated into a single file... Yet executable files and running memory images contain only code for the functions they actually use. Mr. D. J. Patel, AITS, Rajkot Linking The process of collecting and combining various pieces of code and data into a single file that can be loaded (copied) into memory and executed. Linking time Can be done at compile time, i.e. when the source code is translated Or, at load time, i.e. when the program is loaded into memory Or, even at run time. Static Linker Performs the linking at compile time. Takes a collection of relocatable object files and command line arguments and generate a fully linked executable object file that can be loaded and run. Performs two main tasks Introduction to Linkers Symbol resolution: associate each symbol reference with exactly one symbol definition Relocation: relocate code and data sections and modify symbol references to the relocated memory locations Dynamic Linker Performs the linking at load time or at run time. Mr. D. J. Patel, AITS, Rajkot Performing Relocation relocation_factorP = l_originP – t_originP lsymb = tsymb + relocation_factorP Ref : 224 – D M Dhamdhere Mr. D. J. Patel, AITS, Rajkot Relocation Instructions using memory addresses are addresssensitive Relocation is needed if program is to execute correctly in 7 Operating Systems, by Dhananjay Dhamdhere some other memory area: involves changing addresses Ref : 223 –Figure : 7.2 D M Dhamdhere 7 Mr. D. J. Patel, AITS, Rajkot Example - I Suppose an object module contains three programs A,B and C. If they are located at following addresses Prog Address A 200-250 B 302-370 C 480-533 Assume the load address is 300 for the executable A, B and C to be generated. Find: i) Find out size of each Program ii) Load origin of each program iii) Relocation factor for each program Ref : Pg: 226 Example 7.5 – D M Dhamdhere Mr. D. J. Patel, AITS, Rajkot Solution - I Size Relocation Factor A = 51 RFA = 300 – 200 = 100 B = 69 RFB = 351 – 302 = 49 C = 54 RFC = 420 – 480 = -60 Load time Origin A = 300 B = 351 C = 420 Mr. D. J. Patel, AITS, Rajkot Design of Linker Relocation Requirement Linking Requirement Name Table (NTAB) – with the field of Synbols name and Linked_address eg. : Pg: 231 – Example : 7.9 Mr. D. J. Patel, AITS, Rajkot Self Relocating Programs Which program can be modified, or can modfied it self , to execute from a given Load Origin. Classifications : 1) Non Relocatable Program 2) Relocatable Programs 3) Self Relocating Programs Ref : pg: 232 7.3 Dhamdhere Mr. D. J. Patel, AITS, Rajkot Non Relocatable Program 1) Can't Execute in Any Memory Area, Only on Traslated Origin 2) Lack of Address Sensitive Instructions 3) e.g. Hand Coded Machine Language Program Mr. D. J. Patel, AITS, Rajkot Relocatable Programs 1) Can Execute in Any Desired Memory Area 2) Availability of Address Sensitve Instruction 3) e.g. Object Module Mr. D. J. Patel, AITS, Rajkot Self Relocating Programs 1) Can Execute in Any Memory Area 2) Availability of Own Address Sensitve Instruction 3) Relocating Logic Specified on the start of the Program 4) Useful for Time sharing Operating System Mr. D. J. Patel, AITS, Rajkot Linking for Overlays Overlay : It is a part of a program that use same load origin as some part of the program. Advantages : ► Keep in memory only those instructions and data that are needed at any given time. ► Needed when process is larger than amount of memory allocated to it. Mr. D. J. Patel, AITS, Rajkot An Overlay Tree Mr. D. J. Patel, AITS, Rajkot Loader An operating system utility that copies programs from a storage device to main memory, where they can be executed. In addition to copying a program into main memory, the loader can also replace virtual addresses with physical addresses. Most loaders are transparent, i.e., you cannot directly execute them, but the operating system uses them when necessary. Absolute loader can only load origin = linked Origin. Mr. D. J. Patel, AITS, Rajkot