ยซ   2025/02   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Archives
Recent Posts
02-01 21:40

Today
Total

Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

์—ฐ์˜ ๊ธฐ๋ก ๐Ÿช

์ปดํŒŒ์ผ ๊ณผ์ • ๋ณธ๋ฌธ

Computer Science/์ปดํ“จํ„ฐ ๊ตฌ์กฐ๋ก 

์ปดํŒŒ์ผ ๊ณผ์ •

kite707 2023. 9. 14. 22:03

์ด ๊ธ€์—์„œ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ง  ํ”„๋กœ๊ทธ๋žจ์ด ์–ด๋–ป๊ฒŒ ์ปดํŒŒ์ผ๋˜์–ด ์‹คํ–‰๋˜๋Š”์ง€ ์‚ดํŽด๋ณผ ๊ฒƒ์ด๋‹ค.

๋จผ์ € ์•„๋ž˜์™€ ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

#include <stdio.h>
#define MAX 5

int main(){
    printf("hello world\n");  //hello world๋ฅผ ์ถœ๋ ฅ
    int a = MAX;
    return 0;
}

์ดํ›„ ์šฐ๋ฆฌ๊ฐ€ gcc hello.c ๋ผ๋Š” ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด a.outํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค.

์ด a.outํŒŒ์ผ์€ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋ชฉ์  ํŒŒ์ผ(executable object file)์ด๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ๋ณด๊ธฐ์—๋Š” hello.cํŒŒ์ผ์ด ๋ฐ”๋กœ a.outํŒŒ์ผ๋กœ ๋ณ€ํ•œ ๊ฒƒ์œผ๋กœ ๋ณด์—ฌ์ง€์ง€๋งŒ ์‚ฌ์‹ค ๋‚ด๋ถ€์ ์œผ๋กœ ์ „์ฒ˜๋ฆฌ, ์ปดํŒŒ์ผ, ์–ด์…ˆ๋ธ”๋ฆฌ, ๋งํ‚น ๊ณผ์ •์„ ๋ชจ๋‘ ๊ฑฐ์นœ ๊ฒƒ์ด๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ๊ทธ ๊ณผ์ •์„ ํ•˜๋‚˜ํ•˜๋‚˜ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž.

์ „์ฒ˜๋ฆฌ(hello.c→hello.i)

์ „์ฒ˜๋ฆฌ ๊ณผ์ •์—์„œ๋Š” ์ฃผ์„ ์ œ๊ฑฐ, ํ—ค๋” ํŒŒ์ผ ์‚ฝ์ž…, ๋งคํฌ๋กœ ์น˜ํ™˜ ๋“ฑ์˜ ์ž‘์—…์„ ํ•œ๋‹ค. ์ฆ‰ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์„ ๊ฑฐ์นœ ํ›„์˜ hello.cํŒŒ์ผ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€ํ™˜๋  ๊ฒƒ์ด๋‹ค.

...
extern int ftrylockfile (FILE * __stream) __attribute__((__nowthrow__,__leaf__))
...

int main(){
    printf("hello world\n");  
    int a = 5;
    return 0;
}

#include <stdio.h>์™€ #define ๋งคํฌ๋กœ๊ฐ€ ์น˜ํ™˜๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ์ฃผ์„๋„ ์‚ญ์ œ๋˜์—ˆ๋‹ค.

์ด์ œ ์‹ค์ œ๋กœ ํ™•์ธํ•ด๋ณด์ž. gcc -E hello.c -o hello.i๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  cat hello.i ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด hello.iํŒŒ์ผ์„ ์ฝ์–ด๋ณด๋„๋ก ํ•˜์ž.

์˜ˆ์ƒํ–ˆ๋˜๋Œ€๋กœ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์ด ์ž˜ ์ฒ˜๋ฆฌ๋˜์—ˆ๋‹ค. ์ฐธ๊ณ ๋กœ ์œ„์—์„œ ์ž…๋ ฅํ•œ ๋ช…๋ น์–ด๋Š” ์ „์ฒ˜๋ฆฌ ๊ณผ์ •๋งŒ ๊ฑฐ์นœ ๊ฒฐ๊ณผ๋ฌผ์„ hello.i๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ €์žฅํ•˜๋ผ๋Š” ๋œป์ด๋‹ค.

  • o filename: filename์„ ๊ฐ€์ง€๋Š” ๊ฒฐ๊ณผ๋ฌผ์„ ๋งŒ๋“ ๋‹ค.
  • E: ์ „์ฒ˜๋ฆฌ ๊ณผ์ •๊นŒ์ง€๋งŒ ๋งˆ์น˜๊ณ  ๊ฒฐ๊ณผ๋ฌผ ์ €์žฅ.
  • S: ์ปดํŒŒ์ผ ๊ณผ์ •๊นŒ์ง€๋งŒ ๋งˆ์น˜๊ณ  ์–ด์…ˆ๋ธ”๋ฆฌ์–ด ๊ฒฐ๊ณผ๋ฌผ ์ €์žฅ.
  • c: ์–ด์…ˆ๋ธ”๋ฆฌ ๊ณผ์ •๊นŒ์ง€๋งŒ ๋งˆ์น˜๊ณ  ์˜ค๋ธŒ์ ํŠธ ํŒŒ์ผ ๊ฒฐ๊ณผ๋ฌผ์„ ์ €์žฅ.

์ปดํŒŒ์ผ(hello.i→hello.s)

๋‹ค์Œ์€ ์ปดํŒŒ์ผ ๊ณผ์ •์ด๋‹ค. ์ปดํŒŒ์ผ ๊ณผ์ •์—์„œ๋Š” ์†Œ์ŠคํŒŒ์ผ์„ ์–ด์…ˆ๋ธ”๋ฆฌ์–ด๋กœ ๋ฐ”๊พผ๋‹ค. ๋˜ ๋ฌธ๋ฒ• ๊ฒ€์‚ฌ ๋ฐ staticํ•œ ์˜์—ญ์— ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•œ๋‹ค.

๋งŒ์•ฝ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด

int main(){
    int a = 5;
    return 0;
}

์ด๋Ÿฐ์‹์œผ๋กœ ๋‹ˆ๋ชจ๋‹‰ ์‹ฌ๋ณผ์„ ์ด์šฉํ•ด ๊ฐ’์„ ๋ฐ”๊ฟ”์ค€๋‹ค.

main:
        pushq %rbp
        movq %rsp, %rbp
        movl $5, -4(%rbp)
        movl $0, %eax
        popq %rbp
        ret

์‹ค์ œ๋กœ ์œ„ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•œ ๋’ค ์ปดํŒŒ์ผ ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

gcc -S hello.c -o hello.s

์–ด์…ˆ๋ธ”๋ฆฌ(hello.s → hello.o)

๊ทธ ๋‹ค์Œ์œผ๋กœ๋Š” ์–ด์…ˆ๋ธ”๋ฆฌ ๊ณผ์ •์ด ์‹คํ–‰๋˜๋Š”๋ฐ ์–ด์…ˆ๋ธ”๋ฆฌ์–ด๋ฅผ ๋ชฉ์ ํŒŒ์ผ(object file)๋กœ ๋ฐ”๊พธ์–ด ์ฃผ๋Š” ๊ณผ์ •์ด๋‹ค. ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ฝ๋Š” cat๋ช…๋ น์–ด๊ฐ€ ์•„๋‹Œ objdump ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค. -d ์˜ต์…˜์€ ์–ด์…ˆ๋ธ”๋ฆฌ๋กœ ์—ญ๋ณ€ํ™˜ํ•ด ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ผ๋Š” ๋œป์ด๋‹ค.

gcc -c hello.c -o hello.o
objdump -d hello.o

๋งํ‚น

์œ„์—์„œ ๋ชฉ์ ํŒŒ์ผ์„ ๋งŒ๋“ค์—ˆ์œผ๋‹ˆ ๋ฐ”๋กœ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์ง€๋งŒ ์•„์ง ๋งํ‚น ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•œ๋‹ค. ์œ„์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ hello.c์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ๋ณด๋„๋ก ํ•˜์ž.

#include <stdio.h>
#define MAX 5

int main(){
    printf("hello world\n");  //hello world๋ฅผ ์ถœ๋ ฅ
    int a = MAX;
    return 0;
}

printfํ•จ์ˆ˜๋Š” standard C libraryํ•จ์ˆ˜์ด๋‹ค. ๊ทธ๋ž˜์„œ printf.o๋ผ๋Š” ํŒŒ์ผ์— ํ•จ์ˆ˜์˜ ๋‚ด์šฉ์ด ๋ช…์‹œ๋˜์–ด์žˆ๋‹ค. ๋งํ‚น๊ณผ์ •์€ ์ด๋Ÿฌํ•œ ํŒŒ์ผ ๋‚ด์— ์ •์˜๋˜์–ด์žˆ์ง€ ์•Š์€ ํ•จ์ˆ˜์˜ ๋‚ด์šฉ์„ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ด์ œ ์‹ค์ œ ๋งํ‚น ๊ณผ์ •์„ ๊ฑฐ์น˜๊ธฐ ์ „๊ณผ ํ›„๋ฅผ ์ง์ ‘ ๋น„๊ตํ•ด๋ณด๋„๋ก ํ•˜์ž.

์ตœ๋Œ€ํ•œ ๊ฐ„๋‹จํ•˜๊ฒŒ test.cํŒŒ์ผ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑํ•˜๊ณ 

#include <stdio.h>

int main(){
printf("hello world!");
return 0;
}

์•„๋ž˜ test.cํŒŒ์ผ์„ ์–ด์…ˆ๋ธ”๋ฆฌ ๊ณผ์ •๊นŒ์ง€๋งŒ ๊ฑฐ์นœ ๋’ค(๋งํ‚น ์ „) ์‹ฌ๋ณผ ํ…Œ์ด๋ธ”์„ ์‚ดํŽด๋ณด์ž.

gcc -c test.c -o test.o
nm test.o  // ์‹ฌ๋ณผ ํ…Œ์ด๋ธ”์„ ๋ณด๋Š” ๋ช…๋ น์–ด

์ €๊ธฐ์„œ T๋Š” ํ…์ŠคํŠธํŒŒ์ผ์—์„œ ํ•ด๋‹น ์‹ฌ๋ณผ์˜ ์˜๋ฏธ๋ฅผ ์ฐพ์•˜๋‹ค๋Š” ๋œป์ด๊ณ , U๋Š” undefined๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ด์ œ ๋งํ‚น๊ณผ์ •๊นŒ์ง€ ๊ฑฐ์นœ ๋’ค ํ•ด๋‹น ํŒŒ์ผ์˜ ์‹ฌ๋ณผ ํ…Œ์ด๋ธ”์„ ์‚ดํŽด๋ณด์ž.

๊ธฐ๋Œ€ ๊ฒฐ๊ณผ๋Š” _printf์˜ U๊ฐ€ ์‚ฌ๋ผ์ง€๊ฑฐ๋‚˜ ์˜†์— ํ•ด๋‹น ๋ช…๋ น์–ด์˜ ์ฃผ์†Œ๊ฐ€ ๊ธฐ์ž…๋˜๋Š”๊ฒƒ์ธ๋ฐ ์ž˜ ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด ๋ถ€๋ถ„์€ ์ถ”ํ›„ ์ˆ˜์ •ํ•˜๊ฒ ๋‹ค.