Search code examples
gcccompilationldunix-arhardcoded

Memory overlapping with ld and ar commands


I have linked some texts files with this command:

ld -r -b binary -o resources1.o *.txt

And I get a file resources.o with this content:

nm resources1.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

I have other resources2.o file comming from another ld command, but it has diferent content:

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

I would like to combine the two resources.o files in one libSum.a file. So I use this command:

ar rvs libSum.a resources1.o resources2.o

When I link libSum.a to my C program and try to use those texts, I can't beacuse they share the same memory offsets. So binary__textos1_texto1_txt_start have the same direction that _binary_texto4_txt_start (0X00000000).

How could I combine both resource files in one .a lib avoiding memory offset overlapping? Thanks


Solution

  • There was a silly error with file contents. They was the same file with diferent names (a copy&paste error), so when showing its content it seemed to be a memory offset error.

    By now, I'm using the next script to compile all my resources in a "libResources.a" library:

    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
    

    To test my hardcoded resources I use the following C code:

    /*
     ============================================================================
     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;
    }
    

    If you want to test my example, don't forget to link the file "libResources.a" in your project.