LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   problems with linking objects -- must know some nasm\ASM\linker (https://www.linuxquestions.org/questions/programming-9/problems-with-linking-objects-must-know-some-nasm%5Casm%5Clinker-247626/)

Paul6253 10-26-2004 04:39 PM

problems with linking objects -- must know some nasm\ASM\linker
 
Hello

I present 2 problem cases ,each case is followed with tested code
thankyou for any help

--paul--
.................................................................................................... ...........................
CASE 1--

case 1 will compile but segfault when I insert into kernel using insmod

paul@box:~$ nasm -f elf foo.asm -o a.o
paul@box:~$ gcc -c foo.c -o b.o
paul@box:~$ ld -r a.o b.o -o kernelmod.o
paul@box:~$ sudo insmod -f kernelmod.o
Password:
Warning: kernel-module version mismatch
kernelmod.o was compiled for kernel version 2.4.20
while this kernel is version 2.4.20-xfs
Warning: loading kernelmod.o will taint the kernel: no license
See [xxx] for information about tainted modules
Warning: loading kernelmod.o will taint the kernel: forced load
Segmentation fault

gee, other than version mismatch I dont get why this code fails,
it should play some cool tones on my pc speaker

Code:



---------------------code for case1---------------

====== c code which calls asm module =========

#define MODULE
#define __KERNEL__
#include  <linux/module.h>
#include  <linux/kernel.h>









int init_module(void)
{

jump();

}
  void cleanup_module(void)
{




}

========== asm module ===========

 BITS 32

          GLOBAL jump




          SECTION .text

jump:

jmp Play







                          ;status in dx ,1= on 0 = off
ChangeSpeaker:
in al,0x61
cmp dx,1
jne off
or al,3
jmp on

off:
mov dx,3
not dx
and ax,dx
on:
out 0x61,al
RET
;end ChangeSpeaker



Sound:

;hertz in di
mov  dx,1                        ;turn on speaker
call ChangeSpeaker

mov al,0xb6
out 0x43, al

                                ;  calculate tone
        mov    dx,14h
        mov    ax,4F38h          ;  divisor of frequency
        div    di
                                ;  play tone
        out    42h,al            ;  lower byte of frequency
        mov    al,ah            ;
        out    42h,al            ;  higher byte of frequency

RET
;end sound


Delay:
        mov cx, 0xffff
        delay3:
        mov bx, 0xfff
        delay4:
        dec bx
        jnz delay4
        dec cx
        jnz delay3
RET
;end Delay




Play:

        pusha                    ;save regs
        lea si, [TONE_TABLE]    ;load sound data
GET_NEXT:
        mov di,[si]              ;grab a tone
        call Sound              ;play it
        call Delay



        add si,2
        cmp word [si],0
        jne GET_NEXT

        mov  dx,0                        ;turn off speaker
        call ChangeSpeaker





        ;restore regs and return
        popa
RET
;end Play



          SECTION .data

TONE_TABLE    dw 16,32,65,130,261,523,1046,0



CASE 2--
these 2 modules will not compile, they follow the same 'call external asm routine'
format except this should be compiled to ELF EXECUTABLE format ,ie it's NOT
a loadable module

paul@box:~$ nasm -f elf test.asm
paul@box:~$ gcc -O3 test.c test.o -o TEST
In file included from /usr/include/linux/sched.h:14,
from /usr/include/linux/mm.h:4,
from test.c:1:
/usr/include/linux/timex.h:173: field `time' has incomplete type
In file included from /usr/include/asm/smp.h:15,
from /usr/include/linux/smp.h:14,
from /usr/include/linux/sched.h:23,
from /usr/include/linux/mm.h:4,
from test.c:1:
/usr/include/asm/fixmap.h:80: parse error before "pgprot_t"
In file included from /usr/include/asm/smp.h:21,
from /usr/include/linux/smp.h:14,
from /usr/include/linux/sched.h:23,
from /usr/include/linux/mm.h:4,
from test.c:1:
/usr/include/asm/apic.h:85: parse error before "unsigned"
In file included from /usr/include/linux/sched.h:23,
from /usr/include/linux/mm.h:4,
from test.c:1:
/usr/include/linux/smp.h:29: parse error before '(' token

as you can see ,problems with headers.
I need __get_free_pages() ,which is defined in linux/mm.h
Are there really typos in these headers or is it the oder in
which they are included???
I have found that sometimes order matters if header.h is dependant on variables declared in another header, the latter must precede the former


[CODE

--------------------- code case 2---------------------------------


===== c module ============

#include <linux/mm.h>








int * kep;



void y()
{


// ring0 code to jmp to
asm volatile ("mov $0x666,%di; in $0x61,%al" );
asm volatile ("or $3,%al;out %al,$0x61" );
asm volatile ("mov $0xb6,%al;out %al,$0x43");
asm volatile ("mov $0x14,%dx; mov $0x4F38,%ax");
asm volatile ("div %di; out %al,$0x42");
asm volatile ("mov %ah,%al; out %al,$0x42 ");
}


main()
{
kep = __get_free_pages(0x40, 3);
JUMP();


}


================== asm module ====================

; test source file for assembling to ELF
; build with:
; nasm -f elf elftest.asm
; gcc -o elftest elftest.c elftest.o
; (assuming your gcc is ELF)

; This file should test the following:

BITS 32

GLOBAL JUMP
EXTERN kep
EXTERN y


SECTION .text



JUMP:
mov dword esi,[kep]
lea edi,[y]
mov byte [esi],0xea
mov dword [esi+1],edi
jmp dword [kep]










SECTION .data




[/CODE]

320mb 10-26-2004 08:33 PM

Well hmm.........for case code #1 -- I tried this.......
Code:

Chessmaster ~/tiny elf/tiny $ nasm -f elf sound.asm
Chessmaster ~/tiny elf/tiny $ gcc -Wall -s -nostartfiles sound.o
Chessmaster ~/tiny elf/tiny $ ./a.out ; echo $7
Segmentation fault

it compiled and linked just fine and gave me an executable called a.out-- and they it seg faulted as you can see .........I did this without using your C code...........
and when I tried it just exactly as you did........I got the same error.......except I have the 2.4.27 kernel...........BUT the compiled version was for 2.4.22...........

your code looks good, but something in the kernel headers and the version of Glibc for which they were compiled against must be affecting the output.......


All times are GMT -5. The time now is 07:59 PM.