规则如下:
.pushsection 和 .popsection维护一个私有的段栈
1、.pushsection行为:
将当前段(current section)压入段栈。
切换到指定的name段(如果不存在则创建)。
后续的汇编代码写入新段。
2、.popscetion行为:
从段栈弹出最顶层的段名。
直接切换回该段(作为当前段current section)。
.text
nop
.pushsection .data # 压栈text
.byte 42
.pushsection .rodata # 压栈data
.ascii "hello"
.popsection # 回到 .data
.byte 43
.popsection # 回到 .text
nop

⚠️⚠️⚠️规则如下:
GAS(GNU Assembler)内部维护previous_section 变量和current_section变量,当

.text # current=.text, previous=?
nop
.section .data # previous=.text, current=.data
.byte 42
.section .rodata # previous=.data, current=.rodata
.ascii "hello"
.previous # 交换:previous=.rodata,current=.data。回到.data段
.byte 43
.previous # 再次交换: previous=.data,current=.rodata。回到.rodata段
.text # previous=?, current=.text
nop
.section .data # previous=.text, current=.data
.byte 42
.section .rodata # previous=.data, current=.rodata
.ascii "hello"
.previous # 交换,previous=.rodata,current=.data。回到data段
.section .init # 此时在data段,所以previous=.data, current=.init
.byte 43
.previous # 交换,previous=.init,current=.data。回到data段
自测习题:
回答:最终 .byte 99(或 .quad 99 等)位于 .xxx 段。
1、基础
.text
nop
.section .data
.byte 1
.previous
.byte 99
2、稍复杂
.text
.section .rodata
.section .data
.previous
.previous
.byte 99
3、陷阱题
.text
.section .init
.section .fini
.previous
.section .data
.previous
.byte 99
4、混合使用
.text
.pushsection .data
.section .rodata
.previous
.popsection
.byte 99
5、嵌套 push/pop
.text
.pushsection .init
.pushsection .data
.section .rodata
.popsection
.previous
.quad 99
答案:
第一题:.text,第二题:.data,第三题:.init,第四题:.text,第五题:.data
