[CTAP] 4# Linkage

문연수·2022년 8월 17일
0

CTAP

목록 보기
5/9

0. Terminologies

  • object module: that produced by a compiler or assembler
  • load module, executable file: linker combines serveral object modules
  • external object: a linker typically views an object module as containing a collection of exeternal objects
  • external name: Each external object represents the contents of some part of the machine's memory and is identified by an external name.
  • reference: object modules may contain references to external objects in other modules.

1. Words

  • readily: 손쉽게, 순조롭게, 선뜻, 기꺼이
  • over-emphasize: 지나치게 강조하다.
  • up to:
    1. (특정한 수, 정도 등) 까지
    2. (특정한 위치, 시점) 까지
    3. (특정한 기준, 수준) 만큼
    4. (육체적, 정신적으로) ~할 수 있는
    5. (특히 나쁜 짓으로) 하고 있는
  • clash: 충돌, 언쟁, 맞서다, 의견충돌을 일으키다, 언쟁을 벌이다.
  • somehow: 어떻게든, 왜 그런지 (모르겠지만), 왠지
  • prohibit: 금지하다 (= forbid), ~하지 못하게 하다(= prevent)
  • though:
    1. (비록) ... 이긴 하지만 [...인데도/...일지라도) (= although)
    2. (앞에 말한 내용의 어조를 누르거뜨리기 위해 덧붙여) 그래도[하지만] ...이기는 하다.
    3. (문장 끝에 와서) 그렇지만[하지만]
  • presumably: 아마, 짐작건데
  • presume: (실질적인 증거는 없지만 사실일 것으라고) 추정하다[여기다/생각하다]
  • utter:
    1. (강조에 의미로) 완전히[순전한]
    2. (입으로 어떤 소리를) 내다, (말을) 하다.
  • incantation: (마술을 걸기 위한) 주문[주문을 외기]
  • cater (to somebody/something): ~의 구미에 맞추다[~에 영합하다]
    - 영합하다:
     1. 사사로운 이익을 위하여 아첨하며 좇다.
     2. 서로 뜻이 맞다.
  • prone:
    1. (좋지 않을 일을) 하기[당하기] 쉬운 (= liable)
    2. ~하기[당하기]쉬운(->accident-prone)
    3. (배를 바닥에 대고) 엎어져[엎드려] 있는
  • Ostensible: (실제로 그렇지 않겠지만) 표면적으로는 (= apparent), 표면상으로는
  • neglect:
    1. 방치하다
    2. 방치; 소홀
    3. 도외시하다.

2. Summery

- 1. What is a linker?

 An important idea in C is separate compilation: several programs can be compiled at different times and bound together. It can be done by linker.

 Although linkers don't understand C, they do understand machine language and memory layout, and it is up to each C compiler to translate C programs into terms that make sense to the linker.

- 2. Declarations vs. Definitions

The declaration

int a;

appearing outside of any function body is called a definition of the external object a; it says that a is an external integer variable and also allocates storage for it. Because it doesn't specify an initial value, the value is assume to be 0.

extern int a;

is not a definition of a. It still says that a is an external integer variable, but by including the external keyword, it explicitly says that the storage for a is allocated somewhere else. From the linker's viewpoint, such a declaration is a reference to the external object a but doesn't define it.


int a;

in both test1.c and test2.c. or

int a = 7;

in the test1.c and

int a = 9;

in the test2.c

 Here systems vary; But the last thing will rejected most system. The only way to avoid this trouble in all C implementations is to define each external variable exactly once.

- 3. Name conflicts and static modifier

ANSI C makes it easier to avoid conflicting with library names by listing all the functions that might possibly cause such conflicts. Any library function that calls another library function not on the list most do so by a "hidden name".

 This allows a programmer to define a function called, say, read() without worrying that getc() will call that read() instead of the system function.

꽤나 재미있는 내용.


The declaration

static int a;

means the same thing as

int a;

within a single source program file, but a is hidden from other files.

- 4. Arguments, Parameters, and Return values.

 Every C function has a list of parameters, each of which is a variable that is intialized as part of calling the function.

 A function is called by presenting it with a list of arguments. There is no trouble with result types if every function is defined or declared before its first call in every file that calls it.

main() {
	printf("%g\n", square(0.3));
}

 it will give incorrect results when linked with square() because main() assumes that squre() returns an int when actually square() returns a double.

double squre();

 Doing this relies on the caller to supply the right number of arguments of appropriate types. Appropriate does not necessarily mean eqaul: float arguments are automatically converted to double and short or char arguments are converted to int.

- 5. Check external types

extern int n;
long n;

 This is not a valid c program but, many implementations will fail to detect this error.

There are many possibilities:

  1. The implementation is clever enough to detect the type clash.
  2. You are using an implementation that represents int and long values the same way internally.
  3. The two instance of n require different amount of storage. But, they happen to share storage in such way that the value assigned to one are valid for the other.
  4. The two instances of n share storage in such a way that assigning a value to one has the effect of apparently assigning a different value to the other.

Thus the programmer is generally responsible for ensuring that all external definitions of a particular name have the same type in every object module. Moreover, "the same type" should be taken seriously.


/* In test1.c */
char filename[] = "/etc/passwd";

/* In test2.c */s
extern char *filename;

 Although arrays and pointers are very similar in some contexts, they are not the same.

내가 생각하는 다른 이유:

  1. sizeof 연산 결과가 다를 것이다.
  2. 후자의 filename 은 수정 가능하다. 전자도 가능하나 Undefined Behavior.

6. Header files.

 One good way to avoid many problems of this sort is to adopt a simple rule: declare each external object in one place only.

* Conclusion of Chapter 4

header file 만들어 써라.

3. Exercise

https://www.mythos-git.com/Cruzer-S/CTAP/-/tree/main/Chapter04

Exercise 2 는 꽤나 충격적이였다.

4. References

[Book] C Traps and Pitfalls (Andrew Koenig)
[Site] 네이버 영영, 영한사전

profile
2000.11.30

0개의 댓글