Cómo distinguir entre símbolos reubicables y no reubicables dentro de la sección .data.rel

I'm trying to create a simple linker for a barebone ARM application. Currently the loader, that loads the module, will simply add the offset to all records inside the .got y .data.rel sections. This works fine in .got, and for all symbols that need relocation inside .data.rel. It will break though for all non-relocatable data, as those will get this offset too.

Ejemplo:

void some_function() { return; }

struct a {
    void* fptr;
    int number;
};

static struct a = {
   .fptr = some_function,
   .number = 0x1000,
};

Aquí a.fptr will correctly address the actual location of the function, but a.number will incorrectly hold 0x1000 + offset, en lugar de solo 0x1000.

How should I distinguish between the two? Is it enough that I check the .symtab section and only relocate addresses that are found there? But what if a symbol is actually at location 0x1000? Or does the linker address this issue (so it will not put a function at address 0x1000)? Does .symtabs actuall contain all symbols that can be found inside .got y .data.rel?

preguntado el 09 de marzo de 12 a las 14:03

I wrote a basic ELF loader a while ago and I recall that you only add offsets to relocation entries marked as "R_ARM_ABS32". You can find the code here github.com/tangrs/ndless-elfloader/blob/master/elf/elf_load.c -

and how to add relocation entries to the elf file? readelf says it's not there, but I use -r -fpic to compile/link. (btw. you should add this as an answer so I can upvote it) -

1 Respuestas

I wrote a basic ELF loader a while ago and I recall that you only add offsets to relocation entries marked as "R_ARM_ABS32".

Puedes encontrar el código aquí https://github.com/tangrs/ndless-elfloader/blob/master/elf/elf_load.c

I simply linked my ELF files with --emit-relocs turned on. That way, the linker does all the linking, it just tells me what it did so you can fix up offsets during load time.

respondido 11 mar '12, 06:03

The actual problem was that I used gcc, so i had to use "-Xlinker --emit-relocs" in the command line. With relocs enabled the task became easy. - SztupY

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.