[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: assembler compiling

> From: "Petr Hanyk" <hanyk@xxxxxxx.cz>
> Date: Tue, 4 Sep 2001 10:14:21 +0200

> We have developed our own graphical library which uses some
> assembler code (engine.asm).

Unless this library is a completely tested, trusted, known
absolutely stable as it stands on an ETRAX platform, may I
suggest rewriting it in C?  I can't think of a reason why
writing it in assembler code would be a win.

> The assembler (engine.asm) defines global functions with
> underscore prefixed:

Did you perhaps develop this application on a pre-LX platform?

>     .global _draw_point
> ...
> _draw_point:
>     PUSH    SRP
>     MOVE.D  R10,[oldx]
>     MOVE.D  R11,[oldy]
>     CMP.D   [_clip_x_min],R10

But variables are sometimes underscored, sometimes not?

> The assembler functions are  declared in base.h and used in base.cpp.
> base.h:
> ...
> void draw_point(int x, int y) asm("_draw_point");

The usual method to interface to non-C++ functions is to declare
them (in base.h) like this:

#ifdef __cplusplus
extern "C" {
#ifdef __cplusplus

Of course, if the functions are written in assembly code, that
requires that their underscoreness matches that of C

> // this line force linker to linking with fgl.lib implicitly
> #pragma aux draw_point "_*"

That comment is incorrect.  The #pragma will be ignored, because
it isn't recognized, and isn't on any standard gcc platform
AFAICT.  You specify -lfgl on the link line below, so libfgl.a
(or libfgl.so if it exists) will be picked up automatically.
Perhaps this library was developed on another completely
different platform?  With assembly code translated manually, I

> To compile engine.asm we use as-cris engine.asm -o engine.o.

You should use "gcc-cris -xassembler-with-cpp -mlinux" to
assemble it.  Then you don't have to worry about how "-mlinux"
maps into assembler options: it does, but I want to be able to
change the mapping so please use gcc-cris for assembling.

> g++-cris -mlinux -xc++ -isystem /opt/axis/dr1a_lx_20/eroot/include -Wall =
> -O2 -Wno-ctor-dtor-privacy -ansi -pipe -c -o testfastgl.o testfastgl.cpp
> gcc-cris -mlinux -isystem /opt/axis/dr1a_lx_20/eroot/include -s =
> -L/opt/axis/dr1a_lx_20/eroot/lib -s -fPIC testfastgl.o -lfgl -ldl =
> -lstdc++ -o testfastgl

Why compile the *application* position-independent (-fPIC)?
There's no need to do that, and it will impose a speed penalty,
so I suggest removing -fPIC there.

> /usr/local/cris/lib/gcc-lib/cris/2.96/rld-cris: engine.o: uses
> _-prefixed symbols, but writing file with non-prefixed symbols

There it is; the reason.  It might not be obvious from the
message what the error is, but it states the reason why linking
fails.  I think I should tweak the message to (also) say
something like "objects probably compiled with options
specifying different platforms".  I'd be thankful if you could
suggest a message that makes more sense.

> engine.o: file format elf32-us-cris

For the other files, (compiled with -mlinux), this should say
"foo.o: file format elf32-cris".  For purposes of linking,
they're of different formats.

> private flags = 1: [symbols have a _ prefix]
> ...
> We think that the last line is important, because other .o
> files have private flags = 0.

Yes, because engine.o was assembled with other, incorrect
options.  Here's a list of what to do.  Items 2 and 4 below are
(together) optional, but may help maintenance of your code; the
construct you're using seems to work, but is somewhat

Change all register names in *.asm to have a $-prefix.  From
NEWS for cris-dist-1.9 (current version is cris-dist-1.14):

  Registers can now have an optional dollar-sign-prefix,
  e.g. $r10 and $srp.

  When compiling with the gcc -mlinux option, an ELF variant
  is selected by use of an additional assembler option
  --no-underscore, which is link-incompatible with the a.out and
  default ELF format.  With this option, the dollar-sign-prefix
  to register names is mandatory in assembly code, and symbols
  specified in C code no longer have an underscore character
  prepended by the compiler.

Alternatively, you can add this line at the top of all assembly
 .syntax no_register_prefix

Remove all _-prefixes from identifiers, such as _clip_x_min and

Compile the library with "gcc-cris -mlinux -xassembler-with-cpp".

Remove the asm-namers from the declarations of those functions
in your C++-code.  Instead, declare those functions inside
'extern "C" { ... }'.

Remove the -fPIC from the line linking the main application.
(This is optional, but recommended.)

Since you're linking C++ with gcc-cris, you may find undefined
references from the C++ library to functions in the math
library.  We'll handle that if actually happens; I just want you
to be prepared for that event.

brgds, H-P