【问题标题】:Memory overlapping with ld and ar commands内存与 ld 和 ar 命令重叠
【发布时间】:2012-11-30 11:43:47
【问题描述】:

我已经用这个命令链接了一些文本文件:

ld -r -b 二进制 -o resources1.o *.txt

我得到一个文件resources.o,内容如下:

纳米资源1.o

00000018 D _binary_texto4_txt_end
00000018 A _binary_texto4_txt_size
00000000 D _binary_texto4_txt_start
00000031 D _binary_texto5_txt_end
00000019 A _binary_texto5_txt_size
00000018 D _binary_texto5_txt_start
0000004a D _binary_texto6_txt_end
00000019 A _binary_texto6_txt_size
00000031 D _binary_texto6_txt_start

我有来自另一个 ld 命令的其他 resources2.o 文件,但它有不同的内容:

00000018 D _binary___textos1_texto1_txt_end
00000018 A _binary___textos1_texto1_txt_size
00000000 D _binary___textos1_texto1_txt_start
00000031 D _binary___textos1_texto2_txt_end
00000019 A _binary___textos1_texto2_txt_size
00000018 D _binary___textos1_texto2_txt_start
0000004a D _binary___textos1_texto3_txt_end
00000019 A _binary___textos1_texto3_txt_size
00000031 D _binary___textos1_texto3_txt_start

我想将两个 resources.o 文件合并到一个 libSum.a 文件中。所以我使用这个命令:

ar rvs libSum.a resources1.o resources2.o

当我将 libSum.a 链接到我的 C 程序并尝试使用这些文本时,我不能因为它们共享相同的内存偏移量。所以 binary__textos1_texto1_txt_start 与 _binary_texto4_txt_start (0X00000000) 方向一致。

如何将两个资源文件合并到一个 .a 库中以避免内存偏移重叠? 谢谢

【问题讨论】:

    标签: gcc compilation ld unix-ar hardcoded


    【解决方案1】:

    文件内容有一个愚蠢的错误。它们是同一个文件,但名称不同(复制和粘贴错误),因此在显示其内容时,它似乎是内存偏移错误。

    现在,我正在使用下一个脚本来编译“libResources.a”库中的所有资源:

    rm libResources.a
    rm names.txt
    
    basedir=$1
    for dir in "$basedir"/*; do
        if test -d "$dir"; then
        rm "$dir"/*.o
        ld -r -b binary -o "$dir"/resources.o "$dir"/*
        nm "$dir"/resources.o >> names.txt
        fi
    done
    
    for dir in "$basedir"/*; do
        if test -d "$dir"; then
        ar q libResources.a "$dir"/*.o
        fi
    done
    

    为了测试我的硬编码资源,我使用以下 C 代码:

    /*
     ============================================================================
     Name        : linkerTest.c
     ============================================================================
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    extern char _binary___textos1_texto1_txt_start[];
    extern char _binary___textos1_texto1_txt_end[];
    
    extern char _binary___textos2_texto4_txt_start[];
    extern char _binary___textos2_texto4_txt_end[];
    
    int main(void) {
        int i;
        int sizeTexto1 = _binary___textos1_texto1_txt_end - _binary___textos1_texto1_txt_start;
        int sizeTexto4 = _binary___textos2_texto4_txt_end - _binary___textos2_texto4_txt_start;
    
    
        for (i=0;i<sizeTexto1;i++){
            putchar(_binary___textos1_texto1_txt_start[i]);
        }
    
        for (i=0;i<sizeTexto4;i++){
            putchar(_binary___textos2_texto4_txt_start[i]);
        }
    
        return EXIT_SUCCESS;
    }
    

    如果您想测试我的示例,请不要忘记在您的项目中链接文件“libResources.a”。

    【讨论】:

      猜你喜欢
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多