Bug 19251 - Dynamic relocs for .init_array ordered wrong when building shared libraries
Summary: Dynamic relocs for .init_array ordered wrong when building shared libraries
Status: NEW
Alias: None
Product: binutils
Classification: Unclassified
Component: gold (show other bugs)
Version: 2.27
: P2 normal
Target Milestone: ---
Assignee: Cary Coutant
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-11-16 15:21 UTC by Phil Blundell
Modified: 2015-11-16 16:21 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
patch to add new testcases (1.59 KB, patch)
2015-11-16 15:21 UTC, Phil Blundell
Details | Diff
updated patch to add testcases (1.58 KB, patch)
2015-11-16 16:21 UTC, Phil Blundell
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Phil Blundell 2015-11-16 15:21:19 UTC
Created attachment 8784 [details]
patch to add new testcases

Code in Sized_relobj_file::do_relocate reverses the order of the words in .init_array in order to preserve the expected order of entries in .ctors.  This works fine when generating an executable, but it doesn't appear to make any effort to adjust the location of the relocs that will be emitted when generating a shared library.  The consequences of this vary depending on the exact situation:

1. If all constructors have static linkage then:

- for a REL platform, every reloc will be identical (all R_xxx_RELATIVE) and there will be no ill effects.

- for a RELA platform, the words in .init_array will be all zeros because the addend is in the relocation.  Re-ordering .init_array will have no effect and the constructors will run in the wrong order.

2. If all the constructions have global linkage then they will get R_386_32 or equivalent relocs and the addends will all be zero.  In this case, again, reordering the words in .init_array will have no effect and the constructors will run in the wrong order. 

3. If the shared library contains a mix of constructors with static and dynamic linkage then, on a REL platform, the relocs and their addends will no longer match up and the program will almost certainly crash.
Comment 1 Phil Blundell 2015-11-16 16:21:43 UTC
Created attachment 8786 [details]
updated patch to add testcases

Sorry, that patch was not quite right.  Here is a corrected one.