プリンキピア

第4章アルゴリズムの言語による実現:低水準プログラミング言語

Symbol Types and Resolutionシンボルの種類と解決
A linker classifies symbols in three categories as illustrated in Figure 4.23 and Figure 4.24. A symbol can be the name of a variable or a string. In other cases, it can be the function names or procedure, such as
リンカーは、図4.23と図4.24に示すように、シンボルを3種類に分類する。シンボルは変数名や文字列である場合がある。また、関数名や手続きである場合もある。たとえば次のようなものがある。
• Global symbols: Symbols defined by module m that can be referenced by other modules (e.g., non-static C functions and non-static global variables)
• グローバルシンボル:モジュールmが定義し、ほかのモジュールから参照できるシンボル(例:staticでないCの関数と、staticでないグローバル変数)。
• External symbols: Global symbols that are referenced by module m but defined by some other module.
• 外部シンボル:モジュールmが参照するが、別のモジュールが定義するグローバルシンボル。
• Local symbols: Symbols that are defined and referenced exclusively by module m (e.g., C functions and global variables defined with the static attribute); local linker symbols are not local program variables (linker does not deal with the local variables of a function). Also note that local non-static C variables are stored on the stack while local static C variables are stored in either .bss, or .data.
英語のヒント
• ローカルシンボル:モジュールmだけが定義し、参照するシンボル(例:static属性を付けて定義したCの関数とグローバル変数)。リンカーのローカルシンボルは、プログラムのローカル変数ではない。リンカーは、関数のローカル変数を扱わない。また、Cのstaticでないローカル変数はスタックに格納されるが、staticなローカル変数は.bssまたは.dataに格納されることにも注意しよう。
Figure 4.23

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 4.23 The code shown illustrates how the linker identifies local and global symbols. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図4.23 ここに示すコードは、リンカーがローカルシンボルとグローバルシンボルをどう識別するかを示している。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
Figure 4.24

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 4.24 This diagram illustrates how various symbols are organized in .text and .data segments within relocatable object files and are mapped into the .text and .data segment by the linker to create executable object files. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図4.24 さまざまなシンボルが、再配置可能オブジェクトファイル内の.textセグメントと.dataセグメントにどう配置され、実行可能オブジェクトファイルを作る際、リンカーによって.textセグメントと.dataセグメントへどう対応付けられるかを示す。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
Program symbols are either strong (e.g., procedures and initialized globals) or weak (e.g., uninitialized globals). A strong symbol has a unique memory location. Let’s take the example of: int array[2] = {1 ,2};. This creates an ambiguity during the linking process when there is another file that tries to access the same symbol again due to strong definition. On the other hand, a weak symbol allows multiple definitions of the same symbol without creating an ambiguity. This helps during the linking process when another file creates a strong definition of the same name. In languages like C and C++, the weak symbol is defined using the _attribute_((weak)) keyword. Global variables should be avoided (i.e., use static whenever you can, initialize the global variable, or use extern if you reference an external global variable).
英語のヒント
プログラムのシンボルは、強いシンボル(たとえば手続きや、初期化済みのグローバル変数)か、弱いシンボル(たとえば未初期化のグローバル変数)のいずれかである。強いシンボルには、一意のメモリ位置がある。int array[2] = {1 ,2};を例にしよう。強い定義によって、別のファイルが同じシンボルへ再びアクセスしようとすると、リンク処理で曖昧さが生じる。弱いシンボルでは、曖昧さを生まずに同じシンボルを複数定義できる。これは、別のファイルが同じ名前の強い定義を作る場合、リンク処理の助けとなる。CやC++のような言語では、_attribute_((weak))キーワードで弱いシンボルを定義する。グローバル変数は避けるべきである。つまり、可能ならstaticを使い、グローバル変数を初期化し、外部のグローバル変数を参照するならexternを使う。
The linker applies the following rules:
リンカーは、次の規則を適用する。
• Rule 1: Multiple strong symbols are not allowed. Each item can be defined only once, otherwise the linker issues an error.
• 規則1:強いシンボルを複数定義することはできない。各項目は1度だけ定義でき、それ以外の場合、リンカーはエラーを出す。
• Rule 2: Given a strong symbol and multiple weak symbols, choose the strong symbol (references to the weak symbol resolve to the strong symbol).
• 規則2:強いシンボルと複数の弱いシンボルが与えられた場合、強いシンボルを選ぶ。弱いシンボルへの参照は、強いシンボルへ解決される。
• Rule 3: If there are multiple weak symbols, pick an arbitrary one (can override this with gcc –fno-common). “-fno-common” helps in catching accidental common name collisions.
英語のヒント
• 規則3:弱いシンボルが複数ある場合、任意の1つを選ぶ。gcc –fno-commonでこの動作を変更できる。「-fno-common」は、意図せず共通の名前が衝突することを検出するのに役立つ。
Static Libraries静的ライブラリー
Functions commonly used by programmers (e.g., math, I/O, memory management, string manipulation) can be packaged into a file called a library. A static library (or .a, an archive file) is a simple kind of library that that copies the contents of object files into a single file called an archive. The linker tries to resolve unresolved external references by looking for the symbols in one or more archives. An external reference is a symbol that is used in a module, but not defined in that module, so it is expected to be defined in some other module. If an archive member file resolves a reference, the linker links it into the executable. The archiver allows incremental updates; it also recompiles functions that changed and replaces the corresponding .o file in the archive (Figure 4.25).
英語のヒント
プログラマーがよく使う関数(数学、入出力、メモリ管理、文字列操作など)は、ライブラリーと呼ぶファイルにまとめられる。静的ライブラリー(または.a、アーカイブファイル)は、オブジェクトファイルの内容をアーカイブという1つのファイルへコピーする、単純な種類のライブラリーである。リンカーは、1つ以上のアーカイブ内でシンボルを探し、未解決の外部参照を解決しようとする。外部参照とは、あるモジュールで使うが、そのモジュールでは定義されず、別のモジュールで定義されることを想定するシンボルである。アーカイブのメンバーファイルが参照を解決すれば、リンカーはそのファイルを実行可能ファイルへリンクする。アーカイバーは差分更新を可能にする。また、変更された関数を再コンパイルし、アーカイブ内の対応する.oファイルを置き換える(図4.25)。
Figure 4.25

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 4.25 The diagram illustrates how the ar archiver utility is used to create a sample version of the libc.a static library that only includes the atoi.o, printf.o, and random.o object files. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図4.25 arアーカイバーユーティリティーを使って、atoi.o、printf.o、random.oというオブジェクトファイルだけを含む、libc.a静的ライブラリーの例を作る方法を示す。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
Commonly used libraries include libc.a (the C standard library), which handles: I/O, memory allocation, signal handling, string handling, data and time, random numbers, and integer math. Another common library is libm.a (the C math library) that handles floating point math (e.g., sin, cos, tan, log, exp, sqrt).
英語のヒント
よく使うライブラリーには、libc.a(Cの標準ライブラリー)がある。これは、入出力、メモリ割当て、シグナル処理、文字列処理、データと時刻、乱数、整数演算を扱う。もう1つのよく使うライブラリーはlibm.a(Cの数学ライブラリー)で、浮動小数点演算(たとえばsin、cos、tan、log、exp、sqrt)を扱う。
Figure 4.26 illustrates how to link programs with static libraries.
図4.26は、プログラムを静的ライブラリーとリンクする方法を示す。
Figure 4.26

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 4.26 This diagram demonstrates the creation of an executable file using various static libraries. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図4.26 さまざまな静的ライブラリーを使った、実行可能ファイルの作成を示す。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
The linker uses the following algorithm to resolve external references:
リンカーは、次のアルゴリズムで外部参照を解決する。
1. Scan .o files and .a files in the command line order.
1. コマンドラインに並ぶ順序で、.oファイルと.aファイルを走査する。
2. During the scan, keep a list of the current unresolved references.
2. 走査中は、その時点で未解決の参照のリストを保持する。
3. As each new .o or .a file, obj, is encountered, try to resolve each unresolved reference in the list against the symbols defined in obj.
英語のヒント
3. 新しい.oまたは.aファイルobjに出会うたび、リスト内の未解決の各参照を、objで定義されたシンボルに照らして解決しようとする。
4. If any entries in the unresolved list at end of scan, then issue error.
4. 走査の終了時に未解決リストへ項目が残っていれば、エラーを出す。
Therefore, the command line order matters and libraries should be placed at the end of the command line to avoid linker errors as illustrated in Figure 4.27.
したがって、コマンドライン上の順序は重要である。図4.27に示すようなリンカーエラーを避けるため、ライブラリーはコマンドラインの最後に置くべきである。
Figure 4.27

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 4.27 Notice the link errors caused due to the incorrect order of the files. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図4.27 ファイルの順序が誤っているために起きるリンクエラーに注目しよう。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)