Menu

How to link assembled object? (lack of underscore causes `undefined reference')

Help
oset jan
2020-05-24
2020-05-26
  • oset jan

    oset jan - 2020-05-24

    Hello.

    I found this md5 implementation in assembly and tried to test it. So I compiled and got error. So then I tried to compile files separately without linking and it went well but linking thrown error again:

    $ make md5-test-fast-x86  
    gcc -c -std=c99 -O2 md5-test.c
    gcc -c -O1 md5-fast-x86.S
    gcc  -o md5-test-fast-x86 md5-test.o md5-fast-x86.o
    md5-test.o:md5-test.c:(.text+0x4d): undefined reference to `md5_compress'
    md5-test.o:md5-test.c:(.text+0xd5): undefined reference to `md5_compress'
    md5-test.o:md5-test.c:(.text+0x13a): undefined reference to `md5_compress'
    D:/.../mingw32-8.1.0/.../ld.exe: md5-test.o: bad reloc address 0x130 in section `.rdata'ect and there is 
    D:/.../mingw32-8.1.0/.../ld.exe: final link failed: Invalid operation
    collect2.exe: error: ld returned 1 exit status
    Makefile:45: recipe for target 'md5-test-fast-x86' failed
    make: *** [md5-test-fast-x86] Error 1
    

    I testes it under linux and it works without any such errors
    First I thought it was something wrong with ld.exe: md5-test.o: bad reloc address 0x130 in section '.rdata' but soon I realised it's undefined reference that matters. So I chceked obj and there is md5_compress method so I don't get why it won't link. Then I checked C vesion of the same routine and it linked, no problem. So again I checked and there is _md5_compress exported in it.

    Now, I guess, compiler adds underscore to compiled function name/s but why it won't add it to assembled version? And how to add it so it links?

     

    Last edit: oset jan 2020-05-24
  • Sam Tansy

    Sam Tansy - 2020-05-24

    I took a look at it and there is no prefixed underscore in assembled object.

    $ nm md5-test.o md5-fast-x86.o 
    
    md5-test.o:
    00000000 b .bss
    00000000 d .data
    (...)
    00000000 t .text
             U ___main
             U _calloc
    (...)
    00000000 T _main
             U _md5_compress ; <- that is what is "expected" in main()
    00000000 T _md5_hash
             U _memcmp
    (...)
    
    md5-fast-x86.o:
    00000000 b .bss
    00000000 d .data
    00000000 t .text
    00000000 T md5_compress ; <- that is what is provided in md5-fast-x86.o
    

    You could try to add -fleading-underscore as option to gcc but despite the claim in documentation it doens't work, I tell you that.
    You could add something to force adding leading underscore to name of the function in md5-fast-x86.S, like this:

    #if defined(_WIN32)
    # define cdecl(tok) _##tok
    #else
    # define cdecl(tok) tok
    #endif
    
    $ gcc -c -o  md5-fast-x86_.o  md5-fast-x86_.S
    $ nm  md5-fast-x86_.o
    
    00000000 b .bss
    00000000 d .data
    00000000 t .text
    00000000 T _md5_compress
    

    Hope it solves the problem.

     
    • Kai Tietz

      Kai Tietz - 2020-05-25

      Hello,

      well, you can build shared libraries exporting symbols without the
      32-bit ABI's leading underscore ... see here for reference the ld
      tool's option for more detail. Nevertheless the import-library for
      such a shared object (DLL) will contain the leading underscores. You
      will need to use a definition file to create it, or you can link
      directly to the DLL ... if you have headers, which properly define the
      export's calling-convetion.

      Hope that helps
      Kai

      Am So., 24. Mai 2020 um 18:18 Uhr schrieb Sam Tansy
      tansy@users.sourceforge.net:

      I took a look at it and there is no prefixed underscore in assembled object.

      $ nm md5-test.o md5-fast-x86.o

      md5-test.o:
      00000000 b .bss
      00000000 d .data
      (...)
      00000000 t .text
      U ___main
      U _calloc
      (...)
      00000000 T _main
      U _md5_compress ; <- that is what is "expected" in main()
      00000000 T _md5_hash
      U _memcmp
      (...)

      md5-fast-x86.o:
      00000000 b .bss
      00000000 d .data
      00000000 t .text
      00000000 T md5_compress ; <- that is what is provided in md5-fast-x86.o

      You could try to add -fleading-underscore as option to gcc but despite the claim in documentation it doens't work, I tell you that.
      You could add something to force adding leading underscore to name of the function in md5-fast-x86.S, like this:

      if defined(_WIN32)

      define cdecl(tok) _##tok

      else

      define cdecl(tok) tok

      endif

      $ gcc -c -o md5-fast-x86_.o md5-fast-x86_.S
      $ nm md5-fast-x86_.o

      00000000 b .bss
      00000000 d .data
      00000000 t .text
      00000000 T _md5_compress

      Hope it solves the problem.


      How to link assembled object? (lack of underscore causes `undefined reference')


      Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/mingw-w64/discussion/723798/

      To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/

       
  • oset jan

    oset jan - 2020-05-26

    Thanks for advice. I have tested it and it works. Not a perfect solution but works. Unluckily '-fleading-underscore' indeed doesn't work.

    And when comes to dll, thank you as well but it's just 1.6k of code. It meant to be included.

     

    Last edit: oset jan 2020-05-26

Log in to post a comment.