objdump 사용 예제

http://kldp.org/node/24635 의 내용 중 일부를 발췌.

Code영역이라는 것을 알경우 유용한 경우도 있지요.


char *s = “Hello, world!”;
s[0] = ‘T’;


이런 코딩을 하면 Segmentation fault가 일어난다는 것을,
큰따옴표로 구분된 문자열은 Code영역에 위치하기 때문에
read only 속성인데 쓰기를 했으므로 그렇다.. 라고 설명이
가능하지 않은가요?



Code 영역이 아니라 data 영역에 잡히는 것입니다. 위에서도 언급하였지만, data 영역은 writable과 readonly가 따로 있습니다. 물론 없는 architecture도 있습니다. 하지만, 위의 예제에서 seg fault가 난것을 보면, 구분이 되어 있는 (요즘의 linux 같은) machine에서의 예제입니다.


컴파일러는 이것과 관련된 재밌는 option이 있습니다. string literal을 과연 어떤 data 영역에 둘것인가지요.


다음 코드를 살펴보시면, 위 코드를..


$ cat b.c
#include <stdio.h>

int main()
{
char *s = “Hello, world!”;
s[0] = ‘T’;
return 0;
}



위의 코드를 다음과 같이 b1, b2로 만듭니다. 옵션은 -fwritable-strings 입니다.


$ gcc -o b1 b.c
$ gcc -o b2 b.c -fwritable-strings


그리고, 각각의 section중에 data 영역을 dump 해보면… b2에는 존재하지만, b1에는 존재하지 않습니다.


$ objdump -s -j .data b1

b1: file format elf32-i386

Contents of section .data:
80494c8 00000000 00000000 e8940408 00000000 …………….
$ objdump -s -j .data b2

b2: file format elf32-i386

Contents of section .data:
80494b8 00000000 00000000 e8940408 00000000 …………….
80494c8 48656c6c 6f2c2077 6f726c64 21000000 Hello, world!…



readonly data 영역을 dump 해보면, b1에 만 존재합니다.


$ objdump -s -j .rodata b1

b1: file format elf32-i386

Contents of section .rodata:
80484b0 03000000 01000200 48656c6c 6f2c2077 ……..Hello, w
80484c0 6f726c64 2100 orld!.
$ objdump -s -j .rodata b2

b2: file format elf32-i386

Contents of section .rodata:
80484b0 03000000 01000200 ……..

Leave a Reply