Skip to contentSkip to conversation

Linker script cleanup patches for x86

PATCH Superseded Tim Abbott 2 participants 7 messages Sep 16, 2009 – Sep 17, 2009 x86

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tim Abbott

author
#0 lore raw

v3 cover Linker script cleanup patches for x86

This is an updated version of the patch series I sent on September 8 cleaning up the x86 architecture's linker scripts. The only change is that it is rebased on top of Linus's current master.

This cross-architecture linker script cleanup project is in preparation for adding support for building the kernel with -ffunction-sections -fdata-sections, which is a prerequisite for Ksplice.

-Tim Abbott

Anders Kaseorg (1):
x86: fix fragile computation of vsyscall address

Tim Abbott (4):
x86: convert compressed loader to use __HEAD and HEAD_TEXT macros.
x86: convert to use __HEAD and HEAD_TEXT macros.
x86: Use section .data.page_aligned for the idt_table.
x86: Cleanup linker script using new linker script macros.

 arch/x86/boot/compressed/head_32.S     |    3 +-
 arch/x86/boot/compressed/head_64.S     |    3 +-
 arch/x86/boot/compressed/vmlinux.lds.S |    6 ++-
 arch/x86/kernel/head_32.S              |    2 +-
 arch/x86/kernel/head_64.S              |    2 +-
 arch/x86/kernel/traps.c                |    6 +--
 arch/x86/kernel/vmlinux.lds.S          |   79 +++++--------------------------
 7 files changed, 25 insertions(+), 76 deletions(-)
author
#1 lore raw

PATCH v3 1/5 x86: fix fragile computation of vsyscall address

From: Anders Kaseorg <[email protected]>

Previously, the address of the vsyscall page (VSYSCALL_PHYS_ADDR, VSYSCALL_VIRT_ADDR) was computed by arithmetic on the address of the last section. This leads to bugs when new sections are inserted, such as the one fixed by commit d312ceda567ab91acd756cde95ac5fbc6b40ed40. Let's compute it from the current address instead.

---
 arch/x86/kernel/vmlinux.lds.S |   19 +++++++------------
 1 files changed, 7 insertions(+), 12 deletions(-)
arch/x86/kernel/vmlinux.lds.S +7 -12
index 0ccb57d..b96ca47 100644
@@ -135,24 +135,21 @@ SECTIONS
 #ifdef CONFIG_X86_64
 
 #define VSYSCALL_ADDR (-10*1024*1024)
-#define VSYSCALL_PHYS_ADDR ((LOADADDR(.data) + SIZEOF(.data) + \
-                            PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
-#define VSYSCALL_VIRT_ADDR ((ADDR(.data) + SIZEOF(.data) + \
-                            PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
 
-#define VLOAD_OFFSET (VSYSCALL_ADDR - VSYSCALL_PHYS_ADDR)
+#define VLOAD_OFFSET (VSYSCALL_ADDR - __vsyscall_0 + LOAD_OFFSET)
 #define VLOAD(x) (ADDR(x) - VLOAD_OFFSET)
 
-#define VVIRT_OFFSET (VSYSCALL_ADDR - VSYSCALL_VIRT_ADDR)
+#define VVIRT_OFFSET (VSYSCALL_ADDR - __vsyscall_0)
 #define VVIRT(x) (ADDR(x) - VVIRT_OFFSET)
 
+	. = ALIGN(4096);
+	__vsyscall_0 = .;
+
 	. = VSYSCALL_ADDR;
-	.vsyscall_0 : AT(VSYSCALL_PHYS_ADDR) {
+	.vsyscall_0 : AT(VLOAD(.vsyscall_0)) {
 		*(.vsyscall_0)
 	} :user
 
-	__vsyscall_0 = VSYSCALL_VIRT_ADDR;
-
 	. = ALIGN(CONFIG_X86_L1_CACHE_BYTES);
 	.vsyscall_fn : AT(VLOAD(.vsyscall_fn)) {
 		*(.vsyscall_fn)
@@ -192,11 +189,9 @@ SECTIONS
 		*(.vsyscall_3)
 	}
 
-	. = VSYSCALL_VIRT_ADDR + PAGE_SIZE;
+	. = __vsyscall_0 + PAGE_SIZE;
 
 #undef VSYSCALL_ADDR
-#undef VSYSCALL_PHYS_ADDR
-#undef VSYSCALL_VIRT_ADDR
 #undef VLOAD_OFFSET
 #undef VLOAD
 #undef VVIRT_OFFSET
Signature · 1 line
1.6.3.3
#2 lore raw

Re: [PATCH v3 1/5] x86: fix fragile computation of vsyscall address

[Tim Abbott - Wed, Sep 16, 2009 at 04:44:26PM -0400]

|
| From: Anders Kaseorg <[email protected]>
| 
| Previously, the address of the vsyscall page (VSYSCALL_PHYS_ADDR,
| VSYSCALL_VIRT_ADDR) was computed by arithmetic on the address of the
| last section.  This leads to bugs when new sections are inserted, such
| as the one fixed by commit d312ceda567ab91acd756cde95ac5fbc6b40ed40.
| Let's compute it from the current address instead.
| 
| Signed-off-by: Anders Kaseorg <[email protected]>
| ---
|  arch/x86/kernel/vmlinux.lds.S |   19 +++++++------------
|  1 files changed, 7 insertions(+), 12 deletions(-)
| 
| diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
| index 0ccb57d..b96ca47 100644
| --- a/arch/x86/kernel/vmlinux.lds.S
| +++ b/arch/x86/kernel/vmlinux.lds.S
| @@ -135,24 +135,21 @@ SECTIONS
|  #ifdef CONFIG_X86_64
|  
|  #define VSYSCALL_ADDR (-10*1024*1024)
| -#define VSYSCALL_PHYS_ADDR ((LOADADDR(.data) + SIZEOF(.data) + \
| -                            PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
| -#define VSYSCALL_VIRT_ADDR ((ADDR(.data) + SIZEOF(.data) + \
| -                            PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
|  
| -#define VLOAD_OFFSET (VSYSCALL_ADDR - VSYSCALL_PHYS_ADDR)
| +#define VLOAD_OFFSET (VSYSCALL_ADDR - __vsyscall_0 + LOAD_OFFSET)
|  #define VLOAD(x) (ADDR(x) - VLOAD_OFFSET)
|  
| -#define VVIRT_OFFSET (VSYSCALL_ADDR - VSYSCALL_VIRT_ADDR)
| +#define VVIRT_OFFSET (VSYSCALL_ADDR - __vsyscall_0)
|  #define VVIRT(x) (ADDR(x) - VVIRT_OFFSET)
|  
| +	. = ALIGN(4096);

Hi Tim, it seems there should be PAGE_SIZE, or I miss something? (great work btw!)

| + __vsyscall_0 = .;
| +
...

-- Cyrill

author
#3 lore raw

PATCH v3 2/5 x86: convert compressed loader to use __HEAD and HEAD_TEXT macros.

In reply to Tim Abbott

This has the consequence of changing the section name use for head code from ".text.head" to ".head.text".

Linus suggested that we merge the ".text.head" section with ".text" (presumably while preserving the fact that the head code starts at 0). When I tried this it caused the kernel to not boot.

Cc Ingo Molnar <[email protected]>
Cc Sam Ravnborg <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 arch/x86/boot/compressed/head_32.S     |    3 ++-
 arch/x86/boot/compressed/head_64.S     |    3 ++-
 arch/x86/boot/compressed/vmlinux.lds.S |    6 ++++--
 3 files changed, 8 insertions(+), 4 deletions(-)
arch/x86/boot/compressed/head_32.S +2 -1
index 75e4f00..f543b70 100644
@@ -23,13 +23,14 @@
  */
 	.text
 
+#include <linux/init.h>
 #include <linux/linkage.h>
 #include <asm/segment.h>
 #include <asm/page_types.h>
 #include <asm/boot.h>
 #include <asm/asm-offsets.h>
 
-	.section ".text.head","ax",@progbits
+	__HEAD
 ENTRY(startup_32)
 	cld
 	/*
arch/x86/boot/compressed/head_64.S +2 -1
index f62c284..077e1b6 100644
@@ -24,6 +24,7 @@
 	.code32
 	.text
 
+#include <linux/init.h>
 #include <linux/linkage.h>
 #include <asm/segment.h>
 #include <asm/pgtable_types.h>
@@ -33,7 +34,7 @@
 #include <asm/processor-flags.h>
 #include <asm/asm-offsets.h>
 
-	.section ".text.head"
+	__HEAD
 	.code32
 ENTRY(startup_32)
 	cld
arch/x86/boot/compressed/vmlinux.lds.S +4 -2
index cc353e1..f4193bb 100644
@@ -1,3 +1,5 @@
+#include <asm-generic/vmlinux.lds.h>
+
 OUTPUT_FORMAT(CONFIG_OUTPUT_FORMAT, CONFIG_OUTPUT_FORMAT, CONFIG_OUTPUT_FORMAT)
 
 #undef i386
@@ -18,9 +20,9 @@ SECTIONS
 	 * address 0.
 	 */
 	. = 0;
-	.text.head : {
+	.head.text : {
 		_head = . ;
-		*(.text.head)
+		HEAD_TEXT
 		_ehead = . ;
 	}
 	.rodata.compressed : {
Signature · 1 line
1.6.3.3
author
#4 lore raw

PATCH v3 3/5 x86: convert to use __HEAD and HEAD_TEXT macros.

In reply to Tim Abbott

This has the consequence of changing the section name use for head code from ".text.head" to ".head.text". It also eliminates the ".text.head" output section (instead placing head code at the start of the .text output section), which should be harmless.

This patch only changes the sections in the actual kernel, not those in the compressed boot loader.

Cc Ingo Molnar <[email protected]>
Cc Sam Ravnborg <[email protected]>
---
 arch/x86/kernel/head_32.S     |    2 +-
 arch/x86/kernel/head_64.S     |    2 +-
 arch/x86/kernel/vmlinux.lds.S |   12 +++---------
 3 files changed, 5 insertions(+), 11 deletions(-)
arch/x86/kernel/head_32.S +1 -1
index 3765092..6ce80c5 100644
@@ -79,7 +79,7 @@ RESERVE_BRK(pagetables, INIT_MAP_SIZE)
  * any particular GDT layout, because we load our own as soon as we
  * can.
  */
-.section .text.head,"ax",@progbits
+__HEAD
 ENTRY(startup_32)
 	/* test KEEP_SEGMENTS flag to see if the bootloader is asking
 		us to not reload segments */
arch/x86/kernel/head_64.S +1 -1
index d0bc0a1..780cd92 100644
@@ -40,7 +40,7 @@ L4_START_KERNEL = pgd_index(__START_KERNEL_map)
 L3_START_KERNEL = pud_index(__START_KERNEL_map)
 
 	.text
-	.section .text.head
+	__HEAD
 	.code64
 	.globl startup_64
 startup_64:
arch/x86/kernel/vmlinux.lds.S +3 -9
index b96ca47..5c7826d 100644
@@ -65,17 +65,11 @@ SECTIONS
 #endif
 
 	/* Text and read-only data */
-
-	/* bootstrapping code */
-	.text.head : AT(ADDR(.text.head) - LOAD_OFFSET) {
-		_text = .;
-		*(.text.head)
-	} :text = 0x9090
-
-	/* The rest of the text */
 	.text :  AT(ADDR(.text) - LOAD_OFFSET) {
+		_text = .;
+		/* bootstrapping code */
+		HEAD_TEXT
 #ifdef CONFIG_X86_32
-		/* not really needed, already page aligned */
 		. = ALIGN(PAGE_SIZE);
 		*(.text.page_aligned)
 #endif
Signature · 1 line
1.6.3.3
author
#5 lore raw

PATCH v3 4/5 x86: Use section .data.page_aligned for the idt_table.

In reply to Tim Abbott

The .data.idt section is just squashed into the .data.page_aligned output section by the linker script anyway, so it might as well be in the .data.page_aligned section.

This eliminates all references to .data.idt on x86.

---
 arch/x86/kernel/traps.c       |    6 ++----
 arch/x86/kernel/vmlinux.lds.S |    1 -
 2 files changed, 2 insertions(+), 5 deletions(-)
arch/x86/kernel/traps.c +2 -4
index 8326492..ea23d3b 100644
@@ -73,11 +73,9 @@ char ignore_fpu_irq;
 
 /*
  * The IDT has to be page-aligned to simplify the Pentium
- * F0 0F bug workaround.. We have a special link segment
- * for this.
+ * F0 0F bug workaround.
  */
-gate_desc idt_table[NR_VECTORS]
-	__attribute__((__section__(".data.idt"))) = { { { { 0, 0 } } }, };
+gate_desc idt_table[NR_VECTORS] __page_aligned_data = { { { { 0, 0 } } }, };
 #endif
 
 DECLARE_BITMAP(used_vectors, NR_VECTORS);
arch/x86/kernel/vmlinux.lds.S +0 -1
index 5c7826d..7d6cef3 100644
@@ -112,7 +112,6 @@ SECTIONS
 #endif
 
 		PAGE_ALIGNED_DATA(PAGE_SIZE)
-		*(.data.idt)
 
 		CACHELINE_ALIGNED_DATA(CONFIG_X86_L1_CACHE_BYTES)
 
Signature · 1 line
1.6.3.3
author
#6 lore raw

PATCH v3 5/5 x86: Cleanup linker script using new linker script macros.

In reply to Tim Abbott
---
 arch/x86/kernel/vmlinux.lds.S |   47 ++--------------------------------------
 1 files changed, 3 insertions(+), 44 deletions(-)
arch/x86/kernel/vmlinux.lds.S +3 -44
index 7d6cef3..8d2d0a2 100644
@@ -88,13 +88,7 @@ SECTIONS
 
 	NOTES :text :note
 
-	/* Exception table */
-	. = ALIGN(16);
-	__ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) {
-		__start___ex_table = .;
-		*(__ex_table)
-		__stop___ex_table = .;
-	} :text = 0x9090
+	EXCEPTION_TABLE(16) :text = 0x9090
 
 	RO_DATA(PAGE_SIZE)
 
@@ -207,36 +201,12 @@ SECTIONS
 	PERCPU_VADDR(0, :percpu)
 #endif
 
-	.init.text : AT(ADDR(.init.text) - LOAD_OFFSET) {
-		_sinittext = .;
-		INIT_TEXT
-		_einittext = .;
-	}
+	INIT_TEXT_SECTION(PAGE_SIZE)
 #ifdef CONFIG_X86_64
 	:init
 #endif
 
-	.init.data : AT(ADDR(.init.data) - LOAD_OFFSET) {
-		INIT_DATA
-	}
-
-	. = ALIGN(16);
-	.init.setup : AT(ADDR(.init.setup) - LOAD_OFFSET) {
-		__setup_start = .;
-		*(.init.setup)
-		__setup_end = .;
-	}
-	.initcall.init : AT(ADDR(.initcall.init) - LOAD_OFFSET) {
-		__initcall_start = .;
-		INITCALLS
-		__initcall_end = .;
-	}
-
-	.con_initcall.init : AT(ADDR(.con_initcall.init) - LOAD_OFFSET) {
-		__con_initcall_start = .;
-		*(.con_initcall.init)
-		__con_initcall_end = .;
-	}
+	INIT_DATA_SECTION(16)
 
 	.x86_cpu_dev.init : AT(ADDR(.x86_cpu_dev.init) - LOAD_OFFSET) {
 		__x86_cpu_dev_start = .;
@@ -244,8 +214,6 @@ SECTIONS
 		__x86_cpu_dev_end = .;
 	}
 
-	SECURITY_INIT
-
 	. = ALIGN(8);
 	.parainstructions : AT(ADDR(.parainstructions) - LOAD_OFFSET) {
 		__parainstructions = .;
@@ -276,15 +244,6 @@ SECTIONS
 		EXIT_DATA
 	}
 
-#ifdef CONFIG_BLK_DEV_INITRD
-	. = ALIGN(PAGE_SIZE);
-	.init.ramfs : AT(ADDR(.init.ramfs) - LOAD_OFFSET) {
-		__initramfs_start = .;
-		*(.init.ramfs)
-		__initramfs_end = .;
-	}
-#endif
-
 #if !defined(CONFIG_X86_64) || !defined(CONFIG_SMP)
 	PERCPU(PAGE_SIZE)
 #endif
Signature · 1 line
1.6.3.3