RISC-V: Update ABI to the elf_flags after parsing elf attributes.
authorNelson Chu <nelson.chu@sifive.com>
Mon, 19 Oct 2020 05:10:42 +0000 (22:10 -0700)
committerNelson Chu <nelson.chu@sifive.com>
Mon, 9 Nov 2020 01:48:56 +0000 (09:48 +0800)
commit6e1605e43068f57f49bb0cdcea087adbad2cc6ac
tree6e7ba66c750d0eb95594bbfc694686d031c36441
parent4985fbc120f778693a686aaac41979af49cd6aa6
RISC-V: Update ABI to the elf_flags after parsing elf attributes.

Originally, if the -mabi option isn't set, then assembler will set the
abi according to the architecture string in the riscv_after_parse_args.
But we should also check and reset the abi later since the architecture
string may be reset by the elf attributes.  Therefore, set the abi to
the elf_flags in the riscv_after_parse_args seems too early.  Besides,
we have to set the abi_xlen before assembling any instruction, so it
should be safe to call riscv_set_abi_by_arch at the place that we set
start_assemble to TRUE.  However, one minor case is that we won't call
the md_assemble when we are assembling an file without any instruction.
It seems that we still need to set the abi in riscv_elf_final_processing,
to make sure that abi can be updated according to the elf arch attributes.

For the rv32i and most elf toolchains, this patch can fix the mis-matched
ABI errors for Run pr26391-5 and Run pr26391-6 testcases.  Besides, it
also correct the elf header flags of the output objects.  Consider the
new testcases, mabi-fail-02 and mabi-noabi-attr-[01|02|03], they are
failed before applying this patch.

But I still get the mis-matched ABI errors for the following toolchains
when runnung the riscv-gnu-toolchain regressions,

newlib-rv32imafc-ilp32f-[medlow|medany]
linux-rv32imac-ilp32-[medlow|medany]
linux-rv32imafdc-ilp32-[medlow|medany}
linux-rv64imac-lp64-[medlow|medany]
linux-rv64imafdc-lp64-[medlow|medany}

For the newlib-rv32imafc-ilp32f, although we try to choose the abi
according to the elf attributes, we will use FLOAT_ABI_SOFT rather
than the FLOAT_ABI_SINGLE for the assmebly file wihtout setting the
-mabi, but compiler will set the abi to FLOAT_ABI_SINGLE for the
C files.

As for the linux toolchains, we also get fails for Run pr26391-5 and
Run pr26391-6 testcases.  Since the linux toolchain won't generate elf
attributes to correct the ISA, and the --with-arch configure option
isn't set, assembler will try to set the default arch to rv[32|64]g,
which means the FLOAT_ABI_DOUBLE will be choosed, and may be conflict
with the abi set by the toolchain.

Therefore, I would suggest that it's is more safe to set the --with-arch
when building binutils, but it may break some testcases.  For example,
ld-scripts/fill and ld-scripts/empty-address-2 may be broken when c-ext
is set.  We might insert R_RISCV_ALIGN to make sure the 4-byte alignment,
but the dump result will be a bit different from what the testcase expected.

However, this patch only fix the problem - the abi, elf_flags and the
instruction, which is generated according to the abi_xlen, are all fixed
once the elf attributes are set for most elf toolchains. Other mis-matched
ABI problems should be fixed when we always build the binutils with the
--with-arch= configure option.

gas/
* config/tc-riscv.c (explicit_mabi): New boolean to indicate if
the -mabi= option is explictly set.
(md_parse_option): Set explicit_mabi to TRUE if -mabi is set.
(riscv_set_abi_by_arch): New function.  If the -mabi option isn't
set, then we set the abi according to the architecture string.
Otherwise, check if there are conflicts between architecture
and abi setting.
(riscv_after_parse_args): Move the abi setting to md_assemble nad
riscv_elf_final_processing.
(md_assemble): Call the riscv_set_abi_by_arch when we set the
start_assemble to TRUE.
(riscv_elf_final_processing): Likewise, in case the file without
any instruction.

* testsuite/gas/riscv/mabi-attr-01.s: New testcase.
* testsuite/gas/riscv/mabi-attr-02.s: Likewise.
* testsuite/gas/riscv/mabi-attr-03.s: Likewise.
* testsuite/gas/riscv/mabi-fail-01.d: Likewise.
* testsuite/gas/riscv/mabi-fail-01.l: Likewise.
* testsuite/gas/riscv/mabi-fail-02.d: Likewise.
* testsuite/gas/riscv/mabi-fail-02.l: Likewise.
* testsuite/gas/riscv/mabi-noabi-attr-01a.d: Likewise.
* testsuite/gas/riscv/mabi-noabi-attr-01b.d: Likewise.
* testsuite/gas/riscv/mabi-noabi-attr-02a.d: Likewise.
* testsuite/gas/riscv/mabi-noabi-attr-02b.d: Likewise.
* testsuite/gas/riscv/mabi-noabi-attr-03a.d: Likewise.
* testsuite/gas/riscv/mabi-noabi-attr-03b.d: Likewise.
* testsuite/gas/riscv/mabi-noabi-march-01.d: Likewise.
* testsuite/gas/riscv/mabi-noabi-march-02.d: Likewise.
* testsuite/gas/riscv/mabi-noabi-march-03.d: Likewise.
18 files changed:
gas/ChangeLog
gas/config/tc-riscv.c
gas/testsuite/gas/riscv/mabi-attr-01.s [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-attr-02.s [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-attr-03.s [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-fail-01.d [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-fail-01.l [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-fail-02.d [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-fail-02.l [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-noabi-attr-01a.d [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-noabi-attr-01b.d [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-noabi-attr-02a.d [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-noabi-attr-02b.d [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-noabi-attr-03a.d [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-noabi-attr-03b.d [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-noabi-march-01.d [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-noabi-march-02.d [new file with mode: 0644]
gas/testsuite/gas/riscv/mabi-noabi-march-03.d [new file with mode: 0644]