Bug 23872

Summary: MinGW Binaries can be built with misaligned relocation information
Product: binutils Reporter: marc
Component: ldAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: martin, nickc, victor.dyachenko
Priority: P2    
Version: 2.30   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

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 Sourceware Commits 2018-11-09 11:15:34 UTC
The master branch has been updated by Nick Clifton <nickc@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=73af69e74974eaa155eec89867e3ccc77ab39f6d

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.

Cheers
  Nick
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.)