File: ldrt.c

package info (click to toggle)
valgrind 1%3A3.12.0~svn20160714-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 120,428 kB
  • ctags: 70,855
  • sloc: ansic: 674,645; exp: 26,134; xml: 21,574; asm: 7,570; cpp: 7,567; makefile: 7,380; sh: 6,188; perl: 5,855; haskell: 195
file content (135 lines) | stat: -rw-r--r-- 3,758 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

// This should be compiled as Thumb code, since currently V only
// handles the T1 encoding of ldrt.  This all assumes that we are
// in a little-endian world.

#include <stdio.h>
#include <malloc.h>

typedef unsigned int UInt;

__attribute__((noinline)) UInt do_ldrt_imm_132 ( unsigned char* p )
{
  UInt res;
  __asm__ __volatile__(
     "mov r5, %1 ; ldrt r6, [r5, #132] ; mov %0, r6"
      : "=r"(res) : "r"(p) : "r5", "r6"
  );
  return res;
}

__attribute__((noinline)) void do_strt_imm_132 ( unsigned char* p, UInt val )
{
  __asm__ __volatile__(
     "mov r5, %0 ; mov r6, %1 ; strt r6, [r5, #132]"
      : : "r"(p), "r"(val) : "r5", "r6", "memory"
  );
}

__attribute__((noinline)) void do_strbt_imm_132 ( unsigned char* p, UInt* val )
{
  __asm__ __volatile__(
     "mov r5, %0 ; ldr r6, [r5] ; mov r5, %1; strbt r6, [r5, #132]"
      : : "r"(val), "r"(p) : "r5", "r6", "memory"
  );
}

__attribute__((noinline)) UInt do_ldrht_imm_1 (unsigned char* val)
{
  UInt res;
  __asm__ __volatile__(
      "mov r4, %1 ; ldrht r5, [r4, #1]; mov %0, r5"
      : "=r"(res) : "r"(val) : "r4", "r5"
  );
  return res;
}

__attribute__((noinline)) void do_ldrsht_imm_1 (UInt* res)
{
  __asm__ __volatile__(
     "mov r4, %1 ; ldrsht r5, [r4, #1] ; str r5, [r4, #0]"
     : "+r"(res) : : "r4", "r5", "memory"
  );
}

__attribute__((noinline)) void do_strht_imm_132 ( unsigned char* p, UInt* val )
{
  __asm__ __volatile__(
     "mov r5, %0 ; ldr r6, [r5] ; mov r5, %1; strht r6, [r5, #132]"
      : : "r"(val), "r"(p) : "r5", "r6", "memory"
  );
}

__attribute__((noinline)) UInt do_ldrbt_imm_2 (unsigned char* val)
{
  UInt res;
  __asm__ __volatile__(
     "mov r4, %1 ; ldrbt r5, [r4, #2]; mov %0, r5"
     : "=r"(res) : "r"(val) : "r4", "r5"
  );
  return res;
}

__attribute__((noinline)) UInt do_ldrsbt_imm_2 (unsigned char* val)
{
  UInt res;
  __asm__ __volatile__(
     "mov r4, %1 ; ldrsbt r5, [r4, #2]; mov %0, r5"
     : "=r"(res) : "r"(val) : "r4", "r5"
  );
  return res;
}

int main ( void )
{
  UInt i;
  unsigned char* b = malloc(256);
  for (i = 0; i < 256; i++) b[i] = (unsigned char)i;
  UInt r = do_ldrt_imm_132(b);
  free(b);
  printf("result is 0x%08x (should be 0x%08x)\n", r, 0x87868584);

  UInt val = (200 << 0) | (150 << 8) | (100 << 16) | (10 << 24);
  unsigned char* c = malloc(256);
  for (i = 0; i < 256; i++) c[i] = (unsigned char)i;
  do_strt_imm_132(c, val);
  printf("result is %u %u %u %u %u %u (should be %u %u %u %u %u %u)\n",
         c[131], c[132], c[133], c[134], c[135], c[136],
         131, 200, 150, 100, 10, 136);
  free(c);

  UInt val_bt = 0b11111111;
  unsigned char* d = malloc(256);
  for (i = 0; i < 256; i++) d[i] = (unsigned char)i;
  do_strbt_imm_132(d, &val_bt);
  printf("result is %u %u %u (should be %u %u %u)\n",
         d[131], d[132], d[133], 131, 255, 133);
  free(d);

  UInt val_ht = 0xFFFF;
  unsigned char* e = malloc(256);
  for (i = 0; i < 256; i++) e[i] = (unsigned char)i;
  do_strht_imm_132(e, &val_ht);
  printf("result is %u %u %u %u (should be %u %u %u %u)\n",
         e[131], e[132], e[133], e[134], 131, 255, 255, 134);
  free(e);

  UInt val_ldrbt = (200 << 0) | (150 << 8) | (137 << 16) | (10 << 24);
  printf("result is %u (should be %u)\n",
         do_ldrbt_imm_2((unsigned char*)&val_ldrbt), 137);

  UInt val_ldrsbt = (200 << 0) | (150 << 8) | (254 << 16) | (10 << 24);
  printf("result is %u (should be %llu)\n",
         do_ldrsbt_imm_2((unsigned char*)&val_ldrsbt), 4294967294ULL);


  UInt val_ldrht = 0xABFEFD8D;
  printf("result is %u (should be %u)\n",
         do_ldrht_imm_1((unsigned char*)(&val_ldrht)), 65277);

  UInt val_ldrsht = 0x00BADFAA;
  do_ldrsht_imm_1(&val_ldrsht);
  printf("result is 0x%x (should be 0x%x)\n", val_ldrsht, 0xFFFFBADF);

  return 0;
}