That is another way. You could add a few calls:
Error Module::LoadInMemory(<args>);
And then ObjectFile would need:
Error ObjectFile::LoadInMemory(<args>);
The question is what are the arguments going to be? I see a few options:
1 - no arguments, just load the file exactly where at the addresses in the file itself
2 - shift the entire file by some amount with a single load address argument
3 - specify where each section goes with a SectionLoadList
For number 2 above, if you do specify on address, what does that mean when ELF program headers have 3 loadable chunks? Does the first address specify the address of the first chunk and all the rest are relative to it? Do you find the program header chunk with the lowest address and then load it there and all others are relative?
If we specify where each section would go by supplying a SectionLoadList the main problem is that ELF loads files using the program headers and those don't correspond to any sections directly sometimes as one program header can span many sections? There might be some program headers that don't represent sections. What do we do then?
For options 2 and 3 do we then need to apply relocations after loading things?
I would almost vote for #1 where we just load the file where it says it wants to go. Bare board folks should be able to get the linker to emit things at the right addresses and then it works fine for us.
If you do a built-in command I would vote to add some options to the existing "target modules load" command. This command already can take a slide:
help target modules load
Set the load addresses for one or more sections in a target module.
Syntax: target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]
Command Options Usage:
target modules load [-u <none>] [-f <name>] [-s <offset>] [<filename> [<filename> [...]]]
-f <name> ( --file <name> )
Fullpath or basename for module to load.
-s <offset> ( --slide <offset> )
Set the load address for all sections to be the virtual address in the file plus the offset.
-u <none> ( --uuid <none> )
A module UUID value.
This command takes options and free-form arguments. If your arguments resemble option specifiers (i.e., they start with a - or --), you must use ' -- ' between the end of the command options and the beginning of the arguments.
This is currently used by specifying a file "--file <PATH>" and then a section name followed by load address:
(lldb) target modules load --file /tmp/a.out .text 0x1000 .data 0x2000 .bss 0x3000
Or you can specify a slide:
(lldb) target modules load --file /tmp/a.out --slide 0x20000
So we can modify this command by adding a "--load" option:
(lldb) target modules load --file /tmp/a.out --load
If there is no slide, then load it where the file where all addresses in the file exist. If there is a slide, you need to verify this is a relocatable file and then slide everything as needed.
Since the arguments to this command are free form, we could pass these arguments to ObjectFile::LoadInMemory() and let each loader (ObjectFileELF and ObjectFileMach) determine what arguments are valid. Then you could do something like:
(lldb) target modules load --file /tmp/a.out --load program_header[0] 0x1000 program_header[1] 0x2000
And ObjectFileELF will know what to do. Let me know what you think.
Greg