【RISC-V】.pushsection .popsection和.section .previous的区别

规则如下:

.pushsection.popsection维护一个私有的段栈

1、.pushsection行为:

  1. 将当前段(current section)压入段栈。

  2. 切换到指定的name段(如果不存在则创建)。

  3. 后续的汇编代码写入新段。

2、.popscetion行为:

  1. 从段栈弹出最顶层的段名。

  2. 直接切换回该段(作为当前段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

相关推荐
蒹葭玉树3 天前
【C++上岸】C++常见面试题目--操作系统篇(第三十期)
c++·面试·risc-v
国科安芯5 天前
面向星载芯片原子钟的RISC-V架构MCU抗辐照特性研究及可靠性分析
单片机·嵌入式硬件·架构·制造·risc-v·pcb工艺·安全性测试
思尔芯S2C8 天前
FPGA原型验证实战:如何应对外设连接问题
fpga开发·risc-v·soc设计·prototyping·原型验证
加强洁西卡8 天前
【RISC-V】从C到可执行文件分析链接重定位的过程
c语言·开发语言·risc-v
硬汉嵌入式8 天前
基于Rust构建的单片机Ariel RTOS,支持Cortex-M、RISC-V 和 Xtensa
单片机·rust·risc-v
MounRiver_Studio8 天前
RISC-V IDE MRS2进阶分享(三):MRS语言服务器
ide·mcu·risc-v·嵌入式开发
加强洁西卡8 天前
【RISC-V】解决链接器加入全局变量优化后操作系统无法启动的问题
risc-v
MounRiver_Studio8 天前
RISC-V IDE MRS2进阶分享(四):CH32H417双核芯片项目开发
ide·mcu·risc-v·嵌入式开发
加强洁西卡8 天前
【RISC-V】区分加载地址、链接地址、运行地址
risc-v
飞凌嵌入式9 天前
1块集成了4核Cortex-A7高性能CPU、1颗RISC-V MCU、多种高速总线、还兼容树莓派的T153低成本开发板
linux·arm开发·嵌入式硬件·risc-v