37 #include "loader/elf32.h"
38 #include "loader/cle.h"
39 #include "loader/cmod.h"
41 #include "lib/malloc.h"
43 #include "lib/assert.h"
46 #define PRINTF(...) do {} while (0)
48 #define PRINTF(...) printf(__VA_ARGS__)
52 #define CMOD_NMODULES 4
54 struct cmod_info cmod_module[CMOD_NMODULES];
57 cmod_load(
unsigned imod,
59 int (*pread)(
void *,
int, off_t),
66 if(imod >= CMOD_NMODULES) {
67 PRINTF(
"imod to large");
71 if(cmod_module[imod].ram !=
NULL || cmod_module[imod].fini !=
NULL) {
72 PRINTF(
"module busy\n");
77 ret = cle_read_info(&h, pread, off);
80 strcpy(scratch, h.name);
84 cmod_module[imod].ram = malloc(h.datasize + h.bsssize + h.textsize);
85 if(cmod_module[imod].ram ==
NULL) {
86 return CMOD_DATA_TO_LARGE;
92 h.data = cmod_module[imod].ram;
93 h.bss = h.data + h.datasize;
94 h.text = (cle_addr)h.bss + h.bsssize;
96 PRINTF(
"cmod: copy text segment to RAM %p %p\n",
97 h.text, h.text + h.textsize);
98 ret = pread((
void *)h.text, h.textsize, off + h.textoff);
100 if(h.textrelasize > 0) {
101 PRINTF(
"cmod: relocate text in RAM\n");
102 ret = cle_relocate(&h,
106 h.textrelaoff, h.textrelasize);
108 strcpy(scratch, h.name);
113 PRINTF(
"cmod: copy data segment to RAM %p %p\n",
114 h.data, h.data + h.datasize);
115 ret = pread(h.data, h.datasize, off + h.dataoff);
117 if(h.datarelasize > 0) {
118 PRINTF(
"cmod: relocate data segment\n");
119 ret = cle_relocate(&h,
123 h.datarelaoff, h.datarelasize);
125 strcpy(scratch, h.name);
130 PRINTF(
"cmod: zero bss %p %p\n", h.bss, h.bss + h.bsssize);
131 memset(h.bss, 0, h.bsssize);
133 cmod_module[imod].fini = cle_lookup(&h, pread, off,
"_fini");
134 init = cle_lookup(&h, pread, off,
"_init");
137 PRINTF(
"init=%p fini=%p\n", init, cmod_module[imod].fini);
141 return CMOD_NO_STARTPOINT;
145 cmod_unload(
int imod)
147 if(cmod_module[imod].fini !=
NULL) {
148 (*cmod_module[imod].fini)();
149 cmod_module[imod].fini =
NULL;
151 if(cmod_module[imod].ram !=
NULL) {
152 free(cmod_module[imod].ram);
153 cmod_module[imod].ram =
NULL;
162 PRINTF(
"Id Module Address Fini\n");
163 for(i = 0; i < CMOD_NMODULES; i++)
164 if(cmod_module[i].ram !=
NULL)
165 PRINTF(
"%2d %-8s %7p %4p\n", i,
166 cmod_module[i].name, cmod_module[i].ram, cmod_module[i].fini);