Skip to main content
Make title describe what code does per site convention - see https://codereview.stackexchange.com/help/how-to-ask
Source Link

How do it better - choosing best opimized library variant durigduring application startup

I wrote some PoC for load library with best optimization version during application startup. The goal for it check in _initthe _init function code checking by cpuid available opcode extensions and load "the best" variant. I only don't know, how to avoid linking bypass.

Part of selecting procedure:

x86_sse41_plus:
    test ecx, 0x00100000
    jnz x86_sse42_plus
    mov edx, lib_x86_sse41 wrt ..gotoff
    jmp load_lib
x86_sse42_plus:
    test ecx, 0x10000000
    jnz x86_avx_plus
    mov edx, lib_x86_sse42 wrt ..gotoff
    jmp load_lib
x86_avx_plus:
    mov edx, lib_x86_avx wrt ..gotoff
load_lib:
    call _init_pc
_init_pc:
    pop ebx
    lea ebx, [ebx + _GLOBAL_OFFSET_TABLE_+$$-_init_pc wrt ..gotpc]
    lea eax, [ebx + edx]

    sub esp, 8
    push dword 0x0110A
    push eax
    call dlopen wrt ..plt
    add esp, 16
    test eax, eax
    jz _init_end
    sub esp, 12
    push eax
    call dlclose wrt ..plt
    add esp, 16
_init_end:
    pop ebx
    ret

In Makefile I made linking in that way e.g.:

${BINOUT}/libkitdynload_x86_sse42.so:   ${OBJECTS_COMMON} ${OBJECTS_X86_SSE42}
    @echo ld libkitdynload_x86_sse42.so
    @ld -m elf_i386 -shared -o $@ ${OBJECTS_COMMON} ${OBJECTS_X86_SSE42} ${LDFLAGS} -T ${SRCDIR}/linker.ld

example application linking:

${BINOUT}/test: ${SRCDIR}/test.c
    @echo cc test
    @${CC} -m32 ${CFLAGS} -L${BINOUT}/link -o ${BINOUT}/test ${SRCDIR}/test.c -lkitdynload

Part of this bypass is that:

-L${BINOUT}/link

I have to link in that directory any of optimized variant and linking with that fs symbolic link.

Simple application:

#include "version.h"
#include <stdio.h>

int main(int argc, char * argv)
{
  printf("%s %s %s\n", get_kit_dynload_name(), get_kit_dynload_version_str(),
      get_kit_dynload_variant_str());
  return 0;
}

During dynamic linking code will select "the best" variant transparent.

Full code version is available here: https://github.com/jakubjuszczakiewicz/kit-dynload-poc/tree/main/kit-dynloadhere on github

How can it be coded better?

How do it better - choosing best opimized library variant durig application startup

I wrote some PoC for load library with best optimization version during application startup. The goal for it check in _init function code checking by cpuid available opcode extensions and load "the best" variant. I only don't know, how to avoid linking bypass.

Part of selecting procedure:

x86_sse41_plus:
    test ecx, 0x00100000
    jnz x86_sse42_plus
    mov edx, lib_x86_sse41 wrt ..gotoff
    jmp load_lib
x86_sse42_plus:
    test ecx, 0x10000000
    jnz x86_avx_plus
    mov edx, lib_x86_sse42 wrt ..gotoff
    jmp load_lib
x86_avx_plus:
    mov edx, lib_x86_avx wrt ..gotoff
load_lib:
    call _init_pc
_init_pc:
    pop ebx
    lea ebx, [ebx + _GLOBAL_OFFSET_TABLE_+$$-_init_pc wrt ..gotpc]
    lea eax, [ebx + edx]

    sub esp, 8
    push dword 0x0110A
    push eax
    call dlopen wrt ..plt
    add esp, 16
    test eax, eax
    jz _init_end
    sub esp, 12
    push eax
    call dlclose wrt ..plt
    add esp, 16
_init_end:
    pop ebx
    ret

In Makefile I made linking in that way e.g.:

${BINOUT}/libkitdynload_x86_sse42.so:   ${OBJECTS_COMMON} ${OBJECTS_X86_SSE42}
    @echo ld libkitdynload_x86_sse42.so
    @ld -m elf_i386 -shared -o $@ ${OBJECTS_COMMON} ${OBJECTS_X86_SSE42} ${LDFLAGS} -T ${SRCDIR}/linker.ld

example application linking:

${BINOUT}/test: ${SRCDIR}/test.c
    @echo cc test
    @${CC} -m32 ${CFLAGS} -L${BINOUT}/link -o ${BINOUT}/test ${SRCDIR}/test.c -lkitdynload

Part of this bypass is that:

-L${BINOUT}/link

I have to link in that directory any of optimized variant and linking with that fs symbolic link.

Simple application:

#include "version.h"
#include <stdio.h>

int main(int argc, char * argv)
{
  printf("%s %s %s\n", get_kit_dynload_name(), get_kit_dynload_version_str(),
      get_kit_dynload_variant_str());
  return 0;
}

During dynamic linking code will select "the best" variant transparent.

Full code version is available here: https://github.com/jakubjuszczakiewicz/kit-dynload-poc/tree/main/kit-dynload

library variant during application startup

I wrote some PoC for load library with best optimization version during application startup. The goal for it check in the _init function code checking by cpuid available opcode extensions and load "the best" variant. I only don't know, how to avoid linking bypass.

Part of selecting procedure:

x86_sse41_plus:
    test ecx, 0x00100000
    jnz x86_sse42_plus
    mov edx, lib_x86_sse41 wrt ..gotoff
    jmp load_lib
x86_sse42_plus:
    test ecx, 0x10000000
    jnz x86_avx_plus
    mov edx, lib_x86_sse42 wrt ..gotoff
    jmp load_lib
x86_avx_plus:
    mov edx, lib_x86_avx wrt ..gotoff
load_lib:
    call _init_pc
_init_pc:
    pop ebx
    lea ebx, [ebx + _GLOBAL_OFFSET_TABLE_+$$-_init_pc wrt ..gotpc]
    lea eax, [ebx + edx]

    sub esp, 8
    push dword 0x0110A
    push eax
    call dlopen wrt ..plt
    add esp, 16
    test eax, eax
    jz _init_end
    sub esp, 12
    push eax
    call dlclose wrt ..plt
    add esp, 16
_init_end:
    pop ebx
    ret

In Makefile I made linking in that way e.g.:

${BINOUT}/libkitdynload_x86_sse42.so:   ${OBJECTS_COMMON} ${OBJECTS_X86_SSE42}
    @echo ld libkitdynload_x86_sse42.so
    @ld -m elf_i386 -shared -o $@ ${OBJECTS_COMMON} ${OBJECTS_X86_SSE42} ${LDFLAGS} -T ${SRCDIR}/linker.ld

example application linking:

${BINOUT}/test: ${SRCDIR}/test.c
    @echo cc test
    @${CC} -m32 ${CFLAGS} -L${BINOUT}/link -o ${BINOUT}/test ${SRCDIR}/test.c -lkitdynload

Part of this bypass is that:

-L${BINOUT}/link

I have to link in that directory any of optimized variant and linking with that fs symbolic link.

Simple application:

#include "version.h"
#include <stdio.h>

int main(int argc, char * argv)
{
  printf("%s %s %s\n", get_kit_dynload_name(), get_kit_dynload_version_str(),
      get_kit_dynload_variant_str());
  return 0;
}

During dynamic linking code will select "the best" variant transparent.

Full code version is available here on github

How can it be coded better?

Source Link

How do it better - choosing best opimized library variant durig application startup

I wrote some PoC for load library with best optimization version during application startup. The goal for it check in _init function code checking by cpuid available opcode extensions and load "the best" variant. I only don't know, how to avoid linking bypass.

Part of selecting procedure:

x86_sse41_plus:
    test ecx, 0x00100000
    jnz x86_sse42_plus
    mov edx, lib_x86_sse41 wrt ..gotoff
    jmp load_lib
x86_sse42_plus:
    test ecx, 0x10000000
    jnz x86_avx_plus
    mov edx, lib_x86_sse42 wrt ..gotoff
    jmp load_lib
x86_avx_plus:
    mov edx, lib_x86_avx wrt ..gotoff
load_lib:
    call _init_pc
_init_pc:
    pop ebx
    lea ebx, [ebx + _GLOBAL_OFFSET_TABLE_+$$-_init_pc wrt ..gotpc]
    lea eax, [ebx + edx]

    sub esp, 8
    push dword 0x0110A
    push eax
    call dlopen wrt ..plt
    add esp, 16
    test eax, eax
    jz _init_end
    sub esp, 12
    push eax
    call dlclose wrt ..plt
    add esp, 16
_init_end:
    pop ebx
    ret

In Makefile I made linking in that way e.g.:

${BINOUT}/libkitdynload_x86_sse42.so:   ${OBJECTS_COMMON} ${OBJECTS_X86_SSE42}
    @echo ld libkitdynload_x86_sse42.so
    @ld -m elf_i386 -shared -o $@ ${OBJECTS_COMMON} ${OBJECTS_X86_SSE42} ${LDFLAGS} -T ${SRCDIR}/linker.ld

example application linking:

${BINOUT}/test: ${SRCDIR}/test.c
    @echo cc test
    @${CC} -m32 ${CFLAGS} -L${BINOUT}/link -o ${BINOUT}/test ${SRCDIR}/test.c -lkitdynload

Part of this bypass is that:

-L${BINOUT}/link

I have to link in that directory any of optimized variant and linking with that fs symbolic link.

Simple application:

#include "version.h"
#include <stdio.h>

int main(int argc, char * argv)
{
  printf("%s %s %s\n", get_kit_dynload_name(), get_kit_dynload_version_str(),
      get_kit_dynload_variant_str());
  return 0;
}

During dynamic linking code will select "the best" variant transparent.

Full code version is available here: https://github.com/jakubjuszczakiewicz/kit-dynload-poc/tree/main/kit-dynload