linux - How does one link NASM program to libc via ld? -


i have following program nasm (archlinux i686)

section .data lc1: db "library call", 0  section .text extern exit extern printf  ;global main ;main: global _start _start: push    lc1 call    printf  push    0 call    exit 

which assembled command:

nasm -f elf libcall.asm 

if comment 2 lines _start , uncomment 2 lines main, assemble , link command:

gcc libcall.o -o libcall 

then program runs ok. if assemble code _start entry point , link command:

ld libcall.o -o libcall -lc 

then after launching program in bash (via command ./libcall) following error message returned:

bash: ./libcall: no such file or directory 

although libcall file exist. objdump shows following:

[al libcall ]$ objdump -d libcall  libcall:     file format elf32-i386   disassembly of section .plt:  08048190 <printf@plt-0x10>:  8048190:   ff 35 78 92 04 08       pushl  0x8049278  8048196:   ff 25 7c 92 04 08       jmp    *0x804927c  804819c:   00 00                   add    %al,(%eax)     ...  080481a0 <printf@plt>:  80481a0:   ff 25 80 92 04 08       jmp    *0x8049280  80481a6:   68 00 00 00 00          push   $0x0  80481ab:   e9 e0 ff ff ff          jmp    8048190 <printf@plt-0x10>  080481b0 <exit@plt>:  80481b0:   ff 25 84 92 04 08       jmp    *0x8049284  80481b6:   68 08 00 00 00          push   $0x8  80481bb:   e9 d0 ff ff ff          jmp    8048190 <printf@plt-0x10>  disassembly of section .text:  080481c0 <_start>:  80481c0:   68 88 92 04 08          push   $0x8049288  80481c5:   e8 d6 ff ff ff          call   80481a0 <printf@plt>  80481ca:   6a 00                   push   $0x0  80481cc:   e8 df ff ff ff          call   80481b0 <exit@plt> 

how nasm assembly code should linked libc via ld?

there parts of libc/crt come in object files need link. additionally need specify options such dynamic loader (aka. interpreter) use (which reason issue.) use gcc right thing you. if interested can run gcc -v , see horrible command line uses link. have been warned ;)

ps: should use main entry point have commented out.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -