Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc assembler preprocessor not compatible with standard headers

The man page for gcc states

   file.s
       Assembler code.

   file.S
   file.sx
       Assembler code that must be preprocessed.

And many standard include files have

#ifndef __ASSEMBLY__ 
...
#endif

wrappers to allow inclusion from assembly files. I could have sworn I've written programs before with gcc and it defined this when assembling, but now I'm running into problems.

Here's some test code:

test.S

#include <sys/syscall.h>
#include <asm/signal.h>
    .intel_syntax noprefix

    .text
    .global foo // int foo(int pid)
foo:
    mov esi,SIGUSR1
    mov eax,SYS_kill
    syscall
    ret

When I run gcc -c test.S, it complains about all kinds of stuff in the asm/signal.h because it doesn't see __ASSEMBLY__ defined.

For now my work around is:

#ifndef __ASSEMBLY__
#define __ASSEMBLY__
#endif

But this just seems wrong to have to add this to all my files.

Is this a bug in GCC?
Or am I doing something wrong here?

NOTE:
I see in a test that gcc does define __ASSEMBLER__ but most of the header files test for __ASSEMBLY__ (I do see a couple that test for __ASSEMBLER__). Was the appropriate ifdef changed at some point?

I am using Ubuntu 14.04, and gcc reports version: gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2

like image 997
RabbitEars Avatar asked Mar 08 '15 08:03

RabbitEars


1 Answers

__ASSEMBLY__ is a convention that the Linux kernel project made up themselves before they knew about the existence of the gcc predefined macro __ASSEMBLER__.

The linux kernel passes down __ASSEMBLY__ explicitly in linux/Makefile:

KBUILD_AFLAGS   := -D__ASSEMBLY__

There were patches posted on LKML to migrate to __ASSEMBLER__ in 2005 but they were not merged: Re: [RFC][MEGAPATCH] Change ASSEMBLY to ASSEMBLER (defined by GCC from 2.95 to current CVS)

like image 144
scottt Avatar answered Oct 08 '22 18:10

scottt