|
发表于 2022-10-26 22:11:23
|
显示全部楼层
本帖最后由 人造人 于 2022-10-26 22:15 编辑
可以想到,strerror这个函数就是返回了一个字符串常量
这个常量存储在只读存储区
如果操作系统支持的话,对只读存储区的写操作会被捕获到,而系统的默认处理程序会选择杀死这个进程
- #include <stdio.h>
- #include <string.h>
- char *get_str(void) {
- return "hello world!";
- }
- void print(const char *str) {
- puts(str);
- }
- int main(void) {
- char *str = get_str();
- puts(str);
- *str = '0';
- return 0;
- }
复制代码
- sh-5.1$ readelf -S main
- There are 37 section headers, starting at offset 0x4a48:
- Section Headers:
- [Nr] Name Type Address Offset
- Size EntSize Flags Link Info Align
- [ 0] NULL 0000000000000000 00000000
- 0000000000000000 0000000000000000 0 0 0
- [ 1] .interp PROGBITS 0000000000000318 00000318
- 000000000000001c 0000000000000000 A 0 0 1
- 。。。
- [16] .rodata PROGBITS 0000000000002000 00002000
- 0000000000000011 0000000000000000 A 0 0 4
- [17] .eh_frame_hdr PROGBITS 0000000000002014 00002014
- 0000000000000034 0000000000000000 A 0 0 4
- 。。。
- 00000000000001f2 0000000000000000 0 0 1
- [36] .shstrtab STRTAB 0000000000000000 000048d2
- 0000000000000176 0000000000000000 0 0 1
- Key to Flags:
- W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
- L (link order), O (extra OS processing required), G (group), T (TLS),
- C (compressed), x (unknown), o (OS specific), E (exclude),
- D (mbind), l (large), p (processor specific)
- sh-5.1$
- sh-5.1$ xxd main
- 00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............
- 00000010: 0300 3e00 0100 0000 4010 0000 0000 0000 ..>.....@.......
- 00000020: 4000 0000 0000 0000 484a 0000 0000 0000 @.......HJ......
- 。。。
- 00001ff0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
- 00002000: 0100 0200 6865 6c6c 6f20 776f 726c 6421 ....hello world!
- 00002010: 0000 0000 011b 033b 3000 0000 0500 0000 .......;0.......
- 00002020: 0cf0 ffff 6400 0000 2cf0 ffff 4c00 0000 ....d...,...L...
- 。。。
- 00005360: d248 0000 0000 0000 7601 0000 0000 0000 .H......v.......
- 00005370: 0000 0000 0000 0000 0100 0000 0000 0000 ................
- 00005380: 0000 0000 0000 0000 ........
- sh-5.1$
复制代码 |
|