summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormlarkin <mlarkin@openbsd.org>2015-08-12 06:19:25 +0000
committermlarkin <mlarkin@openbsd.org>2015-08-12 06:19:25 +0000
commit414976a8924e019615ba9eff1608dfbb11c057eb (patch)
tree52820fd90bd1b9b8d86c6789c05319e4ea4bcbe9
parentCheck proper HPET period value during attach. (diff)
downloadwireguard-openbsd-414976a8924e019615ba9eff1608dfbb11c057eb.tar.xz
wireguard-openbsd-414976a8924e019615ba9eff1608dfbb11c057eb.zip
Use a linker script when building i386 kernels to allow section padding on
PAGE_SIZE boundaries. This is required to enforce proper separation of sections when adding page protections (coming shortly). This was in snaps before release with no reported side effects. ok deraadt@
-rw-r--r--sys/arch/i386/conf/Makefile.i3867
-rw-r--r--sys/arch/i386/conf/ld.script127
-rw-r--r--sys/arch/i386/i386/locore.s5
-rw-r--r--sys/ddb/db_sym.c8
-rw-r--r--sys/dev/ksyms.c11
5 files changed, 147 insertions, 11 deletions
diff --git a/sys/arch/i386/conf/Makefile.i386 b/sys/arch/i386/conf/Makefile.i386
index 21f1c145e14..4cc51dc8724 100644
--- a/sys/arch/i386/conf/Makefile.i386
+++ b/sys/arch/i386/conf/Makefile.i386
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile.i386,v 1.92 2015/01/13 01:12:49 deraadt Exp $
+# $OpenBSD: Makefile.i386,v 1.93 2015/08/12 06:19:25 mlarkin Exp $
# For instructions on building kernels consult the config(8) and options(4)
# manual pages.
@@ -38,7 +38,8 @@ CMACHFLAGS+= -fno-stack-protector
COPTS?= -O2
CFLAGS= ${DEBUG} ${CWARNFLAGS} ${CMACHFLAGS} ${COPTS} ${PIPE}
AFLAGS= -D_LOCORE -x assembler-with-cpp ${CWARNFLAGS} ${CMACHFLAGS}
-LINKFLAGS= -Ttext 0xD0200120 -e start -N --warn-common -nopie
+LDSCRIPT= ${_machdir}/conf/ld.script
+LINKFLAGS= -T ${LDSCRIPT} -X --warn-common -nopie
.if ${MACHINE} == "amd64"
CFLAGS+= -m32
@@ -74,7 +75,7 @@ NORMAL_S= ${CC} ${AFLAGS} ${CPPFLAGS} -c $<
# ${SYSTEM_LD_TAIL}
SYSTEM_HEAD= locore.o param.o ioconf.o
SYSTEM_OBJ= ${SYSTEM_HEAD} ${OBJS}
-SYSTEM_DEP= Makefile ${SYSTEM_OBJ}
+SYSTEM_DEP= Makefile ${SYSTEM_OBJ} ${LDSCRIPT}
SYSTEM_LD_HEAD= @rm -f $@
SYSTEM_LD= @echo ${LD} ${LINKFLAGS} -o $@ '$${SYSTEM_HEAD} vers.o $${OBJS}'; \
${LD} ${LINKFLAGS} -o $@ ${SYSTEM_HEAD} vers.o ${OBJS}
diff --git a/sys/arch/i386/conf/ld.script b/sys/arch/i386/conf/ld.script
new file mode 100644
index 00000000000..0bbcec474ca
--- /dev/null
+++ b/sys/arch/i386/conf/ld.script
@@ -0,0 +1,127 @@
+/* $OpenBSD: ld.script,v 1.1 2015/08/12 06:19:25 mlarkin Exp $ */
+
+/*
+ * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
+ * Copyright (c) 2009 Tobias Weingartner <weingart@tepid.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
+OUTPUT_ARCH(i386)
+
+/* Define how we want out ELF binary to look like. */
+PHDRS
+{
+ text PT_LOAD FILEHDR PHDRS;
+ rodata PT_LOAD;
+ data PT_LOAD;
+ bss PT_LOAD;
+ openbsd_randomize 0x65a3dbe6; /* PT_OPENBSD_RANDOMIZE */
+}
+
+/*
+ * If we want the text/rodata/data sections aligned on 2M boundaries,
+ * we could use the following instead. Note, file size would increase
+ * due to necessary padding.
+ *
+ *__ALIGN_SIZE = 0x200000;
+ */
+__ALIGN_SIZE = 0x1000;
+__kernel_base_virt = 0xd0200000 + SIZEOF_HEADERS;
+__kernel_base_phys = __kernel_base_virt & 0xffffff;
+
+/* We use physical address to jump to kernel */
+start_phys = LOADADDR(.text) + (start - __kernel_base_virt);
+ENTRY(start_phys)
+SECTIONS
+{
+ __kernel_text_virt = __kernel_base_virt;
+ __kernel_text_phys = __kernel_base_phys;
+ .text (__kernel_text_virt) : AT (__kernel_text_phys)
+ {
+ __text_start = ABSOLUTE(.) & 0xfffff000;
+ __text_size = SIZEOF(.text);
+ __text_load = LOADADDR(.text);
+ locore.o(.text)
+ *(.text .text.*)
+ } :text
+ PROVIDE (__etext = .);
+ PROVIDE (etext = .);
+ _etext = .;
+
+ /* Move rodata to the next page, so we can nuke X and W bit on them */
+ . = ALIGN(__ALIGN_SIZE);
+ __kernel_rodata_virt = .;
+ __kernel_rodata_phys = . & 0xffffff;
+ .rodata (__kernel_rodata_virt) : AT (__kernel_rodata_phys)
+ {
+ __rodata_start = ABSOLUTE(.);
+ __rodata_size = SIZEOF(.rodata);
+ __rodata_load = LOADADDR(.rodata);
+ *(.rodata .rodata.*)
+ *(.codepatch)
+ *(.codepatchend)
+ } :rodata
+ PROVIDE (erodata = .);
+ _erodata = .;
+
+ /* Move data to the next page, so we can add W bit on them */
+ . = ALIGN(__ALIGN_SIZE);
+ __kernel_data_virt = .;
+ __kernel_data_phys = . & 0xffffff;
+ .data (__kernel_data_virt) : AT (__kernel_data_phys)
+ {
+ __data_start = ABSOLUTE(.);
+ __data_size = SIZEOF(.data);
+ __data_load = LOADADDR(.data);
+ *(.data .data.*)
+ } :data
+ . = ALIGN(0x1000);
+ __kernel_randomdata_phys = . & 0xffffff;
+ .openbsd.randomdata : AT (__kernel_randomdata_phys)
+ {
+ *(.openbsd.randomdata)
+ } :data :openbsd_randomize
+ . = ALIGN(0x1000);
+ PROVIDE (edata = .);
+ _edata = .;
+
+ /* BSS starts right after padded data */
+ __kernel_bss_virt = .;
+ __kernel_bss_phys = . & 0xffffff;
+ .bss (__kernel_bss_virt) : AT (__kernel_bss_phys)
+ {
+ __bss_start = ABSOLUTE(.);
+ __bss_size = SIZEOF(.bss);
+ __bss_load = LOADADDR(.bss);
+ *(.bss .bss.*)
+ *(COMMON)
+ /* Align after .bss to ensure correct alignment even if the
+ * .bss section disappears because there are no input sections.
+ */
+ . = ALIGN(0x1000);
+ } :bss
+ __kernel_bss_end = .;
+ . = ALIGN(0x200000);
+ _end = .;
+ PROVIDE (end = .);
+ __kernel_end_phys = . & 0xffffff;
+
+ /* XXX - hack alert, since we are not C++, nuke these */
+ /DISCARD/ :
+ {
+ *(.note.GNU-stack)
+ *(.eh_frame)
+ }
+}
diff --git a/sys/arch/i386/i386/locore.s b/sys/arch/i386/i386/locore.s
index 2d2c696664f..8d367a84a39 100644
--- a/sys/arch/i386/i386/locore.s
+++ b/sys/arch/i386/i386/locore.s
@@ -1,4 +1,4 @@
-/* $OpenBSD: locore.s,v 1.159 2015/07/16 22:06:08 mlarkin Exp $ */
+/* $OpenBSD: locore.s,v 1.160 2015/08/12 06:19:25 mlarkin Exp $ */
/* $NetBSD: locore.s,v 1.145 1996/05/03 19:41:19 christos Exp $ */
/*-
@@ -194,6 +194,7 @@
.globl _C_LABEL(cold), _C_LABEL(cnvmem), _C_LABEL(extmem)
.globl _C_LABEL(cpu_pae)
.globl _C_LABEL(esym)
+ .globl _C_LABEL(ssym)
.globl _C_LABEL(nkptp_max)
.globl _C_LABEL(boothowto), _C_LABEL(bootdev), _C_LABEL(atdevbase)
.globl _C_LABEL(proc0paddr), _C_LABEL(PTDpaddr), _C_LABEL(PTDsize)
@@ -240,6 +241,7 @@ _C_LABEL(cpu_apmi_edx): .long 0 # adv. power management info. 'cpuid'
_C_LABEL(cpu_vendor): .space 16 # vendor string returned by 'cpuid' instruction
_C_LABEL(cpu_brandstr): .space 48 # brand string returned by 'cpuid'
_C_LABEL(cold): .long 1 # cold till we are not
+_C_LABEL(ssym): .long 0 # ptr to start of syms
_C_LABEL(esym): .long 0 # ptr to end of syms
_C_LABEL(cnvmem): .long 0 # conventional memory size
_C_LABEL(extmem): .long 0 # extended memory size
@@ -278,6 +280,7 @@ start: movw $0x1234,0x472 # warm boot
jz 1f
addl $KERNBASE,%eax
1: movl %eax,RELOC(_C_LABEL(esym))
+ movl $__kernel_bss_end, RELOC(_C_LABEL(ssym))
movl 12(%esp),%eax
movl %eax,RELOC(_C_LABEL(bootapiver))
diff --git a/sys/ddb/db_sym.c b/sys/ddb/db_sym.c
index 1e35a097f53..feae038d68b 100644
--- a/sys/ddb/db_sym.c
+++ b/sys/ddb/db_sym.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: db_sym.c,v 1.37 2015/03/14 03:38:46 jsg Exp $ */
+/* $OpenBSD: db_sym.c,v 1.38 2015/08/12 06:19:25 mlarkin Exp $ */
/* $NetBSD: db_sym.c,v 1.24 2000/08/11 22:50:47 tv Exp $ */
/*
@@ -120,13 +120,15 @@ ddb_init(void)
const db_symformat_t **symf;
const char *name = "bsd";
extern char *esym;
-#if defined(__sparc64__) || defined(__mips__) || defined(__amd64__)
+#if defined(__sparc64__) || defined(__mips__) || defined(__amd64__) || \
+ defined(__i386__)
extern char *ssym;
#endif
char *xssym, *xesym;
xesym = esym;
-#if defined(__sparc64__) || defined(__mips__) || defined(__amd64__)
+#if defined(__sparc64__) || defined(__mips__) || defined(__amd64__) || \
+ defined(__i386__)
xssym = ssym;
#else
xssym = (char *)&end;
diff --git a/sys/dev/ksyms.c b/sys/dev/ksyms.c
index cdcb1fa0802..365664e550e 100644
--- a/sys/dev/ksyms.c
+++ b/sys/dev/ksyms.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ksyms.c,v 1.28 2015/03/14 03:38:46 jsg Exp $ */
+/* $OpenBSD: ksyms.c,v 1.29 2015/08/12 06:19:25 mlarkin Exp $ */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
* Copyright (c) 2001 Artur Grabowski <art@openbsd.org>
@@ -38,7 +38,8 @@
#endif
extern char *esym; /* end of symbol table */
-#if defined(__sparc64__) || defined(__mips__) || defined(__amd64__)
+#if defined(__sparc64__) || defined(__mips__) || defined(__amd64__) || \
+ defined(__i386__)
extern char *ssym; /* end of kernel */
#else
extern long end; /* end of kernel */
@@ -60,7 +61,8 @@ void
ksymsattach(int num)
{
-#if defined(__sparc64__) || defined(__mips__) || defined(__amd64__)
+#if defined(__sparc64__) || defined(__mips__) || defined(__amd64__) || \
+ defined(__i386__)
if (esym <= ssym) {
printf("/dev/ksyms: Symbol table not valid.\n");
return;
@@ -74,7 +76,8 @@ ksymsattach(int num)
#ifdef _NLIST_DO_ELF
do {
-#if defined(__sparc64__) || defined(__mips__) || defined(__amd64__)
+#if defined(__sparc64__) || defined(__mips__) || defined(__amd64__) || \
+ defined(__i386__)
caddr_t symtab = ssym;
#else
caddr_t symtab = (caddr_t)&end;