Pallapa Sripairojkul Id. 4322043 Shared Library Pallapa Sripairojkul Id. 4322043
Topics Overview Binding time Shared library in practice Address space management Structure of shared library Creating shared library Linking with shared library Running with shared library The malloc hack and other shared library problems
รูปแสดงการประมวลผลโปรแกรม Source program Overview Compiler or assembler Compile time Object module Other object modules Linkage editor Load module System library load time Dynamically Loaded system library loader รูปแสดงการประมวลผลโปรแกรม Execution time (run time) In-memory binary memory image
Overview Shared library consists of executable code that can be located at any available address in memory Only one copy of shared library’s instructions is loaded The system shares that one copy among multiple programs instead of loading a copy for each program using the library , as in the case with archive (static) library
Overview Advantages Program linked with shared libraries don’t need to be recompiled and relinked when changes are made to those libraries. The use of shared libraries occupies less space in memory and on disk. When multiple programs are linked to a single shared library, the amount of physical memory used by each process can be significantly reduced.
Shared library vs Archive library Shared library Archive library - ld - ar - can be positioned at any - fixed location in process’s available position. Loader private virtual address space assigns a location in private virtual address space - is in /usr/shlib - is in /usr/lib - naming prefix “lib” and - prefix “lib” end with suffix end with suffix “.so” “.a”
Example Main() { ... printf( “How do you like this manual?\n” ); result = strcmp ( “I do.”, answer ); }
a.out file ที่สร้างขึ้นโดยใช้ archive library และ shared library FILE HEADER Program .text library .text for printf(3S) and strcmp(3C) Program .data library .data SYMBOL TABLE STRING TABLE FILE HEADER Program .text Program .data .lib SYMBOL TABLE STRING TABLE สร้างโดยlink editor อ้างถึง library code สำหรับ printf(3S) และ strcmp(3C) ถูกคัดลอกไปยังไฟล์โดย link editor
Processes using an Archive and a Shared Library อาจจะมีการนำไปใช้ในโปรเซสอื่นพร้อม ๆ กัน Archive Version Shared Version Address Space Library นำเข้าสู่ตำแหน่งของหน่วยความจำของโปรเซส Library code ที่อ้างถึงโดย .lib
Binding time Shared library ไม่ถูกนำไปไว้กับ linked program ปัญหาที่อาจเกิดขึ้น ไม่พบ shared library ที่ต้องการ จึงมีข้อความแสดงข้อผิดพลาดและจบการทำงาน มี shared library นั้นอยู่ แต่ library มีการเปลี่ยนแปลงเมื่อโปรแกรมผ่านการ link ไปแล้ว การเปลี่ยนแปลงที่จะไม่มีผลกระทบต่อโปรแกรมคือ การเปลี่ยนแปลงที่ไม่ไปเปลี่ยนตำแหน่งของ library ที่โปรแกรมเรียกใช้
Shared library in practice All three work nearly the same but the differences are instruction. SVR3.2 COFF format LINUX a.out format BSD/OS a.out and ELF format SVR3.2 ต้องการการเปลี่ยนแปลงใน linker เพื่อให้สนับสนุนการค้นหา shared library ขยายความสามารถของ OS ให้สนับสนุนการทำงานในเรื่องของ runtime startup LINUX ต้องการการปรับ linker เล็กน้อย และเพิ่ม system call เพื่อช่วยเหลือในการทำ library mapping BSD/OS ไม่มีการเปลี่ยนแปลงใด ๆ กับ linker หรือ OS แต่จะใช้ shell script เพื่อให้ argument ที่จำเป็นแก่ linker และ standard C library startup routine เพื่อ map กับ library
Address space management แต่ละ shared library จับจองส่วนของ address space ของโปรแกรมที่มีการเรียกใช้มันในขนาดที่คงที่ Library ที่ต่างกันต้องใช้ non-overlapping addresses ถ้ามันมีการใช้ในโปรแกรมเดียวกัน ใช้การปล่อยให้มีที่ว่างระหว่าง library แต่ละตัว เมื่อมันมีการ เปลี่ยนแปลงจนได้เวอร์ชันใหม่มา
Structure of shared library Shared library คือ an executable format file ที่มี library code และ data บรรจุอยู่ข้างใน File header, a.out,COFF, or ELF header (Initialization routine , not always present) Jump Table Code Global data Private data Shared library เริ่มด้วย bootstrap routine เล็ก ๆ ในการ map ส่วนที่เหลือของ library ตามด้วย jump table ซึ่งเก็บ exported address ของแต่ละ public routine ใน library Text section Exported data และ Private data
การสร้าง a.out ผู้ใช้สั่งให้ link editor ค้นหา shared library ด้วยวิธีเดียวกับ การสั่งให้ค้นหา archive library คือ $cc file.c –o file … -l library_file … และการลิงค์ทุก ๆ ไฟล์ในไดเรกทอรีปัจจุบันเข้าด้วยกัน กับ shared C library ผู้ใช้จะต้องใช้คำสั่งต่อไปนี้ $cc *.c –lc_s
Creating shared library ขั้นตอนพื้นฐาน กำหนดว่าที่ address ใดที่ library code และ data จะถูก load ลงไป ดู input library ให้ทั่วตลอดไลบรารี เพื่อดู exported code symbols ทั้งหมด สร้าง jump table ที่มีสมาชิกคือ exported code symbol. ถ้ามีการกำหนดค่าเริ่มต้น หรือ loader routineที่ตอนเริ่มต้นของ libraryให้ทำการคอมไพล์ หรือ แอสเซมเบิล สร้าง shared library : รัน linker และ link ทุก ๆ สิ่งเข้าด้วยกันเป็น executable format file ขนาดใหญ่ สร้าง stub library : ดึงเอา symbols ที่จำเป็นจาก stub routine ที่สร้างขึ้นมาใหม่สำหรับแต่ละlibrary routine ออก แล้วทำการคอมไพล์ stubs routine และรวมเข้าเป็น stub library.
Creating shared library More details in : Creating the jump table Creating the shared library Creating the stub library Version naming
Creating jump table
Creating the shared library
Creating the stub library
Version naming
Linking with shared library
Running with shared library
Shared library problems
Example /* file shrobj.c */ const char *myfunc() { return “hello world”; }
Example (cont.) /* file hello.c */ #include <stdio.h> extern const char *myfunc() main() { printf(“%s\n”,myfunc()); return 0; }
Example (cont.) $ setenv LD_LIBRARY_PATH /stud/43/g4322043/… $ cc –fpic –c $ cc –shared –o libshared.so shrobj.o $ cc hello.c libshared.so $ ./a.out
Summary Using any shared library almost always saves disk storage space, memory, and computer power Running Unix system on smaller machines makes the efficient use of these resources increasingly important. Therefore, you should normally use a shared library whenever it’s available
References -UNIX System V Programmer’s guide. AT&T Prentice-Hall. Eaglewood Cliffs. NJ. -Silberschatz, Abraham. Applied operating system concepts. John Wiley&Sons,Inc. 2000. -Linker and Libraries Guide Solaris 2.5. Sun Microsystem, Inc. -http://www.tru64unix.compaq.com/faqs/publications/base_doc/ DOCUMENTATION/HTML/AA-PS30D-TET1_thml/peg5.html #shlib_chap. -http://www.iecc.com/linker/linker09.html. -http://www.users.cs.umn.edu/~bailey/sharedobjects/ sharedobjects.html. -http://www.erlenstar.demon.co.uk/unix/faq_7.html -http://www.xidak.com/mainsail/v19docs/newhtml/ osu-UNIXU-D.html