23.12.03 최초 작성
/ui/input.h
: Elf64Hdr
구조체 추가#include <stdint.h>
typedef struct {
uint8_t e_ident[16]; /* Magic number and other info */
uint16_t e_type; /* Object file type */
uint16_t e_machine; /* Architecture */
uint32_t e_version; /* Object file version */
uint64_t e_entry; /* Entry point virtual address */
uint64_t e_phoff; /* Program header table file offset */
uint64_t e_shoff; /* Section header table file offset */
uint32_t e_flags; /* Processor-specific flags */
uint16_t e_ehsize; /* ELF header size in bytes */
uint16_t e_phentsize; /* Program header table entry size */
uint16_t e_phnum; /* Program header table entry count */
uint16_t e_shentsize; /* Section header table entry size */
uint16_t e_shnum; /* Section header table entry count */
uint16_t e_shstrndx; /* Section header string table index */
} Elf64Hdr;
/ui/input.c
: elf 명령 입력 시 ./sample/sample.elf
파일의 형식 모니터에 출력하는 코드 추가#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <execinfo.h>
...
int toy_read_elf_header(char **args);
...
char *builtin_str[] = {
"send",
"mu",
"sh",
"mq",
"elf",
"exit"
};
int (*builtin_func[]) (char **) = {
&toy_send,
&toy_mutex,
&toy_shell,
&toy_message_queue,
&toy_read_elf_header,
&toy_exit
};
...
// 지정된 경로의 elf파일을 읽어 모니터에 출력하는 코드
int toy_read_elf_header(char **args)
{
int mqretcode;
toy_msg_t msg;
int in_fd;
char *contents = NULL;
size_t contents_sz;
struct stat st;
Elf64Hdr *map;
in_fd = open("./sample/sample.elf", O_RDONLY);
if ( in_fd < 0 ) {
perror("File is not available");
return 0;
}
if (fstat(in_fd, &st) == 0) {
map = (Elf64Hdr *)mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, in_fd, 0);
if(map == MAP_FAILED){
perror("mmap failed : read_elf");
exit(0);
}
printf("file type : %d\n", map->e_type);
printf("Architecture : %d\n", map->e_machine);
printf("file version : %d\n", map->e_version);
printf("virtual address : %ld\n", map->e_entry);
printf("header table file offset : %ld\n", map->e_phoff);
munmap(map, contents_sz);
}
return 1;
}