The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: vid on August 11, 2010, 10:28:33 AM

Title: Linkinfo in MASM?
Post by: vid on August 11, 2010, 10:28:33 AM
How can I specify linkinfo (things like Visual C __declspec(dllexport), __declspec(lib, "kernel32")) in MASM?
Title: Re: Linkinfo in MASM?
Post by: Twister on August 11, 2010, 12:45:46 PM
If you are creating a .DLL file then you would:

MyDefFileHere.DEF
LIBRARY MyNewProgram

EXPORTS MyNewFunction
Title: Re: Linkinfo in MASM?
Post by: vid on August 11, 2010, 12:46:46 PM
That's exactly what I am trying to get rid of. ;)
Title: Re: Linkinfo in MASM?
Post by: Twister on August 11, 2010, 12:48:28 PM
Are you are trying to remove kernel32 from your program?

I do not understand what you mean by 'trying to get rid of'.
Title: Re: Linkinfo in MASM?
Post by: vid on August 11, 2010, 12:59:33 PM
I am trying to get rid of .DEF file in my project, by storing list of exported symbols in linkinfo section of object file.

In general, I am asking how to specify linkinfo for object files in MASM source.
Title: Re: Linkinfo in MASM?
Post by: japheth on August 11, 2010, 01:31:52 PM
Quote from: vid on August 11, 2010, 12:59:33 PM
I am trying to get rid of .DEF file in my project, by storing list of exported symbols in linkinfo section of object file.

It's possible to use the EXPORT attribute for procedures:


myproc proc EXPORT a1:dword
    ret
myproc endp


this will make Masm add an entry "-export:_myproc@4" to the .drectve section, which is a section filled with "linker commands". It's slightly M$ proprietary.

Title: Re: Linkinfo in MASM?
Post by: MazeGen on August 11, 2010, 02:18:54 PM
Hey Japheth, good catch, this is what vid was looking for. I was trying to make the section manually, however, only MASM directive may start with dot so this doesn't work (and yes, the IMAGE_SCN_LNK_INFO attribute is missing, if it even can be forced in MASM):

.drectve SEGMENT
db "hello linker!"
.drectve ENDS


Do you know of any possibility how to create a section that starts with dot at MASM source code level?
Title: Re: Linkinfo in MASM?
Post by: japheth on August 11, 2010, 02:30:11 PM
Quote from: MazeGen on August 11, 2010, 02:18:54 PM
Hey Japheth, good catch, this is what vid was looking for.

How do you know that?

Quote
Do you know of any possibility how to create a section that starts with dot at MASM source code level?

I guess that's why the OPTION DOTNAME directive exists.

If you own Masm v8+, you can even cause Masm set the info bit:


.drectve segment info

Title: Re: Linkinfo in MASM?
Post by: MazeGen on August 11, 2010, 02:37:40 PM
Quote from: japheth on August 11, 2010, 02:30:11 PM
Quote from: MazeGen on August 11, 2010, 02:18:54 PM
Hey Japheth, good catch, this is what vid was looking for.

How do you know that?

We've been chatting this in private today.

Quote from: japheth on August 11, 2010, 02:30:11 PM
Quote
Do you know of any possibility how to create a section that starts with dot at MASM source code level?

I guess that's why the OPTION DOTNAME directive exists.

If you own Masm v8+, you can even cause Masm set the info bit:


.drectve segment info



My bad, all you wrote is documented. Thanks for the information!
Title: Re: Linkinfo in MASM?
Post by: Twister on August 11, 2010, 03:30:44 PM
Damn! I didn't know you could place 'EXPORT' on a process! :clap: Very nice.
Title: Re: Linkinfo in MASM?
Post by: vid on August 11, 2010, 07:17:31 PM
Thanks Japheth! Good to know new MASM supports making linkinfo section manually. In some cases I also like to add stuff like "/dll /entry:main kernel32.lib", not just list of exported funcs for DLL, but that was the main concern.
Title: Re: Linkinfo in MASM?
Post by: MazeGen on August 12, 2010, 08:46:12 AM
The cleanest way I found:

.686
.MODEL FLAT, STDCALL
OPTION DOTNAME

.drectve SEGMENT INFO DISCARD
db "/subsystem:console"
.drectve ENDS

.CODE
start:
int 3
END start


Two .drectve sections are made, the other is generated by MASM to propagate the entry point to linker. The linker obviously reads all the .drectve sections because I didn't get any errors.

Assemble and link it with just:

ml drectve.asm

I use MASM and LINK 9.00.30729.01.

The manual section has attributes different from the automatic one, but this doesn't seem to be an issue as well:

SECTION HEADER #3
.drectve name
       0 physical address
       0 virtual address
      12 size of raw data
      DD file pointer to raw data (000000DD to 000000EE)
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
2000240 flags
         Initialized Data
         Info
         Discardable
         (no align specified)

RAW DATA #3
  00000000: 2F 73 75 62 73 79 73 74 65 6D 3A 63 6F 6E 73 6F  /subsystem:conso
  00000010: 6C 65                                            le

   Linker Directives
   -----------------
   /subsystem:console

SECTION HEADER #5
.drectve name
       0 physical address
       0 virtual address
       D size of raw data
     167 file pointer to raw data (00000167 to 00000173)
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
     A00 flags
         Info
         Remove
         (no align specified)

RAW DATA #5
  00000000: 2F 45 4E 54 52 59 3A 73 74 61 72 74 20           /ENTRY:start

   Linker Directives
   -----------------
   /ENTRY:start
Title: Re: Linkinfo in MASM?
Post by: Vortex on August 12, 2010, 06:09:03 PM
MS COFF Directive section creator (http://www.masm32.com/board/index.php?topic=6857.0)