针对`.text'在汇编中执行分支查找开关时,无法使用PIE对象时使用
我正在x86-64汇编中编写一个项目,我想使用分支查找表编写一个有效的开关语句。但是,我会遇到独立错误。
我将从我的代码开始。大会取自这个答案。
汇编:
global mySwitch
section .text
mySwitch:
jmp [.jump_table + 2 * edi]
.jump_table: dw .one, .two
.one:
mov eax, 123
ret
.two:
mov eax, 321
ret
C:
#include <stdio.h>
int mySwitch(int arg);
int main() {
printf("%d\n", mySwitch(1));
}
我正在尝试使用以下命令进行编译:
nasm -f elf64 -w+all -w+error switch.asm -o switch_asm.o
gcc -c -Wall -Wextra -std=c17 -O2 switch.c -o switch_c.o
gcc switch_asm.o switch_c.o -o switch
但是第三个返回以下错误:
/usr/bin/ld: switch_asm.o: relocation R_X86_64_32 against `.text' can not be used when making a PIE object; recompile with -fPIE
collect2: error: ld returned 1 exit status
make: *** [makefile:4: all] Error 1
使用-fpie
switch switch违反了分配规则(并且也没有帮助) ,而且我不知道我缺少什么(以前,问题是由缺失的rel
或wrt ..plt ..plt
引起的)。
更新: 将我的汇编代码稍微更改为64位地址,lae
指令类似:
lea rax, [rel .jump_table]
jmp [rax + rdi * 8]
.jump_table: dq .one, .two
编译,并按预期工作。我很想发布这是关于如何在ASM中编写Switch语句的答案,但我不能,因为尽管没有重复的副本,但由于重复的副本而关闭了这个问题。
I am writing a project in x86-64 Assembly and I want to write an efficient switch statement using a branch lookup table. However, I get position independence errors.
I'll start with my code. The assembly was taken from this answer.
Assembly:
global mySwitch
section .text
mySwitch:
jmp [.jump_table + 2 * edi]
.jump_table: dw .one, .two
.one:
mov eax, 123
ret
.two:
mov eax, 321
ret
C:
#include <stdio.h>
int mySwitch(int arg);
int main() {
printf("%d\n", mySwitch(1));
}
I am trying to compile it with the following commands:
nasm -f elf64 -w+all -w+error switch.asm -o switch_asm.o
gcc -c -Wall -Wextra -std=c17 -O2 switch.c -o switch_c.o
gcc switch_asm.o switch_c.o -o switch
but the third one returns the following error:
/usr/bin/ld: switch_asm.o: relocation R_X86_64_32 against `.text' can not be used when making a PIE object; recompile with -fPIE
collect2: error: ld returned 1 exit status
make: *** [makefile:4: all] Error 1
Using the -fPIE
switch is against the rules of the assignment (and also does not help), and I do not know what I am missing (previously, the problem was caused by a missing rel
or wrt ..plt
).
Update:
Changing my assembly code slightly to 64-bit addresses and a lea
instruction like so:
lea rax, [rel .jump_table]
jmp [rax + rdi * 8]
.jump_table: dq .one, .two
compiles, and works as expected. I'd love to post this is an answer as to how to write a switch statement in asm but I cannot because this question is closed due to being a duplicate despite not being a duplicate so oh well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论