Reuse 32 bit C code more safely
v5 cover Reuse 32 bit C code more safely
This series attempt to:
- use more C code, that is replace some assembly code with C;
- avoid some code duplication between C and assembly;
- prevent some issues having relocations in C code.
The idea is extending the current C to binary code conversion done for 32 bit C code called from head.S making sure relocations are safe and allowing external symbols usage from C code.
More details of the implementation are in commit message 1/5, which is the largest patch.
Patch 2/5 reuses code to relocate the trampoline between 32 and 64 bit. Patches 3/5 and 4/5 move some code from assembly to C.
Other RFC commits were excluded, I didn't manage to entangle the issues sharing headers between 32 and 64 bits.
Since RFC code was more tested, also with CI and some incompatibility were fixed. On that it's weird that we need Python 3.8 for Qemu but we still use Python 2 for Xen. Shouldn't we bump requirement to Python 3 even for Xen?
Code boots successfully using:
- BIOS boot;
- EFI boot with Grub2 and ELF file;
- direct EFI boot without Grub.
Code is currently based on "master" branch, currently commit 3f6ee3db2e878398cfcde725399b4d1b04e92269.
Changes since v1:
- 2 preliminary commits for adjust .gitignore;
- last commit split (one variable at a time);
- lot of style and names changes;
- first commit, now 3/6 had some build changes, more details on commit message.
Changes since v2:
- removed merged commits;
- reverted order of 2 commits;
- remove some useless casts;
- added change to comment.
Changes since v3:
- added a preparation commit for Makefiles (mainly written by Andrew Cooper);
- added a comment improvement commit;
- allows also data;
- other minor style changes;
- added some Reviewed-by.
Changes since v4:
- add build32.final.lds build32.other.lds to targets macro;
- place some comments over a rule, not inside;
- simplified linking and producing binary rule;
- renamed built_in_32 to built-in-32, coding style;
- fix minor indentation;
- put magic numbers in Makefile and propagate them;
- minor variable clanups in Python script;
- add dependency to Python script.
Frediano Ziglio (5):
x86/boot: create a C bundle for 32 bit boot code and use it x86/boot: Reuse code to relocate trampoline x86/boot: Use boot_vid_info variable directly from C code x86/boot: Use trampoline_phys variable directly from C code x86/boot: Clarify comment
xen/arch/x86/boot/.gitignore | 5 +-
xen/arch/x86/boot/Makefile | 51 +++-
.../x86/boot/{build32.lds => build32.lds.S} | 41 +++-
xen/arch/x86/boot/cmdline.c | 12 -
xen/arch/x86/boot/head.S | 49 +---
xen/arch/x86/boot/reloc-trampoline.c | 36 +++
xen/arch/x86/boot/reloc.c | 35 +--
xen/arch/x86/efi/efi-boot.h | 15 +-
xen/tools/combine_two_binaries.py | 218 ++++++++++++++++++
9 files changed, 348 insertions(+), 114 deletions(-)
rename xen/arch/x86/boot/{build32.lds => build32.lds.S} (63%)
create mode 100644 xen/arch/x86/boot/reloc-trampoline.c
create mode 100755 xen/tools/combine_two_binaries.pySignature · 1 line
2.34.1
PATCH v5 2/5 x86/boot: Reuse code to relocate trampoline
Move code from efi-boot.h to a separate, new, reloc-trampoline.c file. Reuse this new code, compiling it for 32bit as well, to replace assembly code in head.S doing the same thing.
---
Changes since v3:
- fixed a typo in comment;
- added Reviewed-by.
--- xen/arch/x86/boot/Makefile | 12 ++++++---- xen/arch/x86/boot/build32.lds.S | 5 ++++ xen/arch/x86/boot/head.S | 23 +----------------- xen/arch/x86/boot/reloc-trampoline.c | 36 ++++++++++++++++++++++++++++ xen/arch/x86/efi/efi-boot.h | 15 ++---------- 5 files changed, 52 insertions(+), 39 deletions(-) create mode 100644 xen/arch/x86/boot/reloc-trampoline.c
xen/arch/x86/boot/Makefile +8 -4
@@ -1,12 +1,16 @@ obj-bin-y += head.o obj-bin-y += built-in-32.o +obj-bin-y += $(obj64) obj32 := cmdline.32.o obj32 += reloc.32.o +obj32 += reloc-trampoline.32.o -nocov-y += $(obj32) -noubsan-y += $(obj32) -targets += $(obj32) +obj64 := reloc-trampoline.o + +nocov-y += $(obj32) $(obj64) +noubsan-y += $(obj32) $(obj64) +targets += $(obj32) $(obj64) obj32 := $(addprefix $(obj)/,$(obj32))
@@ -57,7 +61,7 @@ $(obj)/built-in-32.S: $(obj)/built-in-32.other.bin $(obj)/built-in-32.final.bin --bin1 $(obj)/built-in-32.other.bin \ --bin2 $(obj)/built-in-32.final.bin \ --map $(obj)/built-in-32.final.map \ - --exports cmdline_parse_early,reloc \ + --exports cmdline_parse_early,reloc,reloc_trampoline32 \ --output $@ clean-files := built-in-32.*.bin built-in-32.*.map build32.*.lds
xen/arch/x86/boot/build32.lds.S +5 -0
@@ -41,6 +41,11 @@ SECTIONS * Potentially they should be all variables. */ DECLARE_IMPORT(__base_relocs_start); DECLARE_IMPORT(__base_relocs_end); + DECLARE_IMPORT(__trampoline_rel_start); + DECLARE_IMPORT(__trampoline_rel_stop); + DECLARE_IMPORT(__trampoline_seg_start); + DECLARE_IMPORT(__trampoline_seg_stop); + DECLARE_IMPORT(trampoline_phys); . = . + GAP; *(.text) *(.text.*)
xen/arch/x86/boot/head.S +1 -22
@@ -706,28 +706,7 @@ trampoline_setup: mov %edx, sym_offs(l1_bootmap)(%esi, %ecx, 8) /* Apply relocations to bootstrap trampoline. */ - mov sym_esi(trampoline_phys), %edx - lea sym_esi(__trampoline_rel_start), %edi - lea sym_esi(__trampoline_rel_stop), %ecx -1: - mov (%edi), %eax - add %edx, (%edi, %eax) - add $4,%edi - - cmp %ecx, %edi - jb 1b - - /* Patch in the trampoline segment. */ - shr $4,%edx - lea sym_esi(__trampoline_seg_start), %edi - lea sym_esi(__trampoline_seg_stop), %ecx -1: - mov (%edi), %eax - mov %dx, (%edi, %eax) - add $4,%edi - - cmp %ecx, %edi - jb 1b + call reloc_trampoline32 /* Do not parse command line on EFI platform here. */ cmpb $0, sym_esi(efi_platform)
xen/arch/x86/boot/reloc-trampoline.c new file +36 -0
@@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <xen/compiler.h> +#include <xen/stdint.h> +#include <asm/trampoline.h> + +extern const int32_t __trampoline_rel_start[], __trampoline_rel_stop[]; +extern const int32_t __trampoline_seg_start[], __trampoline_seg_stop[]; + +#if defined(__i386__) +void reloc_trampoline32(void) +#elif defined (__x86_64__) +void reloc_trampoline64(void) +#else +#error Unknown architecture +#endif +{ +unsigned long phys = trampoline_phys; +const int32_t *trampoline_ptr; + +/* +* Apply relocations to trampoline. +* +* This modifies the trampoline in place within Xen, so that it will +* operate correctly when copied into place. +*/ +for ( trampoline_ptr = __trampoline_rel_start; + trampoline_ptr < __trampoline_rel_stop; + ++trampoline_ptr ) + *(uint32_t *)(*trampoline_ptr + (long )trampoline_ptr) += phys; + +for ( trampoline_ptr = __trampoline_seg_start; + trampoline_ptr < __trampoline_seg_stop; + ++trampoline_ptr ) + *(uint16_t *)(*trampoline_ptr + (long )trampoline_ptr) = phys >>4 ; +}
xen/arch/x86/efi/efi-boot.h +2 -13
@@ -103,27 +103,16 @@ static void __init efi_arch_relocate_image(unsigned long delta) } } -extern const int32_t __trampoline_rel_start[], __trampoline_rel_stop[]; -extern const int32_t __trampoline_seg_start[], __trampoline_seg_stop[]; +void reloc_trampoline64 (void );static void __init relocate_trampoline (unsigned long phys) { -const int32_t *trampoline_ptr; - trampoline_phys = phys;if ( !efi_enabled (EFI_LOADER ) )return ; -/* Apply relocations to trampoline. */ -for ( trampoline_ptr = __trampoline_rel_start; - trampoline_ptr < __trampoline_rel_stop; - ++trampoline_ptr ) - *(u32 *)(*trampoline_ptr + (long )trampoline_ptr) += phys; -for ( trampoline_ptr = __trampoline_seg_start; - trampoline_ptr < __trampoline_seg_stop; - ++trampoline_ptr ) - *(u16 *)(*trampoline_ptr + (long )trampoline_ptr) = phys >>4 ; +reloc_trampoline64 (); }static void __init place_string (u32 *addr,const char *s)
Signature · 1 line
2.34.1
PATCH v5 3/5 x86/boot: Use boot_vid_info variable directly from C code
In reply to Frediano ZiglioNo more need to pass from assembly code.
---
Changes since v1:
- split the 2 variable changes into 2 commits.
Changes since v2:
- revert commit order.
--- xen/arch/x86/boot/build32.lds.S | 1 + xen/arch/x86/boot/head.S | 10 +--------- xen/arch/x86/boot/reloc.c | 13 +++++-------- 3 files changed, 7 insertions(+), 17 deletions(-)
xen/arch/x86/boot/build32.lds.S +1 -0
@@ -46,6 +46,7 @@ SECTIONS DECLARE_IMPORT(__trampoline_seg_start); DECLARE_IMPORT(__trampoline_seg_stop); DECLARE_IMPORT(trampoline_phys); + DECLARE_IMPORT(boot_vid_info); . = . + GAP; *(.text) *(.text.*)
xen/arch/x86/boot/head.S +1 -9
@@ -514,18 +514,10 @@ trampoline_setup: mov sym_esi(trampoline_phys), %ecx add $TRAMPOLINE_SPACE,%ecx -#ifdef CONFIG_VIDEO - lea sym_esi(boot_vid_info), %edx -#else - xor %edx, %edx -#endif - /* Save Multiboot / PVH info struct (after relocation) for later use. */ - push %edx /* Boot video info to be filled from MB2. */ mov %ebx, %edx /* Multiboot / PVH information address. */ - /* reloc(magic/eax, info/edx, trampoline/ecx, video/stk) using fastcall. */ + /* reloc(magic/eax, info/edx, trampoline/ecx) using fastcall. */ call reloc - add $4, %esp #ifdef CONFIG_PVH_GUEST cmpb $0, sym_esi(pvh_boot)
xen/arch/x86/boot/reloc.c +5 -8
@@ -176,7 +176,7 @@ static multiboot_info_t *mbi_reloc(uint32_t mbi_in, memctx *ctx)return mbi_out; } -static multiboot_info_t *mbi2_reloc (uint32_t mbi_in,uint32_t video_out,memctx *ctx) +static multiboot_info_t *mbi2_reloc (uint32_t mbi_in,memctx *ctx) {const multiboot2_fixed_t *mbi_fix =_p (mbi_in);const multiboot2_memory_map_t *mmap_src;
@@ -185,7 +185,7 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctxmemory_map_t *mmap_dst;multiboot_info_t *mbi_out;#ifdef CONFIG_VIDEO -struct boot_video_info *video =NULL ; +struct boot_video_info *video = &boot_vid_info;#endif uint32_t ptr;unsigned int i, mod_idx =0 ;
@@ -290,12 +290,11 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctx#ifdef CONFIG_VIDEO case MULTIBOOT2_TAG_TYPE_VBE : -if ( video_out ) +if ( video ) {const struct vesa_ctrl_info *ci;const struct vesa_mode_info *mi; - video =_p (video_out); ci = (const void *)get_mb2_data (tag, vbe, vbe_control_info); mi = (const void *)get_mb2_data (tag, vbe, vbe_mode_info);
@@ -321,7 +320,6 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctxif ( (get_mb2_data (tag, framebuffer, framebuffer_type) !=MULTIBOOT2_FRAMEBUFFER_TYPE_RGB ) ) { - video_out =0 ; video =NULL ; }break ;
@@ -346,8 +344,7 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctx }/* SAF-1-safe */ -void *reloc (uint32_t magic,uint32_t in,uint32_t trampoline, -uint32_t video_info) +void *reloc (uint32_t magic,uint32_t in,uint32_t trampoline) {memctx ctx = { trampoline };
@@ -357,7 +354,7 @@ void *reloc(uint32_t magic, uint32_t in, uint32_t trampoline,return mbi_reloc (in, &ctx);case MULTIBOOT2_BOOTLOADER_MAGIC : -return mbi2_reloc (in, video_info, &ctx); +return mbi2_reloc (in, &ctx);case XEN_HVM_START_MAGIC_VALUE :if (IS_ENABLED (CONFIG_PVH_GUEST ) )
Signature · 1 line
2.34.1
PATCH v5 1/5 x86/boot: create a C bundle for 32 bit boot code and use it
In reply to Frediano ZiglioThe current method to include 32 bit C boot code is:
- compile each function we want to use into a separate object file;
- each function is compiled with -fpic option;
- convert these object files to binary files. This operation removes GOP which we don't want in the executable;
- a small assembly part in each file add the entry point;
- code can't have external references, all possible variables are passed by value or pointer;
- include these binary files in head.S.
There are currently some limitations:
- code is compiled separately, it's not possible to share a function (like memcpy) between different functions to use;
- although code is compiled with -fpic there's no certainty there are no relocations, specifically data ones. This can lead into hard to find bugs;
- it's hard to add a simple function;
- having to pass external variables makes hard to do multiple things otherwise functions would require a lot of parameters so code would have to be split into multiple functions which is not easy.
Current change extends the current process:
- all object files are linked together before getting converted making possible to share code between the function we want to call;
- a single object file is generated with all functions to use and exported symbols to easily call;
- variables to use are declared in linker script and easily used inside C code. Declaring them manually could be annoying but makes also easier to check them. Using external pointers can be still an issue if they are not fixed. If an external symbol is not declared this gives a link error.
Some details of the implementation:
- C code is compiled with -fpic flags (as before);
- object files from C code are linked together;
- the single bundled object file is linked with 2 slightly different script files to generate 2 bundled object files;
- the 2 bundled object files are converted to binary removing the need for global offset tables;
- a Python script is used to generate assembly source from the 2 binaries;
- the single assembly file is compiled to generate final bundled object file;
- to detect possible unwanted relocation in data/code code is generated with different addresses. This is enforced starting .text section at different positions and adding a fixed "gap" at the beginning. This makes sure code and data is position independent;
- to detect used symbols in data/code symbols are placed in .text section at different offsets (based on the line in the linker script). This is needed as potentially a reference to a symbol is converted to a reference to the containing section so multiple symbols could be converted to reference to same symbol (section name) and we need to distinguish them;
- --orphan-handling=error option to linker is used to make sure we account for all possible sections from C code;
Current limitations:
- the main one is the lack of support for 64 bit code. It would make sure that even the code used for 64 bit (at the moment EFI code) is code and data position independent. We cannot assume that code that came from code compiled for 32 bit and compiled for 64 bit is code and data position independent, different compiler options lead to different code/data.
---
Changes since v2:
- removed W^X limitation, allowing data;
- added some comments to python script;
- added extension to python script;
- added header to generated assembly code from python script;
- added starting symbol to generated assembly code from python script to make disassembly more clear;
- other minor style changes to python script.
Changes since v4:
- add build32.final.lds build32.other.lds to targets macro;
- place some comments over a rule, not inside;
- simplified linking and producing binary rule;
- renamed built_in_32 to built-in-32, coding style;
- fix minor indentation;
- put magic numbers in Makefile and propagate them;
- minor variable clanups in Python script;
- add dependency to Python script.
---
xen/arch/x86/boot/.gitignore | 5 +-
xen/arch/x86/boot/Makefile | 45 +++-
.../x86/boot/{build32.lds => build32.lds.S} | 35 ++-
xen/arch/x86/boot/cmdline.c | 12 -
xen/arch/x86/boot/head.S | 12 -
xen/arch/x86/boot/reloc.c | 14 --
xen/tools/combine_two_binaries.py | 218 ++++++++++++++++++
7 files changed, 286 insertions(+), 55 deletions(-)
rename xen/arch/x86/boot/{build32.lds => build32.lds.S} (70%)
create mode 100755 xen/tools/combine_two_binaries.pyxen/arch/x86/boot/.gitignore +3 -2
@@ -1,3 +1,4 @@ /mkelf32 -/*.bin -/*.lnk +/build32.*.lds +/built-in-32.*.bin +/built-in-32.*.map
xen/arch/x86/boot/Makefile +35 -10
62 lines not on this page · raw message
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +30 -5
@@ -15,22 +15,47 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -ENTRY(_start) +#ifdef FINAL +# undef GAP +# define GAP 0 +# define MULT 0 +# define TEXT_START +#else +# define MULT 1 +# define TEXT_START TEXT_DIFF +#endif +#define DECLARE_IMPORT(name) name = . + (__LINE__ * MULT) + +ENTRY(dummy_start) SECTIONS { /* Merge code and data into one section. */ - .text : { + .text TEXT_START : { + /* Silence linker warning, we are not going to use it */ + dummy_start = .; + + /* Declare below any symbol name needed. + * Each symbol should be on its own line. + * It looks like a tedious work but we make sure the things we use. + * Potentially they should be all variables. */ + DECLARE_IMPORT(__base_relocs_start); + DECLARE_IMPORT(__base_relocs_end); + . = . + GAP; *(.text) *(.text.*) - *(.data) - *(.data.*) *(.rodata) *(.rodata.*) + *(.data) + *(.data.*) *(.bss) *(.bss.*) } - + /DISCARD/ : { + *(.comment) + *(.comment.*) + *(.note.*) + } /* Dynamic linkage sections. Collected simply so we can check they're empty. */ .got : { *(.got)
xen/arch/x86/boot/cmdline.c +0 -12
@@ -18,18 +18,6 @@* Linux kernel source (linux/lib/string.c). */ -/* -* This entry point is entered from xen/arch/x86/boot/head.S with: -* - %eax = &cmdline, -* - %edx = &early_boot_opts. -*/ -asm ( -" .text \n" -" .globl _start \n" -"_start: \n" -" jmp cmdline_parse_early \n" - ); -#include <xen/compiler.h> #include <xen/kconfig.h> #include <xen/macros.h>
xen/arch/x86/boot/head.S +0 -12
@@ -759,18 +759,6 @@ trampoline_setup: /* Jump into the relocated trampoline. */ lret - /* - * cmdline and reloc are written in C, and linked to be 32bit PIC with - * entrypoints at 0 and using the fastcall convention. - */ -FUNC_LOCAL(cmdline_parse_early) - .incbin "cmdline.bin" -END(cmdline_parse_early) - -FUNC_LOCAL(reloc) - .incbin "reloc.bin" -END(reloc) - ENTRY(trampoline_start) #include "trampoline.S" ENTRY(trampoline_end)
xen/arch/x86/boot/reloc.c +0 -14
@@ -12,20 +12,6 @@* Daniel Kiper <[email protected]> */ -/* -* This entry point is entered from xen/arch/x86/boot/head.S with: -* - %eax = MAGIC, -* - %edx = INFORMATION_ADDRESS, -* - %ecx = TOPMOST_LOW_MEMORY_STACK_ADDRESS. -* - 0x04(%esp) = BOOT_VIDEO_INFO_ADDRESS. -*/ -asm ( -" .text \n" -" .globl _start \n" -"_start: \n" -" jmp reloc \n" - ); -#include <xen/compiler.h> #include <xen/macros.h> #include <xen/types.h>
xen/tools/combine_two_binaries.py new file +218 -0
218 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v5 4/5 x86/boot: Use trampoline_phys variable directly from C code
In reply to Frediano ZiglioNo more need to pass from assembly code.
---
Changes since v1:
- split the 2 variable changes into 2 commits.
Changes since v2:
- revert commit order;
- avoid useless casts.
--- xen/arch/x86/boot/head.S | 6 +----- xen/arch/x86/boot/reloc.c | 8 ++++++-- 2 files changed, 7 insertions(+), 7 deletions(-)
xen/arch/x86/boot/head.S +1 -5
@@ -510,13 +510,9 @@ trampoline_setup: mov %esi, sym_esi(xen_phys_start) mov %esi, sym_esi(trampoline_xen_phys_start) - /* Get bottom-most low-memory stack address. */ - mov sym_esi(trampoline_phys), %ecx - add $TRAMPOLINE_SPACE,%ecx - /* Save Multiboot / PVH info struct (after relocation) for later use. */ mov %ebx, %edx /* Multiboot / PVH information address. */ - /* reloc(magic/eax, info/edx, trampoline/ecx) using fastcall. */ + /* reloc(magic/eax, info/edx) using fastcall. */ call reloc #ifdef CONFIG_PVH_GUEST
xen/arch/x86/boot/reloc.c +6 -2
@@ -19,6 +19,9 @@#include <xen/kconfig.h> #include <xen/multiboot.h> #include <xen/multiboot2.h> +#include <xen/page-size.h> + +#include <asm/trampoline.h> #include <public/arch-x86/hvm/start_info.h>
@@ -344,9 +347,10 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, memctx *ctx) }/* SAF-1-safe */ -void *reloc (uint32_t magic,uint32_t in,uint32_t trampoline) +void *reloc (uint32_t magic,uint32_t in) { -memctx ctx = { trampoline }; +/* Get bottom-most low-memory stack address. */ +memctx ctx = { trampoline_phys +TRAMPOLINE_SPACE };switch ( magic ) {
Signature · 1 line
2.34.1
PATCH v5 5/5 x86/boot: Clarify comment
In reply to Frediano Ziglio--- xen/arch/x86/boot/reloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/boot/reloc.c +1 -1
@@ -65,7 +65,7 @@ typedef struct memctx {/* * Simple bump allocator. * -* It starts from the base of the trampoline and allocates downwards. +* It starts on top of space reserved for the trampoline and allocates downwards. */ uint32_t ptr; } memctx;
Signature · 1 line
2.34.1
Re: [PATCH v5 2/5] x86/boot: Reuse code to relocate trampoline
In reply to Frediano ZiglioQuoted 11 lines · Frediano Ziglio
xen/arch/x86/boot/Makefile +1
-0@@ -1,12 +1,16 @@ obj-bin-y += head.o obj-bin-y += built-in-32.o +obj-bin-y += $(obj64) obj32 := cmdline.32.o obj32 += reloc.32.o
xen/arch/x86/boot/Makefile line 13
Quoted 10 lines
xen/arch/x86/boot/Makefile +6
-3+obj32 += reloc-trampoline.32.o -nocov-y += $(obj32) -noubsan-y += $(obj32) -targets += $(obj32) +obj64 := reloc-trampoline.o + +nocov-y += $(obj32) $(obj64) +noubsan-y += $(obj32) $(obj64) +targets += $(obj32) $(obj64)
Per Anthony's feedback, targets doesn't need to get $(obj64), because they're listed in obj-bin-y already.
Can be fixed on commit if everything else is is ready.
~Andrew
lib32 followup (visibility issue and/or toolchain bug?)
In reply to Frediano ZiglioHello,
Preempting some future work I'm expecting to arrive, I had a go at using __builtin_*() in obj32.
This is formed of 2 patches on top of this series:
https://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=shortlog;h=refs/heads/xen-lib32
Patch 1 introduces lib32 beside obj32, with strlen() being the first broken-out function, and patch 2 swaps to __builtin_strlen().
Both compile, but the difference that patch 2 introduces was unexpected.
With just lib32, and taking strsubcmp() as an example, we get:
00000000 <strsubcmp>:
0: 83 ec 0c sub $0xc,%esp 3: 89 5c 24 04 mov %ebx,0x4(%esp) 7: 89 74 24 08 mov %esi,0x8(%esp) b: 89 c6 mov %eax,%esi d: 89 d3 mov %edx,%ebx f: 89 d0 mov %edx,%eax 11: /-- e8 fc ff ff ff call 12 <strsubcmp+0x12> 12: R_386_PC32 strlen
16: 89 c1 mov %eax,%ecx 18: 89 da mov %ebx,%edx 1a: 89 f0 mov %esi,%eax 1c: /-- e8 fc ff ff ff call 1d <strsubcmp+0x1d> 1d: R_386_PC32 .text.strncmp
21: 8b 5c 24 04 mov 0x4(%esp),%ebx 25: 8b 74 24 08 mov 0x8(%esp),%esi 29: 83 c4 0c add $0xc,%esp 2c: c3 ret
which all seems fine. We get a plain PC32 relocation to strlen (which is now in the separate library).
However, with patch 2 in place (simply swapping the plain extern for __builtin_strlen(), we now get:
00000000 <strsubcmp>:
0: 83 ec 0c sub $0xc,%esp 3: 89 1c 24 mov %ebx,(%esp) 6: 89 74 24 04 mov %esi,0x4(%esp) a: 89 7c 24 08 mov %edi,0x8(%esp) e: /-- e8 fc ff ff ff call f <strsubcmp+0xf>
f: R_386_PC32 __x86.get_pc_thunk.bx
13: 81 c3 02 00 00 00 add $0x2,%ebx 15: R_386_GOTPC _GLOBAL_OFFSET_TABLE_ 19: 89 c7 mov %eax,%edi 1b: 89 d6 mov %edx,%esi 1d: 89 d0 mov %edx,%eax 1f: /-- e8 fc ff ff ff call 20 <strsubcmp+0x20> 20: R_386_PLT32 strlen
24: 89 c1 mov %eax,%ecx 26: 89 f2 mov %esi,%edx 28: 89 f8 mov %edi,%eax 2a: /-- e8 fc ff ff ff call 2b <strsubcmp+0x2b> 2b: R_386_PC32 .text.strncmp
2f: 8b 1c 24 mov (%esp),%ebx 32: 8b 74 24 04 mov 0x4(%esp),%esi 36: 8b 7c 24 08 mov 0x8(%esp),%edi 3a: 83 c4 0c add $0xc,%esp 3d: c3 ret
The builtin hasn't managed to optimise away the call to strlen (that's fine). But, we've ended up spilling %ebx to the stack, calculating the location of the GOT and not using it.
So, as it stands, trying to use __builtin_strlen() results in worse code generation. One thing I noticed was that we're not passing -fvisibility=hidden into CFLAGS_x86_32, but fixing that doesn't help either. We do have the pragma from compiler.h, so I'm out of visibility ideas.
Anything else I've missed?
~Andrew
Re: lib32 followup (visibility issue and/or toolchain bug?)
Quoted 9 lines · Andrew Cooper
Hello,
Preempting some future work I'm expecting to arrive, I had a go at using __builtin_*() in obj32.
This is formed of 2 patches on top of this series:
https://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=shortlog;h=refs/heads/xen-lib32
You are confident we'll have a lot of shared code to need an additional "lib32" in the Makefile!
I would personally stick with obj32.
Note that file should be strlen.c, not strlen.32.c, otherwise you are possibly going to pick up the general rule and not the one in the Makefile (but maybe is what you wanted).
Quoted 33 lines
Patch 1 introduces lib32 beside obj32, with
strlen()being the first broken-out function, and patch 2 swaps to__builtin_strlen().Both compile, but the difference that patch 2 introduces was unexpected.
With just lib32, and taking
strsubcmp()as an example, we get:00000000 <strsubcmp>:
0: 83 ec 0c sub $0xc,%esp 3: 89 5c 24 04 mov %ebx,0x4(%esp) 7: 89 74 24 08 mov %esi,0x8(%esp) b: 89 c6 mov %eax,%esi d: 89 d3 mov %edx,%ebx f: 89 d0 mov %edx,%eax 11: /-- e8 fc ff ff ff call 12 <strsubcmp+0x12> 12: R_386_PC32 strlen 16: 89 c1 mov %eax,%ecx 18: 89 da mov %ebx,%edx 1a: 89 f0 mov %esi,%eax 1c: /-- e8 fc ff ff ff call 1d <strsubcmp+0x1d> 1d: R_386_PC32 .text.strncmp 21: 8b 5c 24 04 mov 0x4(%esp),%ebx 25: 8b 74 24 08 mov 0x8(%esp),%esi 29: 83 c4 0c add $0xc,%esp 2c: c3 retwhich all seems fine. We get a plain PC32 relocation to strlen (which is now in the separate library).
However, with patch 2 in place (simply swapping the plain extern for
__builtin_strlen(), we now get:00000000 <strsubcmp>:
Quoted 13 lines
0: 83 ec 0c sub $0xc,%esp
3: 89 1c 24 mov %ebx,(%esp)
6: 89 74 24 04 mov %esi,0x4(%esp)
a: 89 7c 24 08 mov %edi,0x8(%esp)
e: /-- e8 fc ff ff ff call f <strsubcmp+0xf>
f: R_386_PC32 __x86.get_pc_thunk.bx
13: 81 c3 02 00 00 00 add $0x2,%ebx
15: R_386_GOTPC _GLOBAL_OFFSET_TABLE_
19: 89 c7 mov %eax,%edi
1b: 89 d6 mov %edx,%esi
1d: 89 d0 mov %edx,%eax
1f: /-- e8 fc ff ff ff call 20 <strsubcmp+0x20>
20: R_386_PLT32 strlenPLT means it not declared hidden, otherwise it would have used the relative relocation.
Maybe
size_t strlen(const char *s);
#define strlen(s) __builtin_strlen(s)
xen/compiler.h is included, so all declaration should get the hidden by default ? Or add __attribute__((visibility("hidden"))) explicitly.
Quoted 12 lines
24: 89 c1 mov %eax,%ecx
26: 89 f2 mov %esi,%edx
28: 89 f8 mov %edi,%eax
2a: /-- e8 fc ff ff ff call 2b <strsubcmp+0x2b>
2b: R_386_PC32 .text.strncmp
2f: 8b 1c 24 mov (%esp),%ebx
32: 8b 74 24 04 mov 0x4(%esp),%esi
36: 8b 7c 24 08 mov 0x8(%esp),%edi
3a: 83 c4 0c add $0xc,%esp
3d: c3 retQuoted 4 lines
The builtin hasn't managed to optimise away the call to strlen (that's fine). But, we've ended up spilling %ebx to the stack, calculating the location of the GOT and not using it.
Maybe the ABI for PLT is to have %ebx set to the GOT ? Not sure about it.
Quoted 6 lines
So, as it stands, trying to use
__builtin_strlen()results in worse code generation. One thing I noticed was that we're not passing -fvisibility=hidden intoCFLAGS_x86_32, but fixing that doesn't help either. We do have the pragma fromcompiler.h, so I'm out of visibility ideas.
The -fvisibility=hidden should be overridden by the xen/compiler.h; but should be overridden with hidden!
Maybe strlen is defined by default with another visibility? If you generate the assembly, you should see if the strlen symbol gets the .hidden bless or not.
Quoted 2 lines
Anything else I've missed?
Coffee :-)
Frediano
Re: lib32 followup (visibility issue and/or toolchain bug?)
Quoted 102 lines · Frediano Ziglio
Quoted 9 lines · Andrew Cooper
Hello,
Preempting some future work I'm expecting to arrive, I had a go at using __builtin_*() in obj32.
This is formed of 2 patches on top of this series:
https://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=shortlog;h=refs/heads/xen-lib32You are confident we'll have a lot of shared code to need an additional "lib32" in the
Makefile!
I would personally stick with obj32.
Note that file should bestrlen.c, notstrlen.32.c, otherwise you are possibly going to pick up the general rule and not the one in theMakefile(but maybe is what you wanted).Quoted 33 lines
Patch 1 introduces lib32 beside obj32, with
strlen()being the first broken-out function, and patch 2 swaps to__builtin_strlen().Both compile, but the difference that patch 2 introduces was unexpected.
With just lib32, and taking
strsubcmp()as an example, we get:00000000 <strsubcmp>:
0: 83 ec 0c sub $0xc,%esp 3: 89 5c 24 04 mov %ebx,0x4(%esp) 7: 89 74 24 08 mov %esi,0x8(%esp) b: 89 c6 mov %eax,%esi d: 89 d3 mov %edx,%ebx f: 89 d0 mov %edx,%eax 11: /-- e8 fc ff ff ff call 12 <strsubcmp+0x12> 12: R_386_PC32 strlen 16: 89 c1 mov %eax,%ecx 18: 89 da mov %ebx,%edx 1a: 89 f0 mov %esi,%eax 1c: /-- e8 fc ff ff ff call 1d <strsubcmp+0x1d> 1d: R_386_PC32 .text.strncmp 21: 8b 5c 24 04 mov 0x4(%esp),%ebx 25: 8b 74 24 08 mov 0x8(%esp),%esi 29: 83 c4 0c add $0xc,%esp 2c: c3 retwhich all seems fine. We get a plain PC32 relocation to strlen (which is now in the separate library).
However, with patch 2 in place (simply swapping the plain extern for
__builtin_strlen(), we now get:00000000 <strsubcmp>:
Quoted 13 lines
0: 83 ec 0c sub $0xc,%esp 3: 89 1c 24 mov %ebx,(%esp) 6: 89 74 24 04 mov %esi,0x4(%esp) a: 89 7c 24 08 mov %edi,0x8(%esp) e: /-- e8 fc ff ff ff call f <strsubcmp+0xf> f: R_386_PC32 __x86.get_pc_thunk.bx 13: 81 c3 02 00 00 00 add $0x2,%ebx 15: R_386_GOTPC _GLOBAL_OFFSET_TABLE_ 19: 89 c7 mov %eax,%edi 1b: 89 d6 mov %edx,%esi 1d: 89 d0 mov %edx,%eax 1f: /-- e8 fc ff ff ff call 20 <strsubcmp+0x20> 20: R_386_PLT32 strlenPLT means it not declared hidden, otherwise it would have used the relative relocation.
Maybe
size_tstrlen(const char *s);
#define strlen(s)__builtin_strlen(s)
xen/compiler.his included, so all declaration should get the hidden by default ? Or add __attribute__((visibility("hidden"))) explicitly.Quoted 12 lines
24: 89 c1 mov %eax,%ecx 26: 89 f2 mov %esi,%edx 28: 89 f8 mov %edi,%eax 2a: /-- e8 fc ff ff ff call 2b <strsubcmp+0x2b> 2b: R_386_PC32 .text.strncmp 2f: 8b 1c 24 mov (%esp),%ebx 32: 8b 74 24 04 mov 0x4(%esp),%esi 36: 8b 7c 24 08 mov 0x8(%esp),%edi 3a: 83 c4 0c add $0xc,%esp 3d: c3 retQuoted 4 lines
The builtin hasn't managed to optimise away the call to strlen (that's fine). But, we've ended up spilling %ebx to the stack, calculating the location of the GOT and not using it.
Maybe the ABI for PLT is to have %ebx set to the GOT ? Not sure about it.
Quoted 6 lines
So, as it stands, trying to use
__builtin_strlen()results in worse code generation. One thing I noticed was that we're not passing -fvisibility=hidden intoCFLAGS_x86_32, but fixing that doesn't help either. We do have the pragma fromcompiler.h, so I'm out of visibility ideas.
Quoted 6 lines
The -fvisibility=hidden should be overridden by the
xen/compiler.h; but should be overridden with hidden!
Maybe strlen is defined by default with another visibility? If you generate the assembly, you should see if the strlen symbol gets the .hidden bless or not.
I did some more experiments, but I didn't manage to fix the issue. It looks like when __builtin_strlen fallback to calling strlen it uses a private declaration of strlen. -mregparam argument is taken into account, but not visibility. I tried to declare strlen as hidden (checking also preprocessor output to see if other declaration were present, none found), still the @PLT. I tried to add a "static inline strlen"... and it was not used.
I found this workaround:
xen/arch/x86/boot/cmdline.c +0 -0
@@ -51,7 +51,12 @@ static const char delim_chars_comma[] = ", \n\r\t";
#define delim_chars (delim_chars_comma + 1)
-#define strlen(s) __builtin_strlen(s)
+size_t strlen(const char *s);
+#define strlen(str) \
+ (__extension__ (__builtin_constant_p(str) \
+ ? __builtin_strlen(str) \
+ : strlen(str)))
+
static int strncmp(const char *cs, const char *ct, size_t count) {
xen/arch/x86/boot/strlen.c renamed from xen/arch/x86/boot/strlen.32.c +0 -0
Frediano
Re: lib32 followup (visibility issue and/or toolchain bug?)
In reply to Frediano ZiglioQuoted 87 lines · Frediano Ziglio
Quoted 9 lines · Andrew Cooper
Hello,
Preempting some future work I'm expecting to arrive, I had a go at using __builtin_*() in obj32.
This is formed of 2 patches on top of this series:
https://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=shortlog;h=refs/heads/xen-lib32You are confident we'll have a lot of shared code to need an additional "lib32" in the
Makefile!
I would personally stick with obj32.
Note that file should bestrlen.c, notstrlen.32.c, otherwise you are possibly going to pick up the general rule and not the one in theMakefile(but maybe is what you wanted).Quoted 33 lines
Patch 1 introduces lib32 beside obj32, with
strlen()being the first broken-out function, and patch 2 swaps to__builtin_strlen().Both compile, but the difference that patch 2 introduces was unexpected.
With just lib32, and taking
strsubcmp()as an example, we get:00000000 <strsubcmp>:
0: 83 ec 0c sub $0xc,%esp 3: 89 5c 24 04 mov %ebx,0x4(%esp) 7: 89 74 24 08 mov %esi,0x8(%esp) b: 89 c6 mov %eax,%esi d: 89 d3 mov %edx,%ebx f: 89 d0 mov %edx,%eax 11: /-- e8 fc ff ff ff call 12 <strsubcmp+0x12> 12: R_386_PC32 strlen 16: 89 c1 mov %eax,%ecx 18: 89 da mov %ebx,%edx 1a: 89 f0 mov %esi,%eax 1c: /-- e8 fc ff ff ff call 1d <strsubcmp+0x1d> 1d: R_386_PC32 .text.strncmp 21: 8b 5c 24 04 mov 0x4(%esp),%ebx 25: 8b 74 24 08 mov 0x8(%esp),%esi 29: 83 c4 0c add $0xc,%esp 2c: c3 retwhich all seems fine. We get a plain PC32 relocation to strlen (which is now in the separate library).
However, with patch 2 in place (simply swapping the plain extern for
__builtin_strlen(), we now get:00000000 <strsubcmp>:
Quoted 13 lines
0: 83 ec 0c sub $0xc,%esp 3: 89 1c 24 mov %ebx,(%esp) 6: 89 74 24 04 mov %esi,0x4(%esp) a: 89 7c 24 08 mov %edi,0x8(%esp) e: /-- e8 fc ff ff ff call f <strsubcmp+0xf> f: R_386_PC32 __x86.get_pc_thunk.bx 13: 81 c3 02 00 00 00 add $0x2,%ebx 15: R_386_GOTPC _GLOBAL_OFFSET_TABLE_ 19: 89 c7 mov %eax,%edi 1b: 89 d6 mov %edx,%esi 1d: 89 d0 mov %edx,%eax 1f: /-- e8 fc ff ff ff call 20 <strsubcmp+0x20> 20: R_386_PLT32 strlenPLT means it not declared hidden, otherwise it would have used the relative relocation.
Maybe
size_tstrlen(const char *s);
#define strlen(s)__builtin_strlen(s)
xen/compiler.his included, so all declaration should get the hidden by default ? Or add __attribute__((visibility("hidden"))) explicitly.Quoted 12 lines
24: 89 c1 mov %eax,%ecx 26: 89 f2 mov %esi,%edx 28: 89 f8 mov %edi,%eax 2a: /-- e8 fc ff ff ff call 2b <strsubcmp+0x2b> 2b: R_386_PC32 .text.strncmp 2f: 8b 1c 24 mov (%esp),%ebx 32: 8b 74 24 04 mov 0x4(%esp),%esi 36: 8b 7c 24 08 mov 0x8(%esp),%edi 3a: 83 c4 0c add $0xc,%esp 3d: c3 ret
Quoted 5 lines
Quoted 3 lines
The builtin hasn't managed to optimise away the call to strlen (that's fine). But, we've ended up spilling %ebx to the stack, calculating the location of the GOT and not using it.
Maybe the ABI for PLT is to have %ebx set to the GOT ? Not sure about it.
Yes, PIC PLT entries use %ebx.
Quoted 11 lines
Quoted 5 lines
So, as it stands, trying to use
__builtin_strlen()results in worse code generation. One thing I noticed was that we're not passing -fvisibility=hidden intoCFLAGS_x86_32, but fixing that doesn't help either. We do have the pragma fromcompiler.h, so I'm out of visibility ideas.The -fvisibility=hidden should be overridden by the
xen/compiler.h; but should be overridden with hidden!
Maybe strlen is defined by default with another visibility? If you generate the assembly, you should see if the strlen symbol gets the .hidden bless or not.
Only defined symbols have .hidden emitted for them, afaics. But that also doesn't matter much, as it's the emission of the @plt on the calls which requires the setting up of %ebx prior to those calls. (Arguably in 32-bit code, where all addresses are reachable, "hidden" could serve as a hint that calls don't need to go through the PLT. This maybe considered a missed optimization opportunity.) There's no difference there between calling strlen() or e.g. a custom (extern) StrLen().
And btw, reloc.c and cmdline.c currently also compile to code using __x86.get_pc_thunk.* (and @gotoff relocations for data references).
Jan