]> sbz's 6dev Repos - ctfdump/.git/commitdiff
Move ELF functions into elf.c
authorMartin Pieuchot <mpi@openbsd.org>
Sat, 26 Mar 2016 19:45:33 +0000 (20:45 +0100)
committerMartin Pieuchot <mpi@openbsd.org>
Sun, 27 Mar 2016 12:33:17 +0000 (14:33 +0200)
Makefile
ctfdump.c
elf.c [new file with mode: 0644]

index fe880948b75fae30d415cc8f9af7fb1fd1ccad38..96aaa70aa3912d8428ff82b8dc0bba53b06e3e98 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,6 @@
 
 PROG=          ctfdump
+SRCS=          ctfdump.c elf.c
 
 CFLAGS+=       -Wall -Wno-unused -Werror
 
index 2577900b04b69aa3f8183f11b61118a7c1a5f4f7..d2cb6382d8eb666b865ea8ae8b1f5b302b2a53f3 100644 (file)
--- a/ctfdump.c
+++ b/ctfdump.c
@@ -50,7 +50,6 @@
 #define DUMP_TYPE      (1 << 6)
 
 int             dump(const char *, uint8_t);
-int             iself(const char *, size_t);
 int             isctf(const char *, size_t);
 __dead void     usage(void);
 
@@ -62,12 +61,15 @@ const char  *ctf_off2name(struct ctf_header *, const char *, off_t,
                     unsigned int);
 
 int             elf_dump(const char *, size_t, uint8_t);
+const char     *elf_idx2sym(size_t *, unsigned char);
+
+/* elf.c */
+int             iself(const char *, size_t);
 int             elf_getshstrtab(const char *, size_t, const char **, size_t *);
 int             elf_getsymtab(const char *, const char *, size_t,
                     const Elf_Sym **, size_t *);
 int             elf_getstrtab(const char *, const char *, size_t,
                     const char **, size_t *);
-const char     *elf_idx2sym(size_t *, unsigned char);
 
 #ifdef ZLIB
 char           *decompress(const char *, size_t, off_t);
@@ -161,138 +163,6 @@ dump(const char *path, uint8_t flags)
        return error;
 }
 
-int
-iself(const char *p, size_t filesize)
-{
-       Elf_Ehdr                *eh = (Elf_Ehdr *)p;
-
-       if (eh->e_ehsize < sizeof(Elf_Ehdr) || !IS_ELF(*eh))
-               return 0;
-
-       if (eh->e_ident[EI_CLASS] != ELFCLASS) {
-               warnx("unexpected word size %u", eh->e_ident[EI_CLASS]);
-               return 0;
-       }
-       if (eh->e_ident[EI_VERSION] != ELF_TARG_VER) {
-               warnx("unexpected version %u", eh->e_ident[EI_VERSION]);
-               return 0;
-       }
-       if (eh->e_ident[EI_DATA] >= ELFDATANUM) {
-               warnx("unexpected data format %u", eh->e_ident[EI_DATA]);
-               return 0;
-       }
-       if (eh->e_shoff > filesize) {
-               warnx("bogus section table offset 0x%llx", (off_t)eh->e_shoff);
-               return 0;
-       }
-       if (eh->e_shentsize < sizeof(Elf_Shdr)) {
-               warnx("bogus section header size %u", eh->e_shentsize);
-               return 0;
-       }
-       if (eh->e_shnum > (filesize - eh->e_shoff) / eh->e_shentsize) {
-               warnx("bogus section header count %u", eh->e_shnum);
-               return 0;
-       }
-       if (eh->e_shstrndx >= eh->e_shnum) {
-               warnx("bogus string table index %u", eh->e_shstrndx);
-               return 0;
-       }
-
-       return 1;
-}
-
-int
-elf_getshstrtab(const char *p, size_t filesize, const char **shstrtab,
-    size_t *shstrtabsize)
-{
-       Elf_Ehdr                *eh = (Elf_Ehdr *)p;
-       Elf_Shdr                *sh;
-
-       sh = (Elf_Shdr *)(p + eh->e_shoff + eh->e_shstrndx * eh->e_shentsize);
-       if (sh->sh_type != SHT_STRTAB) {
-               warnx("unexpected string table type");
-               return 1;
-       }
-       if (sh->sh_offset > filesize) {
-               warnx("bogus string table offset");
-               return 1;
-       }
-       if (sh->sh_size > filesize - sh->sh_offset) {
-               warnx("bogus string table size");
-               return 1;
-       }
-       if (shstrtab != NULL)
-               *shstrtab = p + sh->sh_offset;
-       if (shstrtabsize != NULL)
-               *shstrtabsize = sh->sh_size;
-
-       return 0;
-}
-
-int
-elf_getsymtab(const char *p, const char *shstrtab, size_t shstrtabsize,
-    const Elf_Sym **symtab, size_t *nsymb)
-{
-       Elf_Ehdr        *eh = (Elf_Ehdr *)p;
-       Elf_Shdr        *sh;
-       size_t           i;
-
-       for (i = 0; i < eh->e_shnum; i++) {
-               sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
-
-               if (sh->sh_type != SHT_SYMTAB)
-                       continue;
-
-               if ((sh->sh_link >= eh->e_shnum) ||
-                   (sh->sh_name >= shstrtabsize))
-                       continue;
-
-               if (strncmp(shstrtab + sh->sh_name, ELF_SYMTAB,
-                   strlen(ELF_SYMTAB)) == 0) {
-                       if (symtab != NULL)
-                               *symtab = (Elf_Sym *)(p + sh->sh_offset);
-                       if (nsymb != NULL)
-                               *nsymb = (sh->sh_size / sh->sh_entsize);
-
-                       return 0;
-               }
-       }
-
-       return 1;
-}
-
-int
-elf_getstrtab(const char *p, const char *shstrtab, size_t shstrtabsize,
-    const char **strtab, size_t *strtabsize)
-{
-       Elf_Ehdr        *eh = (Elf_Ehdr *)p;
-       Elf_Shdr        *sh;
-       size_t           i;
-
-       for (i = 0; i < eh->e_shnum; i++) {
-               sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
-
-               if (sh->sh_type != SHT_STRTAB)
-                       continue;
-
-               if ((sh->sh_link >= eh->e_shnum) ||
-                   (sh->sh_name >= shstrtabsize))
-                       continue;
-
-               if (strncmp(shstrtab + sh->sh_name, ELF_STRTAB,
-                   strlen(ELF_STRTAB)) == 0) {
-                       if (strtab != NULL)
-                               *strtab = p + sh->sh_offset;
-                       if (strtabsize != NULL)
-                               *strtabsize = sh->sh_size;
-
-                       return 0;
-               }
-       }
-
-       return 1;
-}
-
 const char             *strtab;
 const Elf_Sym          *symtab;
 size_t                  strtabsize, nsymb;
diff --git a/elf.c b/elf.c
new file mode 100644 (file)
index 0000000..cf3df8a
--- /dev/null
+++ b/elf.c
@@ -0,0 +1,153 @@
+/*
+ * Copyright (c) 2016 Martin Pieuchot <mpi@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/param.h>
+#include <sys/exec_elf.h>
+
+#include <err.h>
+#include <string.h>
+
+int
+iself(const char *p, size_t filesize)
+{
+       Elf_Ehdr                *eh = (Elf_Ehdr *)p;
+
+       if (eh->e_ehsize < sizeof(Elf_Ehdr) || !IS_ELF(*eh))
+               return 0;
+
+       if (eh->e_ident[EI_CLASS] != ELFCLASS) {
+               warnx("unexpected word size %u", eh->e_ident[EI_CLASS]);
+               return 0;
+       }
+       if (eh->e_ident[EI_VERSION] != ELF_TARG_VER) {
+               warnx("unexpected version %u", eh->e_ident[EI_VERSION]);
+               return 0;
+       }
+       if (eh->e_ident[EI_DATA] >= ELFDATANUM) {
+               warnx("unexpected data format %u", eh->e_ident[EI_DATA]);
+               return 0;
+       }
+       if (eh->e_shoff > filesize) {
+               warnx("bogus section table offset 0x%llx", (off_t)eh->e_shoff);
+               return 0;
+       }
+       if (eh->e_shentsize < sizeof(Elf_Shdr)) {
+               warnx("bogus section header size %u", eh->e_shentsize);
+               return 0;
+       }
+       if (eh->e_shnum > (filesize - eh->e_shoff) / eh->e_shentsize) {
+               warnx("bogus section header count %u", eh->e_shnum);
+               return 0;
+       }
+       if (eh->e_shstrndx >= eh->e_shnum) {
+               warnx("bogus string table index %u", eh->e_shstrndx);
+               return 0;
+       }
+
+       return 1;
+}
+
+int
+elf_getshstrtab(const char *p, size_t filesize, const char **shstrtab,
+    size_t *shstrtabsize)
+{
+       Elf_Ehdr                *eh = (Elf_Ehdr *)p;
+       Elf_Shdr                *sh;
+
+       sh = (Elf_Shdr *)(p + eh->e_shoff + eh->e_shstrndx * eh->e_shentsize);
+       if (sh->sh_type != SHT_STRTAB) {
+               warnx("unexpected string table type");
+               return 1;
+       }
+       if (sh->sh_offset > filesize) {
+               warnx("bogus string table offset");
+               return 1;
+       }
+       if (sh->sh_size > filesize - sh->sh_offset) {
+               warnx("bogus string table size");
+               return 1;
+       }
+       if (shstrtab != NULL)
+               *shstrtab = p + sh->sh_offset;
+       if (shstrtabsize != NULL)
+               *shstrtabsize = sh->sh_size;
+
+       return 0;
+}
+
+int
+elf_getsymtab(const char *p, const char *shstrtab, size_t shstrtabsize,
+    const Elf_Sym **symtab, size_t *nsymb)
+{
+       Elf_Ehdr        *eh = (Elf_Ehdr *)p;
+       Elf_Shdr        *sh;
+       size_t           i;
+
+       for (i = 0; i < eh->e_shnum; i++) {
+               sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
+
+               if (sh->sh_type != SHT_SYMTAB)
+                       continue;
+
+               if ((sh->sh_link >= eh->e_shnum) ||
+                   (sh->sh_name >= shstrtabsize))
+                       continue;
+
+               if (strncmp(shstrtab + sh->sh_name, ELF_SYMTAB,
+                   strlen(ELF_SYMTAB)) == 0) {
+                       if (symtab != NULL)
+                               *symtab = (Elf_Sym *)(p + sh->sh_offset);
+                       if (nsymb != NULL)
+                               *nsymb = (sh->sh_size / sh->sh_entsize);
+
+                       return 0;
+               }
+       }
+
+       return 1;
+}
+
+int
+elf_getstrtab(const char *p, const char *shstrtab, size_t shstrtabsize,
+    const char **strtab, size_t *strtabsize)
+{
+       Elf_Ehdr        *eh = (Elf_Ehdr *)p;
+       Elf_Shdr        *sh;
+       size_t           i;
+
+       for (i = 0; i < eh->e_shnum; i++) {
+               sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
+
+               if (sh->sh_type != SHT_STRTAB)
+                       continue;
+
+               if ((sh->sh_link >= eh->e_shnum) ||
+                   (sh->sh_name >= shstrtabsize))
+                       continue;
+
+               if (strncmp(shstrtab + sh->sh_name, ELF_STRTAB,
+                   strlen(ELF_STRTAB)) == 0) {
+                       if (strtab != NULL)
+                               *strtab = p + sh->sh_offset;
+                       if (strtabsize != NULL)
+                               *strtabsize = sh->sh_size;
+
+                       return 0;
+               }
+       }
+
+       return 1;
+}