00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #include <types.h>
00054 #include <kern/errno.h>
00055 #include <lib.h>
00056 #include <uio.h>
00057 #include <proc.h>
00058 #include <current.h>
00059 #include <addrspace.h>
00060 #include <vnode.h>
00061 #include <elf.h>
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 static
00078 int
00079 load_segment(struct addrspace *as, struct vnode *v,
00080 off_t offset, vaddr_t vaddr,
00081 size_t memsize, size_t filesize,
00082 int is_executable)
00083 {
00084 struct iovec iov;
00085 struct uio u;
00086 int result;
00087
00088 if (filesize > memsize) {
00089 kprintf("ELF: warning: segment filesize > segment memsize\n");
00090 filesize = memsize;
00091 }
00092
00093 DEBUG(DB_EXEC, "ELF: Loading %lu bytes to 0x%lx\n",
00094 (unsigned long) filesize, (unsigned long) vaddr);
00095
00096 iov.iov_ubase = (userptr_t)vaddr;
00097 iov.iov_len = memsize;
00098 u.uio_iov = &iov;
00099 u.uio_iovcnt = 1;
00100 u.uio_resid = filesize;
00101 u.uio_offset = offset;
00102 u.uio_segflg = is_executable ? UIO_USERISPACE : UIO_USERSPACE;
00103 u.uio_rw = UIO_READ;
00104 u.uio_space = as;
00105
00106 result = VOP_READ(v, &u);
00107 if (result) {
00108 return result;
00109 }
00110
00111 if (u.uio_resid != 0) {
00112
00113 kprintf("ELF: short read on segment - file truncated?\n");
00114 return ENOEXEC;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 #if 0
00132 {
00133 size_t fillamt;
00134
00135 fillamt = memsize - filesize;
00136 if (fillamt > 0) {
00137 DEBUG(DB_EXEC, "ELF: Zero-filling %lu more bytes\n",
00138 (unsigned long) fillamt);
00139 u.uio_resid += fillamt;
00140 result = uiomovezeros(fillamt, &u);
00141 }
00142 }
00143 #endif
00144
00145 return result;
00146 }
00147
00148
00149
00150
00151
00152
00153 int
00154 load_elf(struct vnode *v, vaddr_t *entrypoint)
00155 {
00156 Elf_Ehdr eh;
00157 Elf_Phdr ph;
00158 int result, i;
00159 struct iovec iov;
00160 struct uio ku;
00161 struct addrspace *as;
00162
00163 as = curproc_getas();
00164
00165
00166
00167
00168
00169 uio_kinit(&iov, &ku, &eh, sizeof(eh), 0, UIO_READ);
00170 result = VOP_READ(v, &ku);
00171 if (result) {
00172 return result;
00173 }
00174
00175 if (ku.uio_resid != 0) {
00176
00177 kprintf("ELF: short read on header - file truncated?\n");
00178 return ENOEXEC;
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 if (eh.e_ident[EI_MAG0] != ELFMAG0 ||
00193 eh.e_ident[EI_MAG1] != ELFMAG1 ||
00194 eh.e_ident[EI_MAG2] != ELFMAG2 ||
00195 eh.e_ident[EI_MAG3] != ELFMAG3 ||
00196 eh.e_ident[EI_CLASS] != ELFCLASS32 ||
00197 eh.e_ident[EI_DATA] != ELFDATA2MSB ||
00198 eh.e_ident[EI_VERSION] != EV_CURRENT ||
00199 eh.e_version != EV_CURRENT ||
00200 eh.e_type!=ET_EXEC ||
00201 eh.e_machine!=EM_MACHINE) {
00202 return ENOEXEC;
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 for (i=0; i<eh.e_phnum; i++) {
00221 off_t offset = eh.e_phoff + i*eh.e_phentsize;
00222 uio_kinit(&iov, &ku, &ph, sizeof(ph), offset, UIO_READ);
00223
00224 result = VOP_READ(v, &ku);
00225 if (result) {
00226 return result;
00227 }
00228
00229 if (ku.uio_resid != 0) {
00230
00231 kprintf("ELF: short read on phdr - file truncated?\n");
00232 return ENOEXEC;
00233 }
00234
00235 switch (ph.p_type) {
00236 case PT_NULL: continue;
00237 case PT_PHDR: continue;
00238 case PT_MIPS_REGINFO: continue;
00239 case PT_LOAD: break;
00240 default:
00241 kprintf("loadelf: unknown segment type %d\n",
00242 ph.p_type);
00243 return ENOEXEC;
00244 }
00245
00246 result = as_define_region(as,
00247 ph.p_vaddr, ph.p_memsz,
00248 ph.p_flags & PF_R,
00249 ph.p_flags & PF_W,
00250 ph.p_flags & PF_X);
00251 if (result) {
00252 return result;
00253 }
00254 }
00255
00256 result = as_prepare_load(as);
00257 if (result) {
00258 return result;
00259 }
00260
00261
00262
00263
00264
00265 for (i=0; i<eh.e_phnum; i++) {
00266 off_t offset = eh.e_phoff + i*eh.e_phentsize;
00267 uio_kinit(&iov, &ku, &ph, sizeof(ph), offset, UIO_READ);
00268
00269 result = VOP_READ(v, &ku);
00270 if (result) {
00271 return result;
00272 }
00273
00274 if (ku.uio_resid != 0) {
00275
00276 kprintf("ELF: short read on phdr - file truncated?\n");
00277 return ENOEXEC;
00278 }
00279
00280 switch (ph.p_type) {
00281 case PT_NULL: continue;
00282 case PT_PHDR: continue;
00283 case PT_MIPS_REGINFO: continue;
00284 case PT_LOAD: break;
00285 default:
00286 kprintf("loadelf: unknown segment type %d\n",
00287 ph.p_type);
00288 return ENOEXEC;
00289 }
00290
00291 result = load_segment(as, v, ph.p_offset, ph.p_vaddr,
00292 ph.p_memsz, ph.p_filesz,
00293 ph.p_flags & PF_X);
00294 if (result) {
00295 return result;
00296 }
00297 }
00298
00299 result = as_complete_load(as);
00300 if (result) {
00301 return result;
00302 }
00303
00304 *entrypoint = eh.e_entry;
00305
00306 return 0;
00307 }