Changeset View
Changeset View
Standalone View
Standalone View
head/usr.bin/elfdump/elfdump.c
Show All 38 Lines | |||||
#include <err.h> | #include <err.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#include <inttypes.h> | #include <inttypes.h> | ||||
#include <stddef.h> | #include <stddef.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <termios.h> | |||||
#include <unistd.h> | #include <unistd.h> | ||||
#define ED_DYN (1<<0) | #define ED_DYN (1<<0) | ||||
#define ED_EHDR (1<<1) | #define ED_EHDR (1<<1) | ||||
#define ED_GOT (1<<2) | #define ED_GOT (1<<2) | ||||
#define ED_HASH (1<<3) | #define ED_HASH (1<<3) | ||||
#define ED_INTERP (1<<4) | #define ED_INTERP (1<<4) | ||||
#define ED_NOTE (1<<5) | #define ED_NOTE (1<<5) | ||||
▲ Show 20 Lines • Show All 444 Lines • ▼ Show 20 Lines | main(int ac, char **av) | ||||
u_int64_t phnum; | u_int64_t phnum; | ||||
u_int64_t shentsize; | u_int64_t shentsize; | ||||
u_int64_t shnum; | u_int64_t shnum; | ||||
u_int64_t shstrndx; | u_int64_t shstrndx; | ||||
u_int64_t offset; | u_int64_t offset; | ||||
u_int64_t name; | u_int64_t name; | ||||
u_int64_t type; | u_int64_t type; | ||||
struct stat sb; | struct stat sb; | ||||
unsigned long cmd; | |||||
u_int flags; | u_int flags; | ||||
Elf32_Ehdr *e; | Elf32_Ehdr *e; | ||||
void *p; | void *p; | ||||
void *sh; | void *sh; | ||||
void *v; | void *v; | ||||
int fd; | int fd; | ||||
int ch; | int ch; | ||||
int i; | int i; | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | if (ac == 0 || flags == 0) | ||||
usage(); | usage(); | ||||
if ((fd = open(*av, O_RDONLY)) < 0 || | if ((fd = open(*av, O_RDONLY)) < 0 || | ||||
fstat(fd, &sb) < 0) | fstat(fd, &sb) < 0) | ||||
err(1, "%s", *av); | err(1, "%s", *av); | ||||
cap_rights_init(&rights, CAP_MMAP_R); | cap_rights_init(&rights, CAP_MMAP_R); | ||||
if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS) | if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS) | ||||
err(1, "unable to limit rights for %s", *av); | err(1, "unable to limit rights for %s", *av); | ||||
close(STDIN_FILENO); | close(STDIN_FILENO); | ||||
cap_rights_init(&rights, CAP_WRITE); | cap_rights_init(&rights, CAP_FSTAT, CAP_IOCTL, CAP_WRITE); | ||||
if (cap_rights_limit(STDOUT_FILENO, &rights) < 0 && errno != ENOSYS) | cmd = TIOCGETA; /* required by isatty(3) in printf(3) */ | ||||
err(1, "unable to limit rights for stdout"); | if ((cap_rights_limit(STDOUT_FILENO, &rights) < 0 && errno != ENOSYS) || | ||||
if (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS) | (cap_ioctls_limit(STDOUT_FILENO, &cmd, 1) < 0 && errno != ENOSYS) || | ||||
err(1, "unable to limit rights for stderr"); | (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS) || | ||||
(cap_ioctls_limit(STDERR_FILENO, &cmd, 1) < 0 && errno != ENOSYS)) | |||||
err(1, "unable to limit rights for stdout/stderr"); | |||||
if (cap_enter() < 0 && errno != ENOSYS) | if (cap_enter() < 0 && errno != ENOSYS) | ||||
err(1, "unable to enter capability mode"); | err(1, "unable to enter capability mode"); | ||||
e = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); | e = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); | ||||
if (e == MAP_FAILED) | if (e == MAP_FAILED) | ||||
err(1, NULL); | err(1, NULL); | ||||
if (!IS_ELF(*(Elf32_Ehdr *)e)) | if (!IS_ELF(*(Elf32_Ehdr *)e)) | ||||
errx(1, "not an elf file"); | errx(1, "not an elf file"); | ||||
phoff = elf_get_off(e, e, E_PHOFF); | phoff = elf_get_off(e, e, E_PHOFF); | ||||
▲ Show 20 Lines • Show All 670 Lines • Show Last 20 Lines |