Problem with program memory layout.
Fred Gilham
gilham at csl.sri.com
Thu Jul 24 10:17:44 PDT 2003
Hello,
I'm working on creating CMU Lisp executables that contain both the
startup executable and the lisp image (the actual lisp system code).
The way it currently works is that the startup executable reads the
lisp image and mmaps parts of it, called "spaces", into memory. What
I'm trying to do is add the spaces to the startup executable as ELF
segments so that when the program starts, the spaces will get mapped
into memory by the OS. The idea is that the core and startup code
have to be synchronized and so the core won't run without the
particular startup program it was created with, and sometimes one
forgets to keep the startup program when one upgrades.
I've created a program to write out ELF compatible .o files that
contain the code in the core file. I have also hacked a linker script
that will combine these .o files with the rest of the .o files that
make up the startup program. The resulting program will actually run.
Unfortunately when it calls malloc, malloc isn't able to set itself up
in memory and returns 0. What happens is that malloc calls sbrk and
sbrk returns an EINVAL error.
After some exploration I figured out why this was happening. There's
a field called vm_daddr in the vmspace structure that gets set when
the segments are loaded. The following code in
/usr/src/sys/kern/imgact_elf.c does this:
/*
* Is this .text or .data? We can't use
* VM_PROT_WRITE or VM_PROT_EXEC, it breaks the
* alpha terribly and possibly does other bad
* things so we stick to the old way of figuring
* it out: If the segment contains the program
* entry point, it's a text segment, otherwise it
* is a data segment.
*
* Note that obreak() assumes that data_addr +
* data_size == end of data load area, and the ELF
* file format expects segments to be sorted by
* address. If multiple data segments exist, the
* last one will be used.
*/
if (hdr->e_entry >= phdr[i].p_vaddr &&
hdr->e_entry < (phdr[i].p_vaddr +
phdr[i].p_memsz)) {
text_size = seg_size;
text_addr = seg_addr;
entry = (u_long)hdr->e_entry;
} else {
data_size = seg_size;
data_addr = seg_addr;
}
.....
vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
Sbrk will return EINVAL when the new requested break value is smaller
than the current contents of vm_daddr (this is done in
/usr/src/sys/vm/vm_unix.c). But vm_daddr gets set to the end of my
last segment, rather than the end of the actual "data" segment.
This seems to mean I lose completely.
Can anyone suggest a way around this?
Here's the program header for the executable I create:
Elf file type is EXEC (Executable file)
Entry point 0x8049398
There are 9 program headers, starting at offset 52
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000034 0x08048034 0x08048034 0x00120 0x00120 R 0x4
INTERP 0x000154 0x08048154 0x08048154 0x00019 0x00019 R 0x1
[Requesting program interpreter: /usr/libexec/ld-elf.so.1]
LOAD 0x000000 0x08048000 0x08048000 0x12940 0x12940 R E 0x1000
LOAD 0x012940 0x0805b940 0x0805b940 0x0078c 0x09c50 RW 0x1000
DYNAMIC 0x012edc 0x0805bedc 0x0805bedc 0x000b8 0x000b8 RW 0x4
NOTE 0x000170 0x08048170 0x08048170 0x00018 0x00018 R 0x4
LOAD 0x014000 0x10000000 0x10000000 0xaa0000 0xaa0000 R E 0x1000
LOAD 0xab4000 0x28f00000 0x28f00000 0x27d000 0x27d000 R E 0x1000
LOAD 0xd31000 0x48000000 0x48000000 0x01000 0x01000 R E 0x1000
Section to Segment mapping:
Segment Sections...
00
01 .interp
02 .interp .note.ABI-tag .hash .dynsym .dynstr .rel.bss .rel.plt .init .plt .text .fini .rodata
03 .data .eh_frame .dynamic .ctors .dtors .got .bss
04 .dynamic
05 .note.ABI-tag
06 CORRO
07 CORSTA
08 CORDYN
--
-Fred Gilham gilham at csl.sri.com
"In America, we have a two-party system. There is the stupid
party. And there is the evil party. I am proud to be a member of the
stupid party. Periodically, the two parties get together and do
something that is both stupid and evil. This is called --
bipartisanship." --Republican congressional staffer
More information about the freebsd-hackers
mailing list