ARCH :sh=	uname -p
REL=		$(REL-$(ARCH))
REL-sparc=	.rela
REL-i386=	.rel

# IFF you're using the Sun C 4.0 compiler, you'll want
# to uncomment the following line (dynodump doesn't
# work with the incremental linker).  The += will add
# to anything already in CFLAGS from the environment.
# This is known to work with Sun make(1S) but may not
# with yours, so you'll need to make the appropriate
# adjustments.

#CFLAGS +=  -xildoff

COPTFLAG=-g

# If you want to make binaries optimized for UltraSPARC (and that only
# run on UltraSPARC) then consider uncommenting the following:

#CFLAGS += -xarch=v8plusa -xchip=ultra

all:		main libfoo1.so libfoo2.so

main:		main.o libfoo1.so dynodump.so
		$(LDOPTS) cc $(CFLAGS) $(COPTFLAG) -o $@ main.o -R. -L. -lfoo1 dynodump.so

libfoo1.so:	foo1.o
		$(LDOPTS) cc -o $@ -h libfoo.so -G foo1.o

libfoo2.so:	foo2.o
		$(LDOPTS) cc -o $@ -h libfoo.so -G foo2.o

foo1.o:		foo1.c
		cc $(COPTFLAG) -c -o $@ -Kpic $<

foo2.o:		foo2.c
		cc $(COPTFLAG) -c -o $@ -Kpic $<

clean:
		rm -f main.1 main.1.1 main main.o libfoo.so \
			libfoo1.so libfoo2.so foo1.o foo2.o \
			dynodump.so

dynodump.so: ../dynodump.so
	ln -s ../dynodump.so

../dynodump.so:
	(cd ..; make)

test: all test1 test2 test3 test4 test5

test1:
	@echo
	@echo "<<< Test 1 >>>"
	@echo "Display the initial information for main ..."
	@echo
	@dump -ov main
	@dump -n .bss -hv main
	@dump -n .data -hv main | tail -3
	@dump -n $(REL).data -hv main | tail -3
	@dump -n $(REL).bss -hv main | tail -3
	@echo
	@dump -tv main | nawk '{ if (NR <= 6) print $$0 } /_edata/ || /_end/ { print $$0 }'

test2:
	@echo
	@echo "<<< Test 2 >>>"
	@echo "With a binding to libfoo1, dump main saving the relocations"
	@echo "that have been carried out by the runtime linker (note"
	@echo "that copy relocations are not retained) ..."
	@echo
	@rm -f libfoo.so main.1; ln -s libfoo1.so libfoo.so
	./main 1
	@echo
	@dump -ov main.1
	@dump -n .bss -hv main.1
	@dump -n .data -hv main.1 | tail -3
	@dump -n .heap -hv main.1 | tail -3
	@dump -n $(REL).data -hv main.1 | tail -3
	@dump -n $(REL).bss -hv main.1 | tail -3
	@echo
	@dump -tv main | nawk '{ if (NR <= 6) print $$0 } /_edata/ || /_end/ { print $$0 }'

test3:
	@echo
	@echo "<<< Test 3 >>>"
	@echo "Exercise this new image (without dumping it again).  As"
	@echo "all relocated have been restored from the original, the new"
	@echo "image will run fine against its original library dependancy"
	@echo "(libfoo1), and if the dependency is modified (libfoo2) ..."
	@echo
	@rm -f libfoo.so; ln -s libfoo1.so libfoo.so
	./main.1
	@rm -f libfoo.so; ln -s libfoo2.so libfoo.so
	./main.1

test4:
	@echo
	@echo "<<< Test 4 >>>"
	@echo "Dump the new image a second time ..."
	@echo
	@rm -f libfoo.so main.1.1; ln -s libfoo1.so libfoo.so
	./main.1 1
	@echo
	@dump -ov main.1.1
	@dump -n .bss -hv main.1.1
	@dump -n .data -hv main.1.1 | tail -3
	@dump -n .heap -hv main.1.1 | tail -3
	@dump -n $(REL).data -hv main.1.1 | tail -3
	@dump -n $(REL).bss -hv main.1.1 | tail -3
	@echo
	@dump -tv main | nawk '{ if (NR <= 6) print $$0 } /_edata/ || /_end/ { print $$0 }'

test5:
	@echo
	@echo "<<< Test 5 >>>"
	@echo "Exercise this new image.  Again, it should run fine with"
	@echo "the original (libfoo1) and a modified (libfoo2) dependency..."
	@echo
	@rm -f libfoo.so; ln -s libfoo1.so libfoo.so
	./main.1.1
	@rm -f libfoo.so; ln -s libfoo2.so libfoo.so
	./main.1.1
