From 180c819ed8be897f02ef15a201ce4ea47899749f Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Tue, 12 May 2015 16:52:11 -0700 Subject: [PATCH] Add -mshared option to x86 ELF assembler This patch removes the newly added -mno-shared option from x86 ELF assembler and adds -mshared option to x86 ELF assembler. By default, assembler will optimize out relocations to defined non-weak global branch targets with default visibility. The -mshared option tells the assembler to generate code which may go into a shared library where all non-weak global branch targets with default visibility can be preempted. The resulting code is slightly bigger. This option only affects the handling of branch instructions. gas/ * config/tc-i386.c (no_shared): Renamed to ... (shared): This. (elf_symbol_resolved_in_segment_p): Add relocation argument. Check PLT relocations and shared. (md_estimate_size_before_relax): Pass fragP->fr_var to elf_symbol_resolved_in_segment_p. (OPTION_MNO_SHARED): Renamed to ... (OPTION_MSHARED): This. (md_longopts): Renamed -mno-shared to -mshared. (md_show_usage): Likewise. * doc/c-i386.texi: Likewise. gas/testsuite/ * gas/i386/pcrel.d: Pass -mshared to assembler. * gas/i386/relax-3.d: Likewise. Updated. * gas/i386/x86-64-relax-2.d: Likewise. * gas/i386/relax-3.s: Add test for PLT relocation. * gas/i386/relax-4.d: Remove -mno-shared. Updated. * gas/i386/x86-64-relax-3.d: Likewise. --- gas/config/tc-i386.c | 36 ++++++++++++++++++++++----------- gas/doc/c-i386.texi | 17 ++++++++-------- gas/testsuite/gas/i386/pcrel.d | 1 + gas/testsuite/gas/i386/relax-3.d | 28 +++++++++++++------------ gas/testsuite/gas/i386/relax-3.s | 1 + gas/testsuite/gas/i386/relax-4.d | 28 ++++++++++++------------- gas/testsuite/gas/i386/x86-64-relax-2.d | 24 ++++++++++++---------- gas/testsuite/gas/i386/x86-64-relax-3.d | 28 ++++++++++++------------- 8 files changed, 91 insertions(+), 72 deletions(-) diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c index db263ee..254548f 100644 --- a/gas/config/tc-i386.c +++ b/gas/config/tc-i386.c @@ -525,8 +525,8 @@ static int use_big_obj = 0; #endif #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) -/* 1 if not generating code for a shared library. */ -static int no_shared = 0; +/* 1 if generating code for a shared library. */ +static int shared = 0; #endif /* 1 for intel syntax, @@ -8823,7 +8823,7 @@ i386_frag_max_var (fragS *frag) #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) static int -elf_symbol_resolved_in_segment_p (symbolS *fr_symbol) +elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var) { /* STT_GNU_IFUNC symbol must go through PLT. */ if ((symbol_get_bfdsym (fr_symbol)->flags @@ -8834,13 +8834,24 @@ elf_symbol_resolved_in_segment_p (symbolS *fr_symbol) /* Symbol may be weak or local. */ return !S_IS_WEAK (fr_symbol); - /* Non-weak symbols won't be preempted. */ - if (no_shared) + /* Global symbols with non-default visibility can't be preempted. */ + if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT) return 1; + if (fr_var != NO_RELOC) + switch ((enum bfd_reloc_code_real) fr_var) + { + case BFD_RELOC_386_PLT32: + case BFD_RELOC_X86_64_PLT32: + /* Symbol with PLT relocatin may be preempted. */ + return 0; + default: + abort (); + } + /* Global symbols with default visibility in a shared library may be preempted by another definition. */ - return ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT; + return !shared; } #endif @@ -8867,7 +8878,8 @@ md_estimate_size_before_relax (fragS *fragP, segT segment) if (S_GET_SEGMENT (fragP->fr_symbol) != segment #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || (IS_ELF - && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol)) + && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol, + fragP->fr_var)) #endif #if defined (OBJ_COFF) && defined (TE_PE) || (OUTPUT_FLAVOR == bfd_target_coff_flavour @@ -9537,7 +9549,7 @@ const char *md_shortopts = "qn"; #define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18) #define OPTION_OMIT_LOCK_PREFIX (OPTION_MD_BASE + 19) #define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20) -#define OPTION_MNO_SHARED (OPTION_MD_BASE + 21) +#define OPTION_MSHARED (OPTION_MD_BASE + 21) struct option md_longopts[] = { @@ -9548,7 +9560,7 @@ struct option md_longopts[] = #endif #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) {"x32", no_argument, NULL, OPTION_X32}, - {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED}, + {"mshared", no_argument, NULL, OPTION_MSHARED}, #endif {"divide", no_argument, NULL, OPTION_DIVIDE}, {"march", required_argument, NULL, OPTION_MARCH}, @@ -9610,8 +9622,8 @@ md_parse_option (int c, char *arg) .stab instead of .stab.excl. We always use .stab anyhow. */ break; - case OPTION_MNO_SHARED: - no_shared = 1; + case OPTION_MSHARED: + shared = 1; break; #endif #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \ @@ -10043,7 +10055,7 @@ md_show_usage (FILE *stream) fprintf (stream, _("\ -madd-bnd-prefix add BND prefix for all valid branches\n")); fprintf (stream, _("\ - -mno-shared enable branch optimization for non shared code\n")); + -mshared disable branch optimization for shared code\n")); # if defined (TE_PE) || defined (TE_PEP) fprintf (stream, _("\ -mbig-obj generate big object files\n")); diff --git a/gas/doc/c-i386.texi b/gas/doc/c-i386.texi index 47bcbbb..a1997f5 100644 --- a/gas/doc/c-i386.texi +++ b/gas/doc/c-i386.texi @@ -298,15 +298,16 @@ The @code{.att_syntax} and @code{.intel_syntax} directives will take precedent. This option forces the assembler to add BND prefix to all branches, even if such prefix was not explicitly specified in the source code. -@cindex @samp{-mno-shared} option, i386 -@cindex @samp{-mno-shared} option, x86-64 +@cindex @samp{-mshared} option, i386 +@cindex @samp{-mshared} option, x86-64 @item -mno-shared -On ELF target, the assembler normally generates code which can go into a -shared library where non-weak symbols can be preempted. The -@samp{-mno-shared} option tells the assembler to generate code not for -a shared library, where non-weak symbols won't be preempted. The -resulting code is slightly smaller. This option mainly affects the -handling of branch instructions. +On ELF target, the assembler normally optimizes out relocations to +defined non-weak global branch targets with default visibility. The +@samp{-mshared} option tells the assembler to generate code which +may go into a shared library where all non-weak global branch targets +with default visibility can be preempted. The resulting code is +slightly bigger. This option only affects the handling of branch +instructions. @cindex @samp{-mbig-obj} option, x86-64 @item -mbig-obj diff --git a/gas/testsuite/gas/i386/pcrel.d b/gas/testsuite/gas/i386/pcrel.d index 5b61c23..8a91a1a 100644 --- a/gas/testsuite/gas/i386/pcrel.d +++ b/gas/testsuite/gas/i386/pcrel.d @@ -1,4 +1,5 @@ #objdump: -drw +#as: -mshared #name: i386 pcrel reloc .*: +file format .*i386.* diff --git a/gas/testsuite/gas/i386/relax-3.d b/gas/testsuite/gas/i386/relax-3.d index 8aa94e9..4610553 100644 --- a/gas/testsuite/gas/i386/relax-3.d +++ b/gas/testsuite/gas/i386/relax-3.d @@ -1,3 +1,4 @@ +#as: -mshared #objdump: -dwr .*: +file format .* @@ -5,26 +6,27 @@ Disassembly of section .text: 0+ : -[ ]*[a-f0-9]+: eb 1f jmp 21 -[ ]*[a-f0-9]+: eb 19 jmp 1d -[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 5 5: (R_386_PC)?(DISP)?32 global_def -[ ]*[a-f0-9]+: e9 fc ff ff ff jmp a a: (R_386_PC)?(DISP)?32 weak_def -[ ]*[a-f0-9]+: e9 fc ff ff ff jmp f f: (R_386_PC)?(DISP)?32 weak_hidden_undef -[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 14 14: (R_386_PC)?(DISP)?32 weak_hidden_def -[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 19 19: (R_386_PC)?(DISP)?32 hidden_undef - -0+1d : +[ ]*[a-f0-9]+: eb 24 jmp 26 +[ ]*[a-f0-9]+: eb 1e jmp 22 +[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 5 5: R_386_PC32 global_def +[ ]*[a-f0-9]+: e9 fc ff ff ff jmp a a: R_386_PLT32 global_def +[ ]*[a-f0-9]+: e9 fc ff ff ff jmp f f: R_386_PC32 weak_def +[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 14 14: R_386_PC32 weak_hidden_undef +[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 19 19: R_386_PC32 weak_hidden_def +[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 1e 1e: R_386_PC32 hidden_undef + +0+22 : [ ]*[a-f0-9]+: c3 ret -0+1e : +0+23 : [ ]*[a-f0-9]+: c3 ret -0+1f : +0+24 : [ ]*[a-f0-9]+: c3 ret -0+20 : +0+25 : [ ]*[a-f0-9]+: c3 ret -0+21 : +0+26 : [ ]*[a-f0-9]+: c3 ret #pass diff --git a/gas/testsuite/gas/i386/relax-3.s b/gas/testsuite/gas/i386/relax-3.s index ab52185..48ea917 100644 --- a/gas/testsuite/gas/i386/relax-3.s +++ b/gas/testsuite/gas/i386/relax-3.s @@ -4,6 +4,7 @@ foo: jmp local jmp hidden_def jmp global_def + jmp global_def@PLT jmp weak_def jmp weak_hidden_undef jmp weak_hidden_def diff --git a/gas/testsuite/gas/i386/relax-4.d b/gas/testsuite/gas/i386/relax-4.d index b188841..2039251 100644 --- a/gas/testsuite/gas/i386/relax-4.d +++ b/gas/testsuite/gas/i386/relax-4.d @@ -1,5 +1,4 @@ #source: relax-3.s -#as: -mno-shared #objdump: -dwr .*: +file format .* @@ -7,26 +6,27 @@ Disassembly of section .text: 0+ : -[ ]*[a-f0-9]+: eb 1c jmp 1e -[ ]*[a-f0-9]+: eb 16 jmp 1a -[ ]*[a-f0-9]+: eb 16 jmp 1c -[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 7 7: (R_386_PC)?(DISP)?32 weak_def -[ ]*[a-f0-9]+: e9 fc ff ff ff jmp c c: (R_386_PC)?(DISP)?32 weak_hidden_undef -[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 11 11: (R_386_PC)?(DISP)?32 weak_hidden_def -[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 16 16: (R_386_PC)?(DISP)?32 hidden_undef - -0+1a : +[ ]*[a-f0-9]+: eb 21 jmp 23 +[ ]*[a-f0-9]+: eb 1b jmp 1f +[ ]*[a-f0-9]+: eb 1b jmp 21 +[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 7 7: R_386_PLT32 global_def +[ ]*[a-f0-9]+: e9 fc ff ff ff jmp c c: R_386_PC32 weak_def +[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 11 11: R_386_PC32 weak_hidden_undef +[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 16 16: R_386_PC32 weak_hidden_def +[ ]*[a-f0-9]+: e9 fc ff ff ff jmp 1b 1b: R_386_PC32 hidden_undef + +0+1f : [ ]*[a-f0-9]+: c3 ret -0+1b : +0+20 : [ ]*[a-f0-9]+: c3 ret -0+1c : +0+21 : [ ]*[a-f0-9]+: c3 ret -0+1d : +0+22 : [ ]*[a-f0-9]+: c3 ret -0+1e : +0+23 : [ ]*[a-f0-9]+: c3 ret #pass diff --git a/gas/testsuite/gas/i386/x86-64-relax-2.d b/gas/testsuite/gas/i386/x86-64-relax-2.d index 7b0bd56..c124102 100644 --- a/gas/testsuite/gas/i386/x86-64-relax-2.d +++ b/gas/testsuite/gas/i386/x86-64-relax-2.d @@ -1,4 +1,5 @@ #source: relax-3.s +#as: -mshared #objdump: -dwr .*: +file format .* @@ -7,26 +8,27 @@ Disassembly of section .text: 0+ : -[ ]*[a-f0-9]+: eb 1f jmp 21 -[ ]*[a-f0-9]+: eb 19 jmp 1d +[ ]*[a-f0-9]+: eb 24 jmp 26 +[ ]*[a-f0-9]+: eb 1e jmp 22 [ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 9 5: R_X86_64_PC32 global_def-0x4 -[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq e a: R_X86_64_PC32 weak_def-0x4 -[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 13 f: R_X86_64_PC32 weak_hidden_undef-0x4 -[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 18 14: R_X86_64_PC32 weak_hidden_def-0x4 -[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 1d 19: R_X86_64_PC32 hidden_undef-0x4 +[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq e a: R_X86_64_PLT32 global_def-0x4 +[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 13 f: R_X86_64_PC32 weak_def-0x4 +[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 18 14: R_X86_64_PC32 weak_hidden_undef-0x4 +[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 1d 19: R_X86_64_PC32 weak_hidden_def-0x4 +[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 22 1e: R_X86_64_PC32 hidden_undef-0x4 -0+1d : +0+22 : [ ]*[a-f0-9]+: c3 retq -0+1e : +0+23 : [ ]*[a-f0-9]+: c3 retq -0+1f : +0+24 : [ ]*[a-f0-9]+: c3 retq -0+20 : +0+25 : [ ]*[a-f0-9]+: c3 retq -0+21 : +0+26 : [ ]*[a-f0-9]+: c3 retq #pass diff --git a/gas/testsuite/gas/i386/x86-64-relax-3.d b/gas/testsuite/gas/i386/x86-64-relax-3.d index d0c7ee4..98fd28d 100644 --- a/gas/testsuite/gas/i386/x86-64-relax-3.d +++ b/gas/testsuite/gas/i386/x86-64-relax-3.d @@ -1,5 +1,4 @@ #source: relax-3.s -#as: -mno-shared #objdump: -dwr .*: +file format .* @@ -8,26 +7,27 @@ Disassembly of section .text: 0+ : -[ ]*[a-f0-9]+: eb 1c jmp 1e -[ ]*[a-f0-9]+: eb 16 jmp 1a -[ ]*[a-f0-9]+: eb 16 jmp 1c -[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq b 7: R_X86_64_PC32 weak_def-0x4 -[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 10 c: R_X86_64_PC32 weak_hidden_undef-0x4 -[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 15 11: R_X86_64_PC32 weak_hidden_def-0x4 -[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 1a 16: R_X86_64_PC32 hidden_undef-0x4 - -0+1a : +[ ]*[a-f0-9]+: eb 21 jmp 23 +[ ]*[a-f0-9]+: eb 1b jmp 1f +[ ]*[a-f0-9]+: eb 1b jmp 21 +[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq b 7: R_X86_64_PLT32 global_def-0x4 +[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 10 c: R_X86_64_PC32 weak_def-0x4 +[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 15 11: R_X86_64_PC32 weak_hidden_undef-0x4 +[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 1a 16: R_X86_64_PC32 weak_hidden_def-0x4 +[ ]*[a-f0-9]+: e9 00 00 00 00 jmpq 1f 1b: R_X86_64_PC32 hidden_undef-0x4 + +0+1f : [ ]*[a-f0-9]+: c3 retq -0+1b : +0+20 : [ ]*[a-f0-9]+: c3 retq -0+1c : +0+21 : [ ]*[a-f0-9]+: c3 retq -0+1d : +0+22 : [ ]*[a-f0-9]+: c3 retq -0+1e : +0+23 : [ ]*[a-f0-9]+: c3 retq #pass -- 1.9.3