ta6 2006 amar lior based on lectures notes from arie
play

ta6 2006 Amar Lior Based on lectures notes from Arie - PDF document

ta6 2006 Amar Lior Based on lectures notes from Arie Schlesinger (aries@cs.columbia.edu) Adapted from Computer Organization&Design, H/S interface, Patterson Hennessy@UCB,1999 1 Communicating with People As soon


  1. הנבמ םיבשחמ ta6 2006 Amar Lior Based on lectures notes from Arie Schlesinger (aries@cs.columbia.edu) Adapted from Computer Organization&Design, H/S interface, Patterson Hennessy@UCB,1999 1 Communicating with People � As soon as computers became commercial they were used to process text � Most use 8-bit bytes to represent characters � Using the American Standard Code for Information Interchange (ASCII) � MIPS provide instructions to move bytes � lb $t0, 0($sp) # Read byte from source � sb $t0, 0($gp) # Write byte to destination 2 Strcpy example strcpy: void strcpy (char x[], char y[]) { addi $sp, $sp, -4 int i; sw $s0, 0($sp) i=0; add $s0, $zero, $zero while ((x[i] = y[i]) != ‘\0’) i++; L1: add $t1, $s0, $a1 } lb $t2, 0($t1) add $t3, $s0, $a0 sb $t2, 0($t3) beq $t2, $zero, L2 addi $s0, $s0, 1 j L1 L2: lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra 3 1

  2. Strings in java � Java uses Unicode for characters � By default, it uses 16 bit to represent a character � The MIPS instructions has explicit instructions to load and store such 16-bit quantities � Called halfword � lh $t0, 0($sp) � sh $t0, 0($sp) � Unlike C, Java includes a word that gives the length of the string 4 Translating and Starting a Program C program Loader Compiler Memory Assembly Language Program Assembler Object: Machine language module Object: Machine language module Linker Executable: Machine language program 5 The output of the assembler � The assembler must determine the addresses corresponding to all labels � A symbol table is used to keep track of labels used for branches and data transfer � The object file typically contains: � Object file header : describes size and position of other parts � Text segment � Static data segment � Relocation information: identifies instructions and data words that depend on absolute addresses � Symbol table : contains the remaining labels that are not defined � Debugging information 6 2

  3. Linker � Single change to one line of one procedure requires compiling and assembling the whole program � An alternative is to compile and assemble each procedure independently � This requires a new system program called link editor or linker � Work in 3 steps � Place code and data modules symbolically in memory � Determine the addresses of data and instructions labels � Patch both internal and external references � Uses the relocation information and symbol table to resolve all undefined labels � The reason for the linker is that patching the code is much faster than compiling and reassemble 7 Linker � When all external references are resolved � The linker next determines the memory location each module will occupy � Since the files are assembled in isolation � the assembler could not know where a module’s instruction and data will be placed relative to other modules � When the module is placed in memory all absolute references must be relocated to reflect its true location � The linker produce an executable file that can be run on the computer 8 Linker example Object Header Name Procedure A Text Size 100hex Data Size 20hex Text segment Address Instruction 0 lw $a0, 0(gp) 4 jal 0 … … Data Segment 0 (X) … … Relocation Info Address Instruction type dep 0 lw X 4 jal B Symbol Table Label Address X - B - 9 3

  4. Linker example Object Header Name Procedure B Text Size 200hex Data Size 30hex Text segment Address Instruction 0 sw $a1, 0(gp) 4 jal 0 … … Data Segment 0 (Y) … … Relocation Info Address Instruction type dep 0 sw Y 4 jal A Symbol Table Label Address Y - A - 10 Linker example Executable file header Text Size 300hex Data Size 50hex Text segment Address Instruction 0040 0000 Lw $a0, 8000($gp) 0040 0004 Jal 40 0100 … … 0040 0100 Sw $a1, 8020($gp) 0040 0104 Jal 40 000 … … Data segment Adress 1000 0000 (x) … … 1000 0020 (Y) … … 11 Object symbols example File: t.c mos215:~> nm t.o int main() { U f f(1); 0000002b T g } 00000000 T main int g(void) { mos215:~> nm t1.o return 1; } 00000000 T f File: t1.c int f(void) { return 1; } 12 4

  5. The symbols in the final binary mos215:~> nm ttt 080494f0 D _DYNAMIC 080495bc D _GLOBAL_OFFSET_TABLE_ 080484d4 R _IO_stdin_used w _Jv_RegisterClasses 080495dc A __bss_start 080495d0 D __data_start 08048480 t __do_global_ctors_aux 08048300 t __do_global_dtors_aux 080495d4 D __dso_handle 080494dc A __fini_array_end 080494dc A __fini_array_start 080495dc A _edata 080495e0 A _end 0804825c T _init 080482b0 T _start 080495d0 W data_start 080483a0 T f 08048340 t frame_dummy 08048393 T g 08048368 T main 13 Another symbol example mos215:~/teach/comarc/linux-asm> nm hello 08049494 D _DYNAMIC .data 08049570 D _GLOBAL_OFFSET_TABLE_ hw: 08048470 R _IO_stdin_used .string "hello world\n" w _Jv_RegisterClasses .text 08049584 A __bss_start .global main 08049474 D __data_start main: 08048420 t __do_global_ctors_aux 08048410 T __i686.get_pc_thunk.bx movl $4, %eax 080483c0 T __libc_csu_fini movl $1, %ebx 08048360 T __libc_csu_init movl $hw, %ecx U __libc_start_main@@GLIBC_2.0 movl $12, %edx 08049584 A _edata 08049588 A _end int $0x80 0804822c T _init movl $1, %eax 08048270 T _start xorl %ebx, %ebx 08048294 t call_gmon_start int $0x80 08049474 W data_start ret 08049480 d hw 08048334 T main 0804947c d p.0 14 Loader � The Operating System read the executable file from the disk to memory and starts it � Read the executable file header and determine size of text and data segment � Create an address space large enough for the text and data � Copies the instructions and data � Copies the parameters (if any) of the main program to the stack � Initialize the machine registers and set the stack pointer to the first free location � Jumps to a startup routine that copies the parameters into the argument registers � When the main routine returns, the startup routines call the exit system call 15 5

  6. Dynamically Linked Libraries � Statically linked executables has several disadvantages: � The library routine become part of the executable code � It loads the whole library even if the code is not used when the program is run (can be very big) � Using dynamically linked libraries the library routines are not linked and loaded until the program is run � In the initial versions of DLLs, the loader run a dynamic linker � It uses extra information in the file to find the appropriate library and to update all external references 16 DLL’s � A downside of the pervious version?? � Lazy procedure linking � the routine is linked only after it is called � This uses a level of indirection � There is a dummy routine for each external procedure � This dummy routine call a dynamic linker-loader routine with the id of the routine to load � The linker-loader also change the indirect jump to point directly to the routine � So further callas does not access the linker-loader 17 Example: calling a DLL routine Text Jal … lw jr Data First call to DLL routine Text Li IDl j Text Dynamic Linker Loader j Text/data DLL Routine … 18 Jr 6

  7. Example: calling a DLL routine Text Jal … lw jr Data Subsequent calls to DLL routine Text/data DLL Routine … Jr 19 Tools in Unix mos215:~> ldd /bin/ls linux-gate.so.1 => (0xffffe000) librt.so.1 => /lib/tls/i686/cmov/librt.so.1 (0xb7f34000) libacl.so.1 => /lib/libacl.so.1 (0xb7f2e000) libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7df6000) libpthread.so.0 => /lib/tls/i686/cmov/libpthread.so.0 (0xb7de4000) /lib/ld-linux.so.2 (0xb7f5c000) libattr.so.1 => /lib/libattr.so.1 (0xb7de0000) mos215:~/teach/comarc/linux-asm> ldd /usr/bin/xspim linux-gate.so.1 => (0xffffe000) libXaw.so.7 => /usr/X11R6/lib/libXaw.so.7 (0xb7e8e000) libXmu.so.6 => /usr/X11R6/lib/libXmu.so.6 (0xb7e79000) libXt.so.6 => /usr/X11R6/lib/libXt.so.6 (0xb7e29000) libSM.so.6 => /usr/X11R6/lib/libSM.so.6 (0xb7e20000) libICE.so.6 => /usr/X11R6/lib/libICE.so.6 (0xb7e09000) libXpm.so.4 => /usr/X11R6/lib/libXpm.so.4 (0xb7df3000) libXext.so.6 => /usr/X11R6/lib/libXext.so.6 (0xb7de5000) libX11.so.6 => /usr/X11R6/lib/libX11.so.6 (0xb7d1a000) libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xb7cf4000) libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7bbd000) libdl.so.2 => /lib/tls/i686/cmov/libdl.so.2 (0xb7bb9000) /lib/ld-linux.so.2 (0xb7f07000) mos215:~/teach/comarc/linux-asm> 20 7

Recommend


More recommend