如何在共享库中嵌入数据?

问题描述 投票:1回答:1

例如,我想将dicmap.bin嵌入到共享库libxxx.so中。我编写了一个程序来验证它。

test_dicmap.cpp
#include <stdio.h>
#include <stdint.h>

extern "C" {
extern const uint8_t _binary_dicmap_bin_start[];
extern const uint8_t _binary_dicmap_bin_end[];
extern const void* _binary_dicmap_bin_size;
}

int main()
{
    size_t size = (size_t)&_binary_dicmap_bin_size;
    printf("start=%p, end=%p\nend-start=%zd, size=%zd\n", 
        _binary_dicmap_bin_start, 
        _binary_dicmap_bin_end,
        _binary_dicmap_bin_end - _binary_dicmap_bin_start,
        size);
    printf("data[0..8]=%02x %02x %02x %02x %02x %02x %02x %02x\n", 
            _binary_dicmap_bin_start[0], _binary_dicmap_bin_start[1], 
            _binary_dicmap_bin_start[2], _binary_dicmap_bin_start[3],
            _binary_dicmap_bin_start[4], _binary_dicmap_bin_start[5], 
            _binary_dicmap_bin_start[6], _binary_dicmap_bin_start[7]);
}

但是其_start_end_size无效。

]$ ls dicmap.bin  -l
-rw-rw-r-- 1 kirbyzhou kirbyzhou 198600798 Feb 26 10:58 dicmap.bin

]# objcopy -B i386 -I binary -O elf64-x86-64 dicmap.bin dicmap.o && g++ -o libxxx.so dicmap.o -shared  &&  g++ -L. -lxxx  test_dicmap.cpp
/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/ld: warning: type and size of dynamic symbol `_binary_dicmap_bin_size' are not defined
/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/ld: warning: type and size of dynamic symbol `_binary_dicmap_bin_start' are not defined
/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/ld: warning: type and size of dynamic symbol `_binary_dicmap_bin_end' are not defined

]# ./a.out
start=0x601034, end=0x601034
end-start=0, size=6295604
data[0..8]=00 00 00 00 00 00 00 00

起点和大小应为sizeof dicmap.bin(198600798)。

我的objcopy是rhel7和binutils-2.30-54.el7devtoolset-8

我尝试将共享标志添加到.o文件,但发生错误:

objcopy -B i386 -I binary -O elf64-x86-64 dicmap.bin dicmap.o --set-section-flag .data=share
objcopy: BFD version 2.30-54.el7 internal error, aborting at elf.c:8869 in _bfd_elf_set_section_contents

objcopy: Please report this bug.
binutils-2.27-41.base.el7_7.1.x86_64也有相同的问题。

有什么方法可以帮助我吗?

linux shared-libraries rhel binutils
1个回答
0
投票
现在有两种方法对我有用。
© www.soinside.com 2019 - 2024. All rights reserved.