Esempio n. 1
0
static void
elf_print_note(Elf32_Ehdr *e, void *sh)
{
    u_int64_t offset;
    u_int64_t size;
    u_int64_t name;
    u_int32_t namesz;
    u_int32_t descsz;
    u_int32_t desc;
    char *n, *s;

    offset = elf_get_off(e, sh, SH_OFFSET);
    size = elf_get_size(e, sh, SH_SIZE);
    name = elf_get_word(e, sh, SH_NAME);
    n = (char *)e + offset;
    fprintf(out, "\nnote (%s):\n", shstrtab + name);
    while (n < ((char *)e + offset + size)) {
        namesz = elf_get_word(e, n, N_NAMESZ);
        descsz = elf_get_word(e, n, N_DESCSZ);
        s = n + sizeof(Elf_Note);
        desc = elf_get_word(e, n + sizeof(Elf_Note) + namesz, 0);
        fprintf(out, "\t%s %d\n", s, desc);
        n += sizeof(Elf_Note) + namesz + descsz;
    }
}
Esempio n. 2
0
static void
elf_print_rel(Elf32_Ehdr *e, void *sh)
{
    u_int64_t offset;
    u_int64_t entsize;
    u_int64_t size;
    u_int64_t name;
    u_int64_t info;
    void *r;
    void *v;
    int i;

    offset = elf_get_off(e, sh, SH_OFFSET);
    entsize = elf_get_size(e, sh, SH_ENTSIZE);
    size = elf_get_size(e, sh, SH_SIZE);
    name = elf_get_word(e, sh, SH_NAME);
    v = (char *)e + offset;
    fprintf(out, "\nrelocation (%s):\n", shstrtab + name);
    for (i = 0; (u_int64_t)i < size / entsize; i++) {
        r = (char *)v + i * entsize;
        offset = elf_get_addr(e, r, R_OFFSET);
        info = elf_get_word(e, r, R_INFO);
        fprintf(out, "\n");
        fprintf(out, "entry: %d\n", i);
        fprintf(out, "\tr_offset: %#jx\n", offset);
        fprintf(out, "\tr_info: %jd\n", (intmax_t)info);
    }
}
Esempio n. 3
0
static void
elf_print_ehdr(Elf32_Ehdr *e, void *sh)
{
    u_int64_t class;
    u_int64_t data;
    u_int64_t osabi;
    u_int64_t type;
    u_int64_t machine;
    u_int64_t version;
    u_int64_t entry;
    u_int64_t phoff;
    u_int64_t shoff;
    u_int64_t flags;
    u_int64_t ehsize;
    u_int64_t phentsize;
    u_int64_t phnum;
    u_int64_t shentsize;
    u_int64_t shnum;
    u_int64_t shstrndx;

    class = elf_get_byte(e, e, E_CLASS);
    data = elf_get_byte(e, e, E_DATA);
    osabi = elf_get_byte(e, e, E_OSABI);
    type = elf_get_quarter(e, e, E_TYPE);
    machine = elf_get_quarter(e, e, E_MACHINE);
    version = elf_get_word(e, e, E_VERSION);
    entry = elf_get_addr(e, e, E_ENTRY);
    phoff = elf_get_off(e, e, E_PHOFF);
    shoff = elf_get_off(e, e, E_SHOFF);
    flags = elf_get_word(e, e, E_FLAGS);
    ehsize = elf_get_quarter(e, e, E_EHSIZE);
    phentsize = elf_get_quarter(e, e, E_PHENTSIZE);
    phnum = elf_get_quarter(e, e, E_PHNUM);
    shentsize = elf_get_quarter(e, e, E_SHENTSIZE);
    fprintf(out, "\nelf header:\n");
    fprintf(out, "\n");
    fprintf(out, "\te_ident: %s %s %s\n", ei_classes[class], ei_data[data],
            ei_abis[osabi]);
    fprintf(out, "\te_type: %s\n", e_types[type]);
    fprintf(out, "\te_machine: %s\n", e_machines(machine));
    fprintf(out, "\te_version: %s\n", ei_versions[version]);
    fprintf(out, "\te_entry: %#jx\n", (intmax_t)entry);
    fprintf(out, "\te_phoff: %jd\n", (intmax_t)phoff);
    fprintf(out, "\te_shoff: %jd\n", (intmax_t)shoff);
    fprintf(out, "\te_flags: %jd\n", (intmax_t)flags);
    fprintf(out, "\te_ehsize: %jd\n", (intmax_t)ehsize);
    fprintf(out, "\te_phentsize: %jd\n", (intmax_t)phentsize);
    fprintf(out, "\te_phnum: %jd\n", (intmax_t)phnum);
    fprintf(out, "\te_shentsize: %jd\n", (intmax_t)shentsize);
    if (sh != NULL) {
        shnum = elf_get_shnum(e, sh);
        shstrndx = elf_get_shstrndx(e, sh);
        fprintf(out, "\te_shnum: %jd\n", (intmax_t)shnum);
        fprintf(out, "\te_shstrndx: %jd\n", (intmax_t)shstrndx);
    }
}
Esempio n. 4
0
static void
elf_print_shdr(Elf32_Ehdr *e, void *sh)
{
    u_int64_t shentsize;
    u_int64_t shnum;
    u_int64_t name;
    u_int64_t type;
    u_int64_t flags;
    u_int64_t addr;
    u_int64_t offset;
    u_int64_t size;
    u_int64_t shlink;
    u_int64_t info;
    u_int64_t addralign;
    u_int64_t entsize;
    u_int64_t machine;
    void *v;
    int i;

    if (sh == NULL) {
        fprintf(out, "\nNo section headers\n");
        return;
    }

    machine = elf_get_quarter(e, e, E_MACHINE);
    shentsize = elf_get_quarter(e, e, E_SHENTSIZE);
    shnum = elf_get_shnum(e, sh);
    fprintf(out, "\nsection header:\n");
    for (i = 0; (u_int64_t)i < shnum; i++) {
        v = (char *)sh + i * shentsize;
        name = elf_get_word(e, v, SH_NAME);
        type = elf_get_word(e, v, SH_TYPE);
        flags = elf_get_word(e, v, SH_FLAGS);
        addr = elf_get_addr(e, v, SH_ADDR);
        offset = elf_get_off(e, v, SH_OFFSET);
        size = elf_get_size(e, v, SH_SIZE);
        shlink = elf_get_word(e, v, SH_LINK);
        info = elf_get_word(e, v, SH_INFO);
        addralign = elf_get_size(e, v, SH_ADDRALIGN);
        entsize = elf_get_size(e, v, SH_ENTSIZE);
        fprintf(out, "\n");
        fprintf(out, "entry: %d\n", i);
        fprintf(out, "\tsh_name: %s\n", shstrtab + name);
        fprintf(out, "\tsh_type: %s\n", sh_types(machine, type));
        fprintf(out, "\tsh_flags: %s\n", sh_flags[flags & 0x7]);
        fprintf(out, "\tsh_addr: %#jx\n", addr);
        fprintf(out, "\tsh_offset: %jd\n", (intmax_t)offset);
        fprintf(out, "\tsh_size: %jd\n", (intmax_t)size);
        fprintf(out, "\tsh_link: %jd\n", (intmax_t)shlink);
        fprintf(out, "\tsh_info: %jd\n", (intmax_t)info);
        fprintf(out, "\tsh_addralign: %jd\n", (intmax_t)addralign);
        fprintf(out, "\tsh_entsize: %jd\n", (intmax_t)entsize);
    }
}
Esempio n. 5
0
static uint64_t
elf_get_shstrndx(Elf32_Ehdr *e, void *sh)
{
    uint64_t shstrndx;

    shstrndx = elf_get_quarter(e, e, E_SHSTRNDX);
    if (shstrndx == SHN_XINDEX)
        shstrndx = elf_get_word(e, (char *)sh, SH_LINK);
    return shstrndx;
}
Esempio n. 6
0
/*
 * Helpers for ELF files with shnum or shstrndx values that don't fit in the
 * ELF header.  If the values are too large then an escape value is used to
 * indicate that the actual value is found in one of section 0's fields.
 */
static uint64_t
elf_get_shnum(Elf32_Ehdr *e, void *sh)
{
    uint64_t shnum;

    shnum = elf_get_quarter(e, e, E_SHNUM);
    if (shnum == 0)
        shnum = elf_get_word(e, (char *)sh, SH_SIZE);
    return shnum;
}
Esempio n. 7
0
static void
elf_print_phdr(Elf32_Ehdr *e, void *p)
{
    u_int64_t phentsize;
    u_int64_t phnum;
    u_int64_t type;
    u_int64_t offset;
    u_int64_t vaddr;
    u_int64_t paddr;
    u_int64_t filesz;
    u_int64_t memsz;
    u_int64_t flags;
    u_int64_t align;
    void *v;
    int i;

    phentsize = elf_get_quarter(e, e, E_PHENTSIZE);
    phnum = elf_get_quarter(e, e, E_PHNUM);
    fprintf(out, "\nprogram header:\n");
    for (i = 0; (u_int64_t)i < phnum; i++) {
        v = (char *)p + i * phentsize;
        type = elf_get_word(e, v, P_TYPE);
        offset = elf_get_off(e, v, P_OFFSET);
        vaddr = elf_get_addr(e, v, P_VADDR);
        paddr = elf_get_addr(e, v, P_PADDR);
        filesz = elf_get_size(e, v, P_FILESZ);
        memsz = elf_get_size(e, v, P_MEMSZ);
        flags = elf_get_word(e, v, P_FLAGS);
        align = elf_get_size(e, v, P_ALIGN);
        fprintf(out, "\n");
        fprintf(out, "entry: %d\n", i);
        fprintf(out, "\tp_type: %s\n", p_types[type & 0x7]);
        fprintf(out, "\tp_offset: %jd\n", (intmax_t)offset);
        fprintf(out, "\tp_vaddr: %#jx\n", (intmax_t)vaddr);
        fprintf(out, "\tp_paddr: %#jx\n", (intmax_t)paddr);
        fprintf(out, "\tp_filesz: %jd\n", (intmax_t)filesz);
        fprintf(out, "\tp_memsz: %jd\n", (intmax_t)memsz);
        fprintf(out, "\tp_flags: %s\n", p_flags[flags]);
        fprintf(out, "\tp_align: %jd\n", (intmax_t)align);
    }
}
Esempio n. 8
0
static void
elf_print_symtab(Elf32_Ehdr *e, void *sh, char *str)
{
	u_int64_t machine;
	u_int64_t offset;
	u_int64_t entsize;
	u_int64_t size;
	u_int64_t name;
	u_int64_t value;
	u_int64_t info;
	u_int64_t shndx;
	void *st;
	int len;
	int i;

	machine = elf_get_quarter(e, e, E_MACHINE);
	offset = elf_get_off(e, sh, SH_OFFSET);
	entsize = elf_get_size(e, sh, SH_ENTSIZE);
	size = elf_get_size(e, sh, SH_SIZE);
	name = elf_get_word(e, sh, SH_NAME);
	len = size / entsize;
	fprintf(out, "\nsymbol table (%s):\n", shstrtab + name);
	for (i = 0; i < len; i++) {
		st = (char *)e + offset + i * entsize;
		name = elf_get_word(e, st, ST_NAME);
		value = elf_get_addr(e, st, ST_VALUE);
		size = elf_get_size(e, st, ST_SIZE);
		info = elf_get_byte(e, st, ST_INFO);
		shndx = elf_get_quarter(e, st, ST_SHNDX);
		fprintf(out, "\n");
		fprintf(out, "entry: %d\n", i);
		fprintf(out, "\tst_name: %s\n", str + name);
		fprintf(out, "\tst_value: %#jx\n", value);
		fprintf(out, "\tst_size: %jd\n", (intmax_t)size);
		fprintf(out, "\tst_info: %s %s\n",
		    st_type(machine, ELF32_ST_TYPE(info)),
		    st_bindings[ELF32_ST_BIND(info)]);
		fprintf(out, "\tst_shndx: %jd\n", (intmax_t)shndx);
	}
}
Esempio n. 9
0
int
main(int ac, char **av)
{
    cap_rights_t rights;
    u_int64_t phoff;
    u_int64_t shoff;
    u_int64_t phentsize;
    u_int64_t phnum;
    u_int64_t shentsize;
    u_int64_t shnum;
    u_int64_t shstrndx;
    u_int64_t offset;
    u_int64_t name;
    u_int64_t type;
    struct stat sb;
    u_int flags;
    Elf32_Ehdr *e;
    void *p;
    void *sh;
    void *v;
    int fd;
    int ch;
    int i;

    out = stdout;
    flags = 0;
    while ((ch = getopt(ac, av, "acdeiGhnprsw:")) != -1)
        switch (ch) {
        case 'a':
            flags = ED_ALL;
            break;
        case 'c':
            flags |= ED_SHDR;
            break;
        case 'd':
            flags |= ED_DYN;
            break;
        case 'e':
            flags |= ED_EHDR;
            break;
        case 'i':
            flags |= ED_INTERP;
            break;
        case 'G':
            flags |= ED_GOT;
            break;
        case 'h':
            flags |= ED_HASH;
            break;
        case 'n':
            flags |= ED_NOTE;
            break;
        case 'p':
            flags |= ED_PHDR;
            break;
        case 'r':
            flags |= ED_REL;
            break;
        case 's':
            flags |= ED_SYMTAB;
            break;
        case 'w':
            if ((out = fopen(optarg, "w")) == NULL)
                err(1, "%s", optarg);
            cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
            if (cap_rights_limit(fileno(out), &rights) < 0 && errno != ENOSYS)
                err(1, "unable to limit rights for %s", optarg);
            break;
        case '?':
        default:
            usage();
        }
    ac -= optind;
    av += optind;
    if (ac == 0 || flags == 0)
        usage();
    if ((fd = open(*av, O_RDONLY)) < 0 ||
            fstat(fd, &sb) < 0)
        err(1, "%s", *av);
    cap_rights_init(&rights, CAP_MMAP_R);
    if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
        err(1, "unable to limit rights for %s", *av);
    close(STDIN_FILENO);
    cap_rights_init(&rights, CAP_WRITE);
    if (cap_rights_limit(STDOUT_FILENO, &rights) < 0 && errno != ENOSYS)
        err(1, "unable to limit rights for stdout");
    if (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS)
        err(1, "unable to limit rights for stderr");
    if (cap_enter() < 0 && errno != ENOSYS)
        err(1, "unable to enter capability mode");
    e = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
    if (e == MAP_FAILED)
        err(1, NULL);
    if (!IS_ELF(*(Elf32_Ehdr *)e))
        errx(1, "not an elf file");
    phoff = elf_get_off(e, e, E_PHOFF);
    shoff = elf_get_off(e, e, E_SHOFF);
    phentsize = elf_get_quarter(e, e, E_PHENTSIZE);
    phnum = elf_get_quarter(e, e, E_PHNUM);
    shentsize = elf_get_quarter(e, e, E_SHENTSIZE);
    p = (char *)e + phoff;
    if (shoff > 0) {
        sh = (char *)e + shoff;
        shnum = elf_get_shnum(e, sh);
        shstrndx = elf_get_shstrndx(e, sh);
        offset = elf_get_off(e, (char *)sh + shstrndx * shentsize,
                             SH_OFFSET);
        shstrtab = (char *)e + offset;
    } else {
        sh = NULL;
        shnum = 0;
        shstrndx = 0;
        shstrtab = NULL;
    }
    for (i = 0; (u_int64_t)i < shnum; i++) {
        name = elf_get_word(e, (char *)sh + i * shentsize, SH_NAME);
        offset = elf_get_off(e, (char *)sh + i * shentsize, SH_OFFSET);
        if (strcmp(shstrtab + name, ".strtab") == 0)
            strtab = (char *)e + offset;
        if (strcmp(shstrtab + name, ".dynstr") == 0)
            dynstr = (char *)e + offset;
    }
    if (flags & ED_EHDR)
        elf_print_ehdr(e, sh);
    if (flags & ED_PHDR)
        elf_print_phdr(e, p);
    if (flags & ED_SHDR)
        elf_print_shdr(e, sh);
    for (i = 0; (u_int64_t)i < phnum; i++) {
        v = (char *)p + i * phentsize;
        type = elf_get_word(e, v, P_TYPE);
        switch (type) {
        case PT_INTERP:
            if (flags & ED_INTERP)
                elf_print_interp(e, v);
            break;
        case PT_NULL:
        case PT_LOAD:
        case PT_DYNAMIC:
        case PT_NOTE:
        case PT_SHLIB:
        case PT_PHDR:
            break;
        }
    }
    for (i = 0; (u_int64_t)i < shnum; i++) {
        v = (char *)sh + i * shentsize;
        type = elf_get_word(e, v, SH_TYPE);
        switch (type) {
        case SHT_SYMTAB:
            if (flags & ED_SYMTAB)
                elf_print_symtab(e, v, strtab);
            break;
        case SHT_DYNAMIC:
            if (flags & ED_DYN)
                elf_print_dynamic(e, v);
            break;
        case SHT_RELA:
            if (flags & ED_REL)
                elf_print_rela(e, v);
            break;
        case SHT_REL:
            if (flags & ED_REL)
                elf_print_rel(e, v);
            break;
        case SHT_NOTE:
            name = elf_get_word(e, v, SH_NAME);
            if (flags & ED_NOTE &&
                    strcmp(shstrtab + name, ".note.ABI-tag") == 0)
                elf_print_note(e, v);
            break;
        case SHT_DYNSYM:
            if (flags & ED_SYMTAB)
                elf_print_symtab(e, v, dynstr);
            break;
        case SHT_PROGBITS:
            name = elf_get_word(e, v, SH_NAME);
            if (flags & ED_GOT &&
                    strcmp(shstrtab + name, ".got") == 0)
                elf_print_got(e, v);
            break;
        case SHT_HASH:
            if (flags & ED_HASH)
                elf_print_hash(e, v);
            break;
        case SHT_NULL:
        case SHT_STRTAB:
        case SHT_NOBITS:
        case SHT_SHLIB:
            break;
        }
    }

    return 0;
}