bug with -fleading-underscore doubling underscore in some cases on templates

2007-11-20 Thread Stephane Hockenhull
hi, 

we have found a case where g++ will generate template instantiations with two 
underscore prefixes (std::string, "__ZNSs7reserveEm" instead of just 
"_ZNSs7reserveEm") while other .cpp files will compile just fine.

the template stabs sections are correctly setup as "_ZNSs7reserveEm" but the 
assembly code that calls the functions have two underscores instead so the 
linker cannot find the template and complain of undefined reference to 
__X

I tried moving things around in that particular cpp file to make the bug go 
away (and isolate the case) to no avail.

and std::string is the only template that seem to cause the behaviour

i386-unknown-elf-g++ (GCC) 4.1.1

-- 
Stephane Hockenhull
SSC-Studios.com


why are stl template classes not mangled as other classes and templates

2007-11-26 Thread Stephane Hockenhull

hi, it seem stl templates are not mangled as other classes and templates

I was wondering why and where in the g++ source code is that special case 
implemented?

it seem to cause a problem with -fleading-underscore

-- 
Stephane Hockenhull
SSC-Studios.com

PS: why would someone wants to underscore a flea's "ding" is beyond me ;)


Re: why are stl template classes not mangled as other classes and templates

2007-11-26 Thread Stephane Hockenhull
On Monday 26 November 2007 13:40, Joe Buck wrote:
> On Mon, Nov 26, 2007 at 01:33:54PM -0500, Stephane Hockenhull wrote:
> > hi, it seem stl templates are not mangled as other classes and templates
> >
> > I was wondering why and where in the g++ source code is that special case
> > implemented?
> >
> > it seem to cause a problem with -fleading-underscore
>
> AFAIK there is no special case, however there might be a bug lurking
> somewhere.  Do you have a specific case where -fleading-underscore messes
> up?

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34166

the .stabs section differ from the actual call by 1 underscore prefix

-- 
Stephane Hockenhull
SSC-Studios.com


Re: why are stl template classes not mangled as other classes and templates

2007-11-26 Thread Stephane Hockenhull
On Monday 26 November 2007 14:01, Daniel Jacobowitz wrote:
> On Mon, Nov 26, 2007 at 10:40:35AM -0800, Joe Buck wrote:
> > On Mon, Nov 26, 2007 at 01:33:54PM -0500, Stephane Hockenhull wrote:
> > > hi, it seem stl templates are not mangled as other classes and
> > > templates
> > >
> > > I was wondering why and where in the g++ source code is that special
> > > case implemented?
> > >
> > > it seem to cause a problem with -fleading-underscore
> >
> > AFAIK there is no special case, however there might be a bug lurking
> > somewhere.  Do you have a specific case where -fleading-underscore messes
> > up?
>
> There are a few special cases, e.g. for std::string and
> std::basic_string; the C++ ABI documents have the details.
>
> _ZSi -> std::basic_istream >

those cause problem with -fleading-underscore.

it seem there is more than one data path for the mangling and one of those 
misses adding the extra underscore which cause missing symbols at linking.

-- 
Stephane Hockenhull
SSC-Studios.com


Re: why are stl template classes not mangled as other classes and templates

2007-11-27 Thread Stephane Hockenhull
On Monday 26 November 2007 19:29, Joe Buck wrote:
> On Mon, Nov 26, 2007 at 04:02:48PM -0500, Stephane Hockenhull wrote:
> > On Monday 26 November 2007 14:01, Daniel Jacobowitz wrote:
> > > On Mon, Nov 26, 2007 at 10:40:35AM -0800, Joe Buck wrote:
> > > > On Mon, Nov 26, 2007 at 01:33:54PM -0500, Stephane Hockenhull wrote:
> > > > > hi, it seem stl templates are not mangled as other classes and
> > > > > templates
> > > > >
> > > > > I was wondering why and where in the g++ source code is that
> > > > > special case implemented?
> > > > >
> > > > > it seem to cause a problem with -fleading-underscore
> > > >
> > > > AFAIK there is no special case, however there might be a bug lurking
> > > > somewhere.  Do you have a specific case where -fleading-underscore
> > > > messes up?
> > >
> > > There are a few special cases, e.g. for std::string and
> > > std::basic_string; the C++ ABI documents have the details.
> > >
> > > _ZSi -> std::basic_istream >
> >
> > those cause problem with -fleading-underscore.
> >
> > it seem there is more than one data path for the mangling and one of
> > those misses adding the extra underscore which cause missing symbols at
> > linking.
>
> Hm.  But -fleading-underscore is there to support an old ABI, the one that
> BSD Unix used to use, and the _ZSi is for the newer common ABI.  Probably
> no one tested the interaction, so I don't know what the correct solution
> is.
>
> But why are you using -fleading-underscore?

on the win32 platform all C symbols requires a leading underscore
everything works fine until we start using std::string objects, there is a bug 
with the special case name mangling.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34166

-- 
Stephane Hockenhull
SSC-Studios.com


Re: why are stl template classes not mangled as other classes and templates

2007-11-27 Thread Stephane Hockenhull
On Monday 26 November 2007 22:48, Daniel Jacobowitz wrote:
> It's the default for a lot of targets.  I'd still like to see a
> concrete example of the problem...

we're cross-compiling to win32, everything works fine until we use the 
std::string template, 
the special case name mangling for std::string is partially broken by 
-fleading-underscore.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34166

-- 
Stephane Hockenhull
SSC-Studios.com


Re: why are stl template classes not mangled as other classes and templates

2007-11-27 Thread Stephane Hockenhull
On Tuesday 27 November 2007 11:09, you wrote:
> On 27 November 2007 15:49, Stephane Hockenhull wrote:
> >> But why are you using -fleading-underscore?
> >
> > on the win32 platform all C symbols requires a leading underscore
>
>   Yes, that is the case on almost all platforms.
>
>   And the compiler *automatically* puts leading underscores on symbols on
> all those platforms already.
>
>   So the question remains: why are you using -fleading-underscores?
>
>   -fleading-underscore is intended for use on platforms that *don't*
> require a leading underscore (e.g. ppc), where you might for special
> reasons want to force the symbols to have an underscore anyway.
>
> > everything works fine until we start using std::string objects, there is
> > a bug with the special case name mangling.
> >
> > http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34166
>
>   Yes, but as you yourself explain, the symbols already have leading
> underscores, and when you use -fleading-underscore, because it fails to
> consistently add *extra* underscores.

no, the symbol have a first underscore because that's how G++ mangles them, it 
ALWAYS add 1 underscore (without -fleading-underscore) and some other 
characters.

without -fleading-underscore for example "strstr" is called "strstr" but win32 
libraries need "_strstr" so -fleading-underscore must be used

with -fleading-underscore gcc/g++ adds an underscore to ALL symbols, including 
mangled ones, therefore it need TWO underscores for mangled names and ONE 
underscore for normal identifiers.

which g++ does right 99.9% of the time, but it only does it halfway to 
std::string.

it tries to call it as "__ZNSs7replaceEmmPKc"
but it instanciate it as "_ZNSs7replaceEmmPKc"

which is clearly a bug

I just need to know where in the code this boolean option is located and where 
the mangling is done so I can patch it up.

>   So: why are you using -fleading-underscores?
>
> cheers,
>   DaveK


I'm an underscorophile, underscores makes me horny, that's why I drive around 
town in an blue-screen-painted icecream truck playing windows's boot up 
sounds to attract little windows symbols with underscores and "@" signs. 

joking aside, we need to generate ELF object files for running on windows.

-- 
Stephane Hockenhull
SSC-Studios.com


Re: why are stl template classes not mangled as other classes andtemplates

2007-11-27 Thread Stephane Hockenhull
On Tuesday 27 November 2007 14:00, Dave Korn wrote:
> On 27 November 2007 18:47, 'Daniel Jacobowitz' wrote:
> > On Tue, Nov 27, 2007 at 06:39:09PM -, Dave Korn wrote:
> >>> joking aside, we need to generate ELF object files for running on
> >>> windows.
> >>
> >>   OK, you are now attempting something very very wrong indeed.  The
> >> win32 version of the assembler will not generate ELF files, and even if
> >> it did, windows cannot make any use of the ELF format, it uses PE-COFF. 
> >> And the output file format has absolutely nothing to do with whether or
> >> not the symbols have leading underscores, so claiming that you have to
> >> use -fleading-underscores because you need to generate ELF files is a
> >> non-sequitur.
> >
> > Please give them a little credit.  I would assume they're using an
> > i386-elf compiler and have some mechanism of their own to make the ELF
> > files run.
>
>   I am making no assumptions either way; I'd like some clarification.
>
>   Wouldn't a simpler and more-likely-to-be-reliable technique be to use a
> win32 /compiler/ (so as to get all the ABI issues right, not just the
> amount of underscores on symbols) and only generate/convert to ELF further
> down the toolchain?

no, it would not.
because for one simple fact: COFF format lacks many features of ELF.

this is why we're compiling, assembling, linking, patching, and dynamically 
re-linking everything in ELF format, including using a custom linker script 
that generates an ELF file that is win32 "compatible" (load addresses and 
all)

we then convert the final executable to PE format and run it on the target 
platform.

this even allows us to debug the win32 application from linux using the ELF 
version of the executable file and system's default GDB, or Kdbg, or DDD, or 
what not, with a gdb stub so even if we're running fullscreen under windows 
we can still debug even tho the win32 GUI is frozen and a normal win32 
debugger would just deadlock the system.

this turns debugging under win32 into something akin to working on an embedded 
platform, we also have the same source code running natively on GameCube 
(PPC32-BE), Nintendo DS (ARM-LE), and Linux AMD64 among others.

please dont worry for us about ABI issues and what not, I've got that part 
working, thank you.

now, if only someone actually knew where in the g++ source code the special 
case for std::string is I could fix that, provide a patch, and make the world 
a little bit better.

-- 
Stephane Hockenhull
SSC-Studios.com


Re: why are stl template classes not mangled as other classes andtemplates

2007-11-27 Thread Stephane Hockenhull
On Tuesday 27 November 2007 15:40, Andrew Pinski wrote:
> On 11/27/07, Stephane Hockenhull <[EMAIL PROTECTED]> wrote:
> > no, it would not.
> > because for one simple fact: COFF format lacks many features of ELF.
>
> What features are missing?  COFF is not used for Windows 32 anyways,
> it is PE-COFF.  I am just wondering what exactly is missing that you
> think you need to go this route.
>
> Thanks,
> Andrew Pinski

we had some problem with some missing flags, like weak and others special 
attributes we use in some parts to provide extended features.

but anyway, we have some special processing done where we need to be able to 
read the object files and we also use a custom dynamic linker so everything 
has to be in ELF format.

BFD is really nice, but a bit too big to run on one of our platforms (3MB RAM 
embedded system) so we had to develop custom routines anyway and elf is easy 
enough to parse, so for KISS reasons those routines ends up on all platforms.

I can't say much more.

all I can say is going the all-ELF way right to the end really works nicely, 
*EXCEPT* for that std::string issue.

-- 
Stephane Hockenhull
SSC-Studios.com


Re: why are stl template classes not mangled as other classes andtemplates

2007-11-27 Thread Stephane Hockenhull
On Tuesday 27 November 2007 15:50, Andreas Schwab wrote:
> Stephane Hockenhull <[EMAIL PROTECTED]> writes:
> > now, if only someone actually knew where in the g++ source code the
> > special case for std::string is
>
> grep is your friend.  Look for find_substitution in cp/mangle.c.
>
> Andreas.

*THANK YOU*

... checking now

-- 
Stephane Hockenhull
SSC-Studios.com


Re: why are stl template classes not mangled as other classes andtemplates

2007-11-28 Thread Stephane Hockenhull
On Tuesday 27 November 2007 15:50, Andreas Schwab wrote:
> Stephane Hockenhull <[EMAIL PROTECTED]> writes:
> > now, if only someone actually knew where in the g++ source code the
> > special case for std::string is
>
> grep is your friend.  Look for find_substitution in cp/mangle.c.
>
> Andreas.

something just occured to me ... the std::string template is not compiled into 
the .s file

are those templates pre-compiled into some "magical" hidden library?
I could not find them in my gcc installations both native and i386-unknown-elf

grep 'ZNSs7replaceEmmRKSs' * -R

didnt find anything (compressed?).

where is that template instanciated ?

-- 
Stephane Hockenhull
SSC-Studios.com


Re: why are stl template classes not mangled as other classes andtemplates

2007-11-28 Thread Stephane Hockenhull
On Wednesday 28 November 2007 12:35, Dave Korn wrote:
> On 28 November 2007 17:33, Stephane Hockenhull wrote:
> > something just occured to me ... the std::string template is not compiled
> > into the .s file
> >
> > are those templates pre-compiled into some "magical" hidden library?
> > I could not find them in my gcc installations both native and
> > i386-unknown-elf
> >
> > grep 'ZNSs7replaceEmmRKSs' * -R
> >
> > didnt find anything (compressed?).
> >
> > where is that template instanciated ?
>
>   The instantiations end up in the .o files of your project when you
> compile, not in the compiler or its support libraries.  Does this page:
>
> http://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.html#Template-Inst
>ant iation
>
> answer the question for you?
>
>
> cheers,
>   DaveK

no, it does not.
I know templates *NORMALLY* end up in the .s file after the compilation pass 
then the .o file after the assembly pass.

if you actually read my email I said :
> > the std::string template is > NOT < compiled into the .s 
file

g++ does not seem to put the std::string template's code into the .o file like 
the other templates.

it seem to NOT be instanciated in the .o file.

I guess it would make sense to have the std::string class compiled once 
instead of in every file since its used everywhere, that would greatly speed 
up compile time but would cause problem in my case.

I have the .s file right here and there is no assembly code for the 
std::string template's instance at all. my other templates are there, just 
not std::string.

hence my question: where is it?

and, if it IS precompiled, can I force g++ to compile td::string every time to 
see if that fixes the problem?

-- 
Stephane Hockenhull
SSC-Studios.com


Re: why are stl template classes not mangled as other classes andtemplates

2007-11-28 Thread Stephane Hockenhull
On Wednesday 28 November 2007 14:01, 'Daniel Jacobowitz' wrote:
> On Wed, Nov 28, 2007 at 01:56:58PM -0500, Stephane Hockenhull wrote:
> > hence my question: where is it?
>
> In libstdc++.  You have to link with libstdc++ to use the STL, for
> many reasons including this one.

is there a way to disable this behaviour for stl templates and have them 
compiled every time?

I can't find a command line option for it.

this breaks C++ code using the STL when -fleading-underscore is used, other 
templates compile and link just fine.

-- 
Stephane Hockenhull
SSC-Studios.com


Re: why are stl template classes not mangled as other classes andtemplates

2007-11-28 Thread Stephane Hockenhull
On Wednesday 28 November 2007 14:01, 'Daniel Jacobowitz' wrote:
> On Wed, Nov 28, 2007 at 01:56:58PM -0500, Stephane Hockenhull wrote:
> > hence my question: where is it?
>
> In libstdc++.  You have to link with libstdc++ to use the STL, for
> many reasons including this one.

ar x ../libstdc++.a
grep 'ZNSs7replaceEmmPKc' *.o

does not find any matches.

-- 
Stephane Hockenhull
SSC-Studios.com


Re: why are stl template classes not mangled as other classes andtemplates

2007-11-28 Thread Stephane Hockenhull
On Wednesday 28 November 2007 16:25, Andrew Pinski wrote:
> On 11/28/07, Stephane Hockenhull <[EMAIL PROTECTED]> wrote:
> > is there a way to disable this behaviour for stl templates and have them
> > compiled every time?
> >
> > I can't find a command line option for it.
>
> It is a source level option.  Remove all the use of "extern template"
> in the headers of libstdc++.
>
> -- Pinski

YES! works! thank you very much!

there is a define to disable them in bunch:  _GLIBCXX_EXTERN_TEMPLATE

g++ -c -save-temps -fleading-underscore -D_GLIBCXX_EXTERN_TEMPLATE=0 test.cpp 
-o test.o

templates get compiled in just fine, with correct underscore, no need to 
recompile libstdc++

*drops on the floor side-walking in circle whooping a la Homer S.*

I added a comment to bug for the work around
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34166

but left it unconfirmed, should it be closed, just documented, or the feature 
automatically disabled if someone overrides the platform's default prefix 
rule ??

-- 
Stephane Hockenhull
SSC-Studios.com


Re: why are stl template classes not mangled as other classes andtemplates

2007-11-28 Thread Stephane Hockenhull
On Wednesday 28 November 2007 17:24, Jonathan Wakely wrote:
> On 28/11/2007, Stephane Hockenhull <[EMAIL PROTECTED]> wrote:
> > On Wednesday 28 November 2007 14:01, 'Daniel Jacobowitz' wrote:
> > > On Wed, Nov 28, 2007 at 01:56:58PM -0500, Stephane Hockenhull wrote:
> > > > hence my question: where is it?
> > >
> > > In libstdc++.  You have to link with libstdc++ to use the STL, for
> > > many reasons including this one.
> >
> > is there a way to disable this behaviour for stl templates and have them
> > compiled every time?
> >
> > I can't find a command line option for it.
> >
> > this breaks C++ code using the STL when -fleading-underscore is used,
> > other templates compile and link just fine.
>
> IIRC, -D_GLIBCXX_EXTERN_TEMPLATE=0 might work, but it's neither
> documented nor supported.
>
> Jon

thanks, found it grepping for "extern template"

it does work, worst case if it breaks again we'll stick with the gcc version 
we're currently using until we find some work around :P

-- 
Stephane Hockenhull
SSC-Studios.com