Rev 2989 | Rev 3101 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2989 | Rev 3004 | ||
---|---|---|---|
Line 33... | Line 33... | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <stdio.h> |
37 | #include <stdio.h> |
- | 38 | #include <stdlib.h> |
|
38 | #include <unistd.h> |
39 | #include <unistd.h> |
39 | #include <fcntl.h> |
40 | #include <fcntl.h> |
40 | #include <sys/types.h> |
41 | #include <sys/types.h> |
- | 42 | #include <ipc/ipc.h> |
|
- | 43 | #include <errno.h> |
|
41 | #include <as.h> |
44 | #include <as.h> |
42 | 45 | ||
43 | #include <elf.h> |
46 | #include <elf.h> |
44 | #include <elf_load.h> |
47 | #include <elf_load.h> |
45 | #include <pcb.h> |
48 | #include <pcb.h> |
46 | 49 | ||
47 | #define RTLD_BIAS 0x80000 |
50 | #define RTLD_BIAS 0x80000 |
48 | 51 | ||
- | 52 | static char *pathname = NULL; |
|
- | 53 | ||
- | 54 | void iloader_set_pathname(ipc_callid_t rid, ipc_call_t *request) |
|
- | 55 | { |
|
- | 56 | // ipc_callid_t callid; |
|
- | 57 | size_t len; |
|
- | 58 | char *name_buf; |
|
- | 59 | ||
49 | int main(int argc, char *argv[]) |
60 | /* printf("iloader_set_pathname\n"); |
- | 61 | if (!ipc_data_write_receive(&callid, &len)) { |
|
- | 62 | ipc_answer_0(callid, EINVAL); |
|
- | 63 | ipc_answer_0(rid, EINVAL); |
|
- | 64 | return; |
|
- | 65 | } |
|
- | 66 | */ |
|
- | 67 | len = IPC_GET_ARG2(*request); |
|
- | 68 | printf("alloc %d bytes\n", len+1); |
|
- | 69 | ||
- | 70 | name_buf = malloc(len + 1); |
|
- | 71 | if (!name_buf) { |
|
- | 72 | // ipc_answer_0(callid, ENOMEM); |
|
- | 73 | ipc_answer_0(rid, ENOMEM); |
|
- | 74 | return; |
|
- | 75 | } |
|
- | 76 | ||
- | 77 | printf("write_finalize\n"); |
|
- | 78 | ipc_data_write_finalize(rid, name_buf, len); |
|
- | 79 | // ipc_answer_0(rid, EOK); |
|
- | 80 | ||
- | 81 | if (pathname != NULL) { |
|
- | 82 | free(pathname); |
|
- | 83 | pathname = NULL; |
|
- | 84 | } |
|
- | 85 | ||
- | 86 | pathname = name_buf; |
|
- | 87 | } |
|
- | 88 | ||
- | 89 | int iloader_run(ipc_callid_t rid, ipc_call_t *request) |
|
50 | { |
90 | { |
51 | elf_info_t prog_info; |
- | |
52 | elf_info_t interp_info; |
- | |
53 | char *file_name; |
- | |
54 | pcb_t *pcb; |
- | |
55 | int rc; |
91 | int rc; |
- | 92 | pcb_t *pcb; |
|
56 | 93 | ||
57 | printf("This is loader\n"); |
94 | elf_info_t prog_info; |
58 | getchar(); |
95 | elf_info_t interp_info; |
59 | 96 | ||
60 | printf("Load program\n"); |
97 | printf("Load program '%s'\n", pathname); |
61 | 98 | ||
62 | rc = elf_load_file("/dltest", 0, &prog_info); |
99 | rc = elf_load_file(pathname, 0, &prog_info); |
63 | // rc = elf_load_file("/tetris", 0, &prog_info); |
- | |
64 | if (rc < 0) { |
100 | if (rc < 0) { |
65 | printf("failed to load program\n"); |
101 | printf("failed to load program\n"); |
66 | return 1; |
102 | return 1; |
67 | } |
103 | } |
68 | 104 | ||
69 | printf("Create PCB\n"); |
105 | printf("Create PCB\n"); |
70 | if (elf_create_pcb(&prog_info) < 0) return 1; |
106 | if (elf_create_pcb(&prog_info) < 0) return 1; |
71 | 107 | ||
- | 108 | if (prog_info.interp == NULL) { |
|
- | 109 | /* Statically linked program */ |
|
- | 110 | printf("Run statically linked program\n"); |
|
72 | // elf_run(&prog_info); |
111 | elf_run(&prog_info); |
73 | - | ||
74 | // getchar(); |
112 | return 0; |
- | 113 | } |
|
75 | 114 | ||
76 | printf("Load dynamic linker\n"); |
115 | printf("Load dynamic linker '%s'\n", prog_info.interp); |
77 | file_name = "/rtld.so"; |
- | |
78 | printf("open and read '%s'...\n", file_name); |
- | |
79 | rc = elf_load_file(file_name, RTLD_BIAS, &interp_info); |
116 | rc = elf_load_file("/rtld.so", RTLD_BIAS, &interp_info); |
80 | if (rc < 0) { |
117 | if (rc < 0) { |
81 | printf("failed to load dynamic linker\n"); |
118 | printf("failed to load dynamic linker\n"); |
82 | return 1; |
119 | return 1; |
83 | } |
120 | } |
84 | 121 | ||
85 | /* |
122 | /* |
86 | * Provide rtld with some useful data |
123 | * Provide dynamic linker with some useful data |
87 | */ |
124 | */ |
88 | pcb = (pcb_t *)PCB_ADDRESS; |
125 | pcb = (pcb_t *)PCB_ADDRESS; |
89 | pcb->rtld_dynamic = interp_info.dynamic; |
126 | pcb->rtld_dynamic = interp_info.dynamic; |
90 | pcb->rtld_bias = RTLD_BIAS; |
127 | pcb->rtld_bias = RTLD_BIAS; |
91 | 128 | ||
92 | printf("run dynamic linker\n"); |
129 | printf("run dynamic linker\n"); |
93 | elf_run(&interp_info); |
130 | elf_run(&interp_info); |
94 | 131 | ||
- | 132 | return 0; |
|
- | 133 | } |
|
- | 134 | ||
- | 135 | int main(int argc, char *argv[]) |
|
- | 136 | { |
|
- | 137 | ipc_callid_t callid; |
|
- | 138 | ipc_call_t call; |
|
- | 139 | int retval; |
|
- | 140 | int len; |
|
- | 141 | ||
- | 142 | while (1) { |
|
- | 143 | callid = ipc_wait_for_call(&call); |
|
- | 144 | printf("received call, method=%d\n", IPC_GET_METHOD(call)); |
|
- | 145 | switch (IPC_GET_METHOD(call)) { |
|
- | 146 | case IPC_M_DATA_WRITE: |
|
- | 147 | iloader_set_pathname(callid, &call); |
|
- | 148 | iloader_run(callid, &call); |
|
- | 149 | exit(0); |
|
- | 150 | continue; |
|
- | 151 | default: |
|
- | 152 | retval = ENOENT; |
|
- | 153 | break; |
|
- | 154 | } |
|
- | 155 | if ((callid & IPC_CALLID_NOTIFICATION) == 0) { |
|
- | 156 | printf("responding EINVAL to method %d\n", IPC_GET_METHOD(call)); |
|
- | 157 | ipc_answer_0(callid, EINVAL); |
|
- | 158 | } |
|
- | 159 | } |
|
- | 160 | ||
95 | /* not reached */ |
161 | /* not reached */ |
96 | return 0; |
162 | return 0; |
97 | } |
163 | } |
98 | 164 | ||
99 | /** @} |
165 | /** @} |