Commit c856f7d6 authored by Weibin Sun's avatar Weibin Sun Committed by Vikram Narayanan
Browse files

in progress, elf hdr read, notes read

parent d6dfb02c
#include <linux/elf.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <error.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define roundup(x, y) ( \
{ \
const typeof(y) __y = y; \
(((x) + (__y - 1)) / __y) * __y; \
} \
)
int main(int argc, char* argv[]) {
struct stat stat;
char* buf;
Elf64_Ehdr* ehdr;
Elf64_Phdr* phdr;
int fd, i;
Elf64_Nhdr *nhdr;
fd = open(argv[1], O_RDONLY); // O_RDWR
if (fd < 0) {
perror("vmlinux open");
return 0;
}
if (fstat(fd, &stat) != 0) {
perror("fstat");
goto after_fd_err_out;
}
buf = (char*)mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (buf == MAP_FAILED) {
perror("mmap");
goto after_fd_err_out;
}
ehdr = (Elf64_Ehdr*)buf;
if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
fprintf(stderr, "%s not ELF\n", argv[1]);
goto after_mmap_err_out;
}
printf("ELF class %d\n", ehdr->e_ident[EI_CLASS]);
phdr = (Elf64_Phdr*)(buf + ehdr->e_phoff);
for (i = 0; i < ehdr->e_phnum; ++i) {
if (phdr[i].p_type == PT_LOAD) {
printf("LOAD ");
} else if (phdr[i].p_type == PT_NOTE) {
printf("NOTE ");
} else
printf("NOT CARE ");
printf("HDR: ofs %llx, va %p, pa %p, fsz %llx, msz %llx, al %llx\n",
phdr[i].p_offset,
(void*)phdr[i].p_vaddr,
(void*)phdr[i].p_paddr,
phdr[i].p_filesz,
phdr[i].p_memsz,
phdr[i].p_align);
if (phdr[i].p_type == PT_NOTE) {
char* name, *desc;
__u64 ofs = phdr[i].p_offset;
while (ofs < phdr[i].p_offset + phdr[i].p_filesz) {
nhdr = (Elf64_Nhdr*)(buf + ofs);
printf("NOTE nsz %x, dsz %x, tp %x\n",
nhdr->n_namesz, nhdr->n_descsz, nhdr->n_type);
ofs += sizeof(Elf64_Nhdr);
name = buf + ofs;
ofs += roundup(nhdr->n_namesz, phdr[i].p_align);
desc = buf + ofs;
printf("\tname %s, desc %s\n", name, desc);
ofs += roundup(nhdr->n_descsz, phdr[i].p_align);
}
}
}
after_mmap_err_out:
munmap(buf, stat.st_size);
after_fd_err_out:
close(fd);
err_out:
return 0;
}
......@@ -38,8 +38,6 @@ module_param_string(vmlinux_file, vmlinux_file,
sizeof(vmlinux_file), 0644);
MODULE_PARM_DESC(vmlinux_file, "vmlinux or vmlinuz path");
int lcd_load_vmlinux(const char* kfile,
/* #include "lcd.h" */
#include "lcd_defs.h"
......
......@@ -14,7 +14,7 @@
#include "lcd_defs.h"
int lcd_read_mod_file(const char* filepath,
void** filecontent, long* size) {
void** filecontent, long* size) {
struct file* filp = NULL;
mm_segment_t oldfs;
int err = 0;
......@@ -58,6 +58,73 @@ error_out:
return err;
}
int lcd_load_vmlinux(const char* kfile, lcd_struct *lcd) {
Elf64_Ehdr hdr;
Elf64_Phdr *phdrs;
mm_segment_t oldfs;
struct file* filp;
loff_t pos = 0;
int err = 0;
int i = 0;
oldfs = get_fs();
set_fs(get_ds());
filp = filp_open(kfile, O_RDONLY, 0);
if (IS_ERR(filp)) {
err = PTR_ERR(filp);
printk(KERN_ERR "Error when opening vmlinux file %d\n", err);
goto error_out;
}
err = vfs_read(filp, &hdr, sizeof(hdr), &pos);
if (err != sizeof(hdr)) {
printk(KERN_ERR "Error when reading vmlinux %d\n", err);
goto after_open_error_out;
}
err = 0;
if (memcmp(hdr.e_ident, ELFMAG, SELFMAG) != 0) {
err = -EINVAL;
printk(KERN_ERR "Invalide vmlinux file\n");
goto after_open_error_out;
}
phdrs = kmalloc(sizeof(Elf64_Phdr)*hdr.e_phnum, GPF_KERNEL);
if (!phdrs) {
err = -ENOMEM;
printk(KERN_ERR "Out of mem\n");
goto after_open_error_out;
}
pos = hdr->e_phoff;
err = vfs_read(filp, phdrs, sizeof(Elf64_Phdr)*hdr.e_phnum, &pos);
if (err != sizeof(Elf64_Phdr)*hdr.e_phnum) {
printk(KERN_ERR "Error when reading p hdrs %d\n", err);
goto after_mem_error_out;
}
err = 0;
for (i = 0; i < hdr.e_phnum; ++i) {
if (phdrs[i].p_type != PT_LOAD) {
if (phdrs[i].p_type != PT_NOTE)
continue;
else {
// todo READ NOTE.
}
}
}
after_mem_error_out:
kfree(phdrs);
after_open_error_out:
filp_close(filp, NULL);
error_out:
set_fs(oldfs);
return err;
}
struct exit_reason_item {
int code;
const char *reason;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment