Author Topic: Linking problems  (Read 3342 times)

Offline TheManx

  • Jr. Member
  • *
  • Posts: 4
Linking problems
« on: March 13, 2016, 07:03:35 AM »
I'm a returning ASM programmer from the 6809E & later 8088 days. Lots to relearn!
I'm hitting numerous errors trying to link a hello world example from Tutorial Point

Particulars: I'm on Windows 10. Vidual Studio 2015 installed. MinGw V2013072300 installed. Nasm V2.12 64 bit

So I used a copy of the standard Hello world samples circulating. Here's one of them;

section   .text
   global _main       ;must be declared for using gcc
_main:                     ;tell linker entry point
   mov   edx, len    ;message length
   mov   ecx, msg    ;message to write
   mov   ebx, 1       ;file descriptor (stdout)
   mov   eax, 4       ;system call number (sys_write)
   int   0x80        ;call kernel
   mov   eax, 1       ;system call number (sys_exit)
   int   0x80        ;call kernel

section   .data

msg   db   'Hello, world!',0xa   ;our dear string
len   equ   $ - msg         ;length of our dear string


I'm compiling it with;
  nasm -f win32 helloworld.asm

I've tried linking it with;
     ld -m elf_i386 -s -o helloworld helloworld.o     (from the tutorial site - fails to find the emulation elf_i386)
     link /subsystem:console /nodefaultlib /entry:main Helloworld.obj kernel32.lib          (fails to find the Kernal32.lib - I did HD searches for kernel*.*.. nthin found)

I tried one of the HelloWorld samples from the forums here which had the GetStdHandle, WriteFile & ExitProcess in it. The compile and link suggestions where;
     nasm -f win32 myprog.asm
     ld -o myprog.exe myprog.obj

Failed with undefined reference errors to the symbols. I suspect it wants the library but with kernal32.lib missing and this being Windows 10, I'm not sure what variations windows 10 throws into things.

Any help to get past my linking problems would be appreciated.

Thanks


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2384
  • Country: us
Re: Linking problems
« Reply #1 on: March 13, 2016, 08:35:16 AM »
I don't "do Windows", but I can tell you that the code with "int 0x80" in it is for Linux, and will not run in Windows even if you get it to link. Stick with the code with "ExitProcess" in it. That's for Windows. I can tell you that Jeremy Gordon's GoLink is popular with Nasm Windows users. ld should work, but I can't tell you where to find the library you apparently need. Uppercase "-L" on ld's command line specifies path to the libraries and lowercase "-l" specifies the library, I believe.

This is a fairly common problem and I regret that we don't have a boilerplate solution to it.

Best,
Frank


Offline TheManx

  • Jr. Member
  • *
  • Posts: 4
Re: Linking problems
« Reply #2 on: March 13, 2016, 03:30:47 PM »
I don't "do Windows", but I can tell you that the code with "int 0x80" in it is for Linux, and will not run in Windows even if you get it to link. Stick with the code with "ExitProcess" in it. That's for Windows. I can tell you that Jeremy Gordon's GoLink is popular with Nasm Windows users. ld should work, but I can't tell you where to find the library you apparently need. Uppercase "-L" on ld's command line specifies path to the libraries and lowercase "-l" specifies the library, I believe.

This is a fairly common problem and I regret that we don't have a boilerplate solution to it.

Best,
Frank

Thanks a lot for the reply Frank. All Great information.
To get my feet wet again I'm been using the only Tut I could find online, the TutorialPoint.com one which is in Linux. I don't have a Linux server so I'm kind of improvising and expecting to deal with the occasional translation issue. All good training opportunities.
What kills is that I can't even get the first compile to run.
TutorialPoint has compiler VMs on their site but it doesn't do much good to do everything there if I can't do it elsewhere
.
I'll do some searching for the GoLink information you mentioned.

Offline TheManx

  • Jr. Member
  • *
  • Posts: 4
Re: Linking problems
« Reply #3 on: March 13, 2016, 05:13:28 PM »
Ok so I Pulled down all of the GoTools. Set up my path environment variables. Pulled down a sample hello world from these forums, got a clean compile and than tried to link with GoLink. No Good. Here's the error I got;

"Error!
The following symbols were not defined in the object file or files:-
GetStdHandle
WriteFile
ExitProcess
Output file not made"


Here's the code I used directly for the forums here;

extern GetStdHandle
extern WriteFile
extern ExitProcess

section .data
    msg db "hello windows!", 0
    msglen equ $ - msg ; note: a constant not a variable
section .bss
; Linux returns this in eax
; Windows needs a "place to put it"
    bytes_written resd 1
section .text

global start ; or perhaps winmain
start:
; find out what STDOUT is
    push -11
    call GetStdHandle
; now the handle for STDOUT is in eax
    push 0
    push bytes_written
    push msglen
    push msg
    push eax
    call WriteFile

    push 0
    call ExitProcess
;----------------------------

So I've been through 3 linkers and so far none of them will work. 2 days now on 3 HelloWorld projects. 1 from NASM, one from TutorialPoint and one from  Stackoverflow. I must be missing something huge! Any suggestions or help would be great.
I need to get something to work so I know I have a working and Compiler/Linker combo that I can begin to learn with. I figure it won't do me any good trying to learn the quirks of the complier/linker combo when I can even get them to compile a single thing.

What in the world am I missing?


Thanks in advance!

P.S. Although I'm working in NASM (for the moment) I did try MASM32.... Windows 10 won't even TRY to run it. It tells you right off to forget about it in a well crafted dialog box.


Offline TheManx

  • Jr. Member
  • *
  • Posts: 4
Re: Linking problems
« Reply #4 on: March 13, 2016, 06:59:59 PM »
UPDATE:

I got it working. Here's what needed to be done in case anyone else hits this; (Reminder: this is on Windows 10)

1. I had to install MASM32. (The previous MASM32 file I had was failing so I installed MASM32v11)  - In windows 10 it will do some complaining about missing peices.. Click OK and keep going.
2. I had to set up a LIB environment variable and point it at the MASM32 lib folder (MASM32\lib) where all of the Lib files are stored for MASM32. Kernal32 & User32 are both there.
3. Restart any existing command prompt windows you may have open and intend to use for linking. They need to pick up the LIB variable from step 2.

Now you can use the Link command that cam with the NASM HelloWorld sample.
     link /subsystem:console /nodefaultlib /entry:main HelloWorld.obj kernel32.lib
« Last Edit: March 13, 2016, 07:03:48 PM by TheManx »

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Linking problems
« Reply #5 on: March 13, 2016, 10:48:19 PM »
GoLink needs to know where the DLL's are located in order to fix up the symbols (Microsoft's LINK tool uses a bunch of libraries, GoLink uses the DLL's that are already on your system), you can pass these on the command line or you can add the information to your .drective section. Check out the following thread:

http://forum.nasm.us/index.php?topic=1122.0

About Bryant Keller
bkeller@about.me

Offline nulluse

  • Jr. Member
  • *
  • Posts: 3
Re: Linking problems
« Reply #6 on: April 12, 2016, 06:28:39 PM »
Just FYI, you can build the Linux (Unix) ASM programs to run under Windows in Cygwin without any changes to the source.
Developing in Cygwin is way easier than struggling with Windows quirks IMO.