]> pd.if.org Git - zpackage/blobdiff - elf/needed.c
fix segfault reading statically linked executables
[zpackage] / elf / needed.c
index f4edd3dc1b353a4a825fb9b5d209305c0b71a0c7..28bbb9b0ccfa1e5c43a83275235c9d9a35d0a2d4 100644 (file)
@@ -23,7 +23,7 @@ Elf64_Ehdr *libelf_header(void *elf) {
 }
 
 Elf64_Shdr *libelf_shdr(void *elf, int n) {
-       return 0;
+       return (Elf64_Shdr*)(char *)elf + n;
 }
 
 Elf64_Shdr *libelf_sht_strtab(void *elf) {
@@ -58,7 +58,7 @@ Elf64_Shdr *libelf_section_n(void *elf, int n) {
        return (Elf64_Shdr *)((char *)elf + hdr->e_shoff + n * hdr->e_shentsize);
 }
 
-Elf64_Shdr *libelf_section(void *elf, int type) {
+Elf64_Shdr *libelf_section(void *elf, unsigned int type) {
        int i;
        Elf64_Ehdr *hdr;
        Elf64_Shdr *shdr;
@@ -113,7 +113,7 @@ void *libelf_map(char *path, size_t *fsize) {
                return NULL;
        }
        /* not at least the size of the elf header? */
-       if (sbuf.st_size < sizeof(Elf64_Ehdr)) {
+       if ((size_t)sbuf.st_size < sizeof(Elf64_Ehdr)) {
                close(elffd);
                return NULL;
        }
@@ -177,7 +177,7 @@ int main(int ac, char **av) {
                        exit(6);
        }
 
-       Elf64_Shdr *dsect;
+       Elf64_Shdr *dsect = 0;
        /* find program header table */
        for (i = 0; i < hdr->e_phnum; i++) {
                phdr = (Elf64_Phdr *)((char *)elf + hdr->e_phoff + i * hdr->e_phentsize);
@@ -185,6 +185,11 @@ int main(int ac, char **av) {
                        dsect = (Elf64_Shdr *)((char *)elf + phdr->p_offset);
                }
        }
+       if (!dsect) {
+               /* no dsect, statically linked? */
+               exit(7);
+       }
+
        dyn = (Elf64_Dyn *)((char *)elf + dsect->sh_offset);
        if (!dyn) {
                exit(9);