使用 gcc 将汇编代码与 C 代码链接的正确方法是什么 [英] What is the correct way to link assembly code with C code using gcc
问题描述
假设您有以下程序集文件:
Suppose you have the following assembly files:
.file "print.s"
.intel_syntax noprefix
.text
.globl _start
.type _start, @function
_start:
mov rax, 1
mov rdi, 1
lea rsi, hello_string
mov rdx, 14
syscall
mov rax, 60
mov rdi, 0
syscall
.size _start, .-_start
和
.file "string.s"
.intel_syntax noprefix
.data
.globl hello_string
.type hello_string, @object
hello_string:
.string "Hello, world!\n"
.size hello_string, 14
如果你运行 as string.s -o string.o
,as print.s -o print.o
然后 ld *.o -ohello.elf
,您将获得一个可执行文件,该文件打印 Hello, world!.现在假设您将 print.s 中的 _start
标签更改为 print
并且您有以下 C 文件:
If you run as string.s -o string.o
, as print.s -o print.o
and then ld *.o -o hello.elf
, you get a executable file which prints Hello, world!. Now suppose that you change the _start
label in print.s to print
and you have the following C file:
// main.c
void print(void);
int main (void) {
print();
return 0;
}
我想运行类似 gcc -o main.elf main.c string.s print.s
的东西来获取我的两个程序集文件并将它们链接到标准 C 运行时.所写的此命令不起作用.我收到以下错误消息:
I want to run something like gcc -o main.elf main.c string.s print.s
to take my two assembly files and link them into the standard C runtime. This command as written doesn't work. I get the following error message:
/usr/bin/ld: /tmp/ccc1yRif.o: relocation R_X86_64_32S against symbol `hello_string' can not be used when making a shared object; recompile with -fPIC
我不知道如何正确使用 -fPIC 标志来使工作正常进行.我什至不确定这是否是我应该做的.我可以尝试使用内联汇编器来做到这一点,但对于我的实际用例来说,这会非常烦人,我更愿意学习如何将汇编块正确链接到我的 C 程序中.我在网上找不到太多关于此的信息.
I can't figure out how to correctly use the -fPIC flag to make things work. I am not even sure if that is what I should be doing. I could try and use the inline assembler to do this, but for my actual use cases, that would be extremely annoying and I would much rather learn how to correctly link assembly chunks into my C programs. I haven't been able to find much information about this online.
问题: 如何正确使用 GNU 链接器将汇编文件链接到 C 运行时?
Question: How do you correctly use the GNU linker to link assembly files into the C runtime?
在 如何将 C 目标文件与汇编语言目标文件链接起来?,OP 正在询问如何仅使用 ld
解决类似的链接问题.我想使用 gcc
这是链接问题中推荐的解决方案,但我什至无法让它工作.
In How to link a C object file with a Assembly Language object file?, the OP is asking about how to solve a similar linking problem just using ld
. I want to use gcc
which is the recommended solution in the linked question, but I can't even get that to work.
推荐答案
好的,我现在想通了.错误消息似乎告诉我 gcc 正在尝试创建共享对象.这让我想到了一个问题 gcc 6.2.0 试图在不应该创建共享对象时创建共享对象?
OK, so I have figured it out now. The error message seems to be telling me that gcc is trying to make a shared object. This lead me to the question gcc 6.2.0 is attempting to create shared object when it shouldn't?
解决办法是运行gcc -no-pie main.c string.s print.s -o main.elf
根据手册页,-no-pie
告诉 gcc 不要生成位置独立的可执行文件.
according to the man page, -no-pie
tells gcc to not produce a position indipendent executable.
这篇关于使用 gcc 将汇编代码与 C 代码链接的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!