How does ld and gold deal with C++ duplicate templates?

Ian Lance Taylor iant@google.com
Wed Sep 16 19:23:06 GMT 2020


On Wed, Sep 16, 2020 at 2:26 AM sotrdg sotrdg via Binutils
<binutils@sourceware.org> wrote:
>
> Like the bug I reported a month ago.
>
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96577
>
>
>
> https://godbolt.org/z/haG4E7
>
> https://godbolt.org/z/M8e7Kb
>
>
>
> First:
>
>
>
> #include<vector>
>
> #include<array>
>
> #include<algorithm>
>
>
>
> template<std::contiguous_iterator Iter>
>
> void f(Iter a,Iter b)
>
> {
>
>     std::sort(a,b);
>
> }
>
>
>
> void g(std::vector<int>::iterator b,std::vector<int>::iterator e)
>
> {
>
>     f(b,e);
>
> }
>
>
>
> void h(int* b,int* e)
>
> {
>
>     f(b,e);
>
> }
>
>
>
> Second:
>
>
>
>
>
> #include<vector>
>
> #include<array>
>
> #include<algorithm>
>
>
>
> template<std::contiguous_iterator Iter>
>
> void f(Iter a,Iter b)
>
> {
>
>     std::sort(a,b);
>
> }
>
>
>
> void g(std::vector<int>::iterator b,std::vector<int>::iterator e)
>
> {
>
>     f(std::to_address(b),std::to_address(e));
>
> }
>
>
>
> void h(int* b,int* e)
>
> {
>
>     f(b,e);
>
> }
>
>
>
>
>
> The two sort function should generate exactly the same binary. However the first which is none to_address version generates twice as much as code as the second one.
>
> Will the GNU ld remove the duplication definitions?


For gold, see the --icf option.

Ian


More information about the Binutils mailing list