Recent

Author Topic: [SOLVED]: How to link external asm successfully  (Read 4119 times)

MathMan

  • Full Member
  • ***
  • Posts: 166
[SOLVED]: How to link external asm successfully
« on: November 08, 2015, 12:59:16 pm »
Hello all,

I am using FPC 2.6.4 together with Lazarus 1.4.2 under Ubuntu 15.04 LTS.

I want to move former inline asm to an external asm file which I can assemble with NASM (to gain access to extended asm mnemonics like ADCX, MULX, SHLX etc.) and link the resulting object to a Pascal unit like

Code: Pascal  [Select]
  1. ...
  2.  
  3. uses
  4.   ...
  5.  
  6. {$link LongArith-SL.o}
  7.  
  8. ...
  9.  
  10. function sBitLen( Op1: QWORD ):QWORD;external;
  11.  
  12. ...
  13.  

I use NASM as the assembler of choice with a respective (to the above) declaration like

Code: [Select]

global sBitLen:function

...

    align 16
sBitLen:


I assemble with

Code: [Select]

NASM -f elf64 -g LongArith-SL.asm


which produces the requested object file in the active directory including stabs debugging info. I also set stabs as the debugger info-type in the Lazarus environment. I also checked in the documentation that linked objects are searched in the active directory - which seems to work as I am not getting a file-not-found error (or similar response) on the Pascal {$link ...} statement above.

However I don't get this working - during linking I do get errors that external functions are not available. I suspect it has something to do with the Pascal name-mangling - but reading the docs (FPC and NASM) did not make me any the wiser.

I am sure it is a trivial thing I am doing wrong ...

Kind regards,
MathMan
« Last Edit: November 09, 2015, 07:50:12 am by MathMan »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8112
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to link external asm successfully
« Reply #1 on: November 08, 2015, 01:43:26 pm »
Either specify a calling convention (according to your asm code) or use name directive after external to specify the exact symbol. Check how nasm label it using nm against the .o. Note that different calling convention expects different mangled name.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7510
Re: How to link external asm successfully
« Reply #2 on: November 08, 2015, 01:45:44 pm »
Or dump some .o (nasm, and the calling FPC one) files and check there is not a "_" prefix mismatch.

MathMan

  • Full Member
  • ***
  • Posts: 166
Re: How to link external asm successfully
« Reply #3 on: November 08, 2015, 02:17:23 pm »
Either specify a calling convention (according to your asm code) or use name directive after external to specify the exact symbol. Check how nasm label is using nm against the .o. Note that different calling convention expects different mangled name.

Thanks for the hint. Re your first point that would then be something like

Code: Pascal  [Select]
  1.  
  2. function sBitLen( Op1: QWORD ):QWORD;register;external name 'sBitLen'; // without obj name
  3. function sBitLen( Op1: QWORD ):QWORD;register;external name 'LongArith-SL_sBitLen'; // including obj name
  4.  
  5.  

Re your second point - will check (hope I have nm already installed on my Ubuntu - pretty fresh to Linux, just started 8 weeks ago).

Regards,
MathMan

MathMan

  • Full Member
  • ***
  • Posts: 166
Re: How to link external asm successfully
« Reply #4 on: November 08, 2015, 02:20:01 pm »
Or dump some .o (nasm, and the calling FPC one) files and check there is not a "_" prefix mismatch.

With 'dump' you mean like open with e.g. gedit and look for the strings?

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7510
Re: How to link external asm successfully
« Reply #5 on: November 08, 2015, 04:19:21 pm »
No, I mean objdump from binutils.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8112
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to link external asm successfully
« Reply #6 on: November 08, 2015, 08:13:24 pm »
No, I mean objdump from binutils.
nm will do, too. both should be part of binutils.

MathMan

  • Full Member
  • ***
  • Posts: 166
Re: How to link external asm successfully
« Reply #7 on: November 09, 2015, 07:49:41 am »
No, I mean objdump from binutils.
nm will do, too. both should be part of binutils.

Thanks Leledumbo and marcov - I used objdump and was able to solve the issue. Have a working environment now and already tested first functions.

Regards,
MathMan