gpt4 book ai didi

linux - Debian Mips 系统调用 unistd.h 丢失了吗?

转载 作者:IT王子 更新时间:2023-10-29 00:34:16 28 4
gpt4 key购买 nike

我正在使用 qemu 运行 Debian Squeeze mips 架构,只是想弄清楚我可以放入 $v0 中执行的系统调用是什么。我找到了这个 webpage .尽管此页面确实告诉我写命令使用 4。我执行以下操作:

我正在使用 this image安装 Debian Squeeze。

是的,它使用 mips 作为系统架构:

uname -a
Linux debian 2.6.32-5-4kc-malta #1 Sat Feb 16 12:43:42 UTC 2013 mips GNU/Linux

--测试.asm--

.global __start
.text
__start:
li $v0, 4
li $a0, 1
la $a1, out_string
li $a2, 12
syscall
.data
out_string: .asciiz "Hello World\n"

现在我使用“as”进行组装,使用“ld”进行链接

as -march=mips32 -o test.o test.asm
ld -o test.out test.o
chmod +x test.out
./test.out

不幸的是,这会导致错误:Illegal Instruction

我编译了以下 c 程序以确定写入函数使用的偏移量及其参数以进行双重检查。

--写.c--

#include <stdio.h>

int main(int argc, char *argv[]){
printf("Hello World\n");
return 0;
}

运行 strace -i ./write.out 给出:

[2abbcbe0] write(1, "Hello World\n", 12Hello World)

write 的手册页说明如下:

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);

我尝试使用 write 命令输出“Hello World”,它本身就可以正常工作。

这里描述了 write 函数的作用以及我是如何得出我编写 test.asm 的方式的:

fd 或 $a0 - 从打开调用中获得的文件描述符。它是一个整数值。也可以分别为标准输入、标准输出和标准错误给出值 0、1、2

buff 或 $a1 - 指向一个字符数组,可用于存储从 fd 指向的文件中获取的内容。

nbytes 或 $a2 - 指定要从字符数组写入 fd 指向的文件的字节数。

因此我们得出顺序:

#0x4 being what unistd.h for mips tells us the system call code is 4
$v0, 4
#Set first argument to the std output 1 for the file descriptor
$a0, 1
#Set the address for the string or buff
$a1, out_string
#Set nbytes or total bytes of the string
$a2, 12

这个例子在 Mars 和 Spim 中工作得很好,尽管只有一个参数而不是 3 个:

--example.asm--

.data
out_string: .asciiz "\nHello, World!\n"
.text
main:
li $v0, 4
la $a0, out_string
syscall
li $v0, 10
syscall

因为我在 test.asm 中得到了 Illegal Instruction,这意味着我必须指向一个无效的指令。尽管之前观察到的系统调用代码 here .不正确,即使它是 mips 架构的 unistd.h 文件,我们确认查看写入的手册页及其参数。所以现在我尝试了 strace,认为 $v0 之前指向了错误的指令,并使用 0x2abbcbe0 找到了指令指针。也许这会奏效?让我们试试。

在 strace 中,-i 选项在系统调用时打印指令指针。

所以我编辑asm文件

--测试.asm--

.global __start
.text
__start:
li $v0, 0x2abbcbe0
li $a0, 1
la $a1, out_string
li $a2, 12
syscall
.data
out_string: .asciiz "Hello World\n"

再次运行组装和链接过程:

as -march=mips32 -o test.o test.asm
ld -o test.out test.o
chmod +x test.out
./test.out

不幸的是,这导致了错误:Illegal Instruction again。

我还颠倒了 $a 寄存器,以为我是向后做的,我多次运行 strace 并确认指令指针中的值没有改变,这表明实际上禁用了 aslr。我在/usr/include/中查找了 unistd.h 文件,它看起来与开头显示的网站上的文件完全不同。有趣的是,这种结构在 spim 和 Mars 中都能完美运行,它们都使用 li $v0, 4 模拟它们的打印/写入系统调用。我知道 Mars 和 Spim 使用它们自己的模拟系统调用。以为 strace 会帮我找到它们,但这似乎行不通。

我在哪里可以找到 $v0 的正确值?我觉得我已经将 Google 搜索到了现在毫无意义的地方。

也许我弄乱了 asm 文件及其语法?

让我们尝试一些更简单的方法,只需运行 exit:

.global __start
.text
__start:
li $v0, 1
li $a0, 0
syscall

组装链接并运行

as -march=mips32 -o test.o test.asm
ld -o test.out test.o
chmod +x test.out
./test.out
Illegal Instruction

也试过不用-march=mips32

as -o test.o test.asm
ld -o test.out test.o
chmod +x test.out
./test.out
Illegal Instruction

这应该是最容易执行的事情。

我在这里不知所措,请大家帮忙。

最佳答案

如果您愿意使用 Linux 系统调用,那么在 MIPS 平台(马耳他)上您可以找到系统调用列表 here .在此体系结构上,Linux 将其系统调用从 4000 开始映射。所使用的 ABI 是 MIPS o32。该文件的片段如下。请注意,您可以在系统上的 /usr/include/asm/unistd.h 中找到该文件的副本:

#define __NR_Linux          4000
#define __NR_exit (__NR_Linux + 1)
#define __NR_write (__NR_Linux + 4)
#define __NR_exit_group (__NR_Linux + 246)

下面的代码应该打印Hello World然后退出:

    .text
.globl __start
__start:
li $v0, 4004
li $a0, 1
la $a1, out_string
li $a2, 12
syscall
li $v0, 4001
la $a0, 1
syscall

.data
out_string: .asciiz "Hello World\n"

__NR_exit_group (4246) 在 Linux man pages 中定义并且在一个主要方面不同于普通的 exit (4001):

This system call is equivalent to exit(2) except that it terminates not only the calling thread, but all threads in the calling process's thread group.

关于linux - Debian Mips 系统调用 unistd.h 丢失了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32062807/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com