栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

nasm汇编调用C库

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nasm汇编调用C库

Linux x86-64 与 printf

主程序

default rel            ; make [rel format] the default, you always want this.
extern printf, exit    ; NASM requires declarations of external symbols, unlike GAS
section .rodata
    format db "%#x", 10, 0   ; C 0-terminated string: "%#xn" 
section .text
global main
main:
    sub   rsp, 8             ; re-align the stack to 16 before calling another function

    ; Call printf.
    mov   esi, 0x12345678    ; "%x" takes a 32-bit unsigned int
    lea   rdi, [rel format]
    xor   eax, eax           ; AL=0  no FP args in XMM regs
    call  printf

    ; Return from main.
    xor   eax, eax
    add   rsp, 8
    ret

GitHub 上游.

然后:

nasm -f elf64 -o main.o main.asm
gcc -no-pie -o main.out main.o
./main.out

输出:

0x12345678
why -no-pie ?

How to compile nasm program calling printf? [duplicate]
Asked 1 year, 4 months ago
Modified 1 year, 4 months ago
Viewed 896 times

1

This question already has answers here:
Can’t call C standard library function on 64-bit Linux from assembly (yasm) code (2 answers)
How to print a number in assembly NASM? (6 answers)
Closed last year.

https://cs.lmu.edu/~ray/notes/nasmtutorial/

I got the following error when I try to compile fib.asm on Linux. Could you let me know how to compile it? Thanks.

$ nasm -felf64 fib.asm && gcc fib.o && ./a.out
/usr/bin/ld: fib.o: warning: relocation in read-only section `.text'
/usr/bin/ld: fib.o: relocation R_X86_64_PC32 against symbol `printf@@GLIBC_2.2.5' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status

3
Provide -no-pie to the gcc command to fix this problem. Modern Linux systems default to PIE which many tutorials unfortunately don’t cover. Turning off PIE makes the tutorial work again. –
fuz
Jan 27, 2021 at 3:03
1
You’re right, 32-bit absolute addresses no longer allowed in x86-64 Linux? is more about data accesses like [symbol + rdi], not about the fact that function calls don’t implicitly turn call foo into call foo wrt …plt for you when linking a PIE, only a normal executable. I picked that duplicate first because it’s about PIEs and how that’s now the default (and it mentions -no-pie), but the other Q&As I later added to the top of the duplicate list are much more exact fits, and discuss exactly how to call functions in shared libraries like libc. –
Peter Cordes
Jan 27, 2021 at 3:29
My answer on Can’t call C standard library function on 64-bit Linux from assembly (yasm) code and Ciro’s answer on How to print a number in assembly NASM? both clearly explain what’s going on, and that -no-pie is the easy way, or how to write NASM syntax that works in a PIE. My answer also tries to explain why it doesn’t work when you do it this way, but Ciro’s is more of a simple recipe that’s easier to follow. Both are useful for different reasons, but each sufficient on its own, so I linked both duplicates. –
Peter Cordes
Jan 27, 2021 at 3:44
But why the webpage clearly say nasm -felf64 fib.asm && gcc fib.o && ./a.out works. Does it work in some cases that you are not aware of? How can you be sure your answers cover 100% of the cases? –
user1424739
Jan 27, 2021 at 3:46
1
Because it’s an old tutorial. As 32-bit absolute addresses no longer allowed in x86-64 Linux? explained, Linux distros changes to making -fPIE -pie the default in about 2017, where it wasn’t before, to enable ASLR for sections other than the stack. This fully explains 100% of what you’re seeing, including that exact error message, and why the tutorial didn’t use that option or PIE-compatible asm. (And that explanation is part of why I originally linked that as one of the duplicates.) –
Peter Cordes
Jan 27, 2021 at 3:53
Could you just show the commands that can compile and liink the nasm file? the link that you post is not minimal. I first need a command that is working. –
user1424739
Jan 27, 2021 at 3:57
BTW, that tutorial looks fairly good, but randomly uses lea rdi, [rel message] (good) in some examples vs. mov rdi, message (inefficient) in others, including otherwise-equivalent Linux vs. MacOS puts examples which makes it look like one is the Linux way, one is the MacOS way. That’s not true. How to load address of function or label into register –
Peter Cordes
Jan 27, 2021 at 3:57
Come on dude, if you just want something that works, have you not yet tried fuz’s comment to use gcc -no-pie fib.o? That will work exactly the way it did when the tutorial was written, letting the code from those tutorials work. –
Peter Cordes
Jan 27, 2021 at 3:59
OK. It works. I think you should just post gcc -no-pie -o fib.exe fib.o as an answer. My question is “how …”? The answer is just a command that works. Instead, you posted too many previous answers that do not directly answer my questions. If you really want to link to those previous questions, you should first provide a working command then provide these previous answers to explain why the command works, rather than just provide links to those previous questions which does not directly answer my question. We are just wasting too much text on this simple question. –
user1424739
Jan 27, 2021 at 4:03

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/948127.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号