This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Linkonce vs comdat


Matt Austern <austern@apple.com> writes:

> I'm getting confused about whether the linkonce scheme that the
> compiler uses on ELF systems (i.e. with sections that have names like
> .gnu.linkonce.t.foo) is supposed to be the same thing as the COMDAT
> scheme that's described in
> http://www.codesourcery.com/cxx-abi/abi/prop-72-comdat.html.  It
> doesn't look like the same thing; I have the sense that the word
> COMDAT  is being used in at least two senses.  but, as I said, I'm
> confused.   Quite possible I'm missing something.

It is not the same.  As the codesourcery page implies without really
stating explicitly, there are several different implementations which
could all be called COMDAT.  I believe the name `COMDAT' was
introduced by the Microsoft PE format.

> Perhaps one of the things that I'm missing is the documentation.  I
> couldn't find anything in the ld documentation that said exactly what
> it did with gnu.linkonce sections.  In the compiler documentation I
> did  find a brief discussion in the user manual
> (http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Vague-
> Linkage.html#Vague%20Linkage) and nothing in the internals manual.  So
> mostly I'm just trying to trace this through the source code.  If
> there's somewhere better for me to be looking, I'd appreciate a
> pointer.

I think that this is the complete existing linker documentation, from
bfd/elf.c:

  /* As a GNU extension, if the name begins with .gnu.linkonce, we
     only link a single copy of the section.  This is used to support
     g++.  g++ will emit each template expansion in its own section.
     The symbols will be defined as weak, so that multiple definitions
     are permitted.  The GNU linker extension is to actually discard
     all but one of the sections.  */
  if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
      && elf_next_in_group (newsect) == NULL)
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;

and from bfd/section.c:

.  {* When linking, duplicate sections of the same name should be
.     discarded, rather than being combined into a single section as
.     is usually done.  This is similar to how common symbols are
.     handled.  See SEC_LINK_DUPLICATES below.  *}
.#define SEC_LINK_ONCE 0x100000
.
.  {* If SEC_LINK_ONCE is set, this bitfield describes how the linker
.     should handle duplicate sections.  *}
.#define SEC_LINK_DUPLICATES 0x600000
.
.  {* This value for SEC_LINK_DUPLICATES means that duplicate
.     sections with the same name should simply be discarded.  *}
.#define SEC_LINK_DUPLICATES_DISCARD 0x0
.
.  {* This value for SEC_LINK_DUPLICATES means that the linker
.     should warn if there are any duplicate sections, although
.     it should still only link one copy.  *}
.#define SEC_LINK_DUPLICATES_ONE_ONLY 0x200000
.
.  {* This value for SEC_LINK_DUPLICATES means that the linker
.     should warn if any duplicate sections are a different size.  *}
.#define SEC_LINK_DUPLICATES_SAME_SIZE 0x400000
.
.  {* This value for SEC_LINK_DUPLICATES means that the linker
.     should warn if any duplicate sections contain different
.     contents.  *}
.#define SEC_LINK_DUPLICATES_SAME_CONTENTS 0x600000

>From this we can see that given a set of input sections with the same
name, if that name begins with ".gnu.linkonce", only one of those
input sections will be copied into the output section.

Ian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]