ao-elftohex: Add conditions for skipping ELF sections
authorKeith Packard <keithp@keithp.com>
Sun, 9 Apr 2017 19:48:54 +0000 (12:48 -0700)
committerBdale Garbee <bdale@gag.com>
Sat, 22 Apr 2017 17:52:34 +0000 (11:52 -0600)
Skip sections with size 0, or which are of type SHT_NOBITS or which
don't have the SHF_ALLOC flag set.

This avoids crashing on sections which don't have any data to copy.

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/ao-elftohex/ao-elftohex.c
ao-tools/lib/ao-elf.c

index 265908c59e20f8ceabbead38ee065eab622967b1..f3ab0c38ca4a1fdeff140e14a553979b1aeecff6 100644 (file)
@@ -19,6 +19,7 @@
 #include <getopt.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include "ao-hex.h"
 #include "ao-elf.h"
 #include "ao-verbose.h"
index c44102f869a7fe2d6964b601ebea23f9e529a7f5..cc127b62a8a7e73515313dc4cce7d425c4d3a259 100644 (file)
@@ -186,6 +186,7 @@ load_write(struct ao_hex_image *from, uint32_t address, uint32_t length, void *d
        return new;
 }
 
+#define DBG 0
 /*
  * Construct a large in-memory block for all
  * of the loaded sections of the program
@@ -202,7 +203,7 @@ get_load(Elf *e)
        GElf_Phdr       phdr;
        GElf_Addr       sh_paddr;
        struct ao_hex_image     *load = NULL;
-#if 0
+#if DBG
        char            *section_name;
 #endif
        size_t          nshdr;
@@ -231,7 +232,7 @@ get_load(Elf *e)
 
                /* Get the associated file section */
 
-#if 0
+#if DBG
                fprintf (stderr, "offset %08x vaddr %08x paddr %08x filesz %08x memsz %08x\n",
                         (uint32_t) phdr.p_offset,
                         (uint32_t) phdr.p_vaddr,
@@ -252,18 +253,16 @@ get_load(Elf *e)
                                abort();
                        }
 
-#if 0
+#if DBG
                        section_name = elf_strptr(e, shstrndx, shdr.sh_name);
 #endif
 
-                       if (phdr.p_offset <= shdr.sh_offset && shdr.sh_offset < phdr.p_offset + phdr.p_filesz) {
-
-                               if (shdr.sh_size == 0)
-                                       continue;
-
+                       if (shdr.sh_size != 0 && shdr.sh_type != SHT_NOBITS && (shdr.sh_flags & SHF_ALLOC) &&
+                           phdr.p_offset <= shdr.sh_offset && shdr.sh_offset < phdr.p_offset + phdr.p_filesz)
+                       {
                                sh_paddr = phdr.p_paddr + shdr.sh_offset - phdr.p_offset;
 
-#if 0
+#if DBG
                                fprintf (stderr, "\tsize %08x rom %08x exec %08x %s\n",
                                         (uint32_t) shdr.sh_size,
                                         (uint32_t) sh_paddr,