I am developing an operating system, and I need to load some modules BEFORE paging is set up. So since paging is not set up at this point I need to relocate all of the symbols in the program to there physical address. My problem is that not all symbols can be found in the symbol table and not all relocation info can be found in rel.text. How can I get GCC to export all symbol data???
-
The relocation table must contain enough information to relocate the executable to an arbitrary location. That information need not cover all symbols. Your local variables aren't relocated, for example. – Alexey Frunze Jan 21 '13 at 06:49
-
Did you try compiling with -fPIC, for position independent code? – Vlad Krasnov Jan 21 '13 at 07:09
2 Answers
Surely, ANYTHING needing relocation will be in the relocation table. How else could it be loaded? Whether paging is enabled or not, relocation works exactly the same - entries that are absolute locations in the binary are listed with an offset, and then processed by the loading software. Everything else should be fine without relocation.
Note that a symbol table is not meaningful for resolving relocations in and of itself, as that only gives the location of a symbol.
Are you perhaps thinking of the symbols in your OS itself? If so, it's really a case of exporting the symbols from your OS in an appropriate way. Linux has EXPORT_SYMBOL(name), which builds a symbol table within the kernel itself. [Note that this is NOT the symbols generated by gcc
or ld
, but symbols built by macros, and processed in the kernel.
Edit to clarify, as I ran out of space in "comment":
There are two types of "relocations": Internal ones - where you have absolute references to things in your own module, e.g. pointers to strings, poitners to functions, jump tables for switch statements, and so on - these should simply be a question of adding up the current value with the offset for where the binary is actually located (virtual address of course). The other is "external references", such as when your module calls, say spinlock()
- this is not implemented inside the module, so it will have an "external reference". In this case, there will be a relocation entry with "spinlock" as the name and an offset of where the call to spinlock goes in the module. Now you obviously need a symbol table to look up where in your kernel "spinlock" is located [and if you want to be really complicated, allow for moduels to reference other modules, but I'd leave that until you have one module loading OK first!].

- 126,704
- 14
- 140
- 227
-
I am kind of confused because I thought that the symbol index was stored in reloc_info. The only other data in a relocation entry is the address of were to apply the relocation. So you would have to get the address of what is at rel_offset by looking at the symbol table and finding the value for that symbol right? – user1454902 Jan 21 '13 at 09:00
-
I'm not 100% sure what you are trying to solve (and about to try to sledge to work, so will have to get back to you tonight). I've made an edit to my original answer to try to clarify the two cases of relocating things. If that doesn't cover it, then please explain in more detail, perhaps with an example, what you are trying to solve. – Mats Petersson Jan 21 '13 at 09:08
Really your question is about the linker. And the answer depends on the linker, that you are using.
If it is the standard linker ld
under gcc, try the "-Wl,-r" option.

- 76,821
- 6
- 102
- 177

- 6,791
- 1
- 27
- 36