본문 바로가기

Debugging/Linux

(7)
Link and Symbol Table 오늘은 프로그램 build 시에 개별 source code들이 어떤 형식으로 compile 되고 생성된 relocatable object 파일들이 어떤게 연결되는지에 대해서 알아보겠습니다. 추가로, 지난 번 다뤘던 EFL Format의 Section 중 Symbol Table에 대해서도 다루도록 하겠습니다. 아래 예제로 두 source code를 compile 하면, 각각 relocatable object 파일을 생성하게 됩니다. hwjung@jhaewon-z01:~$ cat functionA.c int functionA(int n) { return n + 10; } hwjung@jhaewon-z01:~$ cat functionMain.c #include int functionA(int n); int m..
CPU Registers and Instructions - Instructions Assembly Language에서 Instruction은 opcode와 operands로 구성됩니다. opcode는 instruction이 어떤 동작을 하는지를 의미하고, operands는 instruction이 사용하는 값을 의미합니다. operands는 Instruction에 따라서, 없는 경우도 있을 수 있고 하나 또는 두 개의 operands가 올 수도 있습니다. operands에는 register나 memory address 또는 값 자체가 사용될 수 있습니다. Instruction Notation opcode와 operands는 AT&T 문법과 Intel 문법이 있는데, windows를 분석할 때 windbg를 사용하면 기본으로 Intel 문법으로 표현되지만 Linux를 분석할 때는 GDB에..
CPU Registers and Instructions - Registers Assembly Language에서 Instruction에 사용되는 Register와 관련한 내용을 다뤄보겠습니다. Instruction은 CISC와 RISC 유형이 있는데요. Intel x86_x64 Processor Architecture는 개별 Instruction의 길이가 서로 다르기 때문에, 이러한 Processor Architecture는 CISC(Complex Instruction Set Computer)라고 합니다. 반대로 개별 Instruction의 길이가 동일한 Processor Architecture는 RISC(Reduced Instruction Set Computer)라고 하며, RISC는 ARM 또는 SPARC Processor에서 사용됩니다. 아래는 우리가 흔히 접하는 x86_6..
64bit Stack Walking 64bit 프로그램에서 대부분의 함수들은 대개 Stack에 RBP Register의 값을 Push 하지 않습니다. 이는 Compile 시에 -fomit-frame-pointer flag 때문에 발생합니다. 이런 경우, 32bit 프로그램에 비해서 Call Stack을 수동으로 쫓아가는 것이 쉽지는 않습니다. 64bit 프로그램에서 Return Address를 확인하기 위해 Stack에 있는 Data와 Instruction Pointer를 이용할 수 있습니다. 다음과 같은 Call Stack 예제를 통해서 하나씩 확인해보겠습니다. ## 아래 Call Stack의 9번 Frame 부터 보면, Symbol 이 없기 때문에 정상적으로 함수명이 표현되지 않는 것을 알 수 있습니다. (gdb) bt ... #7 0..
ELF Format #3 - Section Header 3. Section header ELF file에는 여러 Section이 위치하고, 각 Section 관련 정보는 Section header에 보관됩니다. 또한, 각 Section은 고유의 이름을 가질 수 있고 주요 Section으로는 .text와 .data를 들 수 있습니다. .text section은 프로그램의 instruction을 보관하고 있고, .data section은 프로그램 시작 전 초기화되는 값들을 보관하고 있습니다. 아래는 실행 파일과 오브젝트 파일의 Section header 정보 예제입니다. 예제1) 실행 파일 hwjung@jhaewon-z01:~$ readelf -S helloworld There are 31 section headers, starting at offset 0x39..
ELF Format #2 - Program Header 2. Program header Program header는 ELF header 바로 다음에 위치한 Program header table에 있습니다. 각 Program header는 관련된 Section 정보를 가지고 있습니다. 프로그램을 시작할 때, OS Loader는 Program header에 있는 정보를 기반으로 Segment들을 Virtual Address Space에 위치시킵니다. Segment는 Read Only, Read/Write, Executable 속성으로 분리됩니다. hwjung@jhaewon-z01:~$ readelf -l helloworld Elf file type is DYN (Shared object file) Entry point 0x1060 There are 13 prog..
ELF Format #1 - ELF Header ELF Format(Executable and Linkable Format)은 각 영역(TEXT, DATA, BSS등)을 정의하여 프로그램이 실행 시에 이 영역들을 메모리에 올리기 위해 사용됩니다. ELF Format의 File은 여러 segments와 sections로 구성되며, 단일 segment는 여러 section으로 구성될 수 있습니다. ELF Format에는 ELF header, Program header, Section header가 존재하며 이를 통해 각 영역들을 확인할 수 있습니다. 아래에서는 위에서 언급한 각 header에 대해서 보다 상세하게 알아보도록 하겠습니다. 1. ELF header ELF Format File의 맨 처음에 위치하며, Program header와 Section ..