-
Notifications
You must be signed in to change notification settings - Fork 1
/
barelibc.c
862 lines (783 loc) · 17.6 KB
/
barelibc.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
// Mixture of embedlibc, and ch32v00x_startup.c
// Use with newlib headers.
// Mixture of weblibc, mini-printf and ???
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include <stdint.h>
int errno;
int mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list va);
int mini_vpprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, va_list va);
extern uint32_t UART;
int _write(int fd, const char *buf, int size)
{
for (int i = 0; i < size; i++)
{
UART = buf[i];
}
return size;
}
static int __puts_uart(char *s, int len, void *buf)
{
_write(0, s, len);
return len;
}
int printf(const char *format, ...)
{
va_list args;
va_start(args, format);
int ret_status = mini_vpprintf(__puts_uart, 0, format, args);
va_end(args);
return ret_status;
}
/* Some stuff from MUSL
----------------------------------------------------------------------
Copyright © 2005-2020 Rich Felker, et al.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
----------------------------------------------------------------------
*/
/*
* mbstate_t is an opaque object to keep conversion state, during multibyte
* stream conversions. The content must not be referenced by user programs.
*/
#define CURRENT_UTF8 0
#define IS_CODEUNIT(c) ((unsigned)(c)-0xdf80 < 0x80)
#define MB_CUR_MAX (CURRENT_UTF8 ? 4 : 1)
typedef void *mbstate_t;
#ifdef UNICODE
size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st)
{
if (!s)
return 1;
if ((unsigned)wc < 0x80)
{
*s = wc;
return 1;
}
else if (MB_CUR_MAX == 1)
{
if (!IS_CODEUNIT(wc))
{
errno = 0x02; // EILSEQ
return -1;
}
*s = wc;
return 1;
}
else if ((unsigned)wc < 0x800)
{
*s++ = 0xc0 | (wc >> 6);
*s = 0x80 | (wc & 0x3f);
return 2;
}
else if ((unsigned)wc < 0xd800 || (unsigned)wc - 0xe000 < 0x2000)
{
*s++ = 0xe0 | (wc >> 12);
*s++ = 0x80 | ((wc >> 6) & 0x3f);
*s = 0x80 | (wc & 0x3f);
return 3;
}
else if ((unsigned)wc - 0x10000 < 0x100000)
{
*s++ = 0xf0 | (wc >> 18);
*s++ = 0x80 | ((wc >> 12) & 0x3f);
*s++ = 0x80 | ((wc >> 6) & 0x3f);
*s = 0x80 | (wc & 0x3f);
return 4;
}
errno = 0x02; // EILSEQ;
return -1;
}
int wctomb(char *s, wchar_t wc)
{
if (!s)
return 0;
return wcrtomb(s, wc, 0);
}
#endif
size_t strlen(const char *s)
{
const char *a = s;
for (; *s; s++)
;
return s - a;
}
size_t strnlen(const char *s, size_t n)
{
const char *p = memchr(s, 0, n);
return p ? p - s : n;
}
void *memset(void *dest, int c, size_t n)
{
unsigned char *s = dest;
for (; n; n--, s++)
*s = c;
return dest;
}
char *strcpy(char *d, const char *s)
{
for (; (*d = *s); s++, d++)
;
return d;
}
char *strncpy(char *d, const char *s, size_t n)
{
for (; n && (*d = *s); n--, s++, d++)
;
return d;
}
int strcmp(const char *l, const char *r)
{
for (; *l == *r && *l; l++, r++)
;
return *(unsigned char *)l - *(unsigned char *)r;
}
int strncmp(const char *_l, const char *_r, size_t n)
{
const unsigned char *l = (void *)_l, *r = (void *)_r;
if (!n--)
return 0;
for (; *l && *r && n && *l == *r; l++, r++, n--)
;
return *l - *r;
}
static char *twobyte_strstr(const unsigned char *h, const unsigned char *n)
{
uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
for (h++; *h && hw != nw; hw = hw << 8 | *++h)
;
return *h ? (char *)h - 1 : 0;
}
static char *threebyte_strstr(const unsigned char *h, const unsigned char *n)
{
uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8;
uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8;
for (h += 2; *h && hw != nw; hw = (hw | *++h) << 8)
;
return *h ? (char *)h - 2 : 0;
}
static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n)
{
uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
for (h += 3; *h && hw != nw; hw = hw << 8 | *++h)
;
return *h ? (char *)h - 3 : 0;
}
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define BITOP(a, b, op) \
((a)[(size_t)(b) / (8 * sizeof *(a))] op(size_t) 1 << ((size_t)(b) % (8 * sizeof *(a))))
static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
{
const unsigned char *z;
size_t l, ip, jp, k, p, ms, p0, mem, mem0;
size_t byteset[32 / sizeof(size_t)] = {0};
size_t shift[256];
/* Computing length of needle and fill shift table */
for (l = 0; n[l] && h[l]; l++)
BITOP(byteset, n[l], |=), shift[n[l]] = l + 1;
if (n[l])
return 0; /* hit the end of h */
/* Compute maximal suffix */
ip = -1;
jp = 0;
k = p = 1;
while (jp + k < l)
{
if (n[ip + k] == n[jp + k])
{
if (k == p)
{
jp += p;
k = 1;
}
else
k++;
}
else if (n[ip + k] > n[jp + k])
{
jp += k;
k = 1;
p = jp - ip;
}
else
{
ip = jp++;
k = p = 1;
}
}
ms = ip;
p0 = p;
/* And with the opposite comparison */
ip = -1;
jp = 0;
k = p = 1;
while (jp + k < l)
{
if (n[ip + k] == n[jp + k])
{
if (k == p)
{
jp += p;
k = 1;
}
else
k++;
}
else if (n[ip + k] < n[jp + k])
{
jp += k;
k = 1;
p = jp - ip;
}
else
{
ip = jp++;
k = p = 1;
}
}
if (ip + 1 > ms + 1)
ms = ip;
else
p = p0;
/* Periodic needle? */
if (memcmp(n, n + p, ms + 1))
{
mem0 = 0;
p = MAX(ms, l - ms - 1) + 1;
}
else
mem0 = l - p;
mem = 0;
/* Initialize incremental end-of-haystack pointer */
z = h;
/* Search loop */
for (;;)
{
/* Update incremental end-of-haystack pointer */
if (z - h < l)
{
/* Fast estimate for MAX(l,63) */
size_t grow = l | 63;
const unsigned char *z2 = memchr(z, 0, grow);
if (z2)
{
z = z2;
if (z - h < l)
return 0;
}
else
z += grow;
}
/* Check last byte first; advance by shift on mismatch */
if (BITOP(byteset, h[l - 1], &))
{
k = l - shift[h[l - 1]];
if (k)
{
if (k < mem)
k = mem;
h += k;
mem = 0;
continue;
}
}
else
{
h += l;
mem = 0;
continue;
}
/* Compare right half */
for (k = MAX(ms + 1, mem); n[k] && n[k] == h[k]; k++)
;
if (n[k])
{
h += k - ms;
mem = 0;
continue;
}
/* Compare left half */
for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--)
;
if (k <= mem)
return (char *)h;
h += p;
mem = mem0;
}
}
char *strstr(const char *h, const char *n)
{
/* Return immediately on empty needle */
if (!n[0])
return (char *)h;
/* Use faster algorithms for short needles */
h = strchr(h, *n);
if (!h || !n[1])
return (char *)h;
if (!h[1])
return 0;
if (!n[2])
return twobyte_strstr((void *)h, (void *)n);
if (!h[2])
return 0;
if (!n[3])
return threebyte_strstr((void *)h, (void *)n);
if (!h[3])
return 0;
if (!n[4])
return fourbyte_strstr((void *)h, (void *)n);
return twoway_strstr((void *)h, (void *)n);
}
char *strchr(const char *s, int c)
{
c = (unsigned char)c;
if (!c)
return (char *)s + strlen(s);
for (; *s && *(unsigned char *)s != c; s++)
;
return (char *)s;
}
void *__memrchr(const void *m, int c, size_t n)
{
const unsigned char *s = m;
c = (unsigned char)c;
while (n--)
if (s[n] == c)
return (void *)(s + n);
return 0;
}
char *strrchr(const char *s, int c)
{
return __memrchr(s, c, strlen(s) + 1);
}
void *memcpy(void *dest, const void *src, size_t n)
{
unsigned char *d = dest;
const unsigned char *s = src;
for (; n; n--)
*d++ = *s++;
return dest;
}
int memcmp(const void *vl, const void *vr, size_t n)
{
const unsigned char *l = vl, *r = vr;
for (; n && *l == *r; n--, l++, r++)
;
return n ? *l - *r : 0;
}
void *memmove(void *dest, const void *src, size_t n)
{
char *d = dest;
const char *s = src;
if (d == s)
return d;
if ((uintptr_t)s - (uintptr_t)d - n <= -2 * n)
return memcpy(d, s, n);
if (d < s)
{
for (; n; n--)
*d++ = *s++;
}
else
{
while (n)
n--, d[n] = s[n];
}
return dest;
}
void *memchr(const void *src, int c, size_t n)
{
const unsigned char *s = src;
c = (unsigned char)c;
for (; n && *s != c; s++, n--)
;
return n ? (void *)s : 0;
}
int puts(const char *s)
{
int sl = strlen(s);
_write(0, s, sl);
_write(0, "\n", 1);
return sl + 1;
}
/*
* The Minimal snprintf() implementation
*
* Copyright (c) 2013,2014 Michal Ludvig <michal@logix.cz>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the auhor nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ----
*
* This is a minimal snprintf() implementation optimised
* for embedded systems with a very limited program memory.
* mini_snprintf() doesn't support _all_ the formatting
* the glibc does but on the other hand is a lot smaller.
* Here are some numbers from my STM32 project (.bin file size):
* no snprintf(): 10768 bytes
* mini snprintf(): 11420 bytes (+ 652 bytes)
* glibc snprintf(): 34860 bytes (+24092 bytes)
* Wasting nearly 24kB of memory just for snprintf() on
* a chip with 32kB flash is crazy. Use mini_snprintf() instead.
*
*/
#define mini_strlen strlen
static int
mini_itoa(long value, unsigned int radix, int uppercase, int unsig,
char *buffer)
{
char *pbuffer = buffer;
int negative = 0;
int i, len;
/* No support for unusual radixes. */
if (radix > 16)
return 0;
if (value < 0 && !unsig)
{
negative = 1;
value = -value;
}
/* This builds the string back to front ... */
do
{
int digit = value % radix;
*(pbuffer++) = (digit < 10 ? '0' + digit : (uppercase ? 'A' : 'a') + digit - 10);
value /= radix;
} while (value > 0);
if (negative)
*(pbuffer++) = '-';
*(pbuffer) = '\0';
/* ... now we reverse it (could do it recursively but will
* conserve the stack space) */
len = (pbuffer - buffer);
for (i = 0; i < len / 2; i++)
{
char j = buffer[i];
buffer[i] = buffer[len - i - 1];
buffer[len - i - 1] = j;
}
return len;
}
static int
mini_pad(char *ptr, int len, char pad_char, int pad_to, char *buffer)
{
int i;
int overflow = 0;
char *pbuffer = buffer;
if (pad_to == 0)
pad_to = len;
if (len > pad_to)
{
len = pad_to;
overflow = 1;
}
for (i = pad_to - len; i > 0; i--)
{
*(pbuffer++) = pad_char;
}
for (i = len; i > 0; i--)
{
*(pbuffer++) = *(ptr++);
}
len = pbuffer - buffer;
if (overflow)
{
for (i = 0; i < 3 && pbuffer > buffer; i++)
{
*(pbuffer-- - 1) = '*';
}
}
return len;
}
struct mini_buff
{
char *buffer, *pbuffer;
unsigned int buffer_len;
};
static int
_puts(char *s, int len, void *buf)
{
if (!buf)
return len;
struct mini_buff *b = buf;
char *p0 = b->buffer;
int i;
/* Copy to buffer */
for (i = 0; i < len; i++)
{
if (b->pbuffer == b->buffer + b->buffer_len - 1)
{
break;
}
*(b->pbuffer++) = s[i];
}
*(b->pbuffer) = 0;
return b->pbuffer - p0;
}
#ifdef MINI_PRINTF_ENABLE_OBJECTS
static int (*mini_handler)(void *data, void *obj, int ch, int lhint, char **bf) = 0;
static void (*mini_handler_freeor)(void *data, void *) = 0;
static void *mini_handler_data = 0;
void mini_printf_set_handler(
void *data,
int (*handler)(void *data, void *obj, int ch, int len_hint, char **buf),
void (*freeor)(void *data, void *buf))
{
mini_handler = handler;
mini_handler_freeor = freeor;
mini_handler_data = data;
}
#endif
int mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list va)
{
struct mini_buff b;
b.buffer = buffer;
b.pbuffer = buffer;
b.buffer_len = buffer_len;
if (buffer_len == 0)
buffer = (void *)0;
int n = mini_vpprintf(_puts, (buffer != (void *)0) ? &b : (void *)0, fmt, va);
if (buffer == (void *)0)
{
return n;
}
return b.pbuffer - b.buffer;
}
int mini_vpprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, va_list va)
{
char bf[24];
char bf2[24];
char ch;
#ifdef MINI_PRINTF_ENABLE_OBJECTS
void *obj;
#endif
if (puts == (void *)0)
{
/* run puts in counting mode. */
puts = _puts;
buf = (void *)0;
}
int n = 0;
while ((ch = *(fmt++)))
{
int len;
if (ch != '%')
{
len = 1;
len = puts(&ch, len, buf);
}
else
{
char pad_char = ' ';
int pad_to = 0;
char l = 0;
char *ptr;
ch = *(fmt++);
/* Zero padding requested */
if (ch == '0')
pad_char = '0';
while (ch >= '0' && ch <= '9')
{
pad_to = pad_to * 10 + (ch - '0');
ch = *(fmt++);
}
if (pad_to > (signed int)sizeof(bf))
{
pad_to = sizeof(bf);
}
if (ch == 'l')
{
l = 1;
ch = *(fmt++);
}
switch (ch)
{
case 0:
goto end;
case 'u':
case 'd':
if (l)
{
len = mini_itoa(va_arg(va, unsigned long), 10, 0, (ch == 'u'), bf2);
}
else
{
if (ch == 'u')
{
len = mini_itoa((unsigned long)va_arg(va, unsigned int), 10, 0, 1, bf2);
}
else
{
len = mini_itoa((long)va_arg(va, int), 10, 0, 0, bf2);
}
}
len = mini_pad(bf2, len, pad_char, pad_to, bf);
len = puts(bf, len, buf);
break;
case 'x':
case 'X':
if (l)
{
len = mini_itoa(va_arg(va, unsigned long), 16, (ch == 'X'), 1, bf2);
}
else
{
len = mini_itoa((unsigned long)va_arg(va, unsigned int), 16, (ch == 'X'), 1, bf2);
}
len = mini_pad(bf2, len, pad_char, pad_to, bf);
len = puts(bf, len, buf);
break;
case 'c':
ch = (char)(va_arg(va, int));
len = mini_pad(&ch, 1, pad_char, pad_to, bf);
len = puts(bf, len, buf);
break;
case 's':
ptr = va_arg(va, char *);
len = mini_strlen(ptr);
if (pad_to > 0)
{
len = mini_pad(ptr, len, pad_char, pad_to, bf);
len = puts(bf, len, buf);
}
else
{
len = puts(ptr, len, buf);
}
break;
#ifdef MINI_PRINTF_ENABLE_OBJECTS
case 'O': /* Object by content (e.g. str) */
case 'R': /* Object by representation (e.g. repr)*/
obj = va_arg(va, void *);
len = mini_handler(mini_handler_data, obj, ch, pad_to, &ptr);
if (pad_to > 0)
{
len = mini_pad(ptr, len, pad_char, pad_to, bf);
len = puts(bf, len, buf);
}
else
{
len = puts(ptr, len, buf);
}
mini_handler_freeor(mini_handler_data, ptr);
break;
#endif
default:
len = 1;
len = puts(&ch, len, buf);
break;
}
}
n = n + len;
}
end:
return n;
}
int mini_snprintf(char *buffer, unsigned int buffer_len, const char *fmt, ...)
{
int ret;
va_list va;
va_start(va, fmt);
ret = mini_vsnprintf(buffer, buffer_len, fmt, va);
va_end(va);
return ret;
}
int mini_pprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, ...)
{
int ret;
va_list va;
va_start(va, fmt);
ret = mini_vpprintf(puts, buf, fmt, va);
va_end(va);
return ret;
}
extern uint32_t SYSCON;
void initial_jump() __attribute__((naked)) __attribute((section(".init")));
void initial_jump(void)
{
asm volatile("\n\
.option push\n\
.option norelax\n\
la gp, __global_pointer$\n\
.option pop\n\
la sp, _eusrstack\n");
// Careful: Use registers to prevent overwriting of self-data.
// This clears out BSS.
asm volatile(
" la a0, _sbss\n\
la a1, _ebss\n\
li a2, 0\n\
bge a0, a1, 2f\n\
1: sw a2, 0(a0)\n\
addi a0, a0, 4\n\
blt a0, a1, 1b\n\
2:"
// This loads DATA from FLASH to RAM.
" la a0, _data_lma\n\
la a1, _data_vma\n\
la a2, _edata\n\
1: beq a1, a2, 2f\n\
lw a3, 0(a0)\n\
sw a3, 0(a1)\n\
addi a0, a0, 4\n\
addi a1, a1, 4\n\
bne a1, a2, 1b\n\
2:\n"
#ifdef CPLUSPLUS
// Call __libc_init_array function
" call %0 \n\t"
:
: "i"(__libc_init_array)
: "a0", "a1", "a2", "a3", "a4", "a5", "t0", "t1", "t2", "memory"
#else
:
:
: "a0", "a1", "a2", "a3", "memory"
#endif
);
baremain();
}
void baremain(void)
{
main();
SYSCON = 0x5555; // Power off
}