文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
2. 标签
标签是数字常量符号,被链接器转换成对应的内存地址。
global _start section .rodata hello : db `hello, world!\n` section .text main: mov rax, 1 mov rdi, 1 mov rsi, hello mov rdx, 14 syscall jmp exit exit: mov rax, 60 xor rdi, rdi syscall _start: jmp main
$ objdump -d -M intel ./test 0000000000401000 <main>: 401000: b8 01 00 00 00 mov eax,0x1 401005: bf 01 00 00 00 mov edi,0x1 40100a: 48 be 00 20 40 00 00 movabs rsi,0x402000 401011: 00 00 00 401014: ba 0e 00 00 00 mov edx,0xe 401019: 0f 05 syscall 40101b: eb 00 jmp 40101d <exit> 000000000040101d <exit>: 40101d: b8 3c 00 00 00 mov eax,0x3c 401022: 48 31 ff xor rdi,rdi 401025: 0f 05 syscall 0000000000401027 <_start>: 401027: eb d7 jmp 401000 <main>
$ readelf -s test.o # 编译 Symbol table '.symtab' contains 8 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000000000 0 FILE LOCAL DEFAULT ABS test.s 2: 0000000000000000 0 SECTION LOCAL DEFAULT 1 3: 0000000000000000 0 SECTION LOCAL DEFAULT 2 4: 0000000000000000 1 OBJECT LOCAL DEFAULT 1 hello 5: 0000000000000000 0 NOTYPE LOCAL DEFAULT 2 main 6: 000000000000001d 0 NOTYPE LOCAL DEFAULT 2 exit 7: 0000000000000027 0 NOTYPE GLOBAL DEFAULT 2 _start
$ readelf -s test # 链接 Symbol table '.symtab' contains 13 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000401000 0 SECTION LOCAL DEFAULT 1 2: 0000000000402000 0 SECTION LOCAL DEFAULT 2 3: 0000000000000000 0 SECTION LOCAL DEFAULT 3 4: 0000000000000000 0 SECTION LOCAL DEFAULT 4 5: 0000000000000000 0 FILE LOCAL DEFAULT ABS test.s 6: 0000000000402000 1 OBJECT LOCAL DEFAULT 2 hello 7: 0000000000401000 0 NOTYPE LOCAL DEFAULT 1 main 8: 000000000040101d 0 NOTYPE LOCAL DEFAULT 1 exit 9: 0000000000401027 0 NOTYPE GLOBAL DEFAULT 1 _start 10: 000000000040300e 0 NOTYPE GLOBAL DEFAULT 2 __bss_start 11: 000000000040300e 0 NOTYPE GLOBAL DEFAULT 2 _edata 12: 0000000000403010 0 NOTYPE GLOBAL DEFAULT 2 _end
本地标签
以点(.)开头的标签被称作本地标签(local label),会自动添加前缀。这有助于将大段代码分成多个片段。且不用担心命名冲突,类似名字空间机制。
按源码顺序,前缀为上一个非本地标签名称。本地标签是一种符号命名机制,也是 LOCAL 符号。
global _start section .rodata hello : db `hello, world!\n` section .text main: jmp .print .print: mov rax, 1 mov rdi, 1 mov rsi, hello mov rdx, 14 syscall jmp .exit .exit: mov rax, 60 xor rdi, rdi syscall _start: mov rax, main.print ; 完整标签。 jmp rax
$ objdump -d -M intel ./test 0000000000401000 <main>: 401000: eb 00 jmp 401002 <main.print> 0000000000401002 <main.print>: 401002: b8 01 00 00 00 mov eax,0x1 401007: bf 01 00 00 00 mov edi,0x1 40100c: 48 be 00 20 40 00 00 movabs rsi,0x402000 401013: 00 00 00 401016: ba 0e 00 00 00 mov edx,0xe 40101b: 0f 05 syscall 40101d: eb 00 jmp 40101f <main.exit> 000000000040101f <main.exit>: 40101f: b8 3c 00 00 00 mov eax,0x3c 401024: 48 31 ff xor rdi,rdi 401027: 0f 05 syscall 0000000000401029 <_start>: 401029: 48 b8 02 10 40 00 00 movabs rax,0x401002 401030: 00 00 00 401033: ff e0 jmp rax
除标签外,还可使用 $
表达地址。
$
: 当前内存地址。$$
: 当前所在段(section)起始地址。
可在任意段(.data, .text)内使用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论