链接分为静态链接和动态链接,静态链接使得不同的源文件可以互相调用,形成模块化,而动态链接则通过加载/运行时链接解决了共享库在不同应用中复制多份的浪费问题。
静态链接解决了不同程序之间模块化的问题。此外,修改一个源文件,不必重新编译整个应用程序,只需要编译修改的部分,再次链接即可。
例子:程序A.cpp可以调用B.cpp中的函数或者全局变量,下面通过一个简单的例子展示了从两个源文件到可执行的二进制文件的过程
#mermaid-svg-p3kZuQ38A06dCG9a .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-p3kZuQ38A06dCG9a .label text{fill:#333}#mermaid-svg-p3kZuQ38A06dCG9a .node rect,#mermaid-svg-p3kZuQ38A06dCG9a .node circle,#mermaid-svg-p3kZuQ38A06dCG9a .node ellipse,#mermaid-svg-p3kZuQ38A06dCG9a .node polygon,#mermaid-svg-p3kZuQ38A06dCG9a .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-p3kZuQ38A06dCG9a .node .label{text-align:center;fill:#333}#mermaid-svg-p3kZuQ38A06dCG9a .node.clickable{cursor:pointer}#mermaid-svg-p3kZuQ38A06dCG9a .arrowheadPath{fill:#333}#mermaid-svg-p3kZuQ38A06dCG9a .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-p3kZuQ38A06dCG9a .flowchart-link{stroke:#333;fill:none}#mermaid-svg-p3kZuQ38A06dCG9a .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-p3kZuQ38A06dCG9a .edgeLabel rect{opacity:0.9}#mermaid-svg-p3kZuQ38A06dCG9a .edgeLabel span{color:#333}#mermaid-svg-p3kZuQ38A06dCG9a .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-p3kZuQ38A06dCG9a .cluster text{fill:#333}#mermaid-svg-p3kZuQ38A06dCG9a div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-p3kZuQ38A06dCG9a .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-p3kZuQ38A06dCG9a text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-p3kZuQ38A06dCG9a .actor-line{stroke:grey}#mermaid-svg-p3kZuQ38A06dCG9a .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-p3kZuQ38A06dCG9a .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-p3kZuQ38A06dCG9a #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-p3kZuQ38A06dCG9a .sequenceNumber{fill:#fff}#mermaid-svg-p3kZuQ38A06dCG9a #sequencenumber{fill:#333}#mermaid-svg-p3kZuQ38A06dCG9a #crosshead path{fill:#333;stroke:#333}#mermaid-svg-p3kZuQ38A06dCG9a .messageText{fill:#333;stroke:#333}#mermaid-svg-p3kZuQ38A06dCG9a .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-p3kZuQ38A06dCG9a .labelText,#mermaid-svg-p3kZuQ38A06dCG9a .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-p3kZuQ38A06dCG9a .loopText,#mermaid-svg-p3kZuQ38A06dCG9a .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-p3kZuQ38A06dCG9a .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-p3kZuQ38A06dCG9a .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-p3kZuQ38A06dCG9a .noteText,#mermaid-svg-p3kZuQ38A06dCG9a .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-p3kZuQ38A06dCG9a .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-p3kZuQ38A06dCG9a .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-p3kZuQ38A06dCG9a .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-p3kZuQ38A06dCG9a .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-p3kZuQ38A06dCG9a .section{stroke:none;opacity:0.2}#mermaid-svg-p3kZuQ38A06dCG9a .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-p3kZuQ38A06dCG9a .section2{fill:#fff400}#mermaid-svg-p3kZuQ38A06dCG9a .section1,#mermaid-svg-p3kZuQ38A06dCG9a .section3{fill:#fff;opacity:0.2}#mermaid-svg-p3kZuQ38A06dCG9a .sectionTitle0{fill:#333}#mermaid-svg-p3kZuQ38A06dCG9a .sectionTitle1{fill:#333}#mermaid-svg-p3kZuQ38A06dCG9a .sectionTitle2{fill:#333}#mermaid-svg-p3kZuQ38A06dCG9a .sectionTitle3{fill:#333}#mermaid-svg-p3kZuQ38A06dCG9a .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-p3kZuQ38A06dCG9a .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-p3kZuQ38A06dCG9a .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-p3kZuQ38A06dCG9a .grid path{stroke-width:0}#mermaid-svg-p3kZuQ38A06dCG9a .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-p3kZuQ38A06dCG9a .task{stroke-width:2}#mermaid-svg-p3kZuQ38A06dCG9a .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-p3kZuQ38A06dCG9a .taskText:not([font-size]){font-size:11px}#mermaid-svg-p3kZuQ38A06dCG9a .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-p3kZuQ38A06dCG9a .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-p3kZuQ38A06dCG9a .task.clickable{cursor:pointer}#mermaid-svg-p3kZuQ38A06dCG9a .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-p3kZuQ38A06dCG9a .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-p3kZuQ38A06dCG9a .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-p3kZuQ38A06dCG9a .taskText0,#mermaid-svg-p3kZuQ38A06dCG9a .taskText1,#mermaid-svg-p3kZuQ38A06dCG9a .taskText2,#mermaid-svg-p3kZuQ38A06dCG9a .taskText3{fill:#fff}#mermaid-svg-p3kZuQ38A06dCG9a .task0,#mermaid-svg-p3kZuQ38A06dCG9a .task1,#mermaid-svg-p3kZuQ38A06dCG9a .task2,#mermaid-svg-p3kZuQ38A06dCG9a .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-p3kZuQ38A06dCG9a .taskTextOutside0,#mermaid-svg-p3kZuQ38A06dCG9a .taskTextOutside2{fill:#000}#mermaid-svg-p3kZuQ38A06dCG9a .taskTextOutside1,#mermaid-svg-p3kZuQ38A06dCG9a .taskTextOutside3{fill:#000}#mermaid-svg-p3kZuQ38A06dCG9a .active0,#mermaid-svg-p3kZuQ38A06dCG9a .active1,#mermaid-svg-p3kZuQ38A06dCG9a .active2,#mermaid-svg-p3kZuQ38A06dCG9a .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-p3kZuQ38A06dCG9a .activeText0,#mermaid-svg-p3kZuQ38A06dCG9a .activeText1,#mermaid-svg-p3kZuQ38A06dCG9a .activeText2,#mermaid-svg-p3kZuQ38A06dCG9a .activeText3{fill:#000 !important}#mermaid-svg-p3kZuQ38A06dCG9a .done0,#mermaid-svg-p3kZuQ38A06dCG9a .done1,#mermaid-svg-p3kZuQ38A06dCG9a .done2,#mermaid-svg-p3kZuQ38A06dCG9a .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-p3kZuQ38A06dCG9a .doneText0,#mermaid-svg-p3kZuQ38A06dCG9a .doneText1,#mermaid-svg-p3kZuQ38A06dCG9a .doneText2,#mermaid-svg-p3kZuQ38A06dCG9a .doneText3{fill:#000 !important}#mermaid-svg-p3kZuQ38A06dCG9a .crit0,#mermaid-svg-p3kZuQ38A06dCG9a .crit1,#mermaid-svg-p3kZuQ38A06dCG9a .crit2,#mermaid-svg-p3kZuQ38A06dCG9a .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-p3kZuQ38A06dCG9a .activeCrit0,#mermaid-svg-p3kZuQ38A06dCG9a .activeCrit1,#mermaid-svg-p3kZuQ38A06dCG9a .activeCrit2,#mermaid-svg-p3kZuQ38A06dCG9a .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-p3kZuQ38A06dCG9a .doneCrit0,#mermaid-svg-p3kZuQ38A06dCG9a .doneCrit1,#mermaid-svg-p3kZuQ38A06dCG9a .doneCrit2,#mermaid-svg-p3kZuQ38A06dCG9a .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-p3kZuQ38A06dCG9a .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-p3kZuQ38A06dCG9a .milestoneText{font-style:italic}#mermaid-svg-p3kZuQ38A06dCG9a .doneCritText0,#mermaid-svg-p3kZuQ38A06dCG9a .doneCritText1,#mermaid-svg-p3kZuQ38A06dCG9a .doneCritText2,#mermaid-svg-p3kZuQ38A06dCG9a .doneCritText3{fill:#000 !important}#mermaid-svg-p3kZuQ38A06dCG9a .activeCritText0,#mermaid-svg-p3kZuQ38A06dCG9a .activeCritText1,#mermaid-svg-p3kZuQ38A06dCG9a .activeCritText2,#mermaid-svg-p3kZuQ38A06dCG9a .activeCritText3{fill:#000 !important}#mermaid-svg-p3kZuQ38A06dCG9a .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-p3kZuQ38A06dCG9a g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-p3kZuQ38A06dCG9a g.classGroup text .title{font-weight:bolder}#mermaid-svg-p3kZuQ38A06dCG9a g.clickable{cursor:pointer}#mermaid-svg-p3kZuQ38A06dCG9a g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-p3kZuQ38A06dCG9a g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-p3kZuQ38A06dCG9a .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-p3kZuQ38A06dCG9a .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-p3kZuQ38A06dCG9a .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-p3kZuQ38A06dCG9a .dashed-line{stroke-dasharray:3}#mermaid-svg-p3kZuQ38A06dCG9a #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-p3kZuQ38A06dCG9a #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-p3kZuQ38A06dCG9a #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-p3kZuQ38A06dCG9a #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-p3kZuQ38A06dCG9a #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-p3kZuQ38A06dCG9a #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-p3kZuQ38A06dCG9a #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-p3kZuQ38A06dCG9a #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-p3kZuQ38A06dCG9a .commit-id,#mermaid-svg-p3kZuQ38A06dCG9a .commit-msg,#mermaid-svg-p3kZuQ38A06dCG9a .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-p3kZuQ38A06dCG9a .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-p3kZuQ38A06dCG9a .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-p3kZuQ38A06dCG9a g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-p3kZuQ38A06dCG9a g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-p3kZuQ38A06dCG9a g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-p3kZuQ38A06dCG9a g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-p3kZuQ38A06dCG9a g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-p3kZuQ38A06dCG9a g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-p3kZuQ38A06dCG9a .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-p3kZuQ38A06dCG9a .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-p3kZuQ38A06dCG9a .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-p3kZuQ38A06dCG9a .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-p3kZuQ38A06dCG9a .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-p3kZuQ38A06dCG9a .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-p3kZuQ38A06dCG9a .edgeLabel text{fill:#333}#mermaid-svg-p3kZuQ38A06dCG9a .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-p3kZuQ38A06dCG9a .node circle.state-start{fill:black;stroke:black}#mermaid-svg-p3kZuQ38A06dCG9a .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-p3kZuQ38A06dCG9a #statediagram-barbEnd{fill:#9370db}#mermaid-svg-p3kZuQ38A06dCG9a .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-p3kZuQ38A06dCG9a .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-p3kZuQ38A06dCG9a .statediagram-state .divider{stroke:#9370db}#mermaid-svg-p3kZuQ38A06dCG9a .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-p3kZuQ38A06dCG9a .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-p3kZuQ38A06dCG9a .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-p3kZuQ38A06dCG9a .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-p3kZuQ38A06dCG9a .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-p3kZuQ38A06dCG9a .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-p3kZuQ38A06dCG9a .note-edge{stroke-dasharray:5}#mermaid-svg-p3kZuQ38A06dCG9a .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-p3kZuQ38A06dCG9a .error-icon{fill:#522}#mermaid-svg-p3kZuQ38A06dCG9a .error-text{fill:#522;stroke:#522}#mermaid-svg-p3kZuQ38A06dCG9a .edge-thickness-normal{stroke-width:2px}#mermaid-svg-p3kZuQ38A06dCG9a .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-p3kZuQ38A06dCG9a .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-p3kZuQ38A06dCG9a .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-p3kZuQ38A06dCG9a .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-p3kZuQ38A06dCG9a .marker{fill:#333}#mermaid-svg-p3kZuQ38A06dCG9a .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-p3kZuQ38A06dCG9a { color: rgba(0, 0, 0, 0.75); font: ; } 预处理器 编译器 汇编器 预处理器 编译器 汇编器 main.c sum.c main.i sum.i main.s sum.s main.o sum.o 链接器 可执行的目标文件以main.c为例,编译通常:
通过预处理器将 .c 文件翻译为一个 ASCII 码的 .i 文件(中间文件)通过编译器将 .i 文件翻译为一个 ASCII 码的 .s 汇编语言代码文件通过汇编器将 .s 文件翻译为一个二进制的 .o ,即可重定位的目标文件链接器将 .o 和其他 .o ,以及一些必要的系统目标文件组合,生成最终的可执行二进制文件目标文件分为三类:
可执行的目标文件:可以直接被复制进内存并且执行可重定位的目标文件:包含二进制代码和数据,可以在编译时和其他可重定位目标文件合并,创建一个可执行目标文件共享目标文件:特殊的可重定位目标文件,可以在加载或者运行时被动态地加载进内存并且链接编译器和汇编器生成可重定位的目标文件,而链接器生成可执行的目标文件。
可重定位的目标文件由以下几个部分组成: 下面给出各个部分的注解:
名称英文全称注解.texttext已编译程序的机器代码.rodataread only data只读数据,比如printf的格式化字符串,switch语句的跳转表.datadata已初始化的全局/静态C变量.bssblock storage start未初始化的全局/静态C变量.symtabsymbol table符号表,存放定义和引用的 函数/变量 信息.rel.textrelocated texttext节中需要做重定位修改的指令位置的列表.rel.datarelocated data被 引用/定义 的所有全局变量的重定位信息静态链接程序通常以一组可重定位的目标文件(.o文件)为输入,输出一个可执行的目标文件。
静态链接通常分为两个步骤:
符号解析重定位符号解析通常是对变量命函数名的解析,将符号引用和其定义关联起来。
而重定位则是将代码中涉及内存位置的操作,比如 jmp (pc+偏移)跳转语句,在 .o 文件中,这些位置通常都是相对每个 .o 的起始位置,而在合并 .o 之后,位置发生改变,需要重新定位。
链接器符号分为三类:
全局符号:可以被其他模块引用的符号,如非静态函数和全局变量局部符号:只由当前模块定义并引用的本地符号,比如静态函数和静态全局变量外部符号:当前模块引用外部模块的符号符号解析的重点在于解析同名符号,同名符号又通过强弱来区分:
函数名 和 已初始化的全局变量 是强符号未初始化的全局变量是弱符号链接器通过如下的规则进行同名符号解析:
不允许多个同名强符号,否则链接器报错同时拥有同名强符号和弱符号,那么对弱符号的引用,解析为指向强符号的引用如果同时存在多个同名弱符号,那么随机选择一个下面的图表描述了弱弱符号和强弱符号共存时引用的情况:
更加详细的版本【符号引用重定位 重定位PC相对引用 简单讲解】
符号解析完成之后,需要进行重定位。因为将不同的 .o 合并,而每个 .o 中的符号位置都是相对该 .o 而言的,所以需要进行重定位。
重定位分为三个步骤:
合并相同的节,比如将所有 .o 的 .text 节合并为可执行文件中新的 .text 节对符号定义进行重定位,即确定新节中所有定义符号在虚拟地址空间中的地址(相对该新节的地址),比如为函数确定首地址,为变量确定首地址对引用的符号(比如变量和函数)进行重定位,需要用到 rel.data 和 rel.text 节中的信息重定位条目:
重定位相当简单。假设main.c调用sum.c,以call指令(函数调用)PC+偏移的重定位为例,其实就是计算一下两者之间地址的差值即可,注意加上 r.addend 以补偿call指令的PC:
书上公式: 图解:
老式的静态链接解决方案,将所有 .o 文件打包为一个单独的库文件即 .a 文件(archive,存档文件)
如图:编译 atoi, printf, random函数等等,成libc.a共享库
在构建可执行文件时,只需指定库文件名,然后链接器自动去对应的库中找用到的模块,并且只拷贝用到的模块。
链接到静态库时,通过如下的机制对外部引用进行解析:
按照命令行给出的顺序扫描 .o 和 .a 文件扫描时遇到未解析的引用则存入临时表U每遇到一个新的 .a / .o 都试图用其中的符号解析未知符号表U中的符号如果扫描到最后,U中还有未知符号,则报错。
和静态库链接,也要按照基本法 引用解析机制,因为命令行给出的顺序不同,很可能导致不同的结果:
比如以下的互相调用的情况
#mermaid-svg-JtwJQCALj528gcub .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-JtwJQCALj528gcub .label text{fill:#333}#mermaid-svg-JtwJQCALj528gcub .node rect,#mermaid-svg-JtwJQCALj528gcub .node circle,#mermaid-svg-JtwJQCALj528gcub .node ellipse,#mermaid-svg-JtwJQCALj528gcub .node polygon,#mermaid-svg-JtwJQCALj528gcub .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-JtwJQCALj528gcub .node .label{text-align:center;fill:#333}#mermaid-svg-JtwJQCALj528gcub .node.clickable{cursor:pointer}#mermaid-svg-JtwJQCALj528gcub .arrowheadPath{fill:#333}#mermaid-svg-JtwJQCALj528gcub .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-JtwJQCALj528gcub .flowchart-link{stroke:#333;fill:none}#mermaid-svg-JtwJQCALj528gcub .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-JtwJQCALj528gcub .edgeLabel rect{opacity:0.9}#mermaid-svg-JtwJQCALj528gcub .edgeLabel span{color:#333}#mermaid-svg-JtwJQCALj528gcub .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-JtwJQCALj528gcub .cluster text{fill:#333}#mermaid-svg-JtwJQCALj528gcub div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-JtwJQCALj528gcub .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-JtwJQCALj528gcub text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-JtwJQCALj528gcub .actor-line{stroke:grey}#mermaid-svg-JtwJQCALj528gcub .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-JtwJQCALj528gcub .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-JtwJQCALj528gcub #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-JtwJQCALj528gcub .sequenceNumber{fill:#fff}#mermaid-svg-JtwJQCALj528gcub #sequencenumber{fill:#333}#mermaid-svg-JtwJQCALj528gcub #crosshead path{fill:#333;stroke:#333}#mermaid-svg-JtwJQCALj528gcub .messageText{fill:#333;stroke:#333}#mermaid-svg-JtwJQCALj528gcub .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-JtwJQCALj528gcub .labelText,#mermaid-svg-JtwJQCALj528gcub .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-JtwJQCALj528gcub .loopText,#mermaid-svg-JtwJQCALj528gcub .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-JtwJQCALj528gcub .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-JtwJQCALj528gcub .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-JtwJQCALj528gcub .noteText,#mermaid-svg-JtwJQCALj528gcub .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-JtwJQCALj528gcub .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-JtwJQCALj528gcub .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-JtwJQCALj528gcub .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-JtwJQCALj528gcub .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-JtwJQCALj528gcub .section{stroke:none;opacity:0.2}#mermaid-svg-JtwJQCALj528gcub .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-JtwJQCALj528gcub .section2{fill:#fff400}#mermaid-svg-JtwJQCALj528gcub .section1,#mermaid-svg-JtwJQCALj528gcub .section3{fill:#fff;opacity:0.2}#mermaid-svg-JtwJQCALj528gcub .sectionTitle0{fill:#333}#mermaid-svg-JtwJQCALj528gcub .sectionTitle1{fill:#333}#mermaid-svg-JtwJQCALj528gcub .sectionTitle2{fill:#333}#mermaid-svg-JtwJQCALj528gcub .sectionTitle3{fill:#333}#mermaid-svg-JtwJQCALj528gcub .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-JtwJQCALj528gcub .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-JtwJQCALj528gcub .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-JtwJQCALj528gcub .grid path{stroke-width:0}#mermaid-svg-JtwJQCALj528gcub .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-JtwJQCALj528gcub .task{stroke-width:2}#mermaid-svg-JtwJQCALj528gcub .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-JtwJQCALj528gcub .taskText:not([font-size]){font-size:11px}#mermaid-svg-JtwJQCALj528gcub .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-JtwJQCALj528gcub .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-JtwJQCALj528gcub .task.clickable{cursor:pointer}#mermaid-svg-JtwJQCALj528gcub .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-JtwJQCALj528gcub .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-JtwJQCALj528gcub .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-JtwJQCALj528gcub .taskText0,#mermaid-svg-JtwJQCALj528gcub .taskText1,#mermaid-svg-JtwJQCALj528gcub .taskText2,#mermaid-svg-JtwJQCALj528gcub .taskText3{fill:#fff}#mermaid-svg-JtwJQCALj528gcub .task0,#mermaid-svg-JtwJQCALj528gcub .task1,#mermaid-svg-JtwJQCALj528gcub .task2,#mermaid-svg-JtwJQCALj528gcub .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-JtwJQCALj528gcub .taskTextOutside0,#mermaid-svg-JtwJQCALj528gcub .taskTextOutside2{fill:#000}#mermaid-svg-JtwJQCALj528gcub .taskTextOutside1,#mermaid-svg-JtwJQCALj528gcub .taskTextOutside3{fill:#000}#mermaid-svg-JtwJQCALj528gcub .active0,#mermaid-svg-JtwJQCALj528gcub .active1,#mermaid-svg-JtwJQCALj528gcub .active2,#mermaid-svg-JtwJQCALj528gcub .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-JtwJQCALj528gcub .activeText0,#mermaid-svg-JtwJQCALj528gcub .activeText1,#mermaid-svg-JtwJQCALj528gcub .activeText2,#mermaid-svg-JtwJQCALj528gcub .activeText3{fill:#000 !important}#mermaid-svg-JtwJQCALj528gcub .done0,#mermaid-svg-JtwJQCALj528gcub .done1,#mermaid-svg-JtwJQCALj528gcub .done2,#mermaid-svg-JtwJQCALj528gcub .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-JtwJQCALj528gcub .doneText0,#mermaid-svg-JtwJQCALj528gcub .doneText1,#mermaid-svg-JtwJQCALj528gcub .doneText2,#mermaid-svg-JtwJQCALj528gcub .doneText3{fill:#000 !important}#mermaid-svg-JtwJQCALj528gcub .crit0,#mermaid-svg-JtwJQCALj528gcub .crit1,#mermaid-svg-JtwJQCALj528gcub .crit2,#mermaid-svg-JtwJQCALj528gcub .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-JtwJQCALj528gcub .activeCrit0,#mermaid-svg-JtwJQCALj528gcub .activeCrit1,#mermaid-svg-JtwJQCALj528gcub .activeCrit2,#mermaid-svg-JtwJQCALj528gcub .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-JtwJQCALj528gcub .doneCrit0,#mermaid-svg-JtwJQCALj528gcub .doneCrit1,#mermaid-svg-JtwJQCALj528gcub .doneCrit2,#mermaid-svg-JtwJQCALj528gcub .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-JtwJQCALj528gcub .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-JtwJQCALj528gcub .milestoneText{font-style:italic}#mermaid-svg-JtwJQCALj528gcub .doneCritText0,#mermaid-svg-JtwJQCALj528gcub .doneCritText1,#mermaid-svg-JtwJQCALj528gcub .doneCritText2,#mermaid-svg-JtwJQCALj528gcub .doneCritText3{fill:#000 !important}#mermaid-svg-JtwJQCALj528gcub .activeCritText0,#mermaid-svg-JtwJQCALj528gcub .activeCritText1,#mermaid-svg-JtwJQCALj528gcub .activeCritText2,#mermaid-svg-JtwJQCALj528gcub .activeCritText3{fill:#000 !important}#mermaid-svg-JtwJQCALj528gcub .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-JtwJQCALj528gcub g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-JtwJQCALj528gcub g.classGroup text .title{font-weight:bolder}#mermaid-svg-JtwJQCALj528gcub g.clickable{cursor:pointer}#mermaid-svg-JtwJQCALj528gcub g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-JtwJQCALj528gcub g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-JtwJQCALj528gcub .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-JtwJQCALj528gcub .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-JtwJQCALj528gcub .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-JtwJQCALj528gcub .dashed-line{stroke-dasharray:3}#mermaid-svg-JtwJQCALj528gcub #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-JtwJQCALj528gcub #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-JtwJQCALj528gcub #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-JtwJQCALj528gcub #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-JtwJQCALj528gcub #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-JtwJQCALj528gcub #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-JtwJQCALj528gcub #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-JtwJQCALj528gcub #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-JtwJQCALj528gcub .commit-id,#mermaid-svg-JtwJQCALj528gcub .commit-msg,#mermaid-svg-JtwJQCALj528gcub .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-JtwJQCALj528gcub .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-JtwJQCALj528gcub .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-JtwJQCALj528gcub g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-JtwJQCALj528gcub g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-JtwJQCALj528gcub g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-JtwJQCALj528gcub g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-JtwJQCALj528gcub g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-JtwJQCALj528gcub g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-JtwJQCALj528gcub .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-JtwJQCALj528gcub .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-JtwJQCALj528gcub .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-JtwJQCALj528gcub .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-JtwJQCALj528gcub .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-JtwJQCALj528gcub .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-JtwJQCALj528gcub .edgeLabel text{fill:#333}#mermaid-svg-JtwJQCALj528gcub .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-JtwJQCALj528gcub .node circle.state-start{fill:black;stroke:black}#mermaid-svg-JtwJQCALj528gcub .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-JtwJQCALj528gcub #statediagram-barbEnd{fill:#9370db}#mermaid-svg-JtwJQCALj528gcub .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-JtwJQCALj528gcub .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-JtwJQCALj528gcub .statediagram-state .divider{stroke:#9370db}#mermaid-svg-JtwJQCALj528gcub .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-JtwJQCALj528gcub .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-JtwJQCALj528gcub .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-JtwJQCALj528gcub .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-JtwJQCALj528gcub .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-JtwJQCALj528gcub .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-JtwJQCALj528gcub .note-edge{stroke-dasharray:5}#mermaid-svg-JtwJQCALj528gcub .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-JtwJQCALj528gcub .error-icon{fill:#522}#mermaid-svg-JtwJQCALj528gcub .error-text{fill:#522;stroke:#522}#mermaid-svg-JtwJQCALj528gcub .edge-thickness-normal{stroke-width:2px}#mermaid-svg-JtwJQCALj528gcub .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-JtwJQCALj528gcub .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-JtwJQCALj528gcub .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-JtwJQCALj528gcub .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-JtwJQCALj528gcub .marker{fill:#333}#mermaid-svg-JtwJQCALj528gcub .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-JtwJQCALj528gcub { color: rgba(0, 0, 0, 0.75); font: ; } 调用 调用 调用 调用 func.o libx.a liby.a则应该使用如下的命令行(即在最后补上一个x,解析U中的y的符号,从而形成闭环)
静态库及其静态链接有很多缺点,比如:
n个程序链接到库,库需要复制n份修改系统库,需要每个应用程序重新链接(这对系统的更新是坏的)所以出现动态链接及共享库的概念:
共享库的优点有:
共享库链接过程: 后面的不考?咕了
以下哪个工作不属于动态链接器完成的 ?
A、完成对引用的动态库函数在进程空间的布局。 B、完成动态库函数的编译和汇编。 C、完成可执行文件对动态库函数引用的符号解释。 D、完成可执行文件对动态库函数引用的地址确定。答:编译和汇编是编译阶段的工作,不是链接阶段的,所以选B
ELF格式可用来保存哪些类型的目标文件?
答:可重定位的目标文件、可执行的目标文件、共享目标文件答: m1中int x是强符号,m2中float x 是弱符号,所以绑定到强符号,A正确,B错误。
p1函数被分配到内存中代码区域,p1函数中的临时变量 p1 被分配到栈中,C正确
x,main和p1都不违反强符号只能出现一次的法则,故合法,D正确
假设调用关系如下:hello.c调用libx.a和liby.a中的函数,liby.a调用了libz.a的函数,libz.a调动了libx.a和liby.a中的函数,以下编译正确的是 。
A、gcc –o hello hello.c ./libx.a ./liby.a ./libz.a ./libx.a B、gcc –o hello ./libx.a ./liby.a ./libz.a ./libx.a ./liby.a hello.c C、gcc –o hello hello.c ./libx.a ./liby.a ./libz.a ./libx.a ./liby.a D、gcc –o hello hello.c ./libx.a ./liby.a ./libz.a答:由库链接时的符号解析机制(即U未知符号表的读写)可知,在最后解析 libz.a之后,会存在 libx, liby的符号未被解析,因此需再次添加 libx, liby,所以选 C
可重定位目标文件中机器代码、只读数据、未初始化的全局变量以及已初始化数据通常分别保存在哪些节里?
答: 机器代码:.text 只读数据:.rodata 未初始化的全局变量:.bss 初始化数据:.data