English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 27 September 2021, 22:33   #1
patrik
Registered User
 
patrik's Avatar
 
Join Date: Jan 2005
Location: Umeå
Age: 43
Posts: 922
Can symbols without leading _ be accessed in vbcc?

Hi,

If you are linking against a file with "assembler functions" which are missing the leading _ in the function name, is there any way to access those functions directly in vbcc without a jump/stub function in assembler?

You can declare the function to pass the arguments in the correct registers in vbcc, but I don't know how to reference the function symbol when it doesn't start with _.

To give a concrete example, I would like to call the assembler variant of debug.lib/KPutStr(A0) directly.

The reason for asking this is to save a few bytes in executable size of an utility I am working on, by avoiding having to use the C stub amiga.lib/_KPutStr(), if possible.

Hope to hear some suggestions .

Last edited by patrik; 28 September 2021 at 13:37. Reason: amiga.lib should be debug.lib
patrik is offline  
Old 28 September 2021, 10:53   #2
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
Two possibilities:

1. vlink has the capability to add or delete leading underscores of all symbols from an object or a library. When KPutStr from debug.lib is the only symbol you want to transform, you can do this:
Code:
vlink ... -set-adduscore -ldebug -clr-adduscore ...
2. Better might be an assembler inline function to call it:
Code:
void myKPutStr(__reg("a0")char *)="\txref KPutStr\n\tjsr\tKputStr";
...
  myKPutStr("test");
phx is offline  
Old 29 September 2021, 00:43   #3
patrik
Registered User
 
patrik's Avatar
 
Join Date: Jan 2005
Location: Umeå
Age: 43
Posts: 922
Opted for 2., works great, thank you very much!

Felt like the more controlled way to just do it for the function(s) you choose, instead of the symbols of the whole linker library.

Also, I have to be honest, that I am not sure how to incorporate custom linker commands without changing the build process. Right now I just call vc with all c and asm files in one go so vc calls vlink by itself.
patrik is offline  
Old 29 September 2021, 12:42   #4
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
Quote:
Originally Posted by patrik View Post
Felt like the more controlled way to just do it for the function(s) you choose, instead of the symbols of the whole linker library.
Indeed, the second option is clearly the better one in this case. I just wanted to mention this vlink feature, which can be useful when linking with ELF object files, as they usually define all their symbols without a leading underscore.

Quote:
Also, I have to be honest, that I am not sure how to incorporate custom linker commands without changing the build process.
You would have to look up the vlink-call from the vbcc config file, which is quite complicated:
Code:
vlink -bamigahunk -x -Bstatic -Cvbcc -nostdlib -mrel $VBCC/targets/m68k-amigaos/lib/startup.o your_objects.o -L$VBCC/targets/m68k-amigaos/lib -lvc -o program_name
phx is offline  
Old 17 October 2022, 02:02   #5
patrik
Registered User
 
patrik's Avatar
 
Join Date: Jan 2005
Location: Umeå
Age: 43
Posts: 922
Quote:
Originally Posted by phx View Post
2. Better might be an assembler inline function to call it:
Code:
void myKPutStr(__reg("a0")char *)="\txref KPutStr\n\tjsr\tKputStr";
...
  myKPutStr("test");
As a follow-up to this: is there a way to get the address of the assembler KPutStr in the C code, which is as efficient as if it had been defined as _KPutStr and I just could have “void *addr = &KPutStr”?

The best thing I can think of now is something like:
Code:
void *getKPutStrAddr(void)="\txref KPutStr\n\tlea\tKPutStr(pc),a0\n\tmove.l\ta0,d0\n\trts"
This is a complete function call though, not just an assignment.
patrik is offline  
Old 17 October 2022, 16:52   #6
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
Quote:
Originally Posted by patrik View Post
get the address of the assembler KPutStr in the C code, which is as efficient as if it had been defined as _KPutStr and I just could have “void *addr = &KPutStr”?
Besides your solution with an inline assembler function the best approach would be a separate assembler object, like:
Code:
        data

        xref    KPutStr
        xdef    _addr
_addr:
        dc.l    KPutStr
Then link with it to be able to read the address of KPutStr from addr.

Quote:
The best thing I can think of now is something like:
Code:
void *getKPutStrAddr(void)="\txref KPutStr\n\tlea\tKPutStr(pc),a0\n\tmove.l\ta0,d0\n\trts"
Be aware that you should never use "rts" in such an assembler inline function. The code is inlined, so it shouldn't return from the function it is being inlined in.
phx is offline  
Old 10 November 2022, 17:47   #7
patrik
Registered User
 
patrik's Avatar
 
Join Date: Jan 2005
Location: Umeå
Age: 43
Posts: 922
Sorry for the late thank you, but thank you!
patrik is offline  
Old 10 November 2022, 20:55   #8
bebbo
bye
 
Join Date: Jun 2016
Location: Some / Where
Posts: 680
it's better to pass the string pointer in a2:
Code:
        .text
        .align  2
        .globl  _KPutStr
_KPutStr:
        movem.l a6/a2,-(sp)
        move.l _SysBase,a6
        bra.s .L1
.L3:
        jsr a6@(-0x204:W)
.L1:
        move.b (a2)+,d0
        jne .L3
        movem.l (sp)+,a2/a6
        rts
bebbo is offline  
Old 12 November 2022, 00:07   #9
patrik
Registered User
 
patrik's Avatar
 
Join Date: Jan 2005
Location: Umeå
Age: 43
Posts: 922
Quote:
Originally Posted by bebbo View Post
it's better to pass the string pointer in a2:
Code:
        .text
        .align  2
        .globl  _KPutStr
_KPutStr:
        movem.l a6/a2,-(sp)
        move.l _SysBase,a6
        bra.s .L1
.L3:
        jsr a6@(-0x204:W)
.L1:
        move.b (a2)+,d0
        jne .L3
        movem.l (sp)+,a2/a6
        rts
Yes, you would avoid the additional move.l a0,a2 or saving and restoring a0 to the stack around the call to exec.library/RawPutChar().

Interesting detail on that topic - apparently debug.lib/KPutStr() depends on exec.library/RawPutChar() preserving a0:
Code:
$ hunktool info --brief --disassemble /opt/sdk/NDK3.2R4/lib/debug.lib | grep --before 10 --after 10 "KPutStr"
				_kputchar:
00000000	202f 0004           	move.l  ($4,A7), D0            
				KPutChar:
00000004	2f0e                	move.l  A6, -(A7)              
00000006	2c79 0000 0000      	movea.l $0.l, A6               ; absref32: _AbsExecBase
0000000c	4eae 0000           	jsr     ($0,A6)                ; dext16: _LVORawPutChar
00000010	2c5f                	movea.l (A7)+, A6              
00000012	4e75                	rts                            
				_kputstr:
00000014	206f 0004           	movea.l ($4,A7), A0            
				KPutStr:
00000018	1018                	move.b  (A0)+, D0              
0000001a	6704                	beq     $20                    
0000001c	61e6                	bsr     $4                     
0000001e	60f8                	bra     $18                    
00000020	4e75                	rts                            
				_kgetchar:
00000022	6106                	bsr     $2a                    
00000024	4a80                	tst.l   D0                     
00000026	6bfa                	bmi     $22                    
00000028	4e75                	rts
patrik is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
vbcc memwatch, debugging, symbols carrion Coders. C/C++ 8 17 April 2021 11:43
Are drag'n'drop archives into WinUAE accessed via RDH0:? Foebane support.WinUAE 6 21 June 2019 13:13
Leading Lap MPV DamienD support.Games 1 14 January 2016 22:49
Leading Lap mai HOL data problems 1 09 December 2009 21:16
Leading Lap AGA full version macce2 request.Old Rare Games 4 03 June 2006 23:50

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:21.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.17930 seconds with 15 queries