资源描述
Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,CS61C L12,Running a Program II,(,*,),Garcia,Fall 2006 UCB,Security hole finder,Googles just-released new,/*Code Search*/,which allows you to specify regular expressions to search public source code,also allows hackers to easily find known loopholes!Yikes!,Computer Organization and Design,Lecture 12 Running a Program IIaka Compiling,Assembling,Linking,Loading,Review,C program:,foo.c,Compiler,Assembly program:,foo.s,Assembler,Linker,Executable(mach lang pgm):,a.out,Loader,Memory,Object(mach lang module):,foo.o,lib.o,目标文件格式,目标文件头,:,定义目标文件中其他部分大小和位置,代码段,:,机器码,数据段,:,源文件中数据的二进制表示,重定位信息,:,需要处理的代码中的标识,符号表,:,列出文件中可以被引用的标号和数据,调试信息,A standard format is ELF(except MS),Where Are We Now?,C program:foo.c,Assembly program:foo.s,Executable(mach lang pgm):a.out,Compiler,Assembler,Linker,Loader,Memory,Object(mach lang module):foo.o,lib.o,链接,Linker(1/3),输入,:,目标代码文件,信息表,(e.g.,foo.o,libc.o,for MIPS),输出,:,执行代码,(e.g.,a.out,for MIPS),将几个目标,(.o),文件链接成一个执行文件,(“,linking,”),使分离编译成为可能,修改一个文件,不必编译整个程序,Windows NT source is 40 M lines of code!,原来的名字,“Link Editor”,:源于编辑跳转语句的,“,链接”及链接指令,Linker(3/3),Step 1:,从每个,.o,文件中取出代码段放在一起,.,Step 2:,从每个,.o,文件中取出数据段放在一起,并把结果连接到代码段的尾部,.,Step 3:,确定地址,查找重定位表,处理每项,即,填充所有,绝对地址,四种主要寻址方式,PC-Relative,寻址,(,beq,bne,):,从不重定位,绝对寻址,(,j,jal,):,总是重定位,外部寻址,(,通常是,jal,):,总是重定位,数据寻址,(,通常是,lui,和,ori,):,总是重定位,MIPS,中的绝对寻址,哪些指令需要重定位编辑,?,J-format:jump,jump and link,j/jal,xxxxx,Loads and stores to variables in static area,relative to global pointer,lw/sw,$gp,$x,address,条件分支语句情况如何,?,beq/bne,$rs,$rt,address,PC-,相对寻址,代码移动后地址仍不变,确定地址,(2/2),确定地址,:,在,“,用户”自己的所有符号表中查找地址,(,数据或标号,),如果没有找到,查找库文件,(,如,printf,),一旦绝对地址确定了,在机器码中填写相应内容,链接器的输出,:,执行文件包括代码和数据,(,另外还有头,),静态,vs,动态链接库,我们所描述的是传统的,“,静态链接,”,方式,现在库文件是执行文件的一部分,因此,如果库文件更新了,我们并没更新,(,如果我们有原程序,可以重新编译一次,),执行文件中包含,整个,库,尽管我们不使用所有的,.,执行文件是完整的,.,另一种方案是,动态链接库,(DLL),常见于,Windows,和,UNIX,中,动态链接库,动态链接的最大好处是使用,“,最少公共代码,”,链接器不使用有关程序或库编译方式的信息,(,即不管用的什么编译器或者语言,),这可以描述为,“,在机器语言级进行链接,”,这不是唯一的方式,.,Loader(1/3),输入,:,执行程序,(,如,MIPS,的,a.out,),输出,:(,正在运行的程序,),执行程序存储在磁盘上,.,当需要运行时,装入器(,loader,)的任务就是将程序装入内存中并开始运行,.,在现实中,装入器包含在操作系统中,(OS),装入(,loading,)是操作系统的一个任务,Loader(2/3),装入器做些什么呢,?,读入执行文件头,以确定代码段和数据段的大小,为程序创建足够的地址空间,以容纳代码段、数据段和堆栈段。,从执行文件中复制指令和数据到新创建的地址空间,将传入程序的运行参数复制到栈,初始化机器寄存器,清除大多数寄存,对栈指针指定栈中第一个空余地址,Big-endian and little-endian derive from Jonathan Swifts,Gullivers Travels,in which the Big Endians were a political faction that broke their eggs at the large end(the primitive way)and rebelled against the Lilliputian King who required his subjects(the Little Endians)to break their eggs at the small end.,Big Endian vs.Little Endian,Big Endian,ADDR3 ADDR2 ADDR1 ADDR0,BYTE0,BYTE1,BYTE2,BYTE3,ADDR0 ADDR1 ADDR2 ADDR3,BYTE3,BYTE2,BYTE1,BYTE0,Little Endian,ADDR3 ADDR2 ADDR1 ADDR0,BYTE3,BYTE2,BYTE1,BYTE0,ADDR0 ADDR1 ADDR2 ADDR3,BYTE0,BYTE1,BYTE2,BYTE3,Consider the number 1025 as we normally write it:,BYTE3,BYTE2,BYTE1,BYTE0,00000000, order in which BYTES are stored in memory,Bits always stored as usual.,(E.g.,0 xC2=0b 1100 0010),Example:,C,Asm,Obj,Exe,Run,#include,int main(int argc,char*argv),int i,sum=0;,for(i=0;i=100;i+)sum=sum+i*i;,printf(The sum from 0.100 is%dn,sum);,C Program Source Code:prog.c,“,printf”lives in“libc”,Compilation:MAL,.text,.align2,.globlmain,main:,subu$sp,$sp,32,sw$ra,20($sp),sd$a0,32($sp),sw$0,24($sp),sw$0,28($sp),loop:,lw$t6,28($sp),mul$t7,$t6,$t6,lw$t8,24($sp),addu$t9,$t8,$t7,sw$t9,24($sp),addu$t0,$t6,1,sw$t0,28($sp),ble$t0,100,loop,la$a0,str,lw$a1,24($sp),jal printf,move$v0,$0,lw$ra,20($sp),addiu$sp,$sp,32,jr$ra,.data,.align0,str:,.asciizThe sum from 0.100 is%dn,Where are,7 pseudo-instructions?,.text,.align2,.globlmain,main:,subu$sp,$sp,32,sw$ra,20($sp),sd$a0,32($sp),sw$0,24($sp),sw$0,28($sp),loop:,lw$t6,28($sp),mul$t7,$t6,$t6,lw$t8,24($sp),addu$t9,$t8,$t7,sw$t9,24($sp),addu$t0,$t6,1,sw$t0,28($sp),ble$t0,100,loop,la$a0,str,lw$a1,24($sp),jal printf,move$v0,$0,lw$ra,20($sp),addiu$sp,$sp,32,jr$ra,.data,.align0,str:,.asciizThe sum from 0.100 is%dn,7 pseudo-instructionsunderlined,Compilation:MAL,Assembly step 1:,00 addiu$29,$29,-32,04 sw$31,20($29),08 sw$4,32($29),0c sw$5,36($29),10 sw$0,24($29),14 sw$0,28($29),18 lw$14,28($29),1c multu$14,$14,20 mflo$15,24 lw$24,24($29),28 addu$25,$24,$15,2c sw$25,24($29),30 addiu$8,$14,1,34 sw$8,28($29),38 slti$1,$8,101,3c bne$1,$0,loop,40 lui$4,l.str,44 ori$4,$4,r.str,48 lw$5,24($29),4c jalprintf,50 add$2,$0,$0,54 lw$31,20($29),58 addiu$29,$29,32,5c jr$31,Remove,pseudoinstructions,assign addresses,Assembly step 3,00 addiu$29,$29,-32,04 sw$31,20($29),08 sw$4,32($29),0c sw$5,36($29),10 sw$0,24($29),14 sw$0,28($29),18 lw$14,28($29),1c multu$14,$14,20 mflo$15,24 lw$24,24($29),28 addu$25,$24,$15,2c sw$25,24($29),30 addiu$8,$14,1,34 sw$8,28($29),38 slti$1,$8,101,3c bne$1,$0,-10,40 lui$4,l.str,44 ori$4,$4,r.str,48 lw$5,24($29),4c jal,printf,50 add$2,$0,$0,54 lw$31,20($29),58 addiu$29,$29,32,5c jr$31,Resolve local PC-relative labels,Text segment in object file,0 x00
展开阅读全文