IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Load shared libraries with preferred base address

    hev发表于 2020-09-21 09:48:39
    love 0

    For performance, sometimes we need to adjust the address space layout. The following way allows the shared or executable object to be loaded with preferred base address.

    When the dynamic link maps the shared object, the virtual address of segment(that in ELF program header) will be used as the mapping address hints.

    Type 1
    Link with text-segment starting address.

    # Adjust base address to 0x10000 */
    gcc -shared ... -Wl,-Ttext-segment=0x10000
    

    Type 2
    We can adjust the virtual address of loaded segment by linker scripts.

    0x1 Get link script template

    gcc -shared -Wl,--verbose > ld.script
    

    Clear the contents before and after the ‘equal sign’.

    0x2 Adjust the base address

    ....
    SECTIONS
    {
        /* Adjust base address to 0x10000 */
        . = SEGMENT_START("text-segment", 0x10000) + SIZEOF_HEADERS;
        ...
    }
    ....
    

    0x3 Link with script

    gcc -shared ... -Wl,-T ld.script
    

    Over!



沪ICP备19023445号-2号
友情链接