当前位置:网站首页>CSAPP家庭作业答案7 8 9章
CSAPP家庭作业答案7 8 9章
2022-07-06 09:24:00 【芜湖韩金轮】
答案仅供参考
7.12
解题思路是模仿链接器的行为:使用重定位记录来识别位置然后使用图7.9中的算法计算重新定位的绝对地址,或者简单地从图7.10中的重定位指令中提取它们。有两点需要注意的是第8行的movl指令包含两个需要重定位的引用。第5行和第8行的指令包含对buf[1]的引用,初始值为Ox4,重定位的地址计算为ADDR(buf) +4。
- 第一次重定位是mov 0x0,%edx,对应于图7-10中的15行,将swap.o中偏移量为3-6的位置上的0x0重定位于运行时的真实地址0x80483c8+3=0x80483cb,其值为0x804945c
- 第二次重定位是mov 0x4,%eax,在图中16行,将swap.o中偏移量为8-b位置上的0x4重定位与运行时的真实地址0x80483c8+8=0x80483d0,其值为0x8049458
- 第三次和第四次重定位在movl $0x4,0x0,对应于图7-10的18行,将swap.o中偏移量为10-13的0x0和偏移量为14-17的0x4重定位于运行时的地址0x80483c8+10=0x80483d8和0x80483c8+14=0x80483dc,并将movl $0x4,0x0修改为movl $0x8049458,0x8049548
- 第五次重定位是mov 0x0,%eax,在图7-10的23行,将偏移量为1f-22的0x0重定位于地址0x80483c8+1f=0x80483e7上的值0x8049548
结果如下表所示:
图7-10中的行号 | 地址 | 值 |
---|---|---|
15 | 0x80483cb | 0x004945c |
16 | 0x80483d0 | 0x0049458 |
18 | 0x80483d8 | 0x0049548 |
18 | 0x80483dc | 0x0049458 |
23 | 0x80483e7 | 0x0049548 |
7.13
A
在.text的节的节偏移12处调用了函数,在节偏移为19处发生了值的传递,并且值为0x0,则要被重定位。结合c代码可知在此处将调用p3()的返回值放在edx寄存器中,再和*xp相加,然后再在偏移为21的位置发生函数p2()的调用。所以三次重定位的节偏移分别为12,19,21,分别对指令call 12<p1+0xa>、指令mov 0x0,%eax和指令call 21<p1+0x19>进行重定位。 或者直接使用objdump反汇编也可以得到相同结果。
结果如下表所示:
节偏移 | 重定位类型 | 符号名字 |
---|---|---|
0x12 | R_386_PC32(相对引用) | p3 |
0x19 | R_386_32(绝对引用) | xp |
0x21 | R_386_PC32(相对引用) | p2 |
B
.data 节中修改了xp的值,重定位于x的地址。所以 .data 节只有一次重定位, 节偏移为0x4,重定位类型是重定位绝对引用,符号名字是xp。
节偏移 | 重定位类型 | 符号名字 |
---|---|---|
0x4 | R_386_32(绝对引用) | xp |
8.23
父进程接收并捕获第一个信号,当处理程序还在处理第一个信号时,第二个信号传来被添加到待处理集合中,此时被处理程序阻塞了就还不会被接收,当第三个信号传来,由于类型和第二个信号相同就会被丢弃,第4、5个信号也是如此。当第一个信号处理完毕后,内核注意到还有一个待处理信号(信号2)就会强迫父进程接收,然后执行处理程序,第二次处理完毕后,没有待处理的信号了,就结束。所以只会接收处理两个信号,counter值只会是2,而不是5。
8.24
题目的要求是在子进程中对只读文本段进行写操作,这会导致段错误,所以第二个要求是要输出段错误。而判断异常终止可以使用WIFSIGNALED来判断,若为非0则发生异常终止,修改后的代码如下:
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#define N 2
int main()
{
int status,i;
pid_t pid;
for(i = 0;i < N;i++)
if((pid = fork()) == 0)
{
int *pt = 0x0;
*pt = 15213;
exit(100+i);
}
while((pid = waitpid(-1,&status,0)) > 0)
{
if(WIFEXITED(status))
printf("child %d terminated normally with exit status = %d\n",pid,WEXITSTATUS(status));
else if(WIFSIGNALED(status))
{
fprintf(stderr,"child %d terminated by signal %d",pid,WTERMSIG(status));
psignal(WTERMSIG(status)," ");
}
else
printf("child %d terminated abnormally\n",pid);
}
if(errno != ECHILD)
printf("waitpid error\n");
exit(0);
}
输出如下:
9.11
A. 虚拟地址为0x027c,转换成二进制:00 0010 0111 1100
13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
B .按照9.6.4节的假设,VPO为地址的0位到5位,本题为111100,VPN为6位到13位,本题为00001001,即0x9,TLBI是6-7位为0x1,TLBT为8-13位为0x2。查表可知索引位0x1、标记位为0x2,不命中,MMU需要从页表中的PTE中取出PPN,得到有效的PPN为0x17,没有缺页。
完整表格为:
参数 | 值 |
---|---|
VPN | 0x9 |
TLB索引 | 0x1 |
TLB标记 | 0x2 |
TLB命中?(是/否) | 否 |
缺页?(是/省) | 否 |
PPN | 0x17 |
C. 将得到的物理页帧号0x17( 01 0111 )作为实际物理地址的前6位,偏移量不变,仍为VPO( 11 1100 ),得到最终的物理地址: 0101 1111 1100
11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
D. 将物理地址的6-11位的010111作为CT,其值为0x17,2-5位的1111作为CI其值为0xf,0-1位作为块偏移CO值为0x0。在高速缓存中找索引位0xf的组、标记位0x17的行、偏移为0x0的字节,由高速缓存表可知0xf的组无0x17标记的行,故不命中,无返回字节。 结果如下表:
参数 | 值 |
---|---|
字节偏移 | 0x0 |
缓存索引 | 0xf |
缓存标记 | 0x17 |
缓存命中?(是/否) | 否 |
返回的缓存字节 | - |
9.12
A.虚拟地址为0x03a9,其二进制为00 0011 1010 1001
13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 1 | 0 | 0 | 1 |
B. 按照9.6.4节的假设,VPO为地址的0位到5位,本题为101001,VPN为6位到13位,本题为0000 1110,即0xe,TLBI是6-7位为0x2,TLBT为8-13位为0x3。查表可知索引位0x2、标记位为0x3,由于有效位为0不命中,MMU需要从页表中的PTE中取出PPN,得到有效的PPN为0x11,没有缺页。
完整表格为:
参数 | 值 |
---|---|
VPN | 0xe |
TLB索引 | 0x2 |
TLB标记 | 0x3 |
TLB命中?(是/否) | 否 |
缺页?(是/省) | 否 |
PPN | 0x11 |
C. 将得到的物理页帧号0x11( 01 0001)作为实际物理地址的前6位,偏移量不变,仍为VPO( 10 1001 ),得到最终的物理地址: 0100 0110 1001
11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
D:将物理地址的6-11位的010001作为CT,其值为0x11,2-5位的1010作为CI其值为0xa,0-1位作为块偏移CO值为0x1。在高速缓存中找索引位0xa的组、标记位0x11的行、偏移为0x1的字节,由高速缓存表可知0xa的组无0x11标记的行,故不命中,无返回字节。
完整表格:
参数 | 值 |
---|---|
字节偏移 | 0x1 |
缓存索引 | 0xa |
缓存标记 | 0x11 |
缓存命中?(是/否) | 否 |
返回的缓存字节 | - |
9.13
A.虚拟地址为0x0040,其二进制为00 0000 0100 0000
13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
B. 按照9.6.4节的假设,VPO为地址的0位到5位,本题为000000,,VPN为6位到13位,本题为0000 0001,即0x1,TLBI是6-7位为0x1,TLBT为8-13位为0x0。 当VPN=1时,有效位为0,PPN无。所以就没有物理地址。有缺页。
参数 | 值 |
---|---|
VPN | 0x1 |
TLB索引 | 0x1 |
TLB标记 | 0x0 |
TLB命中?(是/否) | 否 |
缺页?(是/省) | 是 |
PPN | 0x11 |
C.缺页
D.缺页
9.14
代码如下
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
int fd;
if ((fd = open("hello.txt", O_RDWR)) == -1) {
perror("Failed to open hello.txt");
exit(EXIT_FAILURE);
}
struct stat stat_of_file;
if (fstat(fd, &stat_of_file) == -1) {
perror("Failed to get stat of hello.txt");
exit(EXIT_FAILURE);
}
char *p;
if ((p = mmap(NULL, stat_of_file.st_size, PROT_WRITE, MAP_SHARED, fd, 0)) == (void *)-1) {
perror("Failed to mmap");
exit(EXIT_FAILURE);
}
p[0] = 'J';
munmap(p, stat_of_file.st_size);
close(fd);
return 0;
}
运行结果:
边栏推荐
- 函数:字符串反序存放
- My first blog
- 《统计学》第八版贾俊平第四章总结及课后习题答案
- Interview Essentials: what is the mysterious framework asking?
- Bing Dwen Dwen official NFT blind box will be sold for about 626 yuan each; JD home programmer was sentenced for deleting the library and running away; Laravel 9 officially released | Sifu weekly
- 内网渗透之内网信息收集(三)
- Function: find the root of the equation by Newton iterative method
- [issue 18] share a Netease go experience
- Statistics 8th Edition Jia Junping Chapter 7 Summary of knowledge points and answers to exercises after class
- JDBC 的四种连接方式 直接上代码
猜你喜欢
王爽汇编语言详细学习笔记二:寄存器
Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025
Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
四元数---基本概念(转载)
Wang Shuang's detailed notes on assembly language learning I: basic knowledge
后台登录系统,JDBC连接数据库,做小案例练习
Wang Shuang's detailed learning notes of assembly language II: registers
"If life is just like the first sight" -- risc-v
数字电路基础(一)数制与码制
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
随机推荐
DVWA exercise 05 file upload file upload
【指针】八进制转换为十进制
Flash implements forced login
Fundamentals of digital circuits (III) encoder and decoder
《统计学》第八版贾俊平第一章课后习题及答案总结
How does SQLite count the data that meets another condition under the data that has been classified once
"If life is just like the first sight" -- risc-v
Pointeurs: maximum, minimum et moyenne
Keil5 MDK's formatting code tool and adding shortcuts
Face and eye recognition based on OpenCV's own model
Don't you even look at such a detailed and comprehensive written software test question?
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
Function: find 1-1/2+1/3-1/4+1/5-1/6+1/7-... +1/n
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
内网渗透之内网信息收集(三)
Quaternion -- basic concepts (Reprint)
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
【指针】统计一字符串在另一个字符串中出现的次数