[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ld: once multiple symbol definitions are allowed, both definitions e

From: Jim Wilson
Subject: Re: ld: once multiple symbol definitions are allowed, both definitions end up in the executable
Date: Wed, 11 Jan 2017 11:50:17 -0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1

On 01/11/2017 01:49 AM, Pavel Shishpor wrote:
Could please someone advice is it a bug or a feature when we get both
bodies of the functions with the same name in the executable once
multiple symbol definitions are allowed? Here is the example showing the

The only thing that the --allow-multiple-definitions option does is disable the error message that you would normally get. It doesn't change how the linker output is created. So yes, you will end up with both definitions in the output.

It is best to avoid getting multiple definitions in the first place.

If you want the linker to discard unused functions, then each function must be in its own section via gcc -ffunction-sections and then use the linker --gc-sections option. However, this may cause problems of its own, e.g. debug functions may disappear because they appear unused, it won't work for libraries where most functions are meant to be used by an application it is linked with, etc. Or alternatively, you can try making it a COMDAT function, then duplicates get dropped automatically.

For your example, the duplicate f function is in the same text section as the main function, which can't be deleted. The assembler may resolve intra-section references itself. Which means editing the text section to remove the f function may break other things in the same section, which is unsafe, and hence the linker can't do it. Thus the requirement that we can only delete functions that are in their own section.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]