Bug 21700

Summary: powerpc-ibm-aix6.1.0.0 fails with unresolved symbols
Product: binutils Reporter: James Reynolds <james.reynolds>
Component: gasAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: amodra, chigot, james.reynolds, rizwanmacknojia
Priority: P2    
Version: 2.28   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed: 2017-07-05 00:00:00
Attachments: Potential patch
Compiled assembly and CC files
Better patch and example

Description James Reynolds 2017-07-03 09:57:52 UTC
Created attachment 10238 [details]
Potential patch

When creating a Linux / AIX cross-compiler I hit the following error:

  /tmp/ccLI9PmP.s:2660: Error: can't resolve `_ehpersonality.rw_[RW]' {.data section} - `__gcc_unwind_dbase' {*UND* section}

This is caused by this instruction:

  .vbyte  4,LDFCM..0-__gcc_unwind_dbase

This doesn't assemble as TC_VALIDATE_FIX_SUB (which checks to see whether unresolved symbols are admissible in subtraction expressions) is defined as 0 in tc-ppc.h.

Compiling using the default IBM as is successful:

  $ as -u -many eh_personality.s -o eh_personality.o && echo OK
  OK

The attached patch solves the issue, but unfortunately I don't have a small enough example to test whether this is the correct thing to do and I've become stuck on a linker error later in the build.
Comment 1 Alan Modra 2017-07-04 03:03:16 UTC
Please attach the assembly file giving this error, and the resuling object file produced by IBM as.  

The patch you propose is just hiding a problem, perhaps with gcc.  Most relocation formats, including xcoff I believe, don't support relocations with two or more symbols.
Comment 2 James Reynolds 2017-07-04 11:24:45 UTC
Created attachment 10241 [details]
Compiled assembly and CC files

(In reply to Alan Modra from comment #1)
> Please attach the assembly file giving this error, and the resuling object
> file produced by IBM as.  
> 
> The patch you propose is just hiding a problem, perhaps with gcc.  Most
> relocation formats, including xcoff I believe, don't support relocations
> with two or more symbols.

Yes, you are indeed correct, I get an error later with:

  ld-new: .libs/compatibility.o: loader reloc in read-only section .text

The attached file contains the *.cc file that comes from the pre-processing stage, the assembler output of compiling that (on an AIX box) and the result of compiling that using IBM as and using gnu as and my misguided patch.

The errors from this file are:
compatibility.s: Assembler messages:
compatibility.s:959: Error: can't resolve `_compatibility.rw_[RW]' {.data section} - `__gcc_unwind_dbase' {*UND* section}
compatibility.s:1330: Error: can't resolve `_compatibility.rw_[RW]' {.data section} - `__gcc_unwind_dbase' {*UND* section}
compatibility.s:1389: Error: can't resolve `_compatibility.rw_[RW]' {.data section} - `__gcc_unwind_dbase' {*UND* section}
Comment 3 Alan Modra 2017-07-05 05:31:14 UTC
OK, so the AIX assembler emits two relocations for the expression,
R_POS_64 against _compatibility.rw_, and R_NEG against __gcc_unwind_dbase.  The GNU assembler would need to do the same.  grep for RELOC_EXPANSION_POSSIBLE to get an idea of how to do this.

Be warned that the GNU XCOFF support has not been maintained for a long time, and wasn't that good to start with.
Comment 4 James Reynolds 2017-07-05 09:03:58 UTC
(In reply to Alan Modra from comment #3)
> OK, so the AIX assembler emits two relocations for the expression,
> R_POS_64 against _compatibility.rw_, and R_NEG against __gcc_unwind_dbase. 
> The GNU assembler would need to do the same.  grep for
> RELOC_EXPANSION_POSSIBLE to get an idea of how to do this.
> 
OK, I'll give this a bash. I don't think I have the relevant bugzilla permissions to assign this to myself though.

> Be warned that the GNU XCOFF support has not been maintained for a long
> time, and wasn't that good to start with.

Yes... I'm hoping that by fixing some of the issues I can get it good enough for my purposes (smoke tests in CI) which I think a couple of others require as well.
Comment 5 James Reynolds 2017-07-07 15:58:39 UTC
Created attachment 10252 [details]
Better patch and example

I think this patch fixes the issue and another issue - but unfortunately I think this now gets blocked by a linker issue, which I can raise separately.

I've added the relocations to use R_NEG after R_POS[_64] and this seems to match what the IBM linker is doing. I've also adjusted ppc_fix_adjustable so that this code:

        .vbyte  4,LFB..0-$

generates an R_POS / R_NEG as well as this is what the IBM assembler does.

For a small test problem (in archive) this generates the same relocations as IBM modulo positions in the TOC (I think).

Unfortunately... switching to use R_RBR_26 instead of R_BR causes an illegal instruction. Just using R_BR generates a working program but the exception isn't correctly caught. (This is using the IBM linker)

Should I now get this patch landed, close this bug and create another once I've found the root cause of the illegal instruction / R_BR issues?
Comment 6 Rizwan Macknojia 2017-12-22 02:39:41 UTC
Any update on this issue. I am creating a AIX cross-compiler on linux and hitting the similar issue.

Using 
binutils 2.29
gcc 7.2
target powerpc-ibm-aix7.2.0.0
Comment 7 James Reynolds 2017-12-27 12:04:21 UTC
(In reply to Rizwan Macknojia from comment #6)
> Any update on this issue. I am creating a AIX cross-compiler on linux and
> hitting the similar issue.
> 
> Using 
> binutils 2.29
> gcc 7.2
> target powerpc-ibm-aix7.2.0.0

None I'm afraid. I've been dragged away to other projects (and have a new baby, so no personal time either!) but we are looking at much more AIX development in 2018. I've had 2 personal emails about this too - so there is definite interest.

After this though there are a few linker issues too and I still need to get the exception mechanism working. I'll update in January, and possibly make an AIX fork to be merged back when this is operational.
Comment 8 James Reynolds 2018-03-19 09:21:23 UTC
(In reply to James Reynolds from comment #7)
> (In reply to Rizwan Macknojia from comment #6)
> > Any update on this issue. I am creating a AIX cross-compiler on linux and
> > hitting the similar issue.
> > 
> > Using 
> > binutils 2.29
> > gcc 7.2
> > target powerpc-ibm-aix7.2.0.0
> 
> None I'm afraid. I've been dragged away to other projects (and have a new
> baby, so no personal time either!) but we are looking at much more AIX
> development in 2018. I've had 2 personal emails about this too - so there is
> definite interest.
> 
> After this though there are a few linker issues too and I still need to get
> the exception mechanism working. I'll update in January, and possibly make
> an AIX fork to be merged back when this is operational.

I've now fixed the RBR_26 problem - so the relocations now match IBM's relocations exactly. This is in a fork here: https://github.com/JamesReynolds/binutils-gdb

Unfortunately this was not the cause of exceptions not being caught. I think something is mis-calculated in .gcc_except_table. However, I think this bug could be closed if this change could be merged. I'll start the procedure for that today.
Comment 9 James Reynolds 2018-03-21 11:54:04 UTC
(In reply to James Reynolds from comment #8)
> (In reply to James Reynolds from comment #7)
> > (In reply to Rizwan Macknojia from comment #6)
> > > Any update on this issue. I am creating a AIX cross-compiler on linux and
> > > hitting the similar issue.
> > > 
> > > Using 
> > > binutils 2.29
> > > gcc 7.2
> > > target powerpc-ibm-aix7.2.0.0
> > 
> > None I'm afraid. I've been dragged away to other projects (and have a new
> > baby, so no personal time either!) but we are looking at much more AIX
> > development in 2018. I've had 2 personal emails about this too - so there is
> > definite interest.
> > 
> > After this though there are a few linker issues too and I still need to get
> > the exception mechanism working. I'll update in January, and possibly make
> > an AIX fork to be merged back when this is operational.
> 
> I've now fixed the RBR_26 problem - so the relocations now match IBM's
> relocations exactly. This is in a fork here:
> https://github.com/JamesReynolds/binutils-gdb
> 
> Unfortunately this was not the cause of exceptions not being caught. I think
> something is mis-calculated in .gcc_except_table. However, I think this bug
> could be closed if this change could be merged. I'll start the procedure for
> that today.

Further to this I've now cured the exception problem, the subtraction calculation was wrong so the FDE fields were not correctly populated. Fixed in fork. Using a g++ compiled with native tools the following now succeeds:

$ powerpc-ibm-aix6.1-as -mpwr4 exception.s -o exceptions.o
$ g++ exceptions.o -o exceptions
$ ./exceptions
> "Caught"
Comment 10 Rizwan Macknojia 2018-12-04 15:45:22 UTC
Any plans to submit the changes to upstream. Is your fix only works for 32bit or it also address issues with 64bit
Comment 11 James Reynolds 2018-12-04 16:54:53 UTC
(In reply to Rizwan Macknojia from comment #10)
> Any plans to submit the changes to upstream. Is your fix only works for
> 32bit or it also address issues with 64bit

I don't at the moment, I *think* I need a sponsor as I'm outside the project. However, I would quite like to and could pick this up again.

I'll have another try at finding the procedure documentation and fire off some emails.
Comment 12 Clément Chigot 2021-04-14 14:38:41 UTC
Hi all, 

I'm planning to fix this issue in the following days, as part of my work on binutils improvements on AIX. 

James Reynolds, do you mind if I base my work upon your patches ? I didn't find your mail thus I must ask it here, sorry for that.
Comment 13 James Reynolds 2021-04-15 10:22:58 UTC
(In reply to Clément Chigot from comment #12)
> Hi all, 
> 
> I'm planning to fix this issue in the following days, as part of my work on
> binutils improvements on AIX.
> 
> James Reynolds, do you mind if I base my work upon your patches ? I didn't
> find your mail thus I must ask it here, sorry for that.

Hi Clément,

Absolutely. I had a conversation later with someone at IBM and it seems there are a lot of linker changes required after this set of patches, I did get a little way forwards on this but their method of directly patching in functions (e.g. memset) eluded me. I'm no longer working in the area but my latest work is here on github: https://github.com/JamesReynolds/binutils-gdb.

If you need help over email I'm at jum.reynolds@gmail.com.

Thanks,
James
Comment 14 Sourceware Commits 2021-04-20 13:42:39 UTC
The master branch has been updated by Nick Clifton <nickc@sourceware.org>:

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

commit c5df7e442e6cdc9303b7373842370d6ee8f67ea5
Author: Cl?ment Chigot <clement.chigot@atos.net>
Date:   Tue Apr 20 14:40:43 2021 +0100

    Rework the R_NEG support on both gas and ld for the PowerPC AIX targets, in order to manage C++ exceptions built with GCC.
    
    bfd     PR binutils/21700
            * reloc.c (BFD_RELOC_PPC_NEG): New relocation.
            * bfd-in2.h: Regenerate.
            * libbfd.h: Regenerate.
            * coff-rs6000.c (_bfd_xcoff_reloc_type_lookup): Add
            BFD_RELOC_PPC_NEG handler.
            (xcoff_reloc_type_neg): Correctly substract addend.
            * coff64-rs6000.c (xcoff64_howto_table): Add R_NEG_32
            howto.
            (xcoff64_rtype2howto): Add handler for R_NEG_32.
            (xcoff64_reloc_type_lookup): Add BFD_RELOC_PPC_NEG handler.
            * xcofflink.c (xcoff_need_ldrel_p): Check output section
            for R_POS-like relocations. New argument added.
            (xcoff_mark): Adapt to new xcoff_need_ldrel_p argument.
            (xcoff_link_input_bfd): Likewise.
    
    gas     * config/tc-ppc.c (ppc_get_csect_to_adjust): New function.
            (ppc_fix_adjustable): Manage fx_subsy part.
            (tc_gen_reloc): Create second relocation when both
            fx_addsy and fx_subsy are provided.
            * config/tc-ppc.h (RELOC_EXPANSION_POSSIBLE): New define.
            (MAX_RELOC_EXPANSION): Likewise.
            (TC_FORCE_RELOCATION_SUB_SAME): Likewise
            (UNDEFINED_DIFFERENCE_OK): Likewise
            * testsuite/gas/all/gas.exp: Skip difference between two
            undefined symbols test.
    
    ld      * testsuite/ld-powerpc/aix52.exp: Add new test.
            * testsuite/ld-powerpc/aix-neg-reloc-32.d: New test.
            * testsuite/ld-powerpc/aix-neg-reloc-64.d: New test.
            * testsuite/ld-powerpc/aix-neg-reloc.ex: New test.
            * testsuite/ld-powerpc/aix-neg-reloc.s: New test.
Comment 15 Alan Modra 2024-02-29 10:16:30 UTC
Fixed then.