概述: 

  在linux上,从c源码到可执行文件主要需要经历translator(compiler、assembler)生成object file,再经由linker连接成executable object file。今天来研究下linking这一步。

ELF(Executable and Locatable File):

  Object file分三种:relocatable object file, executable object file, shared object file。

  Object file在各种系统上的格式并不一样,在linux上则为elf。Linux上典型的relocatable object file如图:

  分成sections和section header table。 主要是.text(存放已编译程序的机器代码) .data(已初始化的global variables) .symtab(symbol table)。

  Linker的处理对象就是relocatable object file, 主要工作为两个:1、symbol resolution; 2、relocation。

1、symbol resolution

  symbol分三种:

  1、(without static) Global symbols that are defined by module m and can be referenced by other modules;

  2、(extern) Global symbols that are referenced by module m but defined by some other other module;

  3、(static) Local symbols that are defined and referenced exclusively by module m.

  每个object module都有一个symbol table(即elf中.symtab),里面记载了它所定义和引用的symbol。Symbol resolution就是将所有这些object module的引用和定义一一对应起来。

aside:如果出现multiply defined global symbols,也就是所引用的symbol在多处定义,优先选择function和有初始化的全局变量,它们称为strong symbols。如果是未初始化的多处定义,则随机选择,这自然是不好的。我们写程序时应该避免同名的情况。

2、Relocation:

  Relocation分为两步,先合并section和symbol definitions并分配run-time address, 再更改那些symbol references的地址

  

  经过一番努力,终于得到了executable object files,可以直接加载到内存中运行了。

Dynamic Linking:

  前面说的static linking是在发生在编译时,而dynamic linking则是在程序载入内存后。Shared library(在Linux上一般是.so后缀,而在windows上则是DLL)就是动态加载的。

  在编译时所用到的shared library中实际的数据和代码并不会被复制到可执行文件中,复制的是一些relocation and symbol table information。这些信息使得程序可在运行中动态加载所需的数据和代码。

随想:

  因为以前没有接触过这种比较底层的知识,所以乍看之下信息量比较大,虽然很有趣,但脑袋也有点晕了。还是应该抓住重点,一些细节可以以后再回顾。

  但是经过这番努力,对计算机系统的一些比较底层的原理有了进一步了解。毕竟他是我的好朋友,总是那么兢兢业业、诚恳忠实,对这样的朋友有更深了解总是一件令人开心的事(^_^)也能促进我们今后更好的合作嘛!

Chapter 7:Linking的更多相关文章

  1. 1.JVM前奏篇(看官网怎么说)

    JVM(Java Virtual Machine) 前奏篇(看官网规范怎么说) 1.The relation of JDK/JRE/JVM 在下图中,我们所接触的,最熟悉,也是经常打交道的 最顶层 J ...

  2. Notes for Apue —— chapter 4 Files and Directories(文件和目录)

    4.1 Introduction 4.2 stat, fstat, fstatat, and lstat Functions The lstat function is similar to stat ...

  3. JVM Specification 9th Edition (4) Chapter 4. The class File Format

    Chapter 4. The class File Format Table of Contents 4.1. The ClassFile Structure 4.2. Names 4.2.1. Bi ...

  4. JVM Specification 9th Edition (3) Chapter 2. The Structure of the Java Virtual Machine

    Chapter 2. The Structure of the Java Virtual Machine 内容列表 2.1. The class File Format (class文件的格式) 2. ...

  5. JVM Specification 9th Edition (2) Chapter 1. Introduction

    Chapter 1. Introduction 翻译太累了,我就这样的看英文吧. 内容列表 1.1. A Bit of History 1.2. The Java Virtual Machine 1. ...

  6. Beginning Linux Programming 学习--chapter 17 Programming KDE using QT

    KDE: KDE,K桌面环境(K Desktop Environment)的缩写.一种著名的运行于 Linux.Unix 以及FreeBSD 等操作系统上的自由图形桌面环境,整个系统采用的都是 Tro ...

  7. Modern C++ CHAPTER 2(读书笔记)

    CHAPTER 2 Recipe 2-1. Initializing Variables Recipe 2-2. Initializing Objects with Initializer Lists ...

  8. Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- ApiWrapper

    前面两片文章讲解了通过AIDL和Messenger两种方式实现Android IPC.而本文所讲的并不是第三种IPC方式,而是对前面两种方式进行封装,这样我们就不用直接把Aidl文件,java文件拷贝 ...

  9. Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- Messenger

    Messenger类实际是对Aidl方式的一层封装.本文只是对如何在Service中使用Messenger类实现与客户端的通信进行讲解,对Messenger的底层不做说明.阅读Android Prog ...

随机推荐

  1. d3力导向图聚焦

    效果描述 双击节点,节点以及节点一度关联的节点保持高亮状态,其余节点变灰,半径变小,文字消失,并且向内收缩. 效果展示 正常状态 聚焦效果 关键代码 节点变化 激活节点保持高亮的样式,其余节点应用no ...

  2. AspectCore中的IoC容器和依赖注入

    IOC模式和依赖注入是近年来非常流行的一种模式,相信大家都不陌生了,在Asp.Net Core中提供了依赖注入作为内置的基础设施,如果仍不熟悉依赖注入的读者,可以看看由我们翻译的Asp.Net Cor ...

  3. 62、django之MTV模型(urls,view)

    今天就进入到python最重要的阶段了django框架,框架就像胶水一样会将我们前面学的所有知识点粘合在一起,所以以前有哪些部分模糊的可以看看前面的随笔.本篇主要介绍djangoMTV模型,视图层之路 ...

  4. (转)Java开发中的23种设计模式详解

    原文出自:http://blog.csdn.net/zhangerqing 一.设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型 ...

  5. Memcached统计命令

    1. Memcached stats命令: Memcached stats 命令用于返回统计信息例如 PID(进程号).版本号.连接数等. 语法: stats 输出信息说明: pid: memcach ...

  6. LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)$

    Design and implement a data structure for a compressed string iterator. It should support the follow ...

  7. IsoAlgo3d - A PCF 3D Viewer for Desktop, Tablet and Smart phone

    IsoAlgo3d - A PCF 3D Viewer for Desktop, Tablet and Smart phone eryar@163.com Abstract. IsoAlgo3d 通过 ...

  8. 349B - Color the Fence

    Color the Fence Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  9. 暑假练习赛 006 B Bear and Prime 100

    Bear and Prime 100Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:262144KB ...

  10. 关于easyui隐藏后数据不能刷新??

    原因是div用display属性隐藏后不能重新加载table数据 解决方法:使用hide()方法在初始化时隐藏 $("#two").hide(); //点击按钮隐藏与显示表单域 $ ...