rosieblue
article thumbnail
728x90

C์ฝ”๋“œ๋‚˜ ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผ ํ•  ๋•Œ ๊ฒฐ๊ณผ๋ฅผ ์ง€์ •ํ•  ๋•Œ Object File์ด ๋  ๋•Œ๋„ ์žˆ๊ณ ,,, ๊ธฐ๊ณ„์–ด ์ฝ”๋“œ๊ฐ€ ๋  ๋•Œ๋„ ์žˆ๊ณ (?)ํ•ด์„œ ๋„๋Œ€์ฒด ๋‘˜์˜ ์ฐจ์ด๊ฐ€ ๋ญ”์ง€, ๊ทธ๋ฆฌ๊ณ  ์˜ค๋ธŒ์ ํŠธ ํŒŒ์ผ์ด๋ผ๋Š”๊ฒŒ ์ •ํ™•ํ•˜๊ฒŒ ๋ญ˜ ์ง€์นญํ•˜๋Š”์ง€ ๊ถ๊ธˆํ•ด์„œ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ค‘๊ฐ„์— ๋‚˜์˜ค๋Š” '๋งํฌ'์˜ ๊ฐœ๋…์„ ๋‹ค๋ค„๋ณด๊ณ ์ž ํ•œ๋‹ค.

 

์˜ค๋Š˜ ๋‹ค๋ฃฐ ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

  • ๊ธฐ๊ณ„์–ด ์ฝ”๋“œ vs ์˜ค๋ธŒ์ ํŠธ ์ฝ”๋“œ (vs ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ)
  • ๋งํฌ(Link)์˜ ๊ฐœ๋…
  • gcc vs as

 

๊ธฐ๊ณ„์–ด ์ฝ”๋“œ vs ์˜ค๋ธŒ์ ํŠธ ์ฝ”๋“œ (vs ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ) ๋Š” ์•„๋ž˜ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์˜€๋‹ค.

https://stackoverflow.com/questions/466790/assembly-code-vs-machine-code-vs-object-code#:~:text=Object%20code%20is%20a%20portion,code%20of%20a%20completed%20program.

 

Assembly code vs Machine code vs Object code?

What is the difference between object code, machine code and assembly code? Can you give a visual example of their difference?

stackoverflow.com

๊ธ์–ด์˜ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
 
Machine code is binary (1's and 0's) code that can be executed directly by the CPU. If you open a machine code file in a text editor you would see garbage, including unprintable characters (no, not those unprintable characters ;) ).

Object code is a portion of machine code not yet linked into a complete program. It's the machine code for one particular library or module that will make up the completed product. It may also contain placeholders or offsets not found in the machine code of a completed program. The linker will use these placeholders and offsets to connect everything together.

Assembly code is plain-text and (somewhat) human read-able source code that mostly has a direct 1:1 analog with machine instructions. This is accomplished using mnemonics for the actual instructions, registers, or other resources. Examples include JMP and MULT for the CPU's jump and multiplication instructions. Unlike machine code, the CPU does not understand assembly code. You convert assembly code to machine code with the use of an assembler or a compiler, though we usually think of compilers in association with high-level programming language that are abstracted further from the CPU instructions.

 

๊ธฐ๊ณ„์–ด ์ฝ”๋“œ๋Š” ๊ทธ๋ƒฅ 1๊ณผ 0์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ฝ”๋“œ, ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ๋Š” ๊ทธ๋Ÿฌํ•œ ๊ธฐ๊ณ„์–ด ์ฝ”๋“œ๋ฅผ mov, push ์™€ ๊ฐ™์ด ์ธ๊ฐ„์ด ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋ฐ”๊พผ ์ €์ˆ˜์ค€ ์ฝ”๋“œ์ธ ๊ฑด ๋Œ€๋ถ€๋ถ„ ์‚ฌ๋žŒ๋“ค์ด ์•„์‹ค ๊ฒƒ์ด๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด ์ค‘๊ฐ„์— ์žˆ๋Š” ์ € Object Code(์˜ค๋ธŒ์ ํŠธ ์ฝ”๋“œ)๋Š” ๋ญ˜๊นŒ.....?

 

 

Object Code (์˜ค๋ธŒ์ ํŠธ ํŒŒ์ผ, ์˜ค๋ธŒ์ ํŠธ ์ฝ”๋“œ)

Object code๋Š” ์ตœ์ข… ํ”„๋กœ๊ทธ๋žจ์ด ๋งŒ๋“ค์–ด์ง€๊ธฐ ์ „์— ์•„์ง ๋งํฌ๋˜์ง€ ์•Š์€ ๊ธฐ๊ณ„์–ดํŒŒ์ผ์ด๋‹ค.

๋‹ค์Œ์€ add.S๋ผ๋Š” ์–ด์…ˆ๋ธ”๋ฆฌ์ฝ”๋“œ๋ฅผ add.o๋ผ๋Š” ์˜ค๋ธŒ์ ํŠธ ํŒŒ์ผ๋กœ ์–ด์…ˆ๋ธ”ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค. (์˜ค๋ธŒ์ ํŠธ ํŒŒ์ผ ํ™•์žฅ์ž : .o)

์–ด์…ˆ๋ธ”ํ•  ๋•Œ๋Š” ์•„๋ž˜๊ฐ™์ด gcc ์ปดํŒŒ์ผ๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•˜๊ณ  as ์–ด์…ˆ๋ธ”๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

 

$ gcc -c add.S -o add.o
$ file add.o
add.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
$ hexdump -C add.o
00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  01 00 3e 00 01 00 00 00  00 00 00 00 00 00 00 00  |..>.............|
00000020  00 00 00 00 00 00 00 00  10 02 00 00 00 00 00 00  |................|
00000030  00 00 00 00 40 00 00 00  00 00 40 00 0b 00 0a 00  |....@.....@.....|
00000040  55 48 89 e5 89 7d fc 89  75 f8 8b 55 fc 8b 45 f8  |UH...}..u..U..E.|
00000050  01 d0 5d c3 00 47 43 43  3a 20 28 55 62 75 6e 74  |..]..GCC: (Ubunt|
00000060  75 20 37 2e 35 2e 30 2d  33 75 62 75 6e 74 75 31  |u 7.5.0-3ubuntu1|
00000070  7e 31 38 2e 30 34 29 20  37 2e 35 2e 30 00 00 00  |~18.04) 7.5.0...|
00000080  14 00 00 00 00 00 00 00  01 7a 52 00 01 78 10 01  |.........zR..x..|
00000090  1b 0c 07 08 90 01 00 00  1c 00 00 00 1c 00 00 00  |................|
000000a0  00 00 00 00 14 00 00 00  00 41 0e 10 86 02 43 0d  |.........A....C.|
000000b0  06 4f 0c 07 08 00 00 00  00 00 00 00 00 00 00 00  |.O..............|

 

์ด์ฒ˜๋Ÿผ ์˜ค๋ธŒ์ ํŠธ ํŒŒ์ผ๋„ ๊ธฐ๊ณ„์–ด์˜ ์ผ์ข…์ด๋‹ค!

 

gcc vs as ์ ‘๋Š”๊ธ€

๋”๋ณด๊ธฐ

GCC (GNU Compiler Collection)๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์–ธ์–ด (C, C++, Objective-C, Fortran, Ada, ๋“ฑ)์˜ ์ปดํŒŒ์ผ์„ ์ง€์›ํ•˜๋Š” ์ปดํŒŒ์ผ๋Ÿฌ์ด๋ฉฐ, ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ธฐ๋Šฅ๋„ ์žˆ๋‹ค.

 

๋ฐ˜๋ฉด์— as๋Š” ์–ด์…ˆ๋ธ”๋Ÿฌ(assembler)๋ผ๊ณ  ๋ถˆ๋ฆฌ๋ฉฐ, ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ๋ฅผ ๊ธฐ๊ณ„์–ด ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. as๋Š” GCC์˜ ์ผ๋ถ€๋ถ„์ด๊ธฐ ๋•Œ๋ฌธ์— GCC๋ฅผ ์„ค์น˜ํ•˜๋ฉด ์ž๋™์œผ๋กœ as๋„ ์„ค์น˜๋œ๋‹ค.

 

 

as ๋ช…๋ น์–ด ์„ค๋ช…

https://www.ibm.com/docs/en/aix/7.2?topic=program-assembling-as-command

 

 

๋งํฌ( Link)

 

ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ์˜ค๋ธŒ์ ํŠธ ํŒŒ์ผ์ด '๋งํฌ'ํ•˜๊ธฐ ์ „ ํŒŒ์ผ์ด๋ผ๋Š” ๊ฒƒ์„ ์ด์ œ ์•ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋งํฌ๋Š” ๋ฌด์—‡์ผ๊นŒ??

๋งํฌ(Link)๋Š” "์—ฌ๋Ÿฌ ๋ชฉ์  ํŒŒ์ผ๋“ค์„ ์—ฐ๊ฒฐ"ํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋ฐ”์ด๋„ˆ๋ฆฌ๋กœ ๋งŒ๋“œ๋Š” ๊ณผ์ •์ด๋‹ค. 

 

  • ๊ทธ๋ฆผ 1๋ฒˆ์€ ์ปดํŒŒ์ผ(compile)์— ํ•ด๋‹นํ•œ๋‹ค.
  • ๊ทธ๋ฆผ 2๋ฒˆ์€ ๋งํฌ(Link)์— ํ•ด๋‹นํ•œ๋‹ค.
  • 1๋ฒˆ๊ณผ 2๋ฒˆ ๋‘๊ณผ์ •์„ ํ•ฉ์ณ์„œ ๋นŒ๋“œ(Build)๋ผ๊ณ  ํ•œ๋‹ค.

๊ทธ๋ฆผ ์ถœ์ฒ˜ : https://kimvampa.tistory.com/27

 

์˜ˆ๋ฅผ๋“ค์–ด ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  C์ฝ”๋“œ์—์„œ printf ํ•จ์ˆ˜๊ฐ€ ๋“ฑ์žฅํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ๊ทธ ์ฝ”๋“œ๋ฅผ ์–ด์…ˆ๋ธ”๋ฆฌ์–ด๋กœ ๋ฐ”๊พธ๋ฉด call printf ์ด๋Ÿฐ ์ฝ”๋“œ๊ฐ€ ๋“ฑ์žฅํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ๋ฅผ ์˜ค๋ธŒ์ ํŠธ ํŒŒ์ผ๋กœ ๋งŒ๋“ค ๊ฒƒ์ด๋‹ค.

 

ํ•˜์ง€๋งŒ printf ํ•จ์ˆ˜์˜ ๋‚ด์šฉ์€ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ์ฝ”๋“œ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋Š” libc๋ผ๋Š” ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์กด์žฌํ•œ๋‹ค. 

์ด๋•Œ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ์ฝ”๋“œ์— libc์˜ printfํ•จ์ˆ˜ ๋ถ€๋ถ„์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์„ ๋งํ‚น(linking)ํ•œ๋‹ค๊ณ  ํ•˜๋Š” ๊ฒƒ์ด๋‹ค!!! libc๋Š” gcc์˜ ๊ธฐ๋ณธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฒฝ๋กœ์— ์žˆ๋Š”๋ฐ, ๋ง์ปค๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ๊ฐ€ printf๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด libc์˜ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก ์—ฐ๊ฒฐํ•ด ์ค€๋‹ค!!

๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ๋งํฌ๋ฅผ ํ•ด์ฃผ๋Š” ์•„์ด๋ฅผ ๋ง์ปค(Linker)๋ผ๊ณ  ํ•œ๋‹ค.

 

์ •์ ๋งํฌ์™€ ๋™์  ๋งํฌ

๋งํฌ๋Š” ์ •์  ๋งํฌ(static link)์™€ ๋™์  ๋งํฌ(dynamic link)๋กœ ๋‚˜๋‰œ๋‹ค.

  • ์ •์  ๋งํฌ : ์ปดํŒŒ์ผ๋œ ์†Œ์ŠคํŒŒ์ผ์„ ์—ฐ๊ฒฐํ•ด์„œ ์‹คํ–‰๊ฐ€๋Šฅํ•œ ํŒŒ์ผ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ -> ์‹คํ–‰ํŒŒ์ผ์—๋‹ค ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค ๋ณต์‚ฌํ•ด์„œ ์‹คํ–‰ํŒŒ์ผ ๋งŒ๋“ฆ. ํ•˜์ง€๋งŒ ์‹คํ–‰ ํŒŒ์ผ ๋‚ด์— ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€์„œ ๋ฉ”๋ชจ๋ฆฌ ์˜ค์ง€๊ฒŒ ์žก์•„๋จน์Œ
  • ๋™์  ๋งํฌ : ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ๋„์ค‘ ํ”„๋กœ๊ทธ๋žจ ์™ธ๋ถ€์— ์กด์žฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ฐพ์•„์„œ ์—ฐ๊ฒฐํ•˜๋Š” ์ž‘์—….(๋™์  ๋งํฌ๋Š” ๋ง์ปค๊ฐ€ ํ•„์š”๊ฐ€ ์—†๋‹ค.) ->๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ์‚ฌํ•ญ์ด ํ›จ์”ฌ ์ ์Œ

 

์ด๋ฏธ์ง€ ์ถœ์ฒ˜ : https://codedragon.tistory.com/8461

 

๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์ •์  ๋งํฌ๋Š” ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ ์ „์— ์ด๋ฏธ ํ•œ ํŒŒ์ผ์— ๋‹ค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋“ฑ ๋‹ค ํฌํ•จ ์‹œํ‚ค๋Š” ๊ฑฐ๊ณ , ๋™์  ๋งํฌ๋Š” ์‹คํ–‰์ค‘์— ๊ณ„์† ๋™์ ์œผ๋กœ ์ฐธ์กฐํ•˜๋Š”๊ฒƒ์„ ๋งํ•˜๋Š” ๊ฒƒ๊ฐ™๋‹ค.

 

์ž˜ ์„ค๋ช…ํ•ด์ฃผ์‹  ์ฐธ๊ณ ํ•  ๋งŒํ•œ ๊ธ€ : https://jhnyang.tistory.com/entry/%EC%9A%B4%EC%98%81%EC%B2%B4%EC%A0%9CStatic-Linking-vs-Dynamic-Linkingshared-Library-%EC%A0%95%EC%A0%81%EB%A7%81%ED%82%B9-vs-%EB%8F%99%EC%A0%81%EB%A7%81%ED%82%B9

 

[์šด์˜์ฒด์ œ]Static Linking vs Dynamic Linking(shared Library) ์ •์ ๋งํ‚น vs ๋™์ ๋งํ‚น

์šด์˜์ฒด์ œ ๋ชฉ์ฐจ Dynamic Linking์„ ์ดํ•ดํ•˜๋ ค๋ฉด Linking์— ๋Œ€ํ•ด ์ดํ•ด๋ฅผ ํ•˜๊ณ  ์žˆ์–ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ €๋ฒˆ์‹œ๊ฐ„์— ๋งํ‚น์— ๋Œ€ํ•ด์„œ ํฌ์ŠคํŒ…์„ ํ–ˆ์–ด์š” ๋งํ‚น๊ณผ์ •์—์„œ ์˜ค๋ธŒ์ ํŠธ ํŒŒ์ผ์„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๊ฐ™์ด Linking์„

jhnyang.tistory.com

 

 

์ปดํŒŒ์ผ ๊ณผ์ •์„ ๋‚˜ํƒ€๋‚ด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค!

 

์ปดํŒŒ์ผ ๊ณผ์ • ์„ค๋ช…ํ•œ ๊ทธ๋ฆผ

 

 

 

References

https://kimvampa.tistory.com/27

 

์ปดํŒŒ์ผ(Compile), ๋งํฌ(Link), ๋นŒ๋“œ(Build) ์ •๋ฆฌ

๊ฐœ์ธ๊ณต๋ถ€ ํ›„ ์ž๋ฃŒ๋ฅผ ๋‚จ๊ธฐ๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์ด๊ธฐ์— ๋‚ด์šฉ ์ƒ์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชฉํ‘œ ์ปดํŒŒ์ผ, ๋งํฌ, ๋นŒ๋“œ ์ „์ฒด ๊ณผ์ •์„ ์ดํ•ดํ•œ๋‹ค. ์ปดํŒŒ์ผ, ์ปดํŒŒ์ผ๋Ÿฌ ๊ฐœ๋… ๋งํฌ, ๋ง์ปค ๊ฐœ๋… ๋นŒ๋“œ, ๋นŒ๋“œํˆด ๊ฐœ๋… ๋นŒ

kimvampa.tistory.com

https://learn.dreamhack.io/67

 

๋กœ๊ทธ์ธ | Dreamhack

 

dreamhack.io

https://stackoverflow.com/questions/466790/assembly-code-vs-machine-code-vs-object-code#:~:text=Object%20code%20is%20a%20portion,code%20of%20a%20completed%20program.

 

Assembly code vs Machine code vs Object code?

What is the difference between object code, machine code and assembly code? Can you give a visual example of their difference?

stackoverflow.com

https://jhnyang.tistory.com/entry/%EC%9A%B4%EC%98%81%EC%B2%B4%EC%A0%9CStatic-Linking-vs-Dynamic-Linkingshared-Library-%EC%A0%95%EC%A0%81%EB%A7%81%ED%82%B9-vs-%EB%8F%99%EC%A0%81%EB%A7%81%ED%82%B9

 

[์šด์˜์ฒด์ œ]Static Linking vs Dynamic Linking(shared Library) ์ •์ ๋งํ‚น vs ๋™์ ๋งํ‚น

์šด์˜์ฒด์ œ ๋ชฉ์ฐจ Dynamic Linking์„ ์ดํ•ดํ•˜๋ ค๋ฉด Linking์— ๋Œ€ํ•ด ์ดํ•ด๋ฅผ ํ•˜๊ณ  ์žˆ์–ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ €๋ฒˆ์‹œ๊ฐ„์— ๋งํ‚น์— ๋Œ€ํ•ด์„œ ํฌ์ŠคํŒ…์„ ํ–ˆ์–ด์š” ๋งํ‚น๊ณผ์ •์—์„œ ์˜ค๋ธŒ์ ํŠธ ํŒŒ์ผ์„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๊ฐ™์ด Linking์„

jhnyang.tistory.com

 

profile

rosieblue

@Rosieblue

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!