Skip to content

Commit 7ab6ec9

Browse files
author
root
committed
adding newlib0 example
1 parent 4daeded commit 7ab6ec9

File tree

6 files changed

+509
-0
lines changed

6 files changed

+509
-0
lines changed

newlib0/Makefile

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
ARMGNU ?= arm-none-eabi
3+
4+
COPS = -Wall -O2 -nostartfiles -ffreestanding
5+
6+
LIB = -L /opt/gnuarm/arm-none-eabi/lib/ -L/opt/gnuarm/lib/gcc/arm-none-eabi/4.7.2
7+
8+
gcc : uart02.hex uart02.bin
9+
10+
all : gcc
11+
12+
clean :
13+
rm -f *.o
14+
rm -f *.bin
15+
rm -f *.hex
16+
rm -f *.elf
17+
rm -f *.list
18+
rm -f *.img
19+
rm -f *.bc
20+
rm -f *.clang.opt.s
21+
22+
vectors.o : vectors.s
23+
$(ARMGNU)-as vectors.s -o vectors.o
24+
25+
uart02.o : uart02.c
26+
$(ARMGNU)-gcc $(COPS) -c uart02.c -o uart02.o
27+
28+
uart02.elf : memmap vectors.o uart02.o syscalls.o
29+
$(ARMGNU)-ld vectors.o uart02.o syscalls.o -T memmap -o uart02.elf $(LIB) -lc -lgcc
30+
$(ARMGNU)-objdump -D uart02.elf > uart02.list
31+
32+
uart02.bin : uart02.elf
33+
$(ARMGNU)-objcopy uart02.elf -O binary uart02.bin
34+
35+
uart02.hex : uart02.elf
36+
$(ARMGNU)-objcopy uart02.elf -O ihex uart02.hex
37+
38+
syscalls.o : syscalls.c
39+
$(ARMGNU)-gcc $(COPS) -c $(COPS) syscalls.c -o syscalls.o
40+
41+
42+
43+
44+
45+

newlib0/README

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
3+
Somewhat threw this together.
4+
5+
I have not tried newlib in a while, for a while there it was quite
6+
difficult to get a combination of binutils, gcc and newlib that would
7+
build crosscompiled. Somewhat trivial now.
8+
9+
went here
10+
11+
ftp://sources.redhat.com/pub/newlib/index.html
12+
13+
downloaded newlib-1.20.0.tar.gz
14+
15+
untarred it
16+
17+
since I already had a gnu toolchain I built using the scripts in my
18+
build_gcc that I built to /opt/gnuarm
19+
20+
tar xzvf newlib-1.20.0.tar.gz
21+
cd newlib-1.20.0
22+
23+
-- now here is my solution
24+
cd newlib/libc/sys/arm/
25+
mv syscalls.c syscalls.corig
26+
touch syscalls.c
27+
mv libcfunc.c libcfunc.corig
28+
touch libcfunc.c
29+
mv crt0.S crt0.Sorig
30+
touch crt0.S
31+
cd ../../../../
32+
-- should be back in the newlib-1.20.0 directory
33+
34+
./configure --target=arm-none-eabi --prefix=/opt/gnuarm
35+
make
36+
make install
37+
38+
what I would then do is go into syscalls and start pruning, make all
39+
the functions return whatever the proper flavor of success was. to
40+
get printf to work you need isatty to return the right thing. note
41+
I also have a functional malloc here by defining the beginning of my
42+
heap and doing trivial memory management.
43+
44+
The way I used to do all of this is based on something I learned from
45+
others, before building gcc pull in the proper newlib directories
46+
and then build gcc with the right headers and newlib stuff and it
47+
would build it all into one nice package (when it worked) so you didnt
48+
have to specify lib paths. Since I threw this together I had to specify
49+
the lib paths to things you will need to fix those paths for yours.
50+
51+
The reason why I neutered the three files in newlib is it is a quick and
52+
easy way to allow the newlib makefile to continue without complaint but
53+
also to allow the project to own those system calls. Since I run bare
54+
metal each project may want to do something different. If this were an
55+
operating system use of newlib then I would consider one solution for
56+
everyone and have it built into the normal place.
57+
58+
I had to fight the linker a little to get the right code in the right
59+
place. Be careful to check your disassembly of your binary so that
60+
you know that your start code is at the beginning of your image not
61+
somewhere after crti or crtbegin or something like that.
62+
63+
you should see
64+
65+
...
66+
12345678
67+
0000800C
68+
Hello World!
69+
0x0000800C
70+
32780
71+
72+
If it all works. for now I will not leave binaries in the repo even
73+
though I expect this is a challenging build for many.

newlib0/memmap

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
MEMORY
3+
{
4+
ram : ORIGIN = 0x8000, LENGTH = 0x20000
5+
}
6+
7+
SECTIONS
8+
{
9+
.text : { *(.text*) } > ram
10+
.bss : { *(.bss*) } > ram
11+
}
12+

newlib0/syscalls.c

+222
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/* Support files for GNU libc. Files in the system namespace go here.
2+
Files in the C namespace (ie those that do not start with an
3+
underscore) go in .c. */
4+
5+
#include <_ansi.h>
6+
#include <sys/types.h>
7+
#include <sys/stat.h>
8+
#include <sys/fcntl.h>
9+
#include <stdio.h>
10+
#include <time.h>
11+
#include <sys/time.h>
12+
#include <sys/times.h>
13+
#include <errno.h>
14+
#include <reent.h>
15+
16+
void uart_putc ( unsigned int c );
17+
18+
unsigned int heap_end=0x0020000;
19+
unsigned int prev_heap_end;
20+
21+
/* Forward prototypes. */
22+
int _system _PARAMS ((const char *));
23+
int _rename _PARAMS ((const char *, const char *));
24+
int isatty _PARAMS ((int));
25+
clock_t _times _PARAMS ((struct tms *));
26+
int _gettimeofday _PARAMS ((struct timeval *, struct timezone *));
27+
void _raise _PARAMS ((void));
28+
int _unlink _PARAMS ((void));
29+
int _link _PARAMS ((void));
30+
int _stat _PARAMS ((const char *, struct stat *));
31+
int _fstat _PARAMS ((int, struct stat *));
32+
caddr_t _sbrk _PARAMS ((int));
33+
int _getpid _PARAMS ((int));
34+
int _kill _PARAMS ((int, int));
35+
void _exit _PARAMS ((int));
36+
int _close _PARAMS ((int));
37+
int _open _PARAMS ((const char *, int, ...));
38+
int _write _PARAMS ((int, char *, int));
39+
int _lseek _PARAMS ((int, int, int));
40+
int _read _PARAMS ((int, char *, int));
41+
void initialise_monitor_handles _PARAMS ((void));
42+
43+
//static int
44+
//remap_handle (int fh)
45+
//{
46+
//return 0;
47+
//}
48+
49+
void
50+
initialise_monitor_handles (void)
51+
{
52+
}
53+
54+
//static int
55+
//get_errno (void)
56+
//{
57+
//return(0);
58+
//}
59+
60+
//static int
61+
//error (int result)
62+
//{
63+
//errno = get_errno ();
64+
//return result;
65+
//}
66+
67+
68+
int
69+
_read (int file,
70+
char * ptr,
71+
int len)
72+
{
73+
return len;
74+
}
75+
76+
77+
int
78+
_lseek (int file,
79+
int ptr,
80+
int dir)
81+
{
82+
return 0;
83+
}
84+
85+
86+
int
87+
_write (int file,
88+
char * ptr,
89+
int len)
90+
{
91+
int r;
92+
for(r=0;r<len;r++) uart_putc(ptr[r]);
93+
return len;
94+
}
95+
96+
int
97+
_open (const char * path,
98+
int flags,
99+
...)
100+
{
101+
return 0;
102+
}
103+
104+
105+
int
106+
_close (int file)
107+
{
108+
return 0;
109+
}
110+
111+
void
112+
_exit (int n)
113+
{
114+
while(1);
115+
}
116+
117+
int
118+
_kill (int n, int m)
119+
{
120+
return(0);
121+
}
122+
123+
int
124+
_getpid (int n)
125+
{
126+
return 1;
127+
n = n;
128+
}
129+
130+
131+
caddr_t
132+
_sbrk (int incr)
133+
{
134+
prev_heap_end = heap_end;
135+
heap_end += incr;
136+
return (caddr_t) prev_heap_end;
137+
}
138+
139+
int
140+
_fstat (int file, struct stat * st)
141+
{
142+
return 0;
143+
}
144+
145+
int _stat (const char *fname, struct stat *st)
146+
{
147+
return 0;
148+
}
149+
150+
int
151+
_link (void)
152+
{
153+
return -1;
154+
}
155+
156+
int
157+
_unlink (void)
158+
{
159+
return -1;
160+
}
161+
162+
void
163+
_raise (void)
164+
{
165+
return;
166+
}
167+
168+
int
169+
_gettimeofday (struct timeval * tp, struct timezone * tzp)
170+
{
171+
if(tp)
172+
{
173+
tp->tv_sec = 10;
174+
tp->tv_usec = 0;
175+
}
176+
if (tzp)
177+
{
178+
tzp->tz_minuteswest = 0;
179+
tzp->tz_dsttime = 0;
180+
}
181+
return 0;
182+
}
183+
184+
clock_t
185+
_times (struct tms * tp)
186+
{
187+
clock_t timeval;
188+
189+
timeval = 10;
190+
if (tp)
191+
{
192+
tp->tms_utime = timeval; /* user time */
193+
tp->tms_stime = 0; /* system time */
194+
tp->tms_cutime = 0; /* user time, children */
195+
tp->tms_cstime = 0; /* system time, children */
196+
}
197+
return timeval;
198+
};
199+
200+
201+
int
202+
_isatty (int fd)
203+
{
204+
return 1;
205+
fd = fd;
206+
}
207+
208+
int
209+
_system (const char *s)
210+
{
211+
if (s == NULL)
212+
return 0;
213+
errno = ENOSYS;
214+
return -1;
215+
}
216+
217+
int
218+
_rename (const char * oldpath, const char * newpath)
219+
{
220+
errno = ENOSYS;
221+
return -1;
222+
}

0 commit comments

Comments
 (0)