Skip to content

Commit

Permalink
Check for installations of libelf and libdwarf when building and
Browse files Browse the repository at this point in the history
adjust build to suit.
  • Loading branch information
ga committed Nov 2, 2022
1 parent 6abb525 commit 292a7bb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
25 changes: 24 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <libelf.h>" > testit.c && \
$(CC) -E $(CFLAGS) ${AVR_CPPFLAGS} testit.c > /dev/null 2>&1 ; \
then \
echo CFLAGS += -DHAVE_LIBELF >> $@ ; \
echo LDFLAGS += -lelf >> $@ ; \
if \
echo "#include <dwarf.h>" > 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)

Expand All @@ -43,4 +65,5 @@ clean:
$(MAKE) -C examples clean
$(MAKE) -C examples/parts clean
$(MAKE) -C doc clean
rm -f Libs.mk

8 changes: 7 additions & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion simavr/sim/sim_dwarf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -23,6 +23,8 @@
along with simavr. If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_LIBDWARF

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Expand Down Expand Up @@ -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
21 changes: 19 additions & 2 deletions simavr/sim/sim_elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifdef HAVE_LIBELF
#include <libelf.h>
#include <gelf.h>
#else
#undef ELF_SYMBOLS
#define ELF_SYMBOLS 0
#endif

#include "sim_elf.h"
#include "sim_vcd_file.h"
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit 292a7bb

Please sign in to comment.