Noein
Noein

Reputation: 612

How to override `--prefix` option in NASM per symbol?

I'm compiling C code with GCC and assembling some x86 code with NASM on Windows.

Now, GCC by default (and I haven't been able to find an option to change this) prepends an underscore _ to all external symbol names (and expected names).

I need this assembly code to work with GCC on both Windows and Linux and would like to avoid hacks as much as possible (and code duplication; I had separate .s files for Windows/Linux at first).

I found out about (and used) the --prefix flag in NASM. Now for some symbols I'd like NASM to treat them as without the leading underscore (exact situation right now is that I need to reference the entry point in a linker script without the leading underscore). Hence the question here on how to override, per symbol, the --prefix/--postfix flags of NASM.

Feel free to treat this as an XY problem. If there's a way to set the mangling scheme of GCC for C that'd be great, for example.

Upvotes: 1

Views: 521

Answers (1)

rkhb
rkhb

Reputation: 14409

I stumbled upon the same problem. I've created an include file with a lot of defines like

%define printf  _printf
%define puts    _puts
%define scanf   _scanf

and some other stuff.

That file (libc_win32.in) is included by a "master" include file (libc.inc):

%ifndef LIBC_INC
%define LIBC_INC

%ifdef win32
    %include 'libc_win32.inc'
%elifdef win64
    %include 'libc_win64.inc'
%elifdef elf32
    %include 'libc_elf32.inc'
%elifdef elf64
    %include 'libc_elf64.inc'
%else
;   %error "libc.inc"
%endif

%endif

I set the symbols and include the files at the command line:

nasm -fwin32 -dwin32 -plibc.inc ...

or

nasm -felf32 -delf32 -plibc.inc ...

There is a predefined macro called __OUTPUT_FORMAT__, but it works only inside of a macro, not at program start.

Upvotes: 0

Related Questions