Bug 27432 - erroneous symbol offsets taken with __ImageBase relocation using Microsoft COFF .OBJ linked to
Summary: erroneous symbol offsets taken with __ImageBase relocation using Microsoft CO...
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.36
: P2 normal
Target Milestone: 2.37
Assignee: H.J. Lu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-02-18 04:31 UTC by Kilian Kegel
Modified: 2022-06-22 06:31 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed: 2021-02-18 00:00:00


Attachments
screenshot wrong offset calculation (64.00 KB, image/png)
2021-02-18 04:40 UTC, Kilian Kegel
Details
A patch (6.00 KB, patch)
2021-02-18 15:10 UTC, H.J. Lu
Details | Diff
An updated patch (6.02 KB, patch)
2021-02-18 18:23 UTC, H.J. Lu
Details | Diff
ineffective bugbatch from 2021-02-18 18:23 (13.76 KB, application/x-zip-compressed)
2021-02-22 04:07 UTC, Kilian Kegel
Details
original binutils 2.34 ld - on the right side, left side manually corrected version (77.38 KB, image/png)
2021-02-22 04:11 UTC, Kilian Kegel
Details
original binutils 2.35 ld - on the right side, left side manually corrected version (75.37 KB, image/png)
2021-02-22 04:12 UTC, Kilian Kegel
Details
bugfix26583 binutils 2.35 ld - on the right side, left side manually corrected version (75.28 KB, image/png)
2021-02-22 04:13 UTC, Kilian Kegel
Details
bugfix27171 binutils 2.35 ld - on the right side, left side manually corrected version (75.15 KB, image/png)
2021-02-22 04:14 UTC, Kilian Kegel
Details
original binutils 2.36.1 ld - on the right side, left side manually corrected version (76.58 KB, image/png)
2021-02-22 04:15 UTC, Kilian Kegel
Details
bugfix27432 binutils 2.36.1 ld - on the right side, left side manually corrected version (90.73 KB, image/png)
2021-02-22 04:17 UTC, Kilian Kegel
Details
A new patch (6.18 KB, patch)
2021-02-22 13:35 UTC, H.J. Lu
Details | Diff
2361_bugfix_from_2021-02-22_1335UTC.png (73.39 KB, image/png)
2021-02-23 05:31 UTC, Kilian Kegel
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kilian Kegel 2021-02-18 04:31:28 UTC
Hi H.J.


the symbol addresses taken for __ImageBase addressing scheme are wrong.
RIP relative addresses taken from the same section instead are correct.

https://github.com/KilianKegel/GNU-ld-for-MicrosoftCOFF-to-LinuxELF#addr32nb-offset-miscalculation

I would really appreciate if you could fix this issue. 

Best regards,
Kilian
Comment 1 Kilian Kegel 2021-02-18 04:40:46 UTC
Created attachment 13230 [details]
screenshot wrong offset calculation
Comment 2 H.J. Lu 2021-02-18 15:10:20 UTC
Created attachment 13233 [details]
A patch

Try this.
Comment 3 H.J. Lu 2021-02-18 18:23:25 UTC
Created attachment 13234 [details]
An updated patch

Try this instead.
Comment 4 Kilian Kegel 2021-02-22 04:07:01 UTC
Created attachment 13240 [details]
ineffective bugbatch from 2021-02-18 18:23
Comment 5 Kilian Kegel 2021-02-22 04:11:23 UTC
Created attachment 13241 [details]
original binutils 2.34 ld - on the right side, left side manually corrected version
Comment 6 Kilian Kegel 2021-02-22 04:12:15 UTC
Created attachment 13242 [details]
original binutils 2.35 ld - on the right side, left side manually corrected version
Comment 7 Kilian Kegel 2021-02-22 04:13:29 UTC
Created attachment 13243 [details]
bugfix26583 binutils 2.35 ld - on the right side, left side manually corrected version
Comment 8 Kilian Kegel 2021-02-22 04:14:20 UTC
Created attachment 13244 [details]
bugfix27171 binutils 2.35 ld - on the right side, left side manually corrected version
Comment 9 Kilian Kegel 2021-02-22 04:15:14 UTC
Created attachment 13245 [details]
original binutils 2.36.1 ld - on the right side, left side manually corrected version
Comment 10 Kilian Kegel 2021-02-22 04:17:26 UTC
Created attachment 13246 [details]
bugfix27432 binutils 2.36.1 ld - on the right side, left side manually corrected version
Comment 11 Kilian Kegel 2021-02-22 04:34:05 UTC
Hi H.J.

regrettably, your fix does not help at all.

I have uploaded the diff views  "ineffective bugbatch from 2021-02-18 18:23"
as a ZIP archive if you want to review.

But I have found, that in the "original binutils" 2.34 and 2.35
the *displacement* *part* only at offset 0x40103D and 0x401052 is still correct.

It got broken beginning with bugfix 26583.

I have transferred all changes to the git repository here:
https://github.com/KilianKegel/binutils-for-Torito-C-Library.git

0. 235_original_commit_4c74dde9cd52dcedd94b7717b9a829ccf089ce2b
1. 235_bugfix26583_commit_6d23bd49be04c9821ddaff55fc2e8e8409d47750
2. 235_bugfix27171_commit_12b0a802d32d26ce403eba0a4fd808bf65c45257
3. 2361_original_commit_93cead0542c1597dcacc94c60f3716d0171901b0
4. 2361_bugfix27432_commit_aec381629adecb0e2712764142c80ad498b844b4

Thanks,
Kilian
Comment 12 H.J. Lu 2021-02-22 13:35:13 UTC
Created attachment 13248 [details]
A new patch

Please try this.
Comment 13 Kilian Kegel 2021-02-23 05:31:54 UTC
Created attachment 13254 [details]
2361_bugfix_from_2021-02-22_1335UTC.png

Hi H.J.

your fix works. Please find the appended screenshot.
I have not yet tested comprehensively against the previous fixes,
but at the first glance:

One more perfect solution from you.

Thanks a lot,
Kilian
Comment 14 Sourceware Commits 2021-03-06 02:32:38 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

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

commit 8c0546e928b557f10cb5aba2a91f3ecee660029d
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Mar 5 18:24:56 2021 -0800

    elf/x86-64: Subtract __ImageBase for R_AMD64_IMAGEBASE
    
    When linking Windows x86-64 relocatable object files to generate x86-64
    ELF executable, we need to subtract __ImageBase, aka __executable_start,
    for R_AMD64_IMAGEBASE relocation:
    
    1. Add link_info to struct output_elf_obj_tdata to store linker info and
    _bfd_get_link_info() to retrieve it.
    2. Add ldelf_set_output_arch to set up link_info.
    3. Add pex64_link_add_symbols to create an indirect reference to
    __executable_start for __ImageBase to support R_AMD64_IMAGEBASE relocation
    when adding symbols from Windows x86-64 relocatable object files to
    generate x86-64 ELF executable.
    4. Also subtract __ImageBase for R_AMD64_IMAGEBASE when generating x86-64
    ELF executable.
    
    bfd/
    
            PR ld/27425
            PR ld/27432
            * bfd.c (_bfd_get_link_info): New function.
            * elf-bfd.h (output_elf_obj_tdata): Add link_info.
            (elf_link_info): New.
            * libbfd-in.h (_bfd_get_link_info): New prototype.
            * coff-x86_64.c (coff_amd64_reloc): Also subtract __ImageBase for
            R_AMD64_IMAGEBASE when generating x86-64 ELF executable.
            * pe-x86_64.c: Include "coff/internal.h" and "libcoff.h".
            (pex64_link_add_symbols): New function.
            (coff_bfd_link_add_symbols): New macro.
            * libbfd.h: Regenerated.
    
    ld/
    
            PR ld/27425
            PR ld/27432
            * ldelf.c (ldelf_set_output_arch): New function.
            * ldelf.h (ldelf_set_output_arch): New prototype.
            * emultempl/elf.em (LDEMUL_SET_OUTPUT_ARCH): Default to
            ldelf_set_output_arch.
            * ld-x86-64/pe-x86-64-1.od: Expect __executable_start.
            * testsuite/ld-x86-64/pe-x86-64-2.od: Likewise.
            * testsuite/ld-x86-64/pe-x86-64-3.od: Likewise.
            * testsuite/ld-x86-64/pe-x86-64-4.od: Likewise.
            * testsuite/ld-x86-64/pe-x86-64-5.od: Likewise.
            * testsuite/ld-x86-64/pe-x86-64-5.rd: Likewise.
            * testsuite/ld-x86-64/pe-x86-64-6.obj.bz2: New file.
            * testsuite/ld-x86-64/pe-x86-64-6.od: Likewise.
            * testsuite/ld-x86-64/pe-x86-64.exp: Run ld/27425 test.
Comment 15 H.J. Lu 2021-03-06 02:36:35 UTC
Fixed for 2.37.