001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018 CC=gcc
019 AR=ar
020 CP=cp
021 STRIP=strip
022 OBJCOPY=objcopy
023 CFLAGS = -Wall -nostdinc -ffreestanding -I. -I.. -O
024 LIBGCC = $(shell $(CC) -print-libgcc-file-name)
025 LDFLAGS = -Wl,--warn-common -nostdlib -Wl,-Tldscript.lds
026
027
028 all: userprogs.kimg
029
030 -include .mkvars
031
032 PROGS := init myprog1 myprog2 myprog3 myprog4 myprog5 myprog6 \
033 myprog7 myprog8 myprog9 myprog10 myprog11 myprog12 \
034 myprog13 myprog14 banner fstest
035
036 # Build dependencies of the programs
037 fstest: fstest_utils.o
038 $(PROGS) : % : %.o crt.o libc.a
039
040 PWD := $(shell pwd)
041
042
043 $(PROGS):
044 $(CC) -static $(LDFLAGS) -o $@ $^ $(LIBGCC)
045
046
047 libc.a: libc.o string.o stdarg.o debug.o
048
049
050 userprogs.kimg: $(PROGS)
051 @echo "# Generating ELF images for inclusion into the kernel image: $@"
052 @echo "SECTIONS { .userprogs . : { " > .userprogs.lds
053 @i=0 ; \
054 for f in $^ ; do \
055 i=`expr $$i + 1` ; \
056 echo "extern char _begin_userprog$$i, _end_userprog$$i;" \
057 > .userprog$$i.c ; \
058 echo "char *_userprog"$$i"_entry[]" >> .userprog$$i.c ; \
059 echo " __attribute__((section(\".userprogs_table\")))" \
060 >> .userprog$$i.c ; \
061 echo " = { \"$$f\", &_begin_userprog$$i, &_end_userprog$$i };" \
062 >> .userprog$$i.c ; \
063 $(CC) $(CFLAGS) -c .userprog$$i.c -o .userprog$$i.o ; \
064 $(CP) $$f $$f.strip && $(STRIP) -sx $$f.strip ; \
065 $(OBJCOPY) --add-section .userprog$$i=$$f.strip .userprog$$i.o \
066 .userprog$$i.kimg ; \
067 echo " . = ALIGN(4096);" >> .userprogs.lds ; \
068 echo " _begin_userprog$$i = .;" >> .userprogs.lds ; \
069 echo " .userprog$$i.kimg(.userprog$$i);" >> .userprogs.lds ; \
070 echo " _end_userprog$$i = .;" >> .userprogs.lds ; \
071 echo " .userprog$$i.kimg(.rodata); .userprog$$i.kimg(.data);" \
072 >> .userprogs.lds ; \
073 done
074 @echo " _userprogs_table = .; *(.userprogs_table) ; LONG(0);" \
075 >> .userprogs.lds
076 @echo "} /DISCARD/ : { *(.text) *(.data) *(.bss) } }" \
077 >> .userprogs.lds
078 @$(LD) -r -o $@ -T.userprogs.lds
079
080
081 %.a:
082 $(AR) rcv $@ $^
083
084
085 %.o: %.c
086 $(CC) -I$(PWD) -c $< $(CFLAGS) -o $@
087
088
089 %.o: %.S
090 $(CC) -I$(PWD) -c $< $(CFLAGS) -DASM_SOURCE=1 -o $@
091
092
093 clean:
094 $(RM) *.o *.a *~ $(PROGS) *.kimg *.strip
095 $(RM) .userprog*