The Linker Is Not a Magical Program

The Linker Is Not a Magical Program

Walter Bright

DEPRESSiNGLY OFTEN (happened to me again just before I wrote this), the view that many programmers have of the process of going from source code to a statically linked executable in a compiled language is:

1. Edit source code.

2. Compile source code into object files.

3. Something magical happens.

4. Run executable.

Step 3 is, of course, the linking step. Why would I say such an outrageous thing? I’ve been doing tech support for decades, and I get the following con- cerns again and again:

1. The linker says def is defined more than once.

2. The linker says abc is an unresolved symbol.

3. Why is my executable so large?

Followed by “What do I do now?” usually with the phrases “seems to” and “somehow” mixed in, and an aura of utter bafflement. It’s the “seems to” and “somehow” that indicate that the linking process is viewed as a magical pro- cess, presumably understandable only by wizards and warlocks. The process of compiling does not elicit these kinds of phrases, implying that programmers generally understand how compilers work, or at least what they do.

A linker is a stupid, pedestrian, straightforward program. All it does is concate- nate together the code and data sections of the object files, connect the references to symbols with their definitions, pull unresolved symbols out of the library, and write out an executable. That’s it. No spells! No magic! The tedium in writing a linker is typically all about decoding and generating the usually ridiculously over- complicated file formats, but that doesn’t change the essential nature of a linker.

??106 97 Things Every Programmer Should Know

?

???????????????So, let’s say the linker is saying def is defined more than once. Many program- ming languages, such as C, C++, and D, have both declarations and defini- tions. Declarations normally go into header files, like:

extern int iii;

which generates an external reference to the symbol iii. A definition, on the other hand, actually sets aside storage for the symbol, usually appears in the implementation file, and looks like this:

int iii = 3;

How many definitions can there be for each symbol? As in the film Highlander, there can be only one. So, what if a definition of iii appears in more than one implementation file?

// File a.c

int iii = 3;

// File b.c

double iii(int x) { return 3.7; }

The linker will complain about iii being multiply defined.

Not only can there be only one, there must be one. If iii appears only as a declaration, but never a definition, the linker will complain about iii being an unresolved symbol.

To determine why an executable is the size it is, take a look at the map file that linkers optionally generate. A map file is nothing more than a list of all the symbols in the executable, along with their addresses. This tells you what modules were linked in from the library, and the sizes of each module. Now you can see where the bloat is coming from. Often, there will be library mod- ules that you have no idea why were linked in. To figure it out, temporarily remove the suspicious module from the library, and relink. The undefined symbol error then generated will indicate who is referencing that module.

Although it is not always immediately obvious why you get a particular linker message, there is nothing magical about linkers. The mechanics are straightforward; it’s the details you have to figure out in each case.

时间: 2024-11-07 19:53:06

The Linker Is Not a Magical Program的相关文章

ARTS Week 17

Feb 17, 2020 ~ Feb 23, 2020 Algorithm Problem 205.Isomorphic Strings(同构字符串) 题目链接 题目描述:给定两个字符串 s 和 t,判断是否可以通过字符替换从 s 得到 t.顺序不可以改变,同时不允许两个字符映射到同一个字符,但允许一个字符映射到它自身.举例如下: Input: s = "egg", t = "add" Output: true Input: s = "foo",

The Portable Executable File Format from Top to Bottom(每个结构体都非常清楚)

The Portable Executable File Format from Top to Bottom Randy KathMicrosoft Developer Network Technology Group Created: June 12, 1993 Click to open or copy the files in the EXEVIEW sample application for this technical article. Click to open or copy t

Optimizing subroutine calls based on architecture level of called subroutine

A technique is provided for generating stubs. A processing circuit receives a call to a called function. The processing circuit retrieves a called function property of the called function. The processing circuit generates a stub for the called functi

Linking code for an enhanced application binary interface (ABI) with decode time instruction optimization

A code sequence made up multiple instructions and specifying an offset from a base address is identified in an object file. The offset from the base address corresponds to an offset location in a memory configured for storing an address of a variable

Linux Programe/Dynamic Shared Library Entry/Exit Point && Glibc Entry Point/Function

目录 1. 引言 2. C/C++运行库 3. 静态Glibc && 可执行文件 入口/终止函数 4. 动态Glibc && 可执行文件 入口/终止函数 5. 静态Glibc && 共享库 入口/终止函数 6. 动态Glibc && 共享库 入口/终止函数 1. 引言 0x1: glibc Any Unix-like operating system needs a C library: the library which defines t

使用CodeBlocks配置OpenGL开发环境

CodeBlocks版本:13.12 下载OpenGL配置文件 1.glut.dll glut32.dll放入系统盘Windows\System32文件夹 2.glut.h放入CodeBlocks安装目录下的MinGW\include\GL文件夹下 3.glut.lib glut32.lib放入MinGW\lib文件夹下. 新建OpenGL项目 1.新建GLUT project. 2.输入项目名称. 3.Please select GLUT's location-->设置为CodeBlocks安

Building C Projects

C is a compiled language, and as such, C programs need to be converted to executables in order to use them.  Typically, programmers do this with some sort of build system, rather than running the necessary commands manually, because the compilation p

hdu4941 Magical Forest (stl map)

2014多校7最水的题   Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 253    Accepted Submission(s): 120 Problem Description There is a forest can be seen as N * M gri

hdu4941 Magical Forest

Problem Description There is a forest can be seen as N * M grid. In this forest, there is some magical fruits, These fruits can provide a lot of energy, Each fruit has its location(Xi, Yi) and the energy can be provided Ci. However, the forest will m