News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

The linker's meaning

Started by realcr, July 02, 2005, 12:03:22 AM

Previous topic - Next topic

realcr

I usually assemble my code by clicking on the assemble button on RadAsm , so I have never really wrote those commands of ML and LINK in the dos screen , however I always wanted to try it... I can understand the the ML does the assembling , however what does link do? can a program work without link , or it is necessery?

realcr.

hutch--

There arew many variations but MASM seperates the assembly from the linking together stage so that you can construct libraries, seperate modules and use the results with other languages, mainly Microsoft C/C++.

To make a full gui EXE file, you run,


RC.EXE  resource compiler.
CVTRES.EXE  res to obj converter.
ML.EXE  The assembler
LINK.EXE  Links all of the required bits together.


The command lines are not all that hard.


rc.exe /v rsrc.rc
cvtres /machine:ix86 rsrc.res
ml /c /coff yourfile.asm
Link /SUBSYSTEM:WINDOWS yourfile.obj rsrc.obj


With ML the /c means assemble only and /coff specifies the object module type.
With LINK the /SUBSYSTEM:WINDOWS specifies the exe/dll type. The alternative is /SUBSYSTEM:CONSOLE.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Vortex

realcr,

Flat assembler has the capacity to create executables without the usage of linker and resource compiler.

Rifleman

Personally, since I like all the files in a project to have the same name so if I am creating a library, the various files can be in the same directory, I let the linker call cvtres.exe and use a batch file as follows:


@echo off
set file="DesktopStyle"
if exist %file%.obj del %file%.obj
if exist %file%.res del %file%.res
if exist %file%.exe del %file%.exe
if not exist %file%.rc goto nores
if not exist %file%.asm goto errasm

d:\masm32\bin\rc.exe /v %file%.rc
if not exist %file%.res goto nores

d:\masm32\bin\ml.exe /c /coff %file%.asm >trash.can
if errorlevel 1 goto errasm
del trash.can

d:\masm32\bin\link.exe /SUBSYSTEM:WINDOWS /LIBPATH:masm32\lib %file%.obj %file%.res
if errorlevel 1 goto errlink

if exist %file%.obj del %file%.obj
if exist %file%.res del %file%.res
dir /b /a-d
goto TheEnd

:nores
echo _
echo Resource error
goto TheEnd

:errlink
echo _
echo Link error
goto TheEnd

:errasm
echo _
echo Assembly Error
goto TheEnd

:TheEnd
echo _
pause




There are other variants of the above, Mark has one that I like very much, but you can see the point.
Paul

realcr

Quote from: Vortex on July 02, 2005, 07:41:22 AM
realcr,

Flat assembler has the capacity to create executables without the usage of linker and resource compiler.

So what is the real need in a linker if it can be done this way with flat assembler? Is there a way to link more than two object files in the linker , and how does the linker know what to do? are there some kind of instructions or something in the files?

realcr.

Rifleman

realcr,
Do you realize he is talking about FASM, not MASM?

Paul

realcr

Quote from: Rifleman on July 02, 2005, 08:40:29 PM
realcr,
Do you realize he is talking about FASM, not MASM?

Paul


Yes , I do. I have some experience programming with fasm. Rifleman,Vortex,hutch-- and Paul I really appreciate your help,
however I still don't understand the linker's idea. Don't you think something about this idea is wrong if you have to write the same linker stuff again and again (have a look at rifleman message)

d:\masm32\bin\link.exe /SUBSYSTEM:WINDOWS /LIBPATH:masm32\lib %file%.obj %file%.res


where can I find more information about the linker and its uses or meaning? I am sure I'm missing something here. Maybe I didn't understand you explanations well enough.

thank you for your help ,
realcr.

rea


tenkey

The purpose of a linker, or any software with linking capabilities, is to combine code modules. The usual linker combines modules containing machine instructions.

The final executable is generally not capable of being combined with other code, so there is a special format for combinable "object" modules.

The extra information contained in an object module are:

* External names not defined within the module. This includes calls to library routines.
* External names defined within the module. The libraries contain these, but you will define your own when creating multiple modules.
* Locations of "addresses" that need to be adjusted when code and data are merged. The adjustment is called "relocation", and basically changes the addresses from module offsets to real addresses.

The "linking" is the matching of undefined externals with the defined "exported" externals.

It might be noted that DLLs are related to object modules, but exist as separate files, even when "linked". The linker creates a single module (file) out of many.

A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

S/390

FASM just takes a short cut and combines the assemble and link steps into one.

Another point, the linker can take object files from several different languages, and combine them into the final EXE. This is what allows you to call an assembly routine from a C program, for example. As long as the linker knows an obj format (like COFF), it doesn't care what the original language was. You could have a C program call an asm routine, that called a Fortran function, that called a Cobol subroutine, as long as all the compilers can produce object code that the linker understands...

:toothy

realcr

Thank you all for your help.
I think things are much clearer now.  :thumbu

realcr.

Rifleman

realcr,
I am glad you understand things better, now.  One of the downsides of IDEs and Editors with build capabilities is that the user does not learn the fundamentals.  These tools are good but a person should always learn the basics, also.  I prefer Hutch's Editor over some IDEs that actually do some of the coding for you.  They should be avoided IMO as they cause users to become dependant on them.  Stick with QEditor.  Even if you use the Prostart Wizard (excellent tool), you will see all the code that is created, in the editor, which is very important.

Paul

donkey

Well, the linker is not what I would spend alot of time learning. It is more the type of thing that you learn what you need to but not until you need it. After all, version to version it tends to change slightly and in some cases you might want to use a different one. Best leave it to the IDE to handle and concentrate on writing code, after all that is what you're learnign assembly for, not to learn to use somebody elses tools. In cases like this I think the more you can slog off to the IDE the better, it does not help you to learn all of the switches for a linker or how to invoke it from the command line.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

hutch--

For many programmers an IDE is a good thing but only if they are not dependent on them because without the knowledge of how to run the basic tools in their native command line mode, the programmer is a prisoner of one IDE. Assembler and linker options are not there to irritate the programmer, they are to empower the programmer to do more things that better suit their interests and the more the programmer knows, the more flexible they are with what tools they use.

You can use the Microsoft linker that come with MASM but you can also use the excellent linker that Pelle writes which has different options. You may wish to build a console mode app instead of  GUI one or you may wish to merge sections to save size, with a linker run from the command line you have all of these options and you you end up knowing WHY you have them. You may want to use a different resouce compiler that RC.EXE or compile some of the object modules with a C compiler and build a library with Microsoft LINK or Pelle's library manager.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Rifleman

Hutch,
Thanks, that states what I am trying to say exactly.  Donkey is in left field, somewhere as every bit of knowledge is useful and it is never a good idea to restrict what you can do based on what an IDE allows.

Paul