flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > Request for Feature in MS COFF

Author
Thread Post new topic Reply to topic
jimjam



Joined: 28 Jul 2004
Posts: 5
jimjam
In MASM, I can do this:

Code:
; alias.asm
extern _someFunction:DWORD
alias <_someOtherFunction>=<_someFunction>
END
    

Which allows me to write a program like this:
Code:
extern "C" void someFunction() { printf("someFunction!\n"); }
extern "C" void someOtherFunction();
void main(void) {
  someFunction();
     someOtherFunction();
}
    

The linker looks for "someOtherFunction" and finds the alias record in the coff obj, and ends up pointing it towards "someFunction".
It's very useful to me. It doesn't seem to work in fasm; I tried this:
Code:
format MS COFF
extrn someFunction
label someOtherFunction dword at someFunction
public someOtherFunction
    

...but I get an "invalid use of symbol" error at line 2266 of FORMATS.INC

I realize that fasm probably just doesn't support this microsoft specific functionality, but it would be great if it did!

A dumpbin of the masm produced file shows this:
Code:
COFF SYMBOL TABLE
000 00000000 DEBUG  notype       Filename     | .file
    alias.asm
002 002A2263 ABS    notype       Static       | @comp.id
003 00000000 SECT1  notype       Static       | .debug$S
    Section length   52, #relocs    0, #linenums    0, checksum        0
005 00000000 UNDEF  notype       External     | _someFunction
006 00000000 UNDEF  notype       External     | _someFunction
007 00000000 UNDEF  notype       WeakExternal | _someOtherFunction
    Default index        6 Alias record
    

The magic is in the WeakExternal symbol type. How hard would it be to add such a thing? I've had a look at the fasm source and made a rudimentary attempt, but I must admit I'm not smart enough to figure it out.
Post 28 Jul 2004, 18:30
View user's profile Send private message Reply with quote
jimjam



Joined: 28 Jul 2004
Posts: 5
jimjam
I figured out my way through the fasm source and implemented my own request. Idea

in masm you go:
Code:
alias <fakeSymbol>=<realTargetSymbol>    


I added a keyword or two to fasm:
Code:
alias 'fakeSymbol' to realTargetSymbol    


I didn't have alot of time so I just sort of copy&pasted all the stuff relating to extrn and modified it slightly. That's why the syntax looks like it does - that way i didnt have to write much new parsing.

So I'm not sure what to do next... What's the procedure for contributing a patch? Is there a preferred format? Any code conventions?[/code]
Post 29 Jul 2004, 08:21
View user's profile Send private message Reply with quote
Joshua



Joined: 12 Jul 2003
Posts: 56
Location: Belgium
Joshua
Would this do?
Code:
macro alias var1,var2 {
    var1 = var2
}
to fix ,

extrn realTargetSymbol
alias fakeSymbol to realTargetSymbol
    
Post 29 Jul 2004, 18:02
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7750
Location: Kraków, Poland
Tomasz Grysztar
Joshua: no, it won't work when you want to make your alias public.

jimjam: wouldn't it be better if weak external entry was automatically generated when you made some external-based label public? I will try to work on it as soon as I read more about weak externals in MS docs.
Post 29 Jul 2004, 18:34
View user's profile Send private message Visit poster's website Reply with quote
jimjam



Joined: 28 Jul 2004
Posts: 5
jimjam
Privalov, do you mean it would be better the way I tried originally?
Code:
format MS COFF
extrn realTargetSymbol
label fakeSymbol dword at realTargetSymbol
public fakeSymbol    

Like that? One reason I didn't do it that way is that it is alot of typing for something that could be expressed more clearly with the 'alias' syntax. The real reason I didn't do it that way is because it was easier to fit in with the existing codebase. If you automatically generate the WeakExternal when you get to the 'public fakeSymbol', at "add_public_symbol" in FORMATS.INC, you won't know what that label was referring to. Maybe there's a way to look it up, but I didn't see it right away. Also, the WeakExternal record doesn't stand by itself - it refers to another extrn symbol record by index. Even if you can calculate what symbol the label is pointing to at the point of "add_public_symbol", you don't know at which index the pointed-to extern was outputted. It's not kept track of as far as I could see.

The alias syntax I proposed made it easy to implement, that's all. When fasm goes to output an alias record, it outputs an extern right there, so it knows which index to point the WeakExternal at: the one it just outputted. That seems to be the way masm does it as well. You can see that by slightly modifying the original masm example I gave:
Code:
; extern realTargetSymbol:DWORD ;;; not actually needed
realTargetSymbol EQU 12345 ; symbol just has to be mentioned
alias <fakeSymbol>=<realTargetSymbol>
END    

Masm outputs an extern symbol record silently at the point of the alias, then the WeakExternal immediately follows it. It's just easier that way because you don't have to keep track of which extern symbol was output at which symbol table index, just in case someone comes along and tries to publicize an external label, at which point you would have to look it up.

I also chose the 'alias' syntax because anyone who is using this feature is probably coming from masm, I thought it would make sense to approximate their syntax.

Perhaps 'weakextrn' or 'weakexternal' would be a better keyword, since 'alias' might cause other people's existing code to not compile anymore?

Also just to note - I decided to do this because masm dies with internal errors if you play around in any serious way with these WeakExternals. I didn't bother trying to figure out what caused it to crash - maybe the relative order of the different symbols. I dunno. Who cares now? ! Smile
Post 29 Jul 2004, 21:20
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7750
Location: Kraków, Poland
Tomasz Grysztar
Quote:
If you automatically generate the WeakExternal when you get to the 'public fakeSymbol', at "add_public_symbol" in FORMATS.INC, you won't know what that label was referring to. Maybe there's a way to look it up, but I didn't see it right away.

It's very easy to look it up, because this information is anyway needed to make a correct fixups when you refer to such symbol, so it's already kept by all fasm's internal routines. So implementation of this could be much simpler. And I prefer to avoid adding new directives when something can be achieved using the existing ones.
Post 30 Jul 2004, 00:50
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7750
Location: Kraków, Poland
Tomasz Grysztar
OK, done it, now it needs testing - the attachment contains the pre-release of fasm 1.54 with this feature implemented. It should work just the way you tried it for the first time:
Code:
format MS COFF 
extrn someFunction 
label someOtherFunction dword at someFunction 
public someOtherFunction    

but it should be OK also with other, simpler variants:
Code:
extrn 'someFunction' as someOtherFunction
public someOtherFunction    

or
Code:
extrn someFunction
public someFunction as 'someOtherFunction'    

Of course you can make the "alias" macro for this purpose.

Attachment removed - not it's official release.


Last edited by Tomasz Grysztar on 30 Jul 2004, 16:02; edited 1 time in total
Post 30 Jul 2004, 11:36
View user's profile Send private message Visit poster's website Reply with quote
jimjam



Joined: 28 Jul 2004
Posts: 5
jimjam
Oh wow - awesome! Thanks, Privalov. It works perfectly and you're absolutely right not to add a new keyword.
Post 30 Jul 2004, 15:27
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.