Bug 39662 - segmentation failed on correctly compiled mixed C and C++ code
Summary: segmentation failed on correctly compiled mixed C and C++ code
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.2.4
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 39661 (view as bug list)
Depends on:
Blocks:
 
Reported: 2009-04-06 12:20 UTC by Pavel Petrovic
Modified: 2009-04-23 06:06 UTC (History)
2 users (show)

See Also:
Host: i486-linux-gnu
Target: i486-linux-gnu
Build: i486-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments
archive with all sources, and i. .ii, .s files (9.00 KB, application/octet-stream)
2009-04-06 12:23 UTC, Pavel Petrovic
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Pavel Petrovic 2009-04-06 12:20:45 UTC
I understand C is low-level, but I'd love the compiler to refuse to compile or link a mixed C and C++ code that is not mixed correctly.

The story is: parts of the program are implemented in C, parts are in C++. The functions in C++ are made callable from C using the "__attribute__((stdcall))" feature, i.e. these are C-type functions implemented in C++. The main program is in C, and attempts to call the C-style C++ function, which only computes a number. The program crashes on exit from the main() function. It does not crash if compiled using gcc/g++ version 3.3. However, removing the obsolete line "int a = 1;" makes the program crash also when compiled using 3.3 version of the compliers. Here are the sources:
------------------------------------------------------------
/* fnc.h - prototype of C++ function with C-style calling style  int f2(nt int) */

#ifdef __cplusplus
#define EXPORTCALL __attribute__((stdcall)) 
#else
#define EXPORTCALL 
#endif

#ifdef __cplusplus
extern "C" {
#endif

extern int EXPORTCALL f2(int a, int b);

#ifdef __cplusplus
}
#endif
---------------------------------------------------------------
/* fnmc.h - main C program that calls a C++ function   int f2(int, int) */

#include <stdio.h>
#include "fnc.h"

int main(int argc, char **argv)
{
  int a = 1;

  printf("f2(2,3)=%d\n", f2(2,3));
  printf("exiting...\n");
  return;
}
------------------------------------------------------------------
/* fnc.cpp - implementation of the C++ function   int f2(int, int), 
             using the C-calling semantics */

#include "fnc.h"      /* function prototype for f2() */

/* the function only returns the result */

int EXPORTCALL f2(int a, int b)
{
  return a+b;
}
------------------------------------------------------------------
# Makefile

GCC=gcc
GPP=g++

# GCC=gcc-3.3
# GPP=g++-3.3

all:    fn3

fn3:	fnc.cpp fnmc.c
	$(GPP) -v -save-temps -c fnc.cpp -g
	$(GCC) -v -save-temps -c fnmc.c -g
	$(GPP) -v -save-temps -o fn3 fnc.o fnmc.o -g

clean:
	rm -f *.o *.s *.i *.ii
	rm -f fn3 
------------------------------------------------------------------------
Here is the what is obtained when running:


petrovic@student03:~/pasCPP2$ make clean
rm -f *.o *.s *.i *.ii
rm -f fn3 
petrovic@student03:~/pasCPP2$ make
g++ -v -save-temps -c fnc.cpp -g
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu3)
 /usr/lib/gcc/i486-linux-gnu/4.2.4/cc1plus -E -quiet -v -D_GNU_SOURCE fnc.cpp -mtune=generic -fworking-directory -fpch-preprocess -o fnc.ii
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../i486-linux-gnu/include"
ignoring nonexistent directory "/usr/include/i486-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.2
 /usr/include/c++/4.2/i486-linux-gnu
 /usr/include/c++/4.2/backward
 /usr/local/include
 /usr/lib/gcc/i486-linux-gnu/4.2.4/include
 /usr/include
End of search list.
 /usr/lib/gcc/i486-linux-gnu/4.2.4/cc1plus -fpreprocessed fnc.ii -quiet -dumpbase fnc.cpp -mtune=generic -auxbase fnc -g -version -fstack-protector -fstack-protector -o fnc.s
GNU C++ version 4.2.4 (Ubuntu 4.2.4-1ubuntu3) (i486-linux-gnu)
	compiled by GNU C version 4.2.4 (Ubuntu 4.2.4-1ubuntu3).
GGC heuristics: --param ggc-min-expand=98 --param ggc-min-heapsize=128358
Compiler executable checksum: cca9b7b48c023363b938f208576b99cc
 as --traditional-format -V -Qy -o fnc.o fnc.s
GNU assembler version 2.18.0 (i486-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.18.0.20080103
gcc -v -save-temps -c fnmc.c -g
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu3)
 /usr/lib/gcc/i486-linux-gnu/4.2.4/cc1 -E -quiet -v fnmc.c -mtune=generic -fworking-directory -fpch-preprocess -o fnmc.i
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../i486-linux-gnu/include"
ignoring nonexistent directory "/usr/include/i486-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/i486-linux-gnu/4.2.4/include
 /usr/include
End of search list.
 /usr/lib/gcc/i486-linux-gnu/4.2.4/cc1 -fpreprocessed fnmc.i -quiet -dumpbase fnmc.c -mtune=generic -auxbase fnmc -g -version -fstack-protector -fstack-protector -o fnmc.s
GNU C version 4.2.4 (Ubuntu 4.2.4-1ubuntu3) (i486-linux-gnu)
	compiled by GNU C version 4.2.4 (Ubuntu 4.2.4-1ubuntu3).
GGC heuristics: --param ggc-min-expand=98 --param ggc-min-heapsize=128358
Compiler executable checksum: e2971cf2189271aeeb10133511ecea3a
 as --traditional-format -V -Qy -o fnmc.o fnmc.s
GNU assembler version 2.18.0 (i486-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.18.0.20080103
g++ -v -save-temps -o fn3 fnc.o fnmc.o -g
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu3)
 /usr/lib/gcc/i486-linux-gnu/4.2.4/collect2 --eh-frame-hdr -m elf_i386 --hash-style=both -dynamic-linker /lib/ld-linux.so.2 -o fn3 /usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib/crt1.o /usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib/crti.o /usr/lib/gcc/i486-linux-gnu/4.2.4/crtbegin.o -L/usr/lib/gcc/i486-linux-gnu/4.2.4 -L/usr/lib/gcc/i486-linux-gnu/4.2.4 -L/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/i486-linux-gnu/4.2.4/../../.. fnc.o fnmc.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/i486-linux-gnu/4.2.4/crtend.o /usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib/crtn.o
petrovic@student03:~/pasCPP2$ ./fn3 
f2(2,3)=5
exiting...
Segmentation fault
petrovic@student03:~/pasCPP2$

------------------------------------------------------------------
I understand, I am probably doing the mixing of C and C++ incorrectly, although I am not sure where (hints are welcome!), but still, would it be possible to get the compiler/linker to produce a warning, if it is generating a code that crashes? 

P.S. sorry for my previous unfinished post (accidentally clicked ENTER before finishing the report)
Comment 1 Pavel Petrovic 2009-04-06 12:23:39 UTC
Created attachment 17594 [details]
archive with all sources, and i. .ii, .s files
Comment 2 Pavel Petrovic 2009-04-06 12:28:26 UTC
*** Bug 39661 has been marked as a duplicate of this bug. ***
Comment 3 Andrew Pinski 2009-04-22 23:41:45 UTC
#ifdef __cplusplus
#define EXPORTCALL __attribute__((stdcall)) 
#else
#define EXPORTCALL 
#endif


Is wrong, you need to use the same ABI for the function on both sides of the call, that is either use stdcall on both sides (C and C++) or don't use it at all.
Comment 4 Pavel Petrovic 2009-04-23 06:06:46 UTC
Thanks! it works. I was confused by this document: ftp://ftp.freepascal.org/pub/fpc/docs-pdf/CinFreePascal.pdf which seems to claim the opposite.