版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
intbuf[2]={1,2};
intmain(){swap();return0;}main.cswap.cexternintbuf[];
int*bufp0=&buf[0];staticint*bufp1;voidswap(){inttemp;bufp1=&buf[1];temp=*bufp0;*bufp0=*bufp1;*bufp1=temp;}使用gcc5.3.1如果不用extern:《计算机系统》链接中篇《计算机系统》课程教学组2025年春季学期库静态库共享库链接器本讲内容链接的作用&意义链接器的输入/出文件ELF细节链接器工作步骤符号属性符号解析解析规则重定位类型与过程三种目标文件可重定位目标文件(.ofile)包含二进制代码和数据,其形式可以在链接时与其它可重定位目标合并,创建一个可执行目标文件每一个.o文件产生自一个源(.c)文件可执行目标文件(注意:历史遗留a.out作为默认可执行文件名,已经被ELF格式取代)包含二进制代码和数据,可以被直接复制到存储器并执行共享目标文件(.sofile)一种特殊类型的可重定位目标文件,可以在加载或运行时被动态加载到存储器并链接在Windows中称之为动态链接库(DynamicLinkLibraries,DLLs)在Unix/Linux中目标文件的统一格式是ELF可执行和可链接格式ELF目标文件的标准二进制格式最初源自AT&T的SystemVUnix后来由BSDUnix和Linux修订使用统一的格式可重定位目标文件(.o),可执行目标文件(a.out)共享目标文件(.so)通用名称:ELF(executableandlinkableformat)ELF目标文件格式ELF头Wordsize,byteordering,filetype(.o,exec,.so),machinetype,etc.ELFheaderProgram/Segmentheader(requiredforexecutables).textsection.rodatasection.bsssection.symtab
section.rel.txt
section.rel.data
section.debugsectionSectionheadertable0.datasection“链接后的可执行文件链接前的对象文件ELF目标文件格式ELF头Wordsize,byteordering,filetype(.o,exec,.so),machinetype,etc.Segmentheadertable页大小,虚地址内存段,段大小ELFheaderProgram/Segmentheadertable(requiredforexecutables).textsection.rodatasection.bsssection.symtab
section.rel.txt
section.rel.data
section.debugsectionSectionheadertable0.datasectionELF目标文件格式ELF头Wordsize,byteordering,filetype(.o,exec,.so),machinetype,etc.Segmentheadertable页大小,虚地址内存段,段大小ELFheaderProgram/Segmentheadertable(requiredforexecutables).textsection.rodatasection.bsssection.symtab
section.rel.txt
section.rel.data
section.debugsectionSectionheadertable0.datasectionELF目标文件格式ELF头Wordsize,byteordering,filetype(.o,exec,.so),machinetype,etc.Segmentheadertable页大小,虚地址内存段,段大小ELFheaderProgram/Segmentheadertable(requiredforexecutables).textsection.rodatasection.bsssection.symtab
section.rel.txt
section.rel.data
section.debugsectionSectionheadertable0.datasectionELF目标文件格式.symtabsection符号表函数和全局变量名所在section名与位置.rel.textsection.text节中位置列表执行时需要修改的指令地址待修改指令.rel.datasection.data中重定位信息被模块引用或定义的已初始化的全局变量的重定位信息.debugsection调试符号表(gcc-g)
节头部表每个节section的偏移与大小ELFheaderProgram/Segmentheadertable(requiredforexecutables).textsection.rodatasection.bsssection.symtabsection.rel.txtsection.rel.datasection.debugsectionSectionheadertable0.datasectionELF目标文件格式ELF头Wordsize,byteordering,filetype(.o,exec,.so),machinetype,etc.Segmentheadertable页大小,虚地址内存段,段大小.textsection
已编译程序的机器代码.rodatasection只读数据:跳转表,printf语句中的格式串....datasection
已初始化的全局变量已初始化的静态C变量.bsssection未初始化的全局变量,所有初始化为0的全局/静态变量“BlockStartedbySymbol”
,“BetterSaveSpace”不占据实际硬盘空间ELFheaderProgram/Segmentheadertable(requiredforexecutables).textsection.rodatasection.bsssection.symtab
section.rel.txt
section.rel.data
section.debugsectionSectionheadertable0.datasectionELF目标文件格式.symtabsection符号表函数和全局变量名所在section名与位置.rel.textsection.text节中待修改的位置(需要重定位)的信息执行时需要修改的指令地址待修改位置的指令.rel.datasection.data中重定位信息被模块引用或定义的已初始化的全局变量的重定位信息.debugsection调试符号表(gcc-g)ELFheaderProgram/Segmentheadertable(requiredforexecutables).textsection.rodatasection.bsssection.symtab
section.rel.txt
section.rel.data
section.debugsectionSectionheadertable0.datasectionELF目标文件格式ELFheaderProgram/Segmentheadertable(requiredforexecutables).textsection.rodatasection.bsssection.symtabsection.rel.txtsection.rel.datasection.debugsectionSectionheadertable0.datasectionELF目标文件格式ELFheaderProgram/Segmentheadertable(requiredforexecutables).textsection.rodatasection.bsssection.symtabsection.rel.txtsection.rel.datasection.debugsectionSectionheadertable0.datasection不同视角观察ELFELFheader.textsection.rodatasection.bsssection.symtabsection.rel.txtsection.rel.datasection.debugsectionSectionheadertable.datasectionELFheaderProgram/Segmentheadertable.text
segment.rodata
segment.bsssegment.symtabsegment.rel.txtsegment.rel.datasegment.debugsection.datasegment链接视角执行视角链接的本质链接本质:合并相同的“节”main()main.oint*bufp0=&buf[0]swap()swap.o系统代码int
buf[2]={1,2}系统数据可重定位目标文件.text.data.text.data.text.dataintbuf[2]={1,2}Headersmain()swap()int
*bufp0=&buf[0]更多系统代码系统数据.text.symtab.debug.dataint*bufp1.bss系统代码staticint*bufp1.bss可执行目标文件不同视角观察ELFELFheader.textsection.rodatasection.bsssection.symtabsection.rel.txtsection.rel.datasection.debugsectionSectionheadertable.datasectionELFheaderSegmentheadertable.text
segment.rodata
segment.bsssegment.symtabsegment.rel.txtsegment.rel.datasegment.debugsection.datasegment链接视角执行视角节(section)是ELF文件中具有相同特征的最小可处理单位
.text节:代码.data节:数据.rodata:只读数据.bss:未初始化数据不同的段(segment)组成,描述节如何映射到存储段中,可多个节映射到同一段,如:可合并.data节和.bss节,并映射到一个可读可写数据段中
关于链接(链接器)描述错误的是静态链接之后才能生成可执行代码链接主要包括符号解析和重定位两步链接器的输入文件只能是可重定位目标文件Linux上目标文件格式是PE格式ABCDSubmit.bss节中的局部C变量不占实际存储空间E多选题50分ELF文件结构信息定义在/usr/include/elf.h,其中包括符号表信息ELF文件结构信息定义在/usr/include/elf.h,其中包括符号表信息教材示例本机elf.h部分再看示例/*main.c*/externintadd(int,int);intmain(){returnadd(20,13);}/*test.c*/intadd(inti,intj){intx=i+j;returnx;}/*main.c*/externintadd(int,int);intmain(){returnadd(20,13);}/*test.c*/intadd(inti,intj){intx=i+j;returnx;}/*main.c*/externintadd(int,int);intmain(){returnadd(20,13);}/*test.c*/intadd(inti,intj){intx=i+j;returnx;}/*main.c*/externintadd(int,int);intmain(){returnadd(20,13);}/*test.c*/intadd(inti,intj){intx=i+j;returnx;}$
gcc–g–o–p
main.c
test.c继续观察可执行文件p$
gcc–g–o–p
main.c
test.c继续观察可执行文件prel.text消失了!$
gcc–g–o–p
main.c
test.c继续观察可执行文件p链接符号全局符号Global由模块m定义并能被其他模块引用的符号例:非静态C函数和非静态的全局变量
外部符号External由其他模块定义并被模块m引用的全局符号
本地符号Local由模块m定义和引用的本地符号例:带静态属性的C函数和变量本地链接符号不是本地程序变量符号解析intbuf[2]={1,2};
intmain(){swap();return0;}main.cexternintbuf[];
int*bufp0=&buf[0];staticint*bufp1;voidswap(){inttemp;bufp1=&buf
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 疾控中心维修工检修维修安全操作规程
- 2026年健康管理师考试试题及答案
- 健康管理师考试试题(附答案)
- 危险化学品安全生产管理知识题库及答案
- 小班《我的身体会唱歌》游戏教案
- 人教版一年级科学上册比较测量预习 新学期预科精讲课件
- 2026企业AI HR选型实 用手册
- 2026年绿色建筑材料创新趋势报告:推动行业可持续发展
- 慢性病患者医疗救助申请
- 2026年半导体产业创新进展报告
- 全国广告设计师职业能力测试试题及真题
- 社区慢病临床路径循证医学证据构建
- 2026年考研政治真题及答案
- 有机化学官能团保护策略总结
- 小学数学教学目标与单元设计方案
- 未成年人纹身危害课件
- 2025新会计准则培训
- 急性肝衰竭教学课件
- 2025年武汉市光谷第三十二小学教师招聘若干人笔试参考题库附答案解析
- 原材料涨价汇报
- 口腔护士拔牙护理操作规范
评论
0/150
提交评论