public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Anonymous namespaces and global vs.local symbols in nm output
@ 2020-11-24 19:02 Kacvinsky, Tom
  2020-11-24 19:23 ` Xi Ruoyao
  2020-11-24 20:03 ` Jonathan Wakely
  0 siblings, 2 replies; 4+ messages in thread
From: Kacvinsky, Tom @ 2020-11-24 19:02 UTC (permalink / raw)
  To: gcc-help

This code

#include <string>
#include <istream>
#include <iostream>
#include <vector>

typedef std::vector<unsigned char> Signature;

namespace foo {
  bool bar (const std::string& input,
            const Signature& signature,
            std::istream& key)
  {
    return true;
  }
}

when compiled with

g++ -fPIC -o global_noans.so -shared -static-libgcc -static-libstdc++ -Wl,-h,global.so -fuse-ld=gold global_noans.cpp

produces this nm output

0000000000082745 T _ZN3foo3barERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIhSaIhEERSi

Notice how it is a global symbol?

On the other hand, this code#include <string>
#include <istream>
#include <iostream>
#include <vector>

typedef std::vector<unsigned char> Signature;

namespace foo {
  namespace {
    bool bar(const std::string& input,
             const Signature& signature,
             std::istream& key)
    {
      return true;
    }
  }
}

when compiled with

g++ -fPIC -o global.so -shared -static-libgcc -static-libstdc++ -Wl,-h,global.so -fuse-ld=gold global.cpp

produces this nm output

00000000000826b5 t _ZN3foo12_GLOBAL__N_13barERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIhSaIhEERSi

And now we note the symbolis local.  I tried building the shared library with a -Wl,--version-script=global.map, where global.map
is set up to make the symbol glocal instead of local, and that did not work.  I also tried compiling the source code with

void __attribute__ ((visibility ("default")))

but that made no difference, either, in the anonymous namespace symbol being lobal.  I have pinged the binutils list and they said
this might be a g++ version issue (I am using GCC 8.3.0 with binutils 2.34, gold linker).  If this is a g++ issues, I am assuming the issue
is anonymous name spaces make the symbol local, but if that isn't the cause, I'll got back to the binutils folks.  I am also going to try
a later version of GCC and binutils.

Thanks,

TOm



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Anonymous namespaces and global vs.local symbols in nm output
  2020-11-24 19:02 Anonymous namespaces and global vs.local symbols in nm output Kacvinsky, Tom
@ 2020-11-24 19:23 ` Xi Ruoyao
  2020-11-24 20:03 ` Jonathan Wakely
  1 sibling, 0 replies; 4+ messages in thread
From: Xi Ruoyao @ 2020-11-24 19:23 UTC (permalink / raw)
  To: Kacvinsky, Tom, gcc-help

On 2020-11-24 19:02 +0000, Kacvinsky, Tom via Gcc-help wrote:
> This code
> 
> #include <string>
> #include <istream>
> #include <iostream>
> #include <vector>
> 
> typedef std::vector<unsigned char> Signature;
> 
> namespace foo {
>   bool bar (const std::string& input,
>             const Signature& signature,
>             std::istream& key)
>   {
>     return true;
>   }
> }
> 
> when compiled with
> 
> g++ -fPIC -o global_noans.so -shared -static-libgcc -static-libstdc++ -Wl,-
> h,global.so -fuse-ld=gold global_noans.cpp
> 
> produces this nm output
> 
> 0000000000082745 T
> _ZN3foo3barERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorI
> hSaIhEERSi
> 
> Notice how it is a global symbol?
> 
> On the other hand, this code#include <string>
> #include <istream>
> #include <iostream>
> #include <vector>
> 
> typedef std::vector<unsigned char> Signature;
> 
> namespace foo {
>   namespace {
>     bool bar(const std::string& input,
>              const Signature& signature,
>              std::istream& key)
>     {
>       return true;
>     }
>   }
> }
> 
> when compiled with
> 
> g++ -fPIC -o global.so -shared -static-libgcc -static-libstdc++ -Wl,-
> h,global.so -fuse-ld=gold global.cpp
> 
> produces this nm output
> 
> 00000000000826b5 t
> _ZN3foo12_GLOBAL__N_13barERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcE
> EERKSt6vectorIhSaIhEERSi
> 
> And now we note the symbolis local.  I tried building the shared library with
> a -Wl,--version-script=global.map, where global.map
> is set up to make the symbol glocal instead of local, and that did not work. 
> I also tried compiling the source code with
> 
> void __attribute__ ((visibility ("default")))
> 
> but that made no difference, either, in the anonymous namespace symbol being
> lobal.  I have pinged the binutils list and they said
> this might be a g++ version issue (I am using GCC 8.3.0 with binutils 2.34,
> gold linker).  If this is a g++ issues, I am assuming the issue
> is anonymous name spaces make the symbol local, but if that isn't the cause,
> I'll got back to the binutils folks.  I am also going to try
> a later version of GCC and binutils.

The standard says ([basic.link] p4):

> An unnamed namespace or a namespace declared directly or indirectly within an
> unnamed namespace has internal linkage. All other namespaces have external
> linkage. A name having namespace scope that has not been given internal
> linkage above has the same linkage as the enclosing namespace if it is the
> name of
> (4.1) a variable; or
> (4.2) a function; or
> ...
-- 
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Anonymous namespaces and global vs.local symbols in nm output
  2020-11-24 19:02 Anonymous namespaces and global vs.local symbols in nm output Kacvinsky, Tom
  2020-11-24 19:23 ` Xi Ruoyao
@ 2020-11-24 20:03 ` Jonathan Wakely
  2020-11-25  1:28   ` Kacvinsky, Tom
  1 sibling, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2020-11-24 20:03 UTC (permalink / raw)
  To: Kacvinsky, Tom; +Cc: gcc-help

On Tue, 24 Nov 2020 at 19:11, Kacvinsky, Tom via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> This code
>
> #include <string>
> #include <istream>
> #include <iostream>
> #include <vector>
>
> typedef std::vector<unsigned char> Signature;
>
> namespace foo {
>   bool bar (const std::string& input,
>             const Signature& signature,
>             std::istream& key)
>   {
>     return true;
>   }
> }
>
> when compiled with
>
> g++ -fPIC -o global_noans.so -shared -static-libgcc -static-libstdc++ -Wl,-h,global.so -fuse-ld=gold global_noans.cpp
>
> produces this nm output
>
> 0000000000082745 T _ZN3foo3barERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIhSaIhEERSi
>
> Notice how it is a global symbol?
>
> On the other hand, this code#include <string>
> #include <istream>
> #include <iostream>
> #include <vector>
>
> typedef std::vector<unsigned char> Signature;
>
> namespace foo {
>   namespace {
>     bool bar(const std::string& input,
>              const Signature& signature,
>              std::istream& key)
>     {
>       return true;
>     }
>   }
> }
>
> when compiled with
>
> g++ -fPIC -o global.so -shared -static-libgcc -static-libstdc++ -Wl,-h,global.so -fuse-ld=gold global.cpp
>
> produces this nm output
>
> 00000000000826b5 t _ZN3foo12_GLOBAL__N_13barERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIhSaIhEERSi
>
> And now we note the symbolis local.  I tried building the shared library with a -Wl,--version-script=global.map, where global.map
> is set up to make the symbol glocal instead of local, and that did not work.  I also tried compiling the source code with
>
> void __attribute__ ((visibility ("default")))
>
> but that made no difference, either, in the anonymous namespace symbol being lobal.

Why do you want to change it? G++ is doing the right thing.

What are you trying to achieve?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: Anonymous namespaces and global vs.local symbols in nm output
  2020-11-24 20:03 ` Jonathan Wakely
@ 2020-11-25  1:28   ` Kacvinsky, Tom
  0 siblings, 0 replies; 4+ messages in thread
From: Kacvinsky, Tom @ 2020-11-25  1:28 UTC (permalink / raw)
  To: gcc-help

HI,

> -----Original Message-----
> From: Jonathan Wakely <jwakely.gcc@gmail.com>
> Sent: Tuesday, November 24, 2020 3:03 PM
> To: Kacvinsky, Tom <Tom.Kacvinsky@vector.com>
> Cc: gcc-help@gcc.gnu.org
> Subject: Re: Anonymous namespaces and global vs.local symbols in nm
> output
> 
> On Tue, 24 Nov 2020 at 19:11, Kacvinsky, Tom via Gcc-help <gcc-
> help@gcc.gnu.org> wrote:
> >
> > This code
> >
> > #include <string>
> > #include <istream>
> > #include <iostream>
> > #include <vector>
> >
> > typedef std::vector<unsigned char> Signature;
> >
> > namespace foo {
> >   bool bar (const std::string& input,
> >             const Signature& signature,
> >             std::istream& key)
> >   {
> >     return true;
> >   }
> > }
> >
> > when compiled with
> >
> > g++ -fPIC -o global_noans.so -shared -static-libgcc -static-libstdc++
> > g++ -Wl,-h,global.so -fuse-ld=gold global_noans.cpp
> >
> > produces this nm output
> >
> > 0000000000082745 T
> > _ZN3foo3barERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt
> > 6vectorIhSaIhEERSi
> >
> > Notice how it is a global symbol?
> >
> > On the other hand, this code#include <string> #include <istream>
> > #include <iostream> #include <vector>
> >
> > typedef std::vector<unsigned char> Signature;
> >
> > namespace foo {
> >   namespace {
> >     bool bar(const std::string& input,
> >              const Signature& signature,
> >              std::istream& key)
> >     {
> >       return true;
> >     }
> >   }
> > }
> >
> > when compiled with
> >
> > g++ -fPIC -o global.so -shared -static-libgcc -static-libstdc++
> > g++ -Wl,-h,global.so -fuse-ld=gold global.cpp
> >
> > produces this nm output
> >
> > 00000000000826b5 t
> >
> _ZN3foo12_GLOBAL__N_13barERKNSt7__cxx1112basic_stringIcSt11char_trai
> ts
> > IcESaIcEEERKSt6vectorIhSaIhEERSi
> >
> > And now we note the symbolis local.  I tried building the shared
> > library with a -Wl,--version-script=global.map, where global.map is
> > set up to make the symbol glocal instead of local, and that did not
> > work.  I also tried compiling the source code with
> >
> > void __attribute__ ((visibility ("default")))
> >
> > but that made no difference, either, in the anonymous namespace symbol
> being lobal.
> 
> Why do you want to change it? G++ is doing the right thing.
> 
> What are you trying to achieve?

I am not trying to change it, I was more curious than anything as to why what I thought should be
external linkage turned out to be internal linkage.  The reason why I was surprised it was internal
linkage is I thought that we could do some internal testing with an LD_PRELOAD trick, but that did
not take because the symbol in  question is internal linkage.  Which is actually a good thing, it was
the kind of behavior we want.  Again, it was more curiosity than anything.

Thanks for your time responding!

Thanks,

Tom

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-11-25  1:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-24 19:02 Anonymous namespaces and global vs.local symbols in nm output Kacvinsky, Tom
2020-11-24 19:23 ` Xi Ruoyao
2020-11-24 20:03 ` Jonathan Wakely
2020-11-25  1:28   ` Kacvinsky, Tom

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).