Re: Avoiding unnecessary jump relocations in gas?

From: H. Peter Anvin
Date: Mon May 18 2015 - 16:28:54 EST


On 05/18/2015 01:25 PM, H.J. Lu wrote:
> On Mon, May 18, 2015 at 1:06 PM, H. Peter Anvin <hpa@xxxxxxxxx> wrote:
>> On 05/18/2015 01:02 PM, H.J. Lu wrote:
>>>>
>>>> I wonder if it would make sense to have explicit mnemonics for the
>>>> one-byte offset and four-byte offset jump variants. Sometimes users
>>>> want a jump with a 32-bit offset for reasons that have nothing to do
>>>> with link-time or load-time relocations.
>>>>
>>>
>>> There is:
>>>
>>> jmp.d32 foo
>>>
>>
>> How far back does that syntax work?
>>
>
> .d32 support was added by
>
> commit f8a5c266971d7b5b96f973805551c6e88669cada
> Author: H.J. Lu <hjl.tools@xxxxxxxxx>
> Date: Thu Oct 14 13:31:13 2010 +0000
>
> Add .d32 encoding suffix.
>
> and .d8 supported was added by
>
> commit a501d77eeba717f6d54dce44f286f9e3aad83144
> Author: H.J. Lu <hjl.tools@xxxxxxxxx>
> Date: Fri Jan 20 20:53:50 2012 +0000
>
> Add .d8 suffix support to x86 assembler
>

OK, that is probably too recent. The simplest answer I think is just to
.balign 16 each vector. This is init space... some extra padding really
doesn't matter.

Patch attached (still in compile test).

-hpa

diff --git a/arch/x86/include/asm/segment.h b/arch/x86/include/asm/segment.h
index 5a9856e..f11621f 100644
--- a/arch/x86/include/asm/segment.h
+++ b/arch/x86/include/asm/segment.h
@@ -233,7 +233,7 @@
#ifdef __KERNEL__
#ifndef __ASSEMBLY__

-extern const char early_idt_handlers[NUM_EXCEPTION_VECTORS][2+2+5];
+extern const char early_idt_handlers[NUM_EXCEPTION_VECTORS][16];
#ifdef CONFIG_TRACING
# define trace_early_idt_handlers early_idt_handlers
#endif
diff --git a/arch/x86/kernel/head_32.S b/arch/x86/kernel/head_32.S
index 02d2572..1c18826 100644
--- a/arch/x86/kernel/head_32.S
+++ b/arch/x86/kernel/head_32.S
@@ -524,6 +524,7 @@ setup_once:
andl $0,setup_once_ref /* Once is enough, thanks */
ret

+ .balign 16
ENTRY(early_idt_handlers)
# 36(%esp) %eflags
# 32(%esp) %cs
@@ -531,9 +532,8 @@ ENTRY(early_idt_handlers)
# 24(%rsp) error code
i = 0
.rept NUM_EXCEPTION_VECTORS
- .if (EXCEPTION_ERRCODE_MASK >> i) & 1
- ASM_NOP2
- .else
+ .balign 16
+ .if ((EXCEPTION_ERRCODE_MASK >> i) & 1) == 0
pushl $0 # Dummy error code, to make stack frame uniform
.endif
pushl $i # 20(%esp) Vector number
@@ -542,8 +542,7 @@ ENTRY(early_idt_handlers)
.endr
ENDPROC(early_idt_handlers)

- /* This is global to keep gas from relaxing the jumps */
-ENTRY(early_idt_handler)
+early_idt_handler:
cld

cmpl $2,(%esp) # X86_TRAP_NMI
diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S
index 43eafc8..2d80a09 100644
--- a/arch/x86/kernel/head_64.S
+++ b/arch/x86/kernel/head_64.S
@@ -321,7 +321,8 @@ bad_address:
jmp bad_address

__INIT
- .globl early_idt_handlers
+ .balign 16
+ENTRY(early_idt_handlers)
early_idt_handlers:
# 104(%rsp) %rflags
# 96(%rsp) %cs
@@ -329,18 +330,17 @@ early_idt_handlers:
# 80(%rsp) error code
i = 0
.rept NUM_EXCEPTION_VECTORS
- .if (EXCEPTION_ERRCODE_MASK >> i) & 1
- ASM_NOP2
- .else
+ .balign 16
+ .if ((EXCEPTION_ERRCODE_MASK >> i) & 1) == 0
pushq $0 # Dummy error code, to make stack frame uniform
.endif
pushq $i # 72(%rsp) Vector number
jmp early_idt_handler
i = i + 1
.endr
+ENDPROC(early_idt_handlers)

-/* This is global to keep gas from relaxing the jumps */
-ENTRY(early_idt_handler)
+early_idt_handler:
cld

cmpl $2,(%rsp) # X86_TRAP_NMI