ELF library with fixed memory location, only symbols

Programming, for all ages and all languages.
Post Reply
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

ELF library with fixed memory location, only symbols

Post by NickJohnson »

I'm trying to make an interesting setup for a library using GNU binutils for ELF, and can't find instructions on how to do it. I want to make a library that is statically linked to one binary, but has a fixed position in memory (different than the rest of the code in the binary), and is able to be linked to other binaries in a way that it is not listed to be loaded into memory, and not statically linked. The purpose of this is to load it into a part of the address space that is persistent (like the kernel) at bootup along with init, and then access it from any other program. Most of it's function is similar to ld-linux.so on linux.

Essentially, this is what I would like to do:
1. Produce a library called libsys.a
2. Link init with it statically, so the program headers in init read roughly like this:

Code: Select all

Elf file type is EXEC (Executable file)
Entry point 0x8049100
There are 3 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000000 0x08048000 0x08048000 0x01334 0x01334 R E 0x1000
  LOAD           0x001334 0x0804a334 0x0804a334 0x00088 0x00100 RW  0x1000
  LOAD           0x?????? 0xfe000000 0xfe000000 0x????? 0x????? R E 0x1000
3. Produce a library called libsys.so from the same source as libsys.a
4. Link some program (foo) with it, without adding the library's code *and* without listing it as something to be loaded by the ELF loader (or even adding a DYNAMIC segment).

Here are my problems:
1. I can't get the library to be loaded at a fixed address without defining new .text and .data equivalent sections for it.
2. If I do define those sections, I would need a special linker script for each program, which wouldn't be good.
3. I can't get a library to be set to be dynamically linked at a fixed address for all programs.
4. I can't dynamically link a shared library without adding a DYNAMIC segment, which should be unneeded because of the fixed address.

Is it even possible to solve this? Is there a better approach that would load a library into memory only once like this?
Post Reply