|
From: | Pavel Shishpor |
Subject: | Re: ld: once multiple symbol definitions are allowed, both definitions end up in the executable |
Date: | Tue, 17 Jan 2017 15:56:05 +0100 |
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
behavior:
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.
Jim
[Prev in Thread] | Current Thread | [Next in Thread] |