1

I'm working on assembly language (NASM), and I am currently rewriting some functions such as strlen and strncmp for example, and I'm trying do redo strstr.

I made it in C to see how it works and I came with something like :

char    *my_strstr(char *str, char *match)
{
    int i = -1;
    int len = strlen(match);
    while (str[++i] != '\0')
          if (strncmp((str + i), match, len) == 0)
             return (str + i);
    return (NULL);
}

I've got strlen NASM function and my strncmp NASM function. in my header.asm I have :

section .text
    global strlen:function
    global strcmp:function
    global strstr:function

And now the file where I want to call those both function that I did earlier (no check for NULL str either) It probably not work but... that's not the problem

    %include "src/header.asm"
    extern strlen
    extern strncmp

strstr:
    ;; bla bla bla
    call strlen
    mov  rdx, rax ; the return value of strlen is in rax right ?
    ;; bla bla bla
    call strncmp ;
    cmp rax, 0h ; the return value still is in rax right ?
    je bla bla bla
    ;; bla bla bla

And when I compile I got the following error :

srcs/strstr.asm:2: error: no special symbol features supported here
srcs/strstr.asm:3: error: no special symbol features supported here

How to proceed to use those functions ?

I use my dynamic library libasm.so (containing those functions) used by my C files. The standard library doesn't exist anymore, the compilation bypass the stdlib. My compilation command is : gcc main.c -Wl,-rpath=. -L. -lasm -fno-builtin

Thanks !!

void
  • 407
  • 6
  • 18
  • for x86 must be `_strlen` (or better `__imp__strlen`) and `_strncmp` (`__imp__strncmp`) symbols. and of course you need use .lib file where this symbols defined – RbMm Mar 22 '17 at 13:22
  • I forgot to tell you that I use my dynamic library libasm.so (containing those functions) used by my C files. The standard library doesn't exist anymore, the compilation bypass the stdlib. My compilation command is : gcc main.c -Wl,-rpath=. -L. -lasm -fno-builtin – void Mar 22 '17 at 13:54
  • so search this functions by name in `libasm.so` - name must be exactly much – RbMm Mar 22 '17 at 13:58
  • Are `strlen` and `strncmp` defined in the same asm file as strstr? If not you are going to need to tell NASM that those labels are external. What happens if you add `extern strncmp` and `extern strlen` ? `global` only exports labels, and `extern` says certain labels are available outside the current asm file. – Michael Petch Mar 22 '17 at 14:23
  • thank you for you answer, strlen, strstr and strcmp are not implemented in the same ASM file. When I add the extern keyword (strlen, strncmp) in my assembly file (strstr.asm), I have the following error: "No special symbol features supported here". And, if I add the extern keyword (_strlen, _strncmp), I have the following error: "symbol ... undefined". – void Mar 22 '17 at 14:43
  • Can you show me exactly what you used for your extern lines? You don't put `_` on ELF/Linux.If you were developing non-ELF program on Windows that would be required. I hope you didn't put `:function` on the end of those `extern` lines. – Michael Petch Mar 22 '17 at 14:51
  • Thank you for your answer, for readability, I have updated my post, please refer to the strstr.asm file (and also the compilation output) – void Mar 22 '17 at 14:59
  • @RbMm Only if OP is programming on Windows. On UNIX-like systems, this is not the case. – fuz Mar 22 '17 at 16:48

1 Answers1

1

You have to put in the assembly that the function is extern and put an underscore at the beginning of the function when specifying it's external

extern _vd
section .code
global main
           _main:
           call _vd

For anyone using C++ instead of C, you have to mark the function with extern "C" to turn off name-wrangling by the compiler. (See here for more)

#include <stdio.h>
#include <windows.h>

extern "C" void vd()
{
        printf("Hello world");
        ExitProcess(1);
}
Fabian Sievert
  • 810
  • 5
  • 15
Rehd_x89
  • 11
  • 1
  • 2
    That's normal; C++ uses name-mangling to generate unique asm symbols for `foo(int)` vs. `foo(char)` etc. The question mentions C not C++ so no need to go into a lot of detail. – Peter Cordes Mar 14 '23 at 00:30