using #defines from <asm/*> in gas

Тема в разделе "WASM.UNIX", создана пользователем nopnopnop, 15 июл 2006.

  1. nopnopnop

    nopnopnop New Member

    Публикаций:
    0
    Регистрация:
    20 апр 2006
    Сообщения:
    15
    Hi all
    I wondering how to use constants , defined in /usr/include/asm/*.h files in gas.Documentation says,
    if i has an *.S file, i can use c/c++ preprocessor in gas.I has following lines in file demonize.S:
    Код (Text):
    1. #include <asm/unistd.h>
    2. #include <asm/fcntl.h>
    3. .
    4. .
    5. .
    6. movl    $__NR_open,%eax
    7. movl    $O_RDWR,%ebx
    8. orl $O_CREAT,%ebx
    9. orl $O_EXCL,%ebx
    10. .
    11. .
    Can't compile it: errors from ld:
    Код (Text):
    1. as -gstabs -o demonize.o demonize.S
    2. gcc -o cvd main.o setopts.o demonize.o -g
    3. demonize.o(.text+0x2b):demonize.S:42: undefined reference to `__NR_open'
    4. demonize.o(.text+0x30):demonize.S:43: undefined reference to `O_RDWR'
    5. demonize.o(.text+0x36):demonize.S:44: undefined reference to `O_CREAT'
    6. demonize.o(.text+0x3c):demonize.S:45: undefined reference to `O_EXCL'
    7. collect2: ld returned 1 exit status
    8. make: *** [cvd] Ошибка 1
    My Makefile:
    Код (Text):
    1. all: cvd
    2. cvd: main.o setopts.o demonize.o
    3.     gcc -o cvd main.o setopts.o demonize.o -g
    4. main.o: main.S
    5.     as -gstabs -o main.o main.S
    6. setopts.o: setopts.S
    7.     as -gstabs -o setopts.o setopts.S
    8. demonize.o: demonize.S
    9.     as -gstabs -o demonize.o demonize.S
    Thanks
     
  2. Quantum

    Quantum Паладин дзена

    Публикаций:
    0
    Регистрация:
    6 янв 2003
    Сообщения:
    3.143
    Адрес:
    Ukraine
    nopnopnop
    gas treats all unknown (undefined) symbols as externals. You need to ensure thouse constants are actually defined somewhere (i.e. in the *.h files). But it's easier to define 'em directly in your source:
    Код (Text):
    1. O_RDWR = 2
    2. O_CREAT = 00400
    3. O_EXCL = 02000
    4. __NR_open = 5
     
  3. nopnopnop

    nopnopnop New Member

    Публикаций:
    0
    Регистрация:
    20 апр 2006
    Сообщения:
    15
    Hi Quantum
    This is the problem ,for which i want to find solution :) Thouse constants NOT defined! They are defined
    in files <asm/unistd.h> and <asm/fcntl.h>, but i can't refer to them in my assembly code. For a while
    i redefined constats by my self in .data section:
    Код (Text):
    1.             .equ SYS_open, 5
    2.             .equ SYS_write, 4
    3.             .equ SYS_read, 3
    4.             .equ SYS_getpid, 20
    5.             .equ SYS_chdir, 12
    6.             .equ O_WRONLY, 01
    7.             .equ O_RDWR, 02
    8.             .equ O_CREAT, 0100
    9.             .equ O_EXCL, 0200
    10.             .equ O_RDONLY, 00
    11.             .equ O_APPEND, 02000
    But it's definitelly not prefferred way! I took a look at sources in "linux-2.6.17/arch/i386/kernel" *.S files...
    They DO have use of cpp(c/c++ preprocessor: lines #include...),but i can't fugure out how to use this ficher.
    May be i have an error in my Makefile, may be i omiited something in the sintax, may be something else...
    Don't you thing it will be much more convenient(and portable: O_CREAT in my system is 200)
    to use include files provided by the distribution? And, finally, those files, /usr/include/asm/*.h - what is
    good for ?
     
  4. Quantum

    Quantum Паладин дзена

    Публикаций:
    0
    Регистрация:
    6 янв 2003
    Сообщения:
    3.143
    Адрес:
    Ukraine
    nopnopnop
    The # character is interpreted as the start of a single-line comment, so #include won't work in gas. But there's .include keyword available. Not of much help, because the #define's would be treated as comments too. Have you read the man? (here it is: http://www.gnu.org/software/binutils/manual/gas-2.9.1/html_mono/as.html)

    Maybe, it's possible to tweak gcc (-E option performs preprocessing only) to compile *.S files.
     
  5. nopnopnop

    nopnopnop New Member

    Публикаций:
    0
    Регистрация:
    20 апр 2006
    Сообщения:
    15
    HI
    1. -E option not recognized by as ( on my box running FC4 )
    2. # character in *.S files passed to cpp, therefor it could be used as comment
    3. .include derective works fine, but in defferent case. It passes source file to gasp preprosesor( accordig
    to documentation - i can't find such a program ) It's impossible to use this derective to use system-defined
    constatnts.

    I tryed to compile my project like this: gcc -o cvd main.S setopts.S demonize.S
    Dosn't work, but it was close. There was couple of struct definitions in those headers files, thats why
    compilation failed. But again...i now it must work! Here pice of code from "linux/boot/head.S"
    Код (Text):
    1. .text
    2.  
    3. #include <linux/linkage.h>
    4. #include <asm/segment.h>
    5. #include <asm/page.h>
    6.  
    7.     .globl startup_32
    8.    
    9. startup_32:
    10.     cld
    11.     cli
    12.     movl $(__BOOT_DS),%eax
    13.     movl %eax,%ds
    14.     movl %eax,%es
    15.     movl %eax,%fs
    16.     movl %eax,%gs
    When i do something like this ( movl $__NR_open,%eax ) it dosn't work...
     
  6. Iceberg

    Iceberg New Member

    Публикаций:
    0
    Регистрация:
    5 дек 2005
    Сообщения:
    54
    Адрес:
    Санкт-Петербург
    Quantum
    directives
    Код (Text):
    1. #include
    2. #define
    perfectly work in gas.
     
  7. Iceberg

    Iceberg New Member

    Публикаций:
    0
    Регистрация:
    5 дек 2005
    Сообщения:
    54
    Адрес:
    Санкт-Петербург
    nopnopnop
    Here an example on use of structures

    In header file:
    Код (Text):
    1. struct KSome
    2. # ...
    3.     fld Entry, QWORD * KOBJ_TABLE_SIZE
    4. # ...
    5. ends KSome
    6.  
    7. struct ListEntry
    8.     fld Flink,  PVOID
    9.     fld Blink,  PVOID
    10. ends ListEntry
    and in asm file:

    Код (Text):
    1.     movl    $_kFoo + f.Entry, %edi
    2. 0:  movl    %edi,   f.Flink(%edi)       // ListHead->Flink = ListHead->Blink = ListHead;
    3.     movl    %edi,   f.Blink(%edi)
     
  8. nopnopnop

    nopnopnop New Member

    Публикаций:
    0
    Регистрация:
    20 апр 2006
    Сообщения:
    15
    My question was about interaction between gas and preprocessor, not about structs.Take a look at the first
    post: do you see any error in my code ? Whi it dosn't work...
    P.S.
    where did you find description of struct .. ends in gas? Can i have a link?
     
  9. Quantum

    Quantum Паладин дзена

    Публикаций:
    0
    Регистрация:
    6 янв 2003
    Сообщения:
    3.143
    Адрес:
    Ukraine
    nopnopnop
    gcc preprocesses *.S files (capital S) instead of passing them directly to gas. The -E option was there since version 3.1.1. Maybe, you need to upgrade your gcc.

    Iceberg
    Maybe you call gcc instead of gas? #include/#define usage isn't documented for gas, AFAIK.