ubsan: nds32: left shift cannot be represented in type 'int'
authorAlan Modra <amodra@gmail.com>
Sun, 15 Dec 2019 23:05:30 +0000 (09:35 +1030)
committerAlan Modra <amodra@gmail.com>
Mon, 16 Dec 2019 07:03:53 +0000 (17:33 +1030)
Note that using 1u in N32_BIT makes all of N32_BIT, __MASK, __MF, __GF
and __SEXT evaluate as unsigned int (the latter three when when their
v arg is int or smaller).  This would be a problem if assigning the
result to a bfd_vma, long, or other type wider than an int since the
__SEXT result would be zero extended to the wider type.  Fortunately
nds32 target code doesn't use wider types unnecessarily.

include/
* opcode/nds32.h (N32_BIT): Define using 1u.
(__SEXT): Use __MASK and N32_BIT.
(N32_IMMS): Remove duplicate mask.
opcodes/
* nds32-dis.c (print_insn16, print_insn32): Remove forward decls.
(struct objdump_disasm_info): Delete.
(nds32_parse_audio_ext, nds32_parse_opcode): Cast result of
N32_IMMS to unsigned before shifting left.

include/ChangeLog
include/opcode/nds32.h
opcodes/ChangeLog
opcodes/nds32-dis.c

index 1444cc91461ce20be1094cf1c168f40a6415afc5..7f6cc9bf558976dae5dc32df73fb12bb1d98895e 100644 (file)
@@ -1,3 +1,9 @@
+2019-12-16  Alan Modra  <amodra@gmail.com>
+
+       * opcode/nds32.h (N32_BIT): Define using 1u.
+       (__SEXT): Use __MASK and N32_BIT.
+       (N32_IMMS): Remove duplicate mask.
+
 2019-12-12  Luis Machado  <luis.machado@linaro.org>
 
        * diagnostics.h (DIAGNOSTIC_IGNORE_UNUSED_FUNCTION). Remove
index 7791864dd0857b62d7d7e8b8bae8b3608b78875e..0e60840f78aa50ff9806411c22f272b4cd58fb43 100644 (file)
@@ -51,11 +51,12 @@ static const int nds32_r54map[] ATTRIBUTE_UNUSED =
   -1, -1, -1, -1, -1, -1, -1, -1
 };
 
-#define N32_BIT(n)             (1 << (n))
+#define N32_BIT(n)             (1u << (n))
 #define __MASK(n)              (N32_BIT (n) - 1)
 #define __MF(v, off, bs)       (((v) & __MASK (bs)) << (off))
 #define __GF(v, off, bs)       (((v) >> off) & __MASK (bs))
-#define __SEXT(v, bs)          ((((v) & ((1 << (bs)) - 1)) ^ (1 << ((bs) - 1))) - (1 << ((bs) - 1)))
+#define __SEXT(v, bs)          \
+  ((((v) & __MASK ((bs))) ^ N32_BIT ((bs) - 1)) - N32_BIT ((bs) - 1))
 
 /* Make nds32 instructions.  */
 
@@ -150,7 +151,7 @@ static const int nds32_r54map[] ATTRIBUTE_UNUSED =
 #define N32_SUB6(insn)         (((insn) >> 0) & 0x3f)
 #define N32_SWID(insn)         (((insn) >> 5) & 0x3ff)
 #define N32_IMMU(insn, bs)     ((insn) & __MASK (bs))
-#define N32_IMMS(insn, bs)     ((signed) __SEXT (((insn) & __MASK (bs)), bs))
+#define N32_IMMS(insn, bs)     ((signed) __SEXT ((insn), (bs)))
 #define N32_IMM5U(insn)                N32_IMMU (insn, 5)
 #define N32_IMM12S(insn)       N32_IMMS (insn, 12)
 #define N32_IMM14S(insn)       N32_IMMS (insn, 14)
index 2d1c9d3f7d4eec49ccf0f32bfeb7a36596336cf0..65ef685d070badacb74d1577f6118d45a344a9dc 100644 (file)
@@ -1,3 +1,10 @@
+2019-12-16  Alan Modra  <amodra@gmail.com>
+
+       * nds32-dis.c (print_insn16, print_insn32): Remove forward decls.
+       (struct objdump_disasm_info): Delete.
+       (nds32_parse_audio_ext, nds32_parse_opcode): Cast result of
+       N32_IMMS to unsigned before shifting left.
+
 2019-12-16  Alan Modra  <amodra@gmail.com>
 
        * moxie-dis.c (INST2OFFSET): Don't left shift a signed value.
index dfd05cbe45954193b29d7fe5ed3a7084cd81c966..0e41399ef0523850dc10681371831ee50c051018 100644 (file)
@@ -72,10 +72,7 @@ extern struct nds32_opcode nds32_opcodes[];
 extern const field_t operand_fields[];
 extern keyword_t *keywords[];
 extern const keyword_t keyword_gpr[];
-static void print_insn16 (bfd_vma pc, disassemble_info *info,
-                         uint32_t insn, uint32_t parse_mode);
-static void print_insn32 (bfd_vma pc, disassemble_info *info, uint32_t insn,
-                         uint32_t parse_mode);
+
 static uint32_t nds32_mask_opcode (uint32_t);
 static void nds32_special_opcode (uint32_t, struct nds32_opcode **);
 static int get_mapping_symbol_type (struct disassemble_info *, int,
@@ -83,18 +80,6 @@ static int get_mapping_symbol_type (struct disassemble_info *, int,
 static int is_mapping_symbol (struct disassemble_info *, int,
                              enum map_type *);
 
-/* define in objdump.c.  */
-struct objdump_disasm_info
-{
-  bfd *              abfd;
-  asection *         sec;
-  bfd_boolean        require_sec;
-  arelent **         dynrelbuf;
-  long               dynrelcount;
-  disassembler_ftype disassemble_fn;
-  arelent *          reloc;
-};
-
 /* Hash function for disassemble.  */
 
 static htab_t opcode_htab;
@@ -128,8 +113,8 @@ nds32_parse_audio_ext (const field_t *pfd,
   if (pfd->hw_res == HW_INT || pfd->hw_res == HW_UINT)
     {
       if (pfd->hw_res == HW_INT)
-       int_value =
-         N32_IMMS ((insn >> pfd->bitpos), pfd->bitsize) << pfd->shift;
+       int_value = (unsigned) N32_IMMS (insn >> pfd->bitpos,
+                                        pfd->bitsize) << pfd->shift;
       else
        int_value = __GF (insn, pfd->bitpos, pfd->bitsize) << pfd->shift;
 
@@ -321,9 +306,9 @@ nds32_parse_opcode (struct nds32_opcode *opc, bfd_vma pc ATTRIBUTE_UNUSED,
              else if ((pfd->hw_res == HW_INT) || (pfd->hw_res == HW_UINT))
                {
                  if (pfd->hw_res == HW_INT)
-                   int_value =
-                     N32_IMMS ((insn >> pfd->bitpos),
-                           pfd->bitsize) << pfd->shift;
+                   int_value
+                     = (unsigned) N32_IMMS (insn >> pfd->bitpos,
+                                            pfd->bitsize) << pfd->shift;
                  else
                    int_value =
                      __GF (insn, pfd->bitpos, pfd->bitsize) << pfd->shift;
@@ -411,8 +396,8 @@ nds32_parse_opcode (struct nds32_opcode *opc, bfd_vma pc ATTRIBUTE_UNUSED,
          else if ((pfd->hw_res == HW_INT) || (pfd->hw_res == HW_UINT))
            {
              if (pfd->hw_res == HW_INT)
-               int_value =
-                 N32_IMMS ((insn >> pfd->bitpos), pfd->bitsize) << pfd->shift;
+               int_value = (unsigned) N32_IMMS (insn >> pfd->bitpos,
+                                                pfd->bitsize) << pfd->shift;
              else
                int_value =
                  __GF (insn, pfd->bitpos, pfd->bitsize) << pfd->shift;
This page took 0.028186 seconds and 4 git commands to generate.