#
# Makefile for emulation library files..
#

# end_of_text has to be the last object in OBJ 
# because it defines the end_of_text symbol
OBJ = int_entry.o user.o \
	end_of_text.o

SIZE=$(CROSS_COMPILE)size
NM=$(CROSS_COMPILE)nm

vpath %.c ../lib
vpath %.S ../lib

emulib.o: emulib link-r.ld
	@data_size="`$(SIZE) --format=sysv $< | grep ^.data | awk '{print $$2}'`"; \
	  bss_size="`$(SIZE) --format=sysv $< | grep ^.bss | awk '{print $$2}'`"; \
	  if [ $$bss_size -gt 0 -o $$data_size -gt 0 ]; then \
	    echo "ERROR: $<: data or bss size > 0"; \
	    exit 1; \
	  else \
	    true; \
	  fi
	cp $< emulib-s
	$(STRIP) emulib-s
	$(LD) $(EXTRA_LDFLAGS) -r \
	  -defsym _user_task_init=0x`$(NM) emulib | grep user_task_init | cut -f1 -d' '` \
	  -defsym user_level_pager=0x`$(NM) emulib | grep user_level_pager | cut -f1 -d' '` \
	  -defsym emulib_start=0x`$(NM) emulib | grep -w real_sys_call | cut -f1 -d' '` \
	  -defsym emulib_end=0x`$(NM) emulib | grep -w end_of_text | cut -f1 -d' '` \
	  -defsym idt_table=0x`$(NM) emulib | grep -w idt_table | cut -f1 -d' '` \
	  -defsym debug_idt_table=0x`$(NM) emulib | grep -w debug_idt_table | cut -f1 -d' '` \
	  -defsym emulib_idt_descr=0x`$(NM) emulib | grep emulib_idt_descr | cut -f1 -d' '` \
	  -defsym emulib_debug_idt_descr=0x`$(NM) emulib | grep emulib_debug_idt_descr | cut -f1 -d' '` \
	  -T link-r.ld -o $@ emulib-s


emulib: $(OBJ)
	@textstart=`( echo '#define __ASSEMBLY__' ; \
	    echo '#include <l4linux/x86/config.h>' ; \
            echo EMULIB_CODE_ADDRESS ) \
            | $(CC) -E - | tail -1 | xargs echo 'printf "0x%x",' | perl -`; \
	  echo $(LD) $(EXTRA_LDFLAGS) -N -Ttext $$textstart -o $@ $^; \
	  $(LD) $(EXTRA_LDFLAGS) -N -Ttext $$textstart -o $@ $^

int_entry.o: int_entry.S Makefile
	$(CC) -c -D__ASSEMBLY__ -traditional -c $*.S -o $*.o

include $(TOPDIR)/Rules.make

CFLAGS += -DEMULIB

clean:
	rm -f *.o emulib-s emulib
