北航操作系统实验lab1

北航实验lab1

Exercise 1.1

原指令如图

按照指导书修改并运行

发现可以在指定目录下找到vmlinux文件

Exercise 1.2

如下图便是我们需要补充的代码部分

补全后该部分如下

1
2
3
4
5
6
7
8
9
ptr_sh_table = binary + ehdr->e_shoff;    // 计算节头表的位置
sh_entry_count = ehdr->e_shnum; // 获取节头数量
sh_entry_size = ehdr->e_shentsize; // 获取每个节头的大小

// for each section header, output section number and section addr.
for (Nr = 0; Nr < sh_entry_count; ++Nr) { // 遍历所有节头
shdr = (Elf32_Shdr *)(ptr_sh_table + Nr * sh_entry_size); // 定位到当前节头
printf("Section %d: 0x%x\n", Nr, shdr->sh_addr); // 打印节号和地址
}

编译运行

Thinking 1.1

可以明显地看出,之前生成的vmlinux为大端存储,而testELF为小端存储,不能简单的读取大端存储的数据。

Exercise 1.3

第一次填写代码后编译

发现报错,重新阅读代码

发现没有指定内核地址,填写完成代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
OUTPUT_ARCH(mips)
/*
Set the architecture to mips.
*/
ENTRY(_start)
/*
Set the ENTRY point of the program to _start.
*/
SECTIONS
{
. = 0x80010000;
.text : { *(.text) }
.data : { *(.data) }
.bss : { *(.bss) }

/* To do:
fill in the correct address of the key sections
such as text, data, bss...
*/

end = . ;
}

这次成功编译

查看各个section地址

Thinking 1.2

0x8001000

使用jal跳转指令跳转

Exercise 1.4

补充代码如图

重新make并执行

1
/OSLAB/gxemul -E testmips -C R3000 -M64 elf-file

Exercise 1.5

给出了如图的参考

根据它以及其他资料完成代码的补全

重新make并执行之前那个指令

跑完第一遍只拿了20分。。。

查看输出发现是elf的输出格式没写对

修改之后重新提交

这次得了40,发现还是有问题,原来是vmlinux还是有错误

中间重新又走了一遍整遍lab1还是没发现问题在哪,修改了格式,但还是只有40分。我又重新研究了一下报错,原理是1.1的编译连接位置没对

最终重新修改后也是拿到了一百分