Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch/amd64/stand/prekern Gather the section filtering in...



details:   https://anonhg.NetBSD.org/src/rev/0aab93872fd7
branches:  trunk
changeset: 932346:0aab93872fd7
user:      maxv <maxv%NetBSD.org@localhost>
date:      Tue May 05 19:26:47 2020 +0000

description:
Gather the section filtering in a single function, and add a sanity check
when relocating, to make sure the section we're accessing is mappable.

Currently this check fails, because of the Xen section, which has RELAs but
is an unmappable unallocated note.

Also improve the prekern ASSERTs while here.

diffstat:

 sys/arch/amd64/stand/prekern/elf.c     |  35 ++++++++++++++++++++++++---------
 sys/arch/amd64/stand/prekern/prekern.h |   4 +-
 2 files changed, 27 insertions(+), 12 deletions(-)

diffs (97 lines):

diff -r 721cd31a33d9 -r 0aab93872fd7 sys/arch/amd64/stand/prekern/elf.c
--- a/sys/arch/amd64/stand/prekern/elf.c        Tue May 05 18:12:20 2020 +0000
+++ b/sys/arch/amd64/stand/prekern/elf.c        Tue May 05 19:26:47 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: elf.c,v 1.18 2019/01/05 22:11:07 maxv Exp $    */
+/*     $NetBSD: elf.c,v 1.19 2020/05/05 19:26:47 maxv Exp $    */
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -259,6 +259,19 @@
        }
 }
 
+static bool
+elf_section_mappable(Elf_Shdr *shdr)
+{
+       if (!(shdr->sh_flags & SHF_ALLOC)) {
+               return false;
+       }
+       if (shdr->sh_type != SHT_NOBITS &&
+           shdr->sh_type != SHT_PROGBITS) {
+               return false;
+       }
+       return true;
+}
+
 void
 elf_map_sections(void)
 {
@@ -273,11 +286,7 @@
        for (i = 0; i < eif.ehdr->e_shnum; i++) {
                shdr = &eif.shdr[i];
 
-               if (!(shdr->sh_flags & SHF_ALLOC)) {
-                       continue;
-               }
-               if (shdr->sh_type != SHT_NOBITS &&
-                   shdr->sh_type != SHT_PROGBITS) {
+               if (!elf_section_mappable(shdr)) {
                        continue;
                }
 
@@ -383,10 +392,10 @@
         * Update all symbol values with the appropriate offset.
         */
        for (i = 0; i < eif.ehdr->e_shnum; i++) {
-               if (eif.shdr[i].sh_type != SHT_NOBITS &&
-                   eif.shdr[i].sh_type != SHT_PROGBITS) {
+               if (!elf_section_mappable(&eif.shdr[i])) {
                        continue;
                }
+
                ASSERT(eif.shdr[i].sh_offset != 0);
                secva = baseva + eif.shdr[i].sh_offset;
                for (j = 0; j < eif.symcnt; j++) {
@@ -417,7 +426,10 @@
 
                secidx = eif.shdr[i].sh_info;
                if (secidx >= eif.ehdr->e_shnum) {
-                       fatal("elf_kernel_reloc: wrong REL relocation");
+                       fatal("elf_kernel_reloc: REL sh_info is malformed");
+               }
+               if (!elf_section_mappable(&eif.shdr[secidx])) {
+                       fatal("elf_kernel_reloc: REL sh_info not mappable");
                }
                base = (uintptr_t)eif.ehdr + eif.shdr[secidx].sh_offset;
 
@@ -446,7 +458,10 @@
 
                secidx = eif.shdr[i].sh_info;
                if (secidx >= eif.ehdr->e_shnum) {
-                       fatal("elf_kernel_reloc: wrong RELA relocation");
+                       fatal("elf_kernel_reloc: RELA sh_info is malformed");
+               }
+               if (!elf_section_mappable(&eif.shdr[secidx])) {
+                       fatal("elf_kernel_reloc: RELA sh_info not mappable");
                }
                base = (uintptr_t)eif.ehdr + eif.shdr[secidx].sh_offset;
 
diff -r 721cd31a33d9 -r 0aab93872fd7 sys/arch/amd64/stand/prekern/prekern.h
--- a/sys/arch/amd64/stand/prekern/prekern.h    Tue May 05 18:12:20 2020 +0000
+++ b/sys/arch/amd64/stand/prekern/prekern.h    Tue May 05 19:26:47 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: prekern.h,v 1.20 2018/06/20 11:49:37 maxv Exp $        */
+/*     $NetBSD: prekern.h,v 1.21 2020/05/05 19:26:47 maxv Exp $        */
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -37,7 +37,7 @@
 #include "pdir.h"
 #include "redef.h"
 
-#define ASSERT(a) if (!(a)) fatal("ASSERT");
+#define ASSERT(a) if (!(a)) fatal("ASSERT: " #a);
 typedef uint64_t pte_prot_t;
 #define WHITE_ON_BLACK 0x07
 #define RED_ON_BLACK 0x04



Home | Main Index | Thread Index | Old Index