Skip to content
This repository was archived by the owner on Aug 17, 2022. It is now read-only.

Commit a5971ec

Browse files
committed
Support aligning text section from odd addresses
Previously, the alignment directives were not correctly supported in the text section when current alignment was only 1 byte (i.e., when the address was odd). Since there are no 1-byte instructions in RISC-V, this patch resolves the bug by writing a zero byte to obtain 2-byte alignment, at which point a 2-byte NOP can be used to obtain 4-byte alignment. Resolves riscv-collab/riscv-gnu-toolchain#205
1 parent 385e1ea commit a5971ec

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

gas/config/tc-riscv.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -2190,14 +2190,15 @@ riscv_make_nops (char *buf, bfd_vma bytes)
21902190
{
21912191
bfd_vma i = 0;
21922192

2193-
if (bytes % 4 == 2)
2193+
if (bytes % 2 == 1)
2194+
buf[i++] = 0;
2195+
2196+
if ((bytes - i) % 4 == 2)
21942197
{
2195-
md_number_to_chars (buf, RVC_NOP, 2);
2198+
md_number_to_chars (buf + i, RVC_NOP, 2);
21962199
i += 2;
21972200
}
21982201

2199-
gas_assert ((bytes - i) % 4 == 0);
2200-
22012202
for ( ; i < bytes; i += 4)
22022203
md_number_to_chars (buf + i, RISCV_NOP, 4);
22032204
}
@@ -2211,7 +2212,11 @@ bfd_boolean
22112212
riscv_frag_align_code (int n)
22122213
{
22132214
bfd_vma bytes = (bfd_vma)1 << n;
2214-
bfd_vma min_text_alignment = riscv_opts.rvc ? 2 : 4;
2215+
bfd_vma min_text_alignment_order = riscv_opts.rvc ? 1 : 2;
2216+
bfd_vma min_text_alignment = (bfd_vma)1 << min_text_alignment_order;
2217+
2218+
/* First, get back to minimal alignment. */
2219+
frag_align_code (min_text_alignment_order, 0);
22152220

22162221
/* When not relaxing, riscv_handle_align handles code alignment. */
22172222
if (!riscv_opts.relax)

0 commit comments

Comments
 (0)