]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
dwarflint: Check that .shstrtab is sound
authorPetr Machata <pmachata@redhat.com>
Thu, 17 Mar 2011 09:51:58 +0000 (10:51 +0100)
committerPetr Machata <pmachata@redhat.com>
Thu, 17 Mar 2011 09:51:58 +0000 (10:51 +0100)
dwarflint/Makefile.am
dwarflint/sections.cc
dwarflint/tests/garbage-12.bz2 [new file with mode: 0644]
dwarflint/tests/run-bad.sh

index 7c0a9b2f93cbec17268634e24cbfc0ae9ac0543e..3da11ac0f648e0357826e0c77c379a1ec9f94c35 100644 (file)
@@ -130,7 +130,8 @@ EXTRA_DIST = $(EXTRA_TESTS) \
        tests/garbage-8.bz2 \
        tests/garbage-9.bz2 \
        tests/garbage-10.bz2 \
-       tests/garbage-11.bz2
+       tests/garbage-11.bz2 \
+       tests/garbage-12.bz2
 
 installed_TESTS_ENVIRONMENT = libdir=$(DESTDIR)$(libdir) \
                              bindir=$(DESTDIR)$(bindir) \
index add26b894f04e382d62b465cad7e485c4e92af07..d5736c4bedb154e5001c13e377b3c5e7bb6483dd 100644 (file)
@@ -1,5 +1,5 @@
 /* Low-level section handling.
-   Copyright (C) 2009, 2010 Red Hat, Inc.
+   Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
    This file is part of Red Hat elfutils.
 
    Red Hat elfutils is free software; you can redistribute it and/or modify
@@ -252,6 +252,17 @@ namespace
 
     bool check_rel = true;
 
+    /* Try to obtain .shstrtab, which we will need in following.  If
+       we fail, elf is broken.  */
+    Elf_Scn *shstrscn = elf_getscn (elf, file->ehdr.e_shstrndx);
+    if (shstrscn == NULL || elf_rawdata (shstrscn, NULL) == NULL)
+      {
+      invalid_elf:
+       wr_error () << "Broken ELF: " << elf_errmsg (-1) << "."
+                   << std::endl;
+       goto close_and_out;
+      }
+
     for (Elf_Scn *scn = NULL; (scn = elf_nextscn (elf, scn)); )
       {
        REALLOC (file, sec);
@@ -260,15 +271,11 @@ namespace
 
        GElf_Shdr *shdr = gelf_getshdr (scn, &cursec->shdr);
        if (shdr == NULL)
-         {
-         invalid_elf:
-           wr_error () << "Broken ELF: " << elf_errmsg (-1) << "."
-                       << std::endl;
-           goto close_and_out;
-         }
+         goto invalid_elf;
 
        const char *scnname = elf_strptr (elf, file->ehdr.e_shstrndx,
                                          shdr->sh_name);
+       // Validate the section name
        if (scnname == NULL)
          goto invalid_elf;
 
diff --git a/dwarflint/tests/garbage-12.bz2 b/dwarflint/tests/garbage-12.bz2
new file mode 100644 (file)
index 0000000..a38e928
Binary files /dev/null and b/dwarflint/tests/garbage-12.bz2 differ
index 46678f0f758870695d167fa58e2476ecc5d52a57..41c56a5f14040306cd9c98a57cdb07e7e64c5cc9 100755 (executable)
@@ -30,7 +30,7 @@ srcdir=$srcdir/tests
 testfiles hello.bad-1 hello.bad-3 empty-1 \
     garbage-1 garbage-2 garbage-3 garbage-4 \
     garbage-5 garbage-6 garbage-7 garbage-8 \
-    garbage-9 garbage-10 garbage-11
+    garbage-9 garbage-10 garbage-11 garbage-12
 
 testrun_compare ./dwarflint hello.bad-1 <<EOF
 error: .debug_info: DIE 0x83: abbrev section at 0x0 doesn't contain code 83.
@@ -116,3 +116,9 @@ error: .rela.debug_info: offset 0x3600: invalid relocation 256 (<INVALID RELOC>)
 error: .debug_line: table 0: header claims that it has a size of 542, but in fact it has a size of 30.
 error: .debug_info: DIE 0xb (abbr. attribute 0xa): unresolved reference to .debug_line table 0x0.
 EOF
+
+testrun_compare ./dwarflint garbage-12 <<EOF
+error: Broken ELF: invalid section header.
+error: .debug_abbrev: data not found.
+error: .debug_info: data not found.
+EOF