44 #include "loader/elfloader_compat.h"
45 #include "loader/cle.h"
47 #include "lib/malloc.h"
52 #include "lib/assert.h"
55 #define PRINTF(...) do {} while (0)
57 #define PRINTF(...) printf(__VA_ARGS__)
60 struct process *elfloader_loaded_process;
61 void (*elfloader_fini)(void);
63 #define IMAX(a, b) (((a) > (b)) ? (a) : (b))
65 unsigned char *datamemory;
68 extern int __data_load_end;
69 #define TEXTMEMORY (((cle_addr)(&__data_load_end) + ROM_ERASE_UNIT_SIZE) \
70 & ~(ROM_ERASE_UNIT_SIZE - 1))
72 #include <sys/unistd.h>
74 (cle_addr)(((uintptr_t)(&_etext + 1) \
75 + (uintptr_t)&_edata - (uintptr_t)&__data_start \
76 + ROM_ERASE_UNIT_SIZE) \
77 & ~(ROM_ERASE_UNIT_SIZE - 1))
94 ret = cle_read_info(&h, xmem_pread, eepromaddr);
102 if(datamemory !=
NULL) {
108 datamemory = malloc(IMAX(h.textsize, h.datasize + h.bsssize));
109 if(datamemory ==
NULL) {
110 return ELFLOADER_DATA_TO_LARGE;
114 h.bss = datamemory + h.datasize;
117 PRINTF(
"elfloader: copy text segment to RAM %p %p\n",
118 h.data, h.data + h.textsize);
119 ret = xmem_pread(datamemory, h.textsize, eepromaddr + h.textoff);
121 if(h.textrelasize > 0) {
122 PRINTF(
"elfloader: relocate text in RAM\n");
123 ret = cle_relocate(&h,
127 h.textrelaoff, h.textrelasize);
134 PRINTF(
"elfloader: copy text segment to ROM 0x%lx 0x%lx\n",
135 (
unsigned long)h.text,
136 (
unsigned long)h.text + h.textsize);
138 ret = rom_erase((h.textsize+ROM_ERASE_UNIT_SIZE) & ~(ROM_ERASE_UNIT_SIZE-1),
141 ret = rom_pwrite(datamemory, h.textsize, h.text);
144 PRINTF(
"elfloader: copy data segment to RAM %p %p\n",
145 h.data, h.data + h.datasize);
146 ret = xmem_pread(datamemory, h.datasize, eepromaddr + h.dataoff);
147 assert(ret >= h.datasize);
148 if(h.datarelasize > 0) {
149 PRINTF(
"elfloader: relocate data segment\n");
150 ret = cle_relocate(&h,
154 h.datarelaoff, h.datarelasize);
162 PRINTF(
"elfloader: zero bss %p %p\n", h.bss, h.bss + h.bsssize);
163 memset(h.bss, 0, h.bsssize);
166 elfloader_loaded_process = cle_lookup(&h, xmem_pread, eepromaddr,
167 "autostart_processes");
168 elfloader_fini = cle_lookup(&h, xmem_pread, eepromaddr,
"_fini");
174 elfloader_loaded_process =
NULL;
178 if(elfloader_loaded_process !=
NULL) {
179 PRINTF(
"elfloader: launch program\n");
181 elfloader_fini =
NULL;
189 elfloader_unload(
void)
191 if(elfloader_fini !=
NULL) {
193 elfloader_fini =
NULL;
194 }
else if(elfloader_loaded_process !=
NULL) {
196 elfloader_loaded_process =
NULL;
198 if(datamemory !=
NULL) {
void elfloader_init(void)
elfloader initialization function.
int elfloader_load(int fd)
Load and relocate an ELF file.
#define NULL
The null pointer.
cle_scratch elfloader_unknown
If elfloader_load() could not find a specific symbol, it is copied into this array.
void process_start(struct process *p, process_data_t data)
Start a process.
#define ELFLOADER_OK
Return value from elfloader_load() indicating that loading worked.
#define ELFLOADER_NO_STARTPOINT
Return value from elfloader_load() indicating that no starting point could be found in the loaded mod...
void process_exit(struct process *p)
Cause a process to exit.