diff options
author | Alexander Yurchenko <grange@cvs.openbsd.org> | 2005-01-19 19:37:30 +0000 |
---|---|---|
committer | Alexander Yurchenko <grange@cvs.openbsd.org> | 2005-01-19 19:37:30 +0000 |
commit | 80c605a9d022a080f57caa6ababca6bbdecb1bb4 (patch) | |
tree | 2d6cebfacd7270582c8e61c2c2e377c0ce1078bc | |
parent | bc38a25b8d585708b4bc895cc7288ea403265c3b (diff) |
Add elf{32,64}_load_phdrs() for loading program headers.
ok mickey@
-rw-r--r-- | usr.bin/nm/elf.c | 32 | ||||
-rw-r--r-- | usr.bin/nm/elfuncs.h | 4 |
2 files changed, 33 insertions, 3 deletions
diff --git a/usr.bin/nm/elf.c b/usr.bin/nm/elf.c index b273ede7381..e2fca2fa5ba 100644 --- a/usr.bin/nm/elf.c +++ b/usr.bin/nm/elf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: elf.c,v 1.12 2004/10/11 04:50:47 mickey Exp $ */ +/* $OpenBSD: elf.c,v 1.13 2005/01/19 19:37:29 grange Exp $ */ /* * Copyright (c) 2003 Michael Shalayeff @@ -27,7 +27,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: elf.c,v 1.12 2004/10/11 04:50:47 mickey Exp $"; +static const char rcsid[] = "$OpenBSD: elf.c,v 1.13 2005/01/19 19:37:29 grange Exp $"; #endif /* not lint */ #include <sys/param.h> @@ -55,6 +55,7 @@ static const char rcsid[] = "$OpenBSD: elf.c,v 1.12 2004/10/11 04:50:47 mickey E #define swap_quarter swap16 #define elf_fix_header elf32_fix_header #define elf_load_shdrs elf32_load_shdrs +#define elf_load_phdrs elf32_load_phdrs #define elf_fix_shdrs elf32_fix_shdrs #define elf_fix_phdrs elf32_fix_phdrs #define elf_fix_sym elf32_fix_sym @@ -78,6 +79,7 @@ static const char rcsid[] = "$OpenBSD: elf.c,v 1.12 2004/10/11 04:50:47 mickey E #define swap_quarter swap16 #define elf_fix_header elf64_fix_header #define elf_load_shdrs elf64_load_shdrs +#define elf_load_phdrs elf64_load_phdrs #define elf_fix_shdrs elf64_fix_shdrs #define elf_fix_phdrs elf64_fix_phdrs #define elf_fix_sym elf64_fix_sym @@ -165,6 +167,32 @@ elf_load_shdrs(const char *name, FILE *fp, off_t foff, Elf_Ehdr *head) return (shdr); } +Elf_Phdr * +elf_load_phdrs(const char *name, FILE *fp, off_t foff, Elf_Ehdr *head) +{ + Elf_Phdr *phdr; + + if ((phdr = malloc(head->e_phentsize * head->e_phnum)) == NULL) { + warn("%s: malloc phdr", name); + return (NULL); + } + + if (fseeko(fp, foff + head->e_phoff, SEEK_SET)) { + warn("%s: fseeko", name); + free(phdr); + return (NULL); + } + + if (fread(phdr, head->e_phentsize, head->e_phnum, fp) != head->e_phnum) { + warnx("%s: premature EOF", name); + free(phdr); + return (NULL); + } + + elf_fix_phdrs(head, phdr); + return (phdr); +} + int elf_fix_shdrs(Elf_Ehdr *eh, Elf_Shdr *shdr) { diff --git a/usr.bin/nm/elfuncs.h b/usr.bin/nm/elfuncs.h index 269833066f4..a565bbd9228 100644 --- a/usr.bin/nm/elfuncs.h +++ b/usr.bin/nm/elfuncs.h @@ -1,4 +1,4 @@ -/* $OpenBSD: elfuncs.h,v 1.1 2004/10/09 20:26:57 mickey Exp $ */ +/* $OpenBSD: elfuncs.h,v 1.2 2005/01/19 19:37:29 grange Exp $ */ /* * Copyright (c) 2004 Michael Shalayeff @@ -30,6 +30,7 @@ extern char *stab; int elf32_fix_header(Elf32_Ehdr *eh); Elf32_Shdr*elf32_load_shdrs(const char *, FILE *, off_t, Elf32_Ehdr *); +Elf32_Phdr*elf32_load_phdrs(const char *, FILE *, off_t, Elf32_Ehdr *); int elf32_fix_shdrs(Elf32_Ehdr *eh, Elf32_Shdr *shdr); int elf32_fix_phdrs(Elf32_Ehdr *eh, Elf32_Phdr *phdr); int elf32_fix_sym(Elf32_Ehdr *eh, Elf32_Sym *sym); @@ -39,6 +40,7 @@ int elf32_symload(const char *, FILE *, off_t, Elf32_Ehdr *, Elf32_Shdr *, int elf64_fix_header(Elf64_Ehdr *eh); Elf64_Shdr*elf64_load_shdrs(const char *, FILE *, off_t, Elf64_Ehdr *); +Elf64_Phdr*elf64_load_phdrs(const char *, FILE *, off_t, Elf64_Ehdr *); int elf64_fix_shdrs(Elf64_Ehdr *eh, Elf64_Shdr *shdr); int elf64_fix_phdrs(Elf64_Ehdr *eh, Elf64_Phdr *phdr); int elf64_fix_sym(Elf64_Ehdr *eh, Elf64_Sym *sym); |