]> pd.if.org Git - zpackage/blobdiff - elf/soname.c
remove stray debug fprintf
[zpackage] / elf / soname.c
index 99770159b4e3109a0cd0175c7faf87994fb99198..8618152402d38c69c4240526d9b96a39f0162771 100644 (file)
@@ -9,7 +9,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <elf.h>
+
+#include "elf.h"
 
 int main(int ac, char **av) {
        void *elfbase;
@@ -22,6 +23,18 @@ int main(int ac, char **av) {
        char *name, *dynname;
        Elf64_Dyn *dent;
 
+       if (ac < 2) {
+               fprintf(stderr, "usage: soname <file>\n");
+               exit(EXIT_FAILURE);
+       }
+
+       if (lstat(av[1], &sbuf) == -1) {
+               exit(1);
+       }
+       if (!S_ISREG(sbuf.st_mode)) {
+               exit(1);
+       }
+
        elffd = open(av[1], O_RDONLY);
        if (elffd == -1) {
                exit(1);
@@ -29,6 +42,14 @@ int main(int ac, char **av) {
        if (fstat(elffd, &sbuf) == -1) {
                exit(1);
        }
+       /* not a regular file? */
+       if (!S_ISREG(sbuf.st_mode)) {
+               exit(1);
+       }
+       /* not at least the size of the elf header? */
+       if ((size_t)sbuf.st_size < sizeof(Elf64_Ehdr)) {
+               exit(1);
+       }
 
        elfbase = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, elffd, 0);
        if (!elfbase) {
@@ -97,9 +118,10 @@ int main(int ac, char **av) {
        for (dent = (Elf64_Dyn *)((char *)elfbase + dynsect->sh_offset); dent->d_tag != DT_NULL; dent++) {
                if (dent->d_tag == DT_SONAME) {
                        printf("%s\n", dynname + dent->d_un.d_val);
-                       break;
+                       /* TODO can there be more than one? */
+                       exit(0);
                }
        }
 
-       return 0;
+       return 11;
 }