diff --git a/Makefile b/Makefile index 34b719014..644d2d858 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ PREFIX = ${DESTDIR} all: build-simavr build-tests build-examples build-parts -build-simavr: +build-simavr: Libs.mk $(MAKE) -C simavr RELEASE=$(RELEASE) build-tests: build-simavr @@ -34,6 +34,28 @@ install-simavr: install-parts: $(MAKE) -C examples/parts install RELEASE=$(RELEASE) DESTDIR=$(DESTDIR) PREFIX=$(PREFIX) + +# Find whether libelf and libdwarf are available. + +Libs.mk: + @echo CONF $@ + @echo "# Autogenerated: do not edit." > $@ + @if \ + echo "#include " > testit.c && \ + $(CC) -E $(CFLAGS) ${AVR_CPPFLAGS} testit.c > /dev/null 2>&1 ; \ + then \ + echo CFLAGS += -DHAVE_LIBELF >> $@ ; \ + echo LDFLAGS += -lelf >> $@ ; \ + if \ + echo "#include " > testit.c && \ + $(CC) -E $(CFLAGS) ${AVR_CPPFLAGS} testit.c > /dev/null 2>&1 ; \ + then \ + echo CFLAGS += -DHAVE_LIBDWARF >> $@ ; \ + echo LDFLAGS += -ldwarf >> $@ ; \ + fi \ + fi + @rm -f testit.c + doc: $(MAKE) -C doc RELEASE=$(RELEASE) @@ -43,4 +65,5 @@ clean: $(MAKE) -C examples clean $(MAKE) -C examples/parts clean $(MAKE) -C doc clean + rm -f Libs.mk diff --git a/Makefile.common b/Makefile.common index 7981945b9..67fa6a384 100644 --- a/Makefile.common +++ b/Makefile.common @@ -114,7 +114,13 @@ OBJ := obj-${shell $(CC) -dumpmachine} LIBDIR := ${shell pwd}/${SIMAVR}/${OBJ} LDFLAGS += -L${LIBDIR} -lsimavr -lm -LDFLAGS += -lelf -ldwarf +# Are libelf and/or libdwarf installed? +# The included file is created when running "make" at the top level. + +ifeq ($(shell test -f $(SIMAVR)/../Libs.mk || echo Exists), Exists) +$(warning Missing library configuration file. Make from top level to create it.) +endif +-include $(SIMAVR)/../Libs.mk ifeq (${WIN}, Msys) LDFLAGS += -lws2_32 diff --git a/simavr/sim/sim_dwarf.c b/simavr/sim/sim_dwarf.c index ed954eb7d..c86456c77 100644 --- a/simavr/sim/sim_dwarf.c +++ b/simavr/sim/sim_dwarf.c @@ -5,7 +5,7 @@ Extract debug symbols and line numbers from .elf file for use by gdb ("info io_registers" command) and tracing. - Copyright 2021, Giles Atkinson + Copyright 2021, 2022 Giles Atkinson This file is part of simavr. @@ -23,6 +23,8 @@ along with simavr. If not, see . */ +#ifdef HAVE_LIBDWARF + #include #include #include @@ -349,3 +351,8 @@ int avr_read_dwarf(avr_t *avr, const char *filename) close(fd); return 0; } + +# else // No libdwarf +#include "sim_avr.h" +int avr_read_dwarf(avr_t *avr, const char *filename) { return 0; } +#endif diff --git a/simavr/sim/sim_elf.c b/simavr/sim/sim_elf.c index 2277c1c2e..04a44a8c0 100644 --- a/simavr/sim/sim_elf.c +++ b/simavr/sim/sim_elf.c @@ -29,8 +29,14 @@ #include #include #include + +#ifdef HAVE_LIBELF #include #include +#else +#undef ELF_SYMBOLS +#define ELF_SYMBOLS 0 +#endif #include "sim_elf.h" #include "sim_vcd_file.h" @@ -91,12 +97,13 @@ avr_load_firmware( avr->avcc = firmware->avcc; if (firmware->aref) avr->aref = firmware->aref; -#if CONFIG_SIMAVR_TRACE && ELF_SYMBOLS +#if ELF_SYMBOLS +#if CONFIG_SIMAVR_TRACE /* Store the symbols read from the ELF file. */ int scount = firmware->flashsize >> 1; uint32_t addr; - const char ** table; + const char ** table; // Allocate table of Flash address strings. @@ -157,6 +164,7 @@ avr_load_firmware( avr_read_dwarf(avr, firmware->dwarf_file); free(firmware->dwarf_file); #endif +#endif // ELF_SYMBOLS avr_loadcode(avr, firmware->flash, firmware->flashsize, firmware->flashbase); @@ -270,6 +278,7 @@ avr_load_firmware( avr_vcd_start(avr->vcd); } +#ifdef HAVE_LIBELF static void elf_parse_mmcu_section( elf_firmware_t * firmware, @@ -623,3 +632,11 @@ elf_read_firmware( close(fd); return 0; } +#else // HAVE_LIBELF not defined. +int +elf_read_firmware(const char * file, elf_firmware_t * firmware) +{ + AVR_LOG(NULL, LOG_ERROR, "ELF format is not supported by this build.\n"); + return -1; +} +#endif