* @brief Load the program header table from an ELF file. * * This function reads and validates the program header table from the ELF file. * It ensures the program header table is properly aligned and within the file bounds. * * @param elf_info Pointer to the ELF file information structure. * @return RT_EOK on success, -RT_ERROR on failure. */
| 390 | * @return RT_EOK on success, -RT_ERROR on failure. |
| 391 | */ |
| 392 | static int elf_load_phdr(elf_info_t *elf_info) |
| 393 | { |
| 394 | Elf_Ehdr *ehdr = &elf_info->ehdr; |
| 395 | uint32_t size; |
| 396 | int ret; |
| 397 | |
| 398 | if (ehdr->e_phnum < 1) |
| 399 | { |
| 400 | return -RT_ERROR; |
| 401 | } |
| 402 | |
| 403 | if (ehdr->e_phentsize != sizeof(Elf_Phdr)) |
| 404 | { |
| 405 | return -RT_ERROR; |
| 406 | } |
| 407 | |
| 408 | size = sizeof(Elf_Phdr) * ehdr->e_phnum; |
| 409 | if ((ehdr->e_phoff + size) > elf_info->file_len) |
| 410 | { |
| 411 | return -RT_ERROR; |
| 412 | } |
| 413 | |
| 414 | elf_info->phdr = rt_malloc(size); |
| 415 | if (elf_info->phdr == RT_NULL) |
| 416 | { |
| 417 | LOG_E("%s : alloc phdr failed", __func__); |
| 418 | return -RT_ENOMEM; |
| 419 | } |
| 420 | |
| 421 | ret = elf_file_read(elf_info->fd, (rt_uint8_t *)elf_info->phdr, size, ehdr->e_phoff); |
| 422 | if (ret != RT_EOK) |
| 423 | { |
| 424 | rt_free(elf_info->phdr); |
| 425 | elf_info->phdr = RT_NULL; |
| 426 | LOG_E("%s : elf_file_read failed, ret = %d", __func__, ret); |
| 427 | return -RT_ERROR; |
| 428 | } |
| 429 | |
| 430 | return RT_EOK; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * @brief Load the ELF interpreter specified in the program header. |
no test coverage detected