Rev 3223 | Rev 3787 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 3223 | Rev 3315 | ||
|---|---|---|---|
| Line 69... | Line 69... | ||
| 69 | static unsigned int elf_load(elf_ld_t *elf, size_t so_bias); |
69 | static unsigned int elf_load(elf_ld_t *elf, size_t so_bias); |
| 70 | static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry); |
70 | static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry); |
| 71 | static int section_header(elf_ld_t *elf, elf_section_header_t *entry); |
71 | static int section_header(elf_ld_t *elf, elf_section_header_t *entry); |
| 72 | static int load_segment(elf_ld_t *elf, elf_segment_header_t *entry); |
72 | static int load_segment(elf_ld_t *elf, elf_segment_header_t *entry); |
| 73 | 73 | ||
| - | 74 | /** Read until the buffer is read in its entirety. */ |
|
| - | 75 | static int my_read(int fd, char *buf, size_t len) |
|
| - | 76 | { |
|
| - | 77 | int cnt = 0; |
|
| - | 78 | do { |
|
| - | 79 | buf += cnt; |
|
| - | 80 | len -= cnt; |
|
| - | 81 | cnt = read(fd, buf, len); |
|
| - | 82 | } while ((cnt > 0) && ((len - cnt) > 0)); |
|
| - | 83 | ||
| - | 84 | return cnt; |
|
| - | 85 | } |
|
| - | 86 | ||
| 74 | /** Load ELF binary from a file. |
87 | /** Load ELF binary from a file. |
| 75 | * |
88 | * |
| 76 | * Load an ELF binary from the specified file. If the file is |
89 | * Load an ELF binary from the specified file. If the file is |
| 77 | * an executable program, it is loaded unbiased. If it is a shared |
90 | * an executable program, it is loaded unbiased. If it is a shared |
| 78 | * object, it is loaded with the bias @a so_bias. Some information |
91 | * object, it is loaded with the bias @a so_bias. Some information |
| Line 154... | Line 167... | ||
| 154 | { |
167 | { |
| 155 | elf_header_t header_buf; |
168 | elf_header_t header_buf; |
| 156 | elf_header_t *header = &header_buf; |
169 | elf_header_t *header = &header_buf; |
| 157 | int i, rc; |
170 | int i, rc; |
| 158 | 171 | ||
| 159 | rc = read(elf->fd, header, sizeof(elf_header_t)); |
172 | rc = my_read(elf->fd, header, sizeof(elf_header_t)); |
| 160 | if (rc < 0) { |
173 | if (rc < 0) { |
| 161 | printf("read error\n"); |
174 | printf("read error\n"); |
| 162 | return EE_INVALID; |
175 | return EE_INVALID; |
| 163 | } |
176 | } |
| 164 | 177 | ||
| Line 221... | Line 234... | ||
| 221 | 234 | ||
| 222 | /* Seek to start of segment header */ |
235 | /* Seek to start of segment header */ |
| 223 | lseek(elf->fd, header->e_phoff |
236 | lseek(elf->fd, header->e_phoff |
| 224 | + i * sizeof(elf_segment_header_t), SEEK_SET); |
237 | + i * sizeof(elf_segment_header_t), SEEK_SET); |
| 225 | 238 | ||
| - | 239 | rc = my_read(elf->fd, &segment_hdr, |
|
| 226 | rc = read(elf->fd, &segment_hdr, sizeof(elf_segment_header_t)); |
240 | sizeof(elf_segment_header_t)); |
| 227 | if (rc < 0) { |
241 | if (rc < 0) { |
| 228 | printf("read error\n"); |
242 | printf("read error\n"); |
| 229 | return EE_INVALID; |
243 | return EE_INVALID; |
| 230 | } |
244 | } |
| 231 | 245 | ||
| Line 242... | Line 256... | ||
| 242 | 256 | ||
| 243 | /* Seek to start of section header */ |
257 | /* Seek to start of section header */ |
| 244 | lseek(elf->fd, header->e_shoff |
258 | lseek(elf->fd, header->e_shoff |
| 245 | + i * sizeof(elf_section_header_t), SEEK_SET); |
259 | + i * sizeof(elf_section_header_t), SEEK_SET); |
| 246 | 260 | ||
| - | 261 | rc = my_read(elf->fd, §ion_hdr, |
|
| 247 | rc = read(elf->fd, §ion_hdr, sizeof(elf_section_header_t)); |
262 | sizeof(elf_section_header_t)); |
| 248 | if (rc < 0) { |
263 | if (rc < 0) { |
| 249 | printf("read error\n"); |
264 | printf("read error\n"); |
| 250 | return EE_INVALID; |
265 | return EE_INVALID; |
| 251 | } |
266 | } |
| 252 | 267 | ||
| Line 359... | Line 374... | ||
| 359 | 374 | ||
| 360 | /* |
375 | /* |
| 361 | * For the course of loading, the area needs to be readable |
376 | * For the course of loading, the area needs to be readable |
| 362 | * and writeable. |
377 | * and writeable. |
| 363 | */ |
378 | */ |
| 364 | a = as_area_create((uint8_t *)base + bias, |
379 | a = as_area_create((uint8_t *)base + bias, mem_sz, |
| 365 | mem_sz, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE); |
380 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE); |
| 366 | if (a == (void *)(-1)) { |
381 | if (a == (void *)(-1)) { |
| 367 | printf("memory mapping failed\n"); |
382 | printf("memory mapping failed\n"); |
| 368 | return EE_MEMORY; |
383 | return EE_MEMORY; |
| 369 | } |
384 | } |
| 370 | 385 | ||
| Line 396... | Line 411... | ||
| 396 | while (left > 0) { |
411 | while (left > 0) { |
| 397 | now = 16384; |
412 | now = 16384; |
| 398 | if (now > left) now = left; |
413 | if (now > left) now = left; |
| 399 | 414 | ||
| 400 | // printf("read %d...", now); |
415 | // printf("read %d...", now); |
| 401 | rc = read(elf->fd, dp, now); |
416 | rc = my_read(elf->fd, dp, now); |
| 402 | // printf("->%d\n", rc); |
417 | // printf("->%d\n", rc); |
| 403 | 418 | ||
| 404 | if (rc < 0) { |
419 | if (rc < 0) { |
| 405 | printf("read error\n"); |
420 | printf("read error\n"); |
| 406 | return EE_INVALID; |
421 | return EE_INVALID; |