计算机系统篇之链接(12):Chapter 7 Linking 习题与解答
Author:stormQ
Monday, 15. July 2019 11:18AM
习题
- 7.6
7.6 解答
- 7.7
7.7 解答
- 7.8
7.8 解答
- 7.9
7.9 解答
- 7.10
7.10 解答
- 7.11
7.11 解答
- 7.12
7.12 解答
- 7.13
7.13 解答
解答
7.6 解答
swap.c:
extern int buf[];
int *bufp0 = &buf[0];
static int *bufp1;
static void incr()
{
static int count = 0;
count++;
}
void swap()
{
int temp;
incr();
bufp1 = &buf[1];
temp = *bufp0;
*bufp0 = *bufp1;
*bufp1 = temp;
}
生成可重定位目标文件 swap.o:
$ gcc -c swap.c
查看 swap.o 的符号表:
$ readelf -s swap.o
Symbol table '.symtab' contains 14 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FILE LOCAL DEFAULT ABS swap.c
2: 0000000000000000 0 SECTION LOCAL DEFAULT 1
3: 0000000000000000 0 SECTION LOCAL DEFAULT 3
4: 0000000000000000 0 SECTION LOCAL DEFAULT 5
5: 0000000000000000 8 OBJECT LOCAL DEFAULT 5 bufp1
6: 0000000000000000 22 FUNC LOCAL DEFAULT 1 incr
7: 0000000000000008 4 OBJECT LOCAL DEFAULT 5 count.1760
8: 0000000000000000 0 SECTION LOCAL DEFAULT 7
9: 0000000000000000 0 SECTION LOCAL DEFAULT 8
10: 0000000000000000 0 SECTION LOCAL DEFAULT 6
11: 0000000000000000 8 OBJECT GLOBAL DEFAULT 3 bufp0
12: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND buf
13: 0000000000000016 74 FUNC GLOBAL DEFAULT 1 swap
查看 swap.o 的表头:
$ readelf -S swap.o
There are 13 section headers, starting at offset 0x420:
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .text PROGBITS 0000000000000000 00000040
0000000000000060 0000000000000000 AX 0 0 1
[ 2] .rela.text RELA 0000000000000000 000002b8
00000000000000c0 0000000000000018 I 11 1 8
[ 3] .data PROGBITS 0000000000000000 000000a0
0000000000000008 0000000000000000 WA 0 0 8
[ 4] .rela.data RELA 0000000000000000 00000378
0000000000000018 0000000000000018 I 11 3 8
[ 5] .bss NOBITS 0000000000000000 000000a8
000000000000000c 0000000000000000 WA 0 0 8
[ 6] .comment PROGBITS 0000000000000000 000000a8
0000000000000033 0000000000000001 MS 0 0 1
[ 7] .note.GNU-stack PROGBITS 0000000000000000 000000db
0000000000000000 0000000000000000 0 0 1
[ 8] .eh_frame PROGBITS 0000000000000000 000000e0
0000000000000058 0000000000000000 A 0 0 8
[ 9] .rela.eh_frame RELA 0000000000000000 00000390
0000000000000030 0000000000000018 I 11 8 8
[10] .shstrtab STRTAB 0000000000000000 000003c0
000000000000005e 0000000000000000 0 0 1
[11] .symtab SYMTAB 0000000000000000 00000138
0000000000000150 0000000000000018 12 11 8
[12] .strtab STRTAB 0000000000000000 00000288
000000000000002d 0000000000000000 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
因此,该题的解答为:
符号 | swap.symtab 条目? | 符号类型 | 定义符号的模块 | 节 |
---|---|---|---|---|
buf | yes | extern | m.o | .data |
bufp0 | yes | global | swap.o | .data |
bufp1 | yes | local | swap.o | .bss |
swap | yes | global | swap.o | .text |
temp | no | - | - | - |
incr | yes | local | swap.o | .text |
count | yes | local | swap.o | .bss |
7.7 解答
a)先执行原来的代码
bar5.c:
double x;
void f()
{
x = -0.0;
}
foo5.c:
#include <stdio.h>
void f();
int x = 15213;
int y = 15212;
int main()
{
f();
printf("x = 0x%x(%d) y = 0x%x(%d)\n", x, x, y, y);
return 0;
}
**注意:**这里调整了书中变量x
和y
的定义顺序。因为在我们的系统中(X86-64 Linux, Li