This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Support aligning text section from odd addresses


From: Andrew Waterman <andrew@sifive.com>

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 https://github.com/riscv/riscv-gnu-toolchain/issues/205

gas/ChangeLog

2016-12-16 Andrew Waterman <andrew@sifive.com>

        * config/tc-riscv.c (riscv_make_nops): Emit 2-byte NOPs.
        * config/tc-riscv.c (riscv_frag_align_code): New variable
        min_text_alignment_order, to pass to frag_align_code.
---
 gas/config/tc-riscv.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c
index 8c78f61..9492da0 100644
--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -2190,14 +2190,20 @@ riscv_make_nops (char *buf, bfd_vma bytes)
 {
   bfd_vma i = 0;
 
-  if (bytes % 4 == 2)
+  /* RISC-V instructions cannot begin or end on odd addresses, so this case
+     means we are not within a valid instruction sequence.  It is thus safe
+     to use a zero byte, even though that is not a valid instruction.  */
+  if (bytes % 2 == 1)
+    buf[i++] = 0;
+
+  /* Use at most one 2-byte NOP.  */
+  if ((bytes - i) % 4 == 2)
     {
-      md_number_to_chars (buf, RVC_NOP, 2);
+      md_number_to_chars (buf + i, RVC_NOP, 2);
       i += 2;
     }
 
-  gas_assert ((bytes - i) % 4 == 0);
-
+  /* Fill the remainder with 4-byte NOPs.  */
   for ( ; i < bytes; i += 4)
     md_number_to_chars (buf + i, RISCV_NOP, 4);
 }
@@ -2211,7 +2217,11 @@ bfd_boolean
 riscv_frag_align_code (int n)
 {
   bfd_vma bytes = (bfd_vma)1 << n;
-  bfd_vma min_text_alignment = riscv_opts.rvc ? 2 : 4;
+  bfd_vma min_text_alignment_order = riscv_opts.rvc ? 1 : 2;
+  bfd_vma min_text_alignment = (bfd_vma)1 << min_text_alignment_order;
+
+  /* First, get back to minimal alignment.  */
+  frag_align_code (min_text_alignment_order, 0);
 
   /* When not relaxing, riscv_handle_align handles code alignment.  */
   if (!riscv_opts.relax)
-- 
2.10.2


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]