ld: output section alignment and empty section.

Alan Modra amodra@gmail.com
Tue Mar 10 06:42:25 GMT 2020


On Thu, Mar 05, 2020 at 02:55:44PM +0100, KONRAD Frederic wrote:
> Hi,
> 
> I just figured out that the following linker script hunk:
> 
>   . = ALIGN(0x4);
>   .rodata :
>   {
>     . = ALIGN(0x4);
>   }
> 
> is creating an empty .rodata output section while I was expecting it not to be
> emited.
> 
> This is because we flag ". = ALIGN(xxx)" as SEC_KEEP in exp_fold_tree_1.
> 
> Dropping that for ". = ALIGN(constant)" seems to fix the behavior:

Yes it would, but you could also write the align inside .rodata using

  . = ALIGN(. != 0 ? 4 : 1);

That is one of the few forms of assigning to dot that ld recognises as
*not* resulting in "keep this section".  This is documented under node
"Output Section Discarding" in ld.info.

I don't think there is a good reason to change this behaviour as there
may even be scripts that rely on a simpler ALIGN to keep the section.

> Is that because strip_excluded_output_sections happens before the relaxation?
> Could that be a problem in the case of a constant alignment?

Yes and yes.  It is possible to create a script that assigns an
address (before the colon in an output section statement) or alignment
(after the colon) to a section less than the constant alignment in
some ". = ALIGN();" statement in that section, and the addresses of
prior sections conspire to make the alignment do nothing before
relaxation but to add padding when prior sections change.  The patches
that were reverted with e0a3af227e ran into something like that when I
tried to make the single operand ALIGN behave like the two operand
form.

> What do you think?

Fix your script.  :-)

-- 
Alan Modra
Australia Development Lab, IBM



More information about the Binutils mailing list