using libc from nasm

asm

    Next

  • 1. Sorry, wrong group
    Sorry, this posting should be sent to another group
  • 2. Opting out of spam trap testing
    Those of you who do not wish to have your From: names spam trapped, may opt out by either sending an empty email message to clax86-realfrom, using the same From: address as the one which you use in clax86 postings, or by posting a message with a subject of "real from addresses".
  • 3. Win32 software interrupts?
    I want to use some Win95 interrupts on a windows32 aplication. Is it possible? Ralf Brown interrupts list describes int 21h, ax=7160h, cx=8002h, as "Windows95 - LONG FILENAME - GET CANONICAL LONG FILENAME OR PATH". If it's possible, I've got a problem. This function requires that: DS:SI -> ASCIZ short filename or path ES:DI -> 261-byte buffer for canonicalized long name But my variable adresses are in 32bit mode. What if they're above the DOS 1mb scope? How could I address them in this function, if ever? Many thanks in advance 8)
  • 4. Pentium 4 - Register
    Hallo, ist zwar nicht ganz on-topic, aber hier sind sicherlich die Leute, die sowas auch schon mal gesucht/gefunden haben. Hab im Netz vor geraumer Zeit ne Seite gefunden, auf der zusammengefa der FPU-Stack, sowie die MMX- und XMM-Register abgebildet waren (alles zusammen ein Bild). Es war auch ersichtlich, da?beispielsweise die MMX-Register auf den FPU-Stack abgebildet sind. Kennt zuflig jemand diese (oder eine nliche) Grafik, die hierer einen schen erblick gibt, die man sich also auch ausdrucken und an die Wand heften kann? Danke schon mal, Manfred

using libc from nasm

Postby Adam Bozanich » Sat, 14 Feb 2004 05:38:23 GMT

Hi all.  I am trying to learn asm with NASM on a FreeBSD system.  I really need
to debug my programs while I learn, so I want to use printf.  This is what I am
using to assemble and link:

nasm -f elf use_printf.asm
ld -s -o use_printf use_printf.asm -lc  

but then when I run the program:

$./use_printf
/usr/libexec/ld-elf.so.1: /lib/libc.so.5: Undefined symbol "environ"

Can somebody see where I am going wrong?  This is kindof holding me back.  I
added the 'extern environ' and 'extern __progname' beause I get this otherwise:

/usr/lib/libc.so: undefined reference to `environ'
/usr/lib/libc.so: undefined reference to `__progname'

Here's what I have( I think I have commented out the bsd specific stuff... ):

extern  printf
extern  environ
extern  __progname

section .data
mesg            db       'the number is %d\n',0
mesglen         equ      $-mesg

errormesg       db       'libc error',0ah,0     
errormesglen    equ      $-errormesg

newline         db      10
number          dw      0x10

;kernel:
;       int 80h
;       ret

align 4
section .text
global _start
_start:

        push dword      number 
        push dword      mesg 
        call printf

        ; error if eax < 1  ( we should have wrote some chars )
        cmp eax , 0x1
        jl      .error   

        ; use write() system call to print message
;       push    dword   mesglen
;       push    dword   mesg
;       push    dword   0x1   ; stdout
;       mov     eax   , 0x4   ; 4 == write system call
;       call    kernel

;       ; output a newline
;       push    dword   1
;       push    dword   newline
;       push    dword   0x1     
;       mov     eax   , 0x4 
;       call    kernel


;       mov  eax  , 0x1 ; exit syscall number
;       push dword  0x0 ; exit status
;       call kernel

.error:
;       push dword  errormesglen
;       push dword  errormesg
;       push dword  0x1
;       mov         eax , 0x4
;       call        kernel

;       mov         eax  , 0x1
;       push        dword  0xff
;       call        kernel




Re: using libc from nasm

Postby Phil Carmody » Sat, 14 Feb 2004 16:12:48 GMT

Adam Bozanich < XXXX@XXXXX.COM > writes:


Who do you think is supplying the definition of environ?

Phil

-- 
Unpatched IE vulnerability: DNSError folder disclosure
Description: Gaining access to local security zones
Reference:  http://www.**--****.com/ 


Re: using libc from nasm

Postby Frank Kotler » Sat, 14 Feb 2004 18:55:19 GMT



Hi Adam,

I've never run FreeBSD. I know it's different from Linux, but I'll take 
a guess anyway! :)


This has gotta be "use_printf.o", not "use_printf.asm", right? I forget 
what the error message is if you try to link your .asm file, but I know 
from experience it doesn't work. :)


I don't think declaring these "extern" in your file is going to help. 
I'm not sure, but I suspect these variables must be declared in the C 
startup code - just guessing from their names. Try linking with gcc 
instead of directly with ld. I've been getting away with linking 
straight with ld in Linux, although some people have warned me that it 
might not always work - maybe this is one of these times. See if:

gcc -s -o use_printf use_printf.o -lc

helps any. You'll have to change your entrypoint to "main", too - 
"global main", and "main" where you've got "_start"... If it works, you 
may be able to see what the command line to ld is (add a "-v" switch?) 
and copy it to go back to linking direct with ld, if you want to.


No underscore for ELF - you got that right! Printf is *used* in your 
program, but the function is elsewhere - that's what you want "extern" for.


These *don't* appear in your program, so declaring them "extern" won't 
do any good. I think the problem is that they're declared "extern' 
somewhere else - maybe ld-elf.so? - and wherever they actually exist 
isn't being linked in.

Number's only declared as a word, so you're pushing some garbage besides 
your number. Actually, you're pushing the address of the number, not the 
number itself. Try changing the number to "dd 0x10" instead of "dw 
0x10", and "push dword [number]"...


Probably want to "add esp, 8" here - C expects the caller to clean up 
stack. This hasn't got anything to do with the problem you're having, 
but once you get it to link, I think this'll cause a seg-fault somewhere 
down the line.

You wanted to use "printf" 'cause it's "easier" than displaying the 
number yourself, right? Maybe for floating point numbers it is...

Best,
Frank


Re: using libc from nasm

Postby Ulf Andersson » Tue, 17 Feb 2004 04:41:31 GMT

ello Adam, and others...

Maybe you have already found all the answers you need to go further with your
assembly language explorations, but anyway. If you haven't already done so, you
should read the FreeBSD developers handbook, chapter IV. The link is found
below.
Another good thing to do is to get really acquainted with the linker. Browse
the
manual and the supplementary documents.

Last time I did anything in assembly on a UNIX system, all names in the
libraries had
an underscroe prefixing every name. I.e. environ would be _environ in an
assembly
language program. This is actually a compiler convention. One way to find out
more
about global name references is to look at the output from the C compiler.

Try "gcc -S whatever.c" which will produce the file whatever.s which is the
assembly
program. See "man 1 gcc" for more info.

Here is the link to the FreeBSD developers manual.

http://www.freebsd.org/doc/en_US.ISO8859-1/books/developers-handbook/

There is a plethora of documents at the FreeBSD.org site.

By the way, is there a particular reason for using nasm?

Good luck.

/Ulf Andersson

"Adam Bozanich" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
need
am
otherwise:



Re: using libc from nasm

Postby Adam Bozanich » Mon, 23 Feb 2004 07:09:40 GMT

On Sun, 15 Feb 2004 19:41:31 +0000 (UTC)





That's where I started!  It's a great tutorial.


Because the developers-handbook used it ;)  Also, the syntax is a little easier
to understand at first, and I think that it might be a bit more "portable".

I have used gas ( GNU Assembler ) a little now,  but prefer to use Nasm.

using nasm and then 'objdump -d file.o' is a nice way to see the differences.

Thanks a lot to all who replied.  I am sorry for the delay.  Frank, your
suggestions fixed the problem.  I appreciate it a lot.

-Adam


Similar Threads:

1.c++ linux compiler using nasm?

does anyone know of a c++ compiler that generates Intel syntax in the
assembly list?  I'm currently learning c++ & asm on windows with MS VS 2005
& MASM, respectively, but when I fool around on linux with gcc, the gas /
AT&T syntax hurts my eyes. :)

On windows, I like to write simple C/C++ programs, compile, and see what the
compiler did by viewing the disassembly, which includes "intel-like"
eye-candy along with the opcodes.  I would like to do something similar on
Linux, if possible.  (32 bit system, opensuse)


-- 
thanks, Brian 
- reply to newsgroup

2.NASM macros, defines that will be used later

Hello,

I was playing with macros in NASM lately, but I came out of ideas.

My goal was to create macro that will take single asm line, count it's
size and somewhere later create an array with all instructions sizes
that was counted by this macro. I was trying with something like:

%macro _size_i 1+
   %ifndef ___SIZE_
   %define ___SIZE_
      %assign ___DATA_SIZE 1

      ; create initial definition of array like db (%%endinstr -
%%instr)

      ;%error 1st
   %elifdef ___SIZE
      %assign ___DATA_SIZE ___DATA_SIZE+1

      ; create another array definition using all previous definitions
of this array like
      ; db (@1.%%endinstr - @1.%%instr), (@2.%%endinstr - @2.%%instr),
(@3.%%endinstr - @3.%%instr)

      ;%error 2nd
   %endif ; ___SIZE
        ;db (%%endinstr - %%instr)
;      __SECT__
   %%instr:
        %1
   %%endinstr:
%endmacro

First macro call could create:
%define __SIZE_DEFINE_001 db 02,
second: %define __SIZE_DEFINE_002 __SIZE_DEFINE_001 {, 04
third: %define __SIZE_DEFINE_003 __SIZE_DEFINE_002 {, 01}

and so one. But the %define doesn't use __SIZE_DEFINE_001 like previous
define, but just like a single string.

It would not be a problem if this macro will be used in .text section
and array definied in .data, but since this code is in data then the
whole array definition must be done by some defines for later use.

Btw... NASM Manual for macros is nice, but maybe you know some other
resources with example macros and so on? Masm source examples are easy
to find, but not many ppl use some advanced macros for theirs work.

Thanks,
 Liquid

3.More Nasm Macro generators using Perl

First and foremost.
Description:
The following perl programs generate the "nasm socket library" for linux 
specific code. The constants are not 100% done yet, but reasonable IMO 
enough to work with.


Second, my news server doesn't let me send attachments. So i'll just 
include the sources here.

Third, the licence:

<<<<<<<<<<<<<<<<<
Copyright (c) Percival 2004
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
3. The name of the the names of its contributors may be used to endorse
    or promote products derived from this software without specific
    prior written permission.

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS 
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
THE POSSIBILITY OF SUCH DAMAGE.
<<<<<<<<<<<

Short and sweet. It applies to all the programs in this specific post. 
Everyone can use it, lurkers, hackers, etc. Just follow the licence. And 
seriously, I am glad of that Warranties line :) I am free from sueing 
you sue happy america.

Fourth, a sample program demonstrating the "Nasm autogenerated socket 
library"

Hopefully, this will make socket level programming in assembly MUCH MUCH 
easier.

test.asm (nasm) Aka, smallest telnet client you ever saw (628 bytes 
after being stripped)
<<<<<<<<<<<<<<
%include "socket.inc"
%include "unistdm.inc"
%include "socket_constants.inc"

port equ 21

global _start

section .text
_start:
	socketcall.SOCKET AF_INET, SOCK_STREAM, 0
	mov edi, eax
	socketcall.CONNECT edi, struct1, sizeof.struct1
	cmp eax, 0
	jl .exit

  .loop:
  	SYS.read edi, buffer, buffer.length
	cmp eax, 0
	jle .exit
	mov esi, eax
	SYS.write 1, buffer, esi
	SYS.read 0, buffer2, buffer2.length
	mov esi, eax ; eax is modified inside the macro first... I
		     ; should change that
	SYS.write edi, buffer2, esi
	jmp .loop
  .exit:
  	SYS.exit 0

section .data
struct1:
	dw AF_INET
	dw htons(port)
	db 127,0,0,1
	times 8 db 0
sizeof.struct1 equ $-struct1

section .bss
buffer.length equ 0x100
buffer resb buffer.length
buffer2.length equ 0x50
buffer2 resb buffer2.length
<<<<<<<<<<

Now that we got the smallest and shittiest telnet client you ever saw 
out of the way, lets get on to them perl scripts.

bit2const.pl
<<<<<<<<<<
#!/usr/bin/perl
#Run this file using
#./bit2const.pl /usr/include/bits/socket.h

use warnings;

while (<>){
	if ($_ =~ /#define\s*PF(\w*)\s*(\w*)/){
		print "\nPF$1 equ\t$2\n";
		print "AF$1 equ\t$2\n";
	}
	if ($_ =~ /^\s*SOCK_(\w*)\s*=\s*([1234567890]*)/){
		print "SOCK_$1 equ $2\n";
	}
}
<<<<<<<<<<<< EOF <<<<<<<<<<<<<




net2nasm.pl
<<<<<<<<<<<<<
#!/usr/bin/perl
#Run this file using
#./net2nasm.pl /usr/include/linux/net.h

use warnings;

while (<>){
	if ($_ =~ /#define\s*SYS_(\w*)\s*(\d*)/){
		print "sock.$1\teq\t$2\n";
	}
}
<<<<<<<<<< EOF <<<<<<<<<<<<<<<<



sock2macro.pl
<<<<<<<<<
#!/usr/bin/perl
#Requires ./net2nasm.pl
#./net2nasm.pl /usr/include/linux/net.h | ./sock2macro.pl

use warnings;

print <<"EOF";
%define htons(short) ((short >> 8) & 0xFF) | ((short & 0xFF) << 8)
EOF
while (<>){
	if ($_ =~ /sock\.(\w*)\teq\t(\d*)/){
print <<"EOF";
%macro socketcall.$1 1-*
	%rep %0
	  %rotate -1
	  push dword %1
	%endrep

	mov eax, 102 ; sys.socketcall
	mov ebx, $2
	mov ecx, esp
	int 80h
	add esp, %0 * 4
%endmacro
EOF
	}
}
<<<<<<<<<<< EOF <<<<<<<<<

bit2const.pl creates the constants required for socket programming. 
Again, i don't think it got them all, but it got enough of them to 
create telnet.

net2nasm.pl creates the constants that the kernel uses for socketcalls.
sock2macro.pl creates the macros from the constants defined in net2nasm.

None do error checking of any kind, and sock2macro.pl creates macros 
that are "register parameter safe". Meaning it is safe to do:
socketcall.SOCKET eax, ebx, ecx

unlike my other macro generator library. The other two don't generate 
macros, so they don't suffer from this at all.

If you wish for improvements, just post them here. Any requests for a 
specific kernel level system call thing... I *might* do it. I am only a 
novice programmer, so don't expect anything special.

I tested these... They should work... not sure if they work for 
everyone:) I am running Linux 2.6.6 debian.

Percival

4.File I/O using NASM in 32-bit mode

Hello,
   I'd like to open, read and close a file using Nasm (32-bit mode) on 
Windows 2000 OS, but cannot find any examples of nasm file I/O with 
Google or Yahoo.

Does anyone have any examples they could share with a relative newbie?

Any help would be greatly appreciated.

Thanks,
   Phil

5.Debugging NASM using GDB

Hello,

Can any one tell me how I can add debug symbols in NASM so that I can
debug my code using GDB (GNU debugger.)

Muler

6. Using GDB to debug a program assembled with NASM

7. basic question about using nasm

8. using nasm and segment overrides



Return to asm

 

Who is online

Users browsing this forum: No registered users and 37 guest