第九题 - ld 脚本 - 指定自定义 section

本节目录

题目要求

编写一个 ld 文件(命名为 custom_section_.ld),设置内存区域起始地址为 0x8000000,长度为 0x2000,指定 setions 包含一个自定义的 section 名为 my_custom_section,并在 my_custom_section 内放置 my_custom_data,其地址为 0x1234。

输入

# 将会在测试脚本中尝试:
# 打印 `my_custom_data` 的地址,匹配是否为 0000000000001234
$(readelf -s "$EXECUTABLE" | grep ' my_custom_data$' | awk '{print $2}')

输出

运行测试时,检测到"Test passed."字样。

已有代码介绍

MEMORY {
    /* >>> 在这里定义内存区域,设置内存区域起始地址为 `0x8000000`,长度为 `0x2000`。 */

    /* <<< */
}


SECTIONS {
    .text : {
        *(.text)
        /* 其他段的定义 */
    }


    /* 定义只读数据段,包含常量数据 */
    .rodata : {
        *(.rodata)
    } > RAM

    /* 定义BSS段,包含未初始化的全局变量和静态变量 */
    .bss : {
        *(.bss)
    } > RAM

    /* >>> 在这里添加 my_custom_sectoin,并在其内放置 `my_custom_data`,地址设置 0x1234 。*/

    /* <<< */


    /* 定义程序头表 */
    . = ALIGN(4);
    .interp : { *(.interp) }
    .note.gnu.build-id : { *(.note.gnu.build-id) }
    .dynamic : { *(.dynamic) }
    .hash : { *(.hash) }
    .gnu.hash : { *(.gnu.hash) }
    .dynsym : { *(.dynsym) }
    .dynstr : { *(.dynstr) }
    .gnu.version : { *(.gnu.version) }
    .gnu.version_d : { *(.gnu.version_d) }
    .gnu.version_r : { *(.gnu.version_r) }
    .eh_frame : { *(.eh_frame) }
    . = ALIGN(8);
    . = . + SIZEOF_HEADERS;
}

提示

你需要了解 ld 脚本的结构,之后,在 Memory 和 SECTIONS 内,按照中文注释完成对应要求。有和上一题相似的地方的话,可以考虑复用。

注意事项

无特殊注意事项