Re: Linking libraries?



Solar wrote:
Hi,
I'm a bit new to assembly. I'm programming on a linux machine (using
nasm) and as part of a programming challenge set by a friend, I am
trying to write a program that gets and displays the PATH environment
variable.
I made a stub C program to get the environment variable and found that
getenv() leaves a pointer to the string in eax, etc (please bear with
me, a lot of this stuff is new to me and I can't find any good
tutorials on the web).

However, I'm having a problem linking my program.

At the top of the file I have:

extern _getenv
global _start

section .data
; yada yada

section .text
; yada yada

As I was made aware that this is the way to tell ld that the getenv
function is in another object(? - I could be wrong, feel free to
correct me :))

I'm getting the following errors:

matt@matrix ~ $ ld -o path path.o
path.o: In function `_start':
path.asm:(.text+0x6): undefined reference to `_getenv'

Its a bit mystical to me, as far as I can see its all correct. I
thought that maybe part of the problem would be how I'm using ld, but I
couldn't find anything in the man pages.

If anyone could point me in the right direction as to how I'd solve
this, it would be very helpful :) Also, if anyone has any suggestions
for any online tutorials that are specific to assembly programming
under linux, that would be helpful too. Being the generic student, I'm
too poor/lazy to buy one (delete as appropriate)!

You're only linking the object file you created into an executable. ld
doesn't automagically link with the C library as well, you need to
specify it explicitly. If you're willing to follow a few rules that C
requires, you can use ld implicitly through gcc and the C library comes
along for the ride:

section .data

; Yadda yadda

section .text

global _main
extern _getenv

_main:
; Yadda yadda
xor eax,eax
ret

$ gcc path.o -o path

That saves you a few headaches, but removes a little bit of flexibility
because you have to satisfy the C runtime startup code.

.



Relevant Pages

  • Linking libraries?
    ... I'm programming on a linux machine (using ... extern _getenv ...
    (alt.lang.asm)
  • Re: Linking libraries?
    ... I'm programming on a linux machine (using ... extern _getenv ...
    (alt.lang.asm)
  • TRY / EXCEPT - Is this Bad???
    ... is it BAD programming technique to do this? ... INITIALIZE ... Procedure Main (blah blah); ... yadda yadda: String; ...
    (borland.public.delphi.language.basm)
  • Updating my Platform SDK
    ... doing some new programming and I need to access Tree structures. ... My SDK is out of date, and I know i cna be updated, yada yada yada. ...
    (microsoft.public.vc.language)