RISC-V: process rs_align_code also when relaxing

riscv_handle_align() runs after all input was processed. Whether
relaxation is enabled for any particular piece of code is not recorded
anywhere. (This issue was even "worked around" in a gas testcase, which
is adjusted accordingly.) Furthermore, as demonstrated by an ld
testcase, tail padding in an object file's executable sections depended
on whether relaxation was enabled at the end of assembly: NOPs were
emitted only when relaxation was off; zeroes were emitted with
relaxation enabled. (It could probably be either way, but it should be
independent of relaxation state at the end of assembly. Except of course
write.c, in a comment ahead of #define-ing SUB_SEGMENT_ALIGN(),
explicitly says "proper nop-filling".)

While re-indenting, drop the "odd_padding" variable. It's used exactly
once, and having the actual expression right in the if() is imo helping
readers to understand what the intentions are.

While touching the ld testcase, also tighten the expectations for the
addresses of the two symbols: The last two digits have to have fixed
values.
This commit is contained in:
Jan Beulich
2024-08-23 09:22:30 +02:00
parent 57db1af8ba
commit b994624fa5
3 changed files with 57 additions and 37 deletions

View File

@@ -5023,15 +5023,12 @@ riscv_handle_align (fragS *fragP)
switch (fragP->fr_type)
{
case rs_align_code:
/* When relaxing, riscv_frag_align_code handles code alignment. */
if (!riscv_opts.relax)
{
bfd_signed_vma bytes = (fragP->fr_next->fr_address
- fragP->fr_address - fragP->fr_fix);
/* We have 4 byte uncompressed nops. */
bfd_signed_vma size = 4;
bfd_signed_vma excess = bytes % size;
bfd_boolean odd_padding = (excess % 2 == 1);
char *p = fragP->fr_literal + fragP->fr_fix;
if (bytes <= 0)
@@ -5040,7 +5037,7 @@ riscv_handle_align (fragS *fragP)
/* Insert zeros or compressed nops to get 4 byte alignment. */
if (excess)
{
if (odd_padding)
if (excess % 2)
riscv_add_odd_padding_symbol (fragP);
riscv_make_nops (p, excess);
fragP->fr_fix += excess;

View File

@@ -1,7 +1,4 @@
.attribute arch, "rv32ic"
.option norelax # FIXME: assembler fill the paddings after parsing everything,
# so we probably won't fill anything for the norelax region when
# the riscv_opts.relax is enabled at somewhere.
.section .text.cross.section.A, "ax"
.option push

View File

@@ -7,7 +7,7 @@
Disassembly of section .text:
0+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