Symbol Table Relocation Table Object File Format Where Are We Now

Symbol Table Relocation Table Object File Format Where Are We Now

inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Symbol Table Lecture 19 – Running a Program II . List of “items” in this file that may be used by other files. (Compiling, Assembling, Linking, Loading) Hello to . What are they? Lecturer SOE 2008-03-05 Neil Sharma Labels: function calling Dan Garcia from the 3rd row! Data: anything in the .data section; variables which may be accessed across files Researchers at Princeton have developed a flexible electricity-producing sheet of rubber that can use body movements into electricity. Breathing generates 1 W, walking around the room generates 70 W. Shoes may be the best place, to power/recharge cell phones & iPods. www.nytimes.com/2010/03/02/science/02obribbon.html CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (3) Garcia, Spring 2010 © UCB Relocation Table Object File Format . List of “items” this file needs the address later. object file header: size and position of the other . What are they? pieces of the object file Any label jumped to: j or jal . text segment: the machine code internal . data segment: binary representation of the data in external (including lib files) the source file Any piece of data connected with an address . relocation information: identifies lines of code that such as the la instruction need to be “handled” . symbol table: list of this file’s labels and data that can be referenced . debugging information . A standard format is ELF (except MS) http://www.skyfree.org/linux/references/ELF_Format.pdf CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (4) Garcia, Spring 2010 © UCB CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (5) Garcia, Spring 2010 © UCB Where Are We Now? Linker (1/3) . Input: Object Code files, information tables (e.g., foo.o,libc.o for MIPS) . Output: Executable Code (e.g., a.out for MIPS) . Combines several object (.o) files into a single executable (“linking”) . Enable Separate Compilation of files Changes to one file do not require recompilation of whole program Windows NT source was > 40 M lines of code! Old name “Link Editor” from editing the “links” in jump and link instructions CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (6) Garcia, Spring 2010 © UCB CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (7) Garcia, Spring 2010 © UCB Linker (2/3) Linker (3/3) . Step 1: Take text segment from each .o file and .o file 1 put them together. text 1 . Step 2: Take data segment from each .o file, put a.out data 1 them together, and concatenate this onto end of Relocated text 1 info 1 text segments. Relocated text 2 Linker . Step 3: Resolve References .o file 2 Relocated data 1 Go through Relocation Table; handle each entry text 2 Relocated data 2 That is, fill in all absolute addresses data 2 info 2 CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (8) Garcia, Spring 2010 © UCB CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (9) Garcia, Spring 2010 © UCB Four Types of Addresses we’ll discuss Absolute Addresses in MIPS . PC-Relative Addressing (beq, bne) . Which instructions need relocation editing? never relocate J-format: jump, jump and link . Absolute Address (j, jal) j/jal" xxxxx" always relocate Loads and stores to variables in static area, relative . External Reference (usually jal) to global pointer always relocate lw/sw" $gp" $x" address" . Data Reference (often lui and ori) What about conditional branches? always relocate beq/bne" $rs" $rt" address" PC-relative addressing preserved even if code moves CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (10) Garcia, Spring 2010 © UCB CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (11) Garcia, Spring 2010 © UCB Resolving References (1/2) Resolving References (2/2) .! Linker assumes first word of first text segment is .! To resolve references: at address 0x00000000. ! search for reference (data or label) in all “user” ! (More later when we study “virtual memory”) symbol tables .! Linker knows: ! if not found, search library files (for example, for printf) ! length of each text and data segment ! once absolute address is determined, fill in the ! ordering of text and data segments machine code appropriately .! Linker calculates: .! Output of linker: executable file containing text ! absolute address of each label to be jumped to and data (plus header) (internal or external) and each piece of data being referenced CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (12) Garcia, Spring 2010 © UCB CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (13) Garcia, Spring 2010 © UCB en.wikipedia.org/wiki/Dynamic_linking Static vs Dynamically linked libraries Dynamically linked libraries .! What we’ve described is the traditional way: .! Space/time issues statically-linked approach ! + Storing a program requires less disk space ! The library is now part of the executable, so if the ! + Sending a program requires less time library updates, we don’t get the fix (have to ! + Executing two programs requires less memory (if recompile if we have source) they share a library) ! It includes the entire library even if not all of it will be ! – At runtime, there’s time overhead to do link used. .! Upgrades ! Executable is self-contained. ! + Replacing one file (libXYZ.so) upgrades every .! An alternative is dynamically linked libraries program that uses library “XYZ” (DLL), common on Windows & UNIX platforms ! – Having the executable isn’t enough anymore Overall, dynamic linking adds quite a bit of complexity to the compiler, linker, and operating system. However, it provides many benefits that often outweigh these. CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (14) Garcia, Spring 2010 © UCB CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (15) Garcia, Spring 2010 © UCB Dynamically linked libraries Administrivia…Midterm on Monday! .! The prevailing approach to dynamic linking uses .! Review Sat @ Time/location TBA machine code as the “lowest common .! Exam Mon @ 7-10pm in 1 Pimentel denominator” .! Covers labs, hw, proj, lec, book through today ! The linker does not use information about how the program or library was compiled (i.e., what compiler .! Bring… or language) ! NO cells, calculators, pagers, PDAs ! This can be described as “linking at the machine ! 2 writing implements (we’ll provide write-in exam code level” booklets) – pencils ok! ! Your green sheet (make sure to correct green sheet ! This isn’t the only way to do it... bugs) CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (16) Garcia, Spring 2010 © UCB CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (17) Garcia, Spring 2010 © UCB Upcoming Calendar Where Are We Now? Week # Mon Wed Thu Lab Fri #7 MIPS Inst Running Running Running Format III Program I Program Program II CS164 This week SDS I SDS II SDS SDS III #8 (TA) (TA) Midterm Next week 7-10pm 1 Pimentel #9 Students SDS IV SDS CPU I Explain (no class, Next next week Midterm! see webcast) Next3 week Spring Break! CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (18) Garcia, Spring 2010 © UCB CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (19) Garcia, Spring 2010 © UCB Loader (1/3) Loader (2/3) .! Input: Executable Code .! So what does a loader do? (e.g., a.out for MIPS) ! Reads executable file’s header to determine size of .! Output: (program is run) text and data segments ! Creates new address space for program large .! Executable files are stored on disk. enough to hold text and data segments, along with .! When one is run, loader’s job is to load it into a stack segment memory and start it running. ! Copies instructions and data from executable file .! In reality, loader is the operating system (OS) into the new address space ! loading is one of the OS tasks CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (20) Garcia, Spring 2010 © UCB CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (21) Garcia, Spring 2010 © UCB Loader (3/3) Things to Remember (1/3) .! Copies arguments passed to the program onto the stack .! Initializes machine registers ! Most registers cleared, but stack pointer assigned address of 1st free stack location .! Jumps to start-up routine that copies program’s arguments from stack to registers & sets the PC ! If main routine returns, start-up routine terminates program with the exit system call CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (22) Garcia, Spring 2010 © UCB CS61C L19 : Running a Progam II … Compiling, Assembling, Linking, and Loading (25) Garcia, Spring 2010 © UCB Things to Remember (2/3) Things to Remember 3/3 .! Compiler converts a single HLL file into a single assembly .! Stored Program concept is very powerful. It language file. means that instructions sometimes act just like .! Assembler removes pseudoinstructions, converts what it can data. Therefore we can use programs to to machine language, and creates a checklist for the linker (relocation table). A .s file becomes a .o file. manipulate other programs! ! Does 2 passes to resolve addresses, handling internal forward ! Compiler ⇒ Assembler ⇒ Linker (⇒ Loader) references .! Linker combines

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us