Bug 23872 - MinGW Binaries can be built with misaligned relocation information
Summary: MinGW Binaries can be built with misaligned relocation information
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.30
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2018-11-07 20:50 UTC by marc
Modified: 2019-03-21 09:38 UTC (History)
3 users (show)

See Also:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description marc 2018-11-07 20:50:51 UTC
Clang does not align certain sections in its object files, so when they are linked it is possible that the runtime relocation sections are not aligned. This causes the linker to re-align them, but the value of "rt_psrelocs_start" is set before the realignment, so when the dll is loaded it looks in the wrong place for the relocations and fails.

This can be fixed by forcing realignment by adding the line ". = ALIGN(4);" to the linker script immediately before setting rt_psrelocs_start.

This bug was also reported to MinGW64: https://sourceforge.net/p/mingw-w64/bugs/769/
Comment 1 cvs-commit@gcc.gnu.org 2018-11-09 11:15:34 UTC
The master branch has been updated by Nick Clifton <nickc@sourceware.org>:


commit 73af69e74974eaa155eec89867e3ccc77ab39f6d
Author: Marc <marc@groundctl.com>
Date:   Fri Nov 9 11:13:50 2018 +0000

    Allow for compilers that do not produce aligned .rdat sections in PE format files.
    	PR 23872
    	* scripttempl/pep.sc (pe.sc): Ensure rdata_runtime_pseudo_relocs
    	are aligned.
    	* scripttempl/pep.sc (pep.sc): Likewise.
Comment 2 Nick Clifton 2018-11-09 11:19:28 UTC
Hi Marc,

  It sounds a bit suspicious that clang is not aligning the relocs, but
  I see no reason why the linker scripts should not cope with the fact,
  so I have checked in your patch.

Comment 3 Martin Storsjö 2018-11-26 07:37:44 UTC
(In reply to Nick Clifton from comment #2)
>   It sounds a bit suspicious that clang is not aligning the relocs

Just for discussion, clang doesn't align the relocs (as they are produced by ld) - I presume you meant why clang doesn't align the rdata section.

Judging from the linker script, wouldn't it mostly be a case of clang producing object files with .rdata sections that end with an uneven number of bytes? And I don't see how that would be suspicious. I haven't dug into a full case of this happening though.

In any case, the applied patch certainly is right though.

(I'm just arguing because I don't think there is a bug to be fixed on the clang side wrt this, as someone is led to believe in https://bugs.llvm.org/show_bug.cgi?id=39754.)