1 minute read

Computer Systems : A Programmer’s Perspective (Chapter 7)

이전 글 - 1. 전처리기, 컴파일러, 어셈블러
이전 글 - 2. Static linking

3. Relocation 과 Loader

Relocation

앞선 글에서는 여러 개의 relocatable object file을 받아 든 linker가, symbol들의 모든 참조(reference)를 딱 하나의 정의(definition)와 연결시키는 과정인 symbol resolution에 대해 살펴보았다.
이제 서로 다른 여러 object module 출신의 reference들이 무엇을 의미하는지 다 알게 되었다. 지금부터 할 일은,

Executable Object Files

모든 과정을 거쳐 완성된 것은 executable object file이다. 메모리에 load돼서 실행되는 데 필요한 모든 정보가 준비된 파일이다.

ELF executable file은 다음과 같은 구조로 되어 있다.

image

Relocatable object file이랑 유사하게 생겼다.
헤더에는 전반적인 파일의 format에 대한 정보와 entry point 의 정보가 담겨 있다. entry point 란, 이 프로그램이 처음 실행될 때 수행할 첫 instruction의 주소를 의미한다.
읽기 전용 구역(code section)에는 relocation을 거친 .text.rodata가 들어 있고, .init이 추가되었다. .init에는 _init이라는 함수가 들어 있는데, 프로그램이 처음 시작될 때 불리는 함수이다.
읽기-쓰기 구역(data section)에는 relocation을 거친 .data가 존재한다. .rel 부분은 이제 필요 없으니 존재하지 않는다.

Loader

리눅스 쉘에 ./prog를 입력하면 executable object file을 실행할 수 있다. 어떤 과정으로 ‘실행’이라는 것이 일어나는지 살펴보자.

먼저, 쉘은 built-in command가 아니므로 운영체제의 loader 라고 불리는 memory-resident code를 실행시킨다. Loader는 디스크에 있는 executable object file의 code section과 data section을 메모리(가상 메모리)에 복사하고(map하고) entry point 로 가서 프로그램의 첫 instruction을 수행한다.

이처럼 프로그램을 메모리에 올리고 실행하는 과정을 loading이라고 부른다.

image

Loader는 실행될 때 위 그림과 같은 memory image를 생성한다. Executable object file의 program header table을 보고 각 chunk들을 code segment, data segment에 복사한다.

Tags:

Categories:

Updated: