From 6c597c18a72aa2bd459a30b103b9ba63233f43e9 Mon Sep 17 00:00:00 2001 From: drowe67 Date: Wed, 22 May 2013 12:55:26 +0000 Subject: [PATCH] some more stlink files git-svn-id: https://svn.code.sf.net/p/freetel/code@1262 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2-dev/stm32/README.txt | 14 +- codec2-dev/stm32/stlink/elfsym.c | 145 +++++++++++++++++++++ codec2-dev/stm32/stlink/elfsym.h | 14 ++ codec2-dev/stm32/{ => stlink}/stlink.patch | 0 4 files changed, 168 insertions(+), 5 deletions(-) create mode 100644 codec2-dev/stm32/stlink/elfsym.c create mode 100644 codec2-dev/stm32/stlink/elfsym.h rename codec2-dev/stm32/{ => stlink}/stlink.patch (100%) diff --git a/codec2-dev/stm32/README.txt b/codec2-dev/stm32/README.txt index bc9ad743..eac2d04b 100644 --- a/codec2-dev/stm32/README.txt +++ b/codec2-dev/stm32/README.txt @@ -6,6 +6,7 @@ TODO + Describe what gdb_stdio does, describe what UT does. + Where raw files end up. + Dump files and how to use them. + + check if "CFLAGS: -mlittle-endian -mthumb -mthumb-interwork" needed Getting Started ------------------------- @@ -28,7 +29,10 @@ Getting Started $ git clone https://github.com/texane/stlink.git $ cd stlink ~/stlink$ git checkout bbecbc1e81b15b85829149424d048d96bd844939 - ~/stlink$ patch -p0 < ~/codec2-dev/stlink.patch + ~/stlink$ patch -p0 < ~/codec2-dev/stm32/stlink/stlink.patch + ~/stlink$ cp ~/codec2-dev/stm32/stlink/elfsym.* . + ~/stlink$ ./autogen.sh + ~/stlink$ ./configure ~/stlink$ make . Place a copy of hts1a.raw in the stlink directory and start st-util: @@ -36,7 +40,7 @@ Getting Started ~/stlink$ cp ~/codec2-dev/raw/hts1a . ~/stlink$ sudo ./st-util -f /home/david/codec2-dev/stm32/stm32f4_codec2.elf -. Start gdb: +. In _another_ console start gdb: $ ~/codec2-dev/stm32$ ~/sat/bin/arm-none-eabi-gdb stm32f4_codec2.elf @@ -77,8 +81,8 @@ Process 1. Profiling macros. -2. enable DUMP to dump files, note proofiling times will be corrupted -by this due to latency in talking to Host +2. enable DUMP variable in Makefile to dump files, note profiling +times will be corrupted by this due to latency in talking to Host 3. Compare outputs using octave/diff_codec. Worked example: @@ -88,5 +92,5 @@ diff_codec("~/stlink/ref/hts1a_out_1300.raw", "~/stlink/hts1a_out_1300.raw","~/s Gotcha ------ -using printf rather than gdb_stdio_printf, regular stdio functions are stubbed out so will link, just nothing will happen. +Using printf rather than gdb_stdio_printf, regular stdio functions are stubbed out so will link, just nothing will happen. diff --git a/codec2-dev/stm32/stlink/elfsym.c b/codec2-dev/stm32/stlink/elfsym.c new file mode 100644 index 00000000..1a62981b --- /dev/null +++ b/codec2-dev/stm32/stlink/elfsym.c @@ -0,0 +1,145 @@ +/* + elfsym.c + + Read symbol adresses from a .elf file. + + Based on libelf-howto.c from: http://em386.blogspot.com + + Unit test with: + + gcc elfsym.c -o elfsym -D__UNITTEST__ -Wall -lelf + ./elfsym elf_file.elf +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "elfsym.h" + +#define ERR -1 + +int elfsym_open(char file[]) { + int fd; /* File Descriptor */ + char *base_ptr; /* ptr to our object in memory */ + struct stat elf_stats; /* fstat struct */ + + if((fd = open(file, O_RDWR)) == ERR) { + printf("couldnt open %s\n", file); + return ERR; + } + + if((fstat(fd, &elf_stats))) { + printf("could not fstat %s\n", file); + close(fd); + return ERR; + } + + if((base_ptr = (char *) malloc(elf_stats.st_size)) == NULL) { + fprintf(stderr, "could not malloc\n"); + close(fd); + return ERR; + } + + if((read(fd, base_ptr, elf_stats.st_size)) < elf_stats.st_size) { + fprintf(stderr, "could not read %s\n", file); + free(base_ptr); + close(fd); + return ERR; + } + + /* Check libelf version first */ + + if(elf_version(EV_CURRENT) == EV_NONE) { + fprintf(stderr, "WARNING Elf Library is out of date!\n"); + } + + free(base_ptr); + + return fd; +} + + +void elfsym_close(int fd) { + close(fd); +} + +unsigned int elfsym_get_symbol_address(int fd, char symbol_name[]) +{ + Elf_Scn *scn; /* Section Descriptor */ + Elf_Data *edata; /* Data Descriptor */ + GElf_Sym sym; /* Symbol */ + GElf_Shdr shdr; /* Section Header */ + Elf *elf; /* Our Elf pointer for libelf */ + unsigned int symbol_address; + int symbol_count; + int i; + + /* Iterate through section headers, stop when we find symbols, + and check for match */ + + elf = elf_begin(fd, ELF_C_READ, NULL); + if (elf == 0) { + fprintf(stderr, "could not elf_begin\n"); + } + symbol_address = 0; + scn = NULL; + + while((scn = elf_nextscn(elf, scn)) != 0) { + gelf_getshdr(scn, &shdr); + + // When we find a section header marked SHT_SYMTAB stop and get symbols + edata = NULL; + if(shdr.sh_type == SHT_SYMTAB) { + // edata points to our symbol table + edata = elf_getdata(scn, edata); + + // how many symbols are there? this number comes from the size of + // the section divided by the entry size + symbol_count = shdr.sh_size / shdr.sh_entsize; + + // loop through to grab all symbols + for(i = 0; i < symbol_count; i++) { + // libelf grabs the symbol data using gelf_getsym() + gelf_getsym(edata, i, &sym); + + if (strcmp(symbol_name, + elf_strptr(elf, shdr.sh_link, sym.st_name)) == 0) { + symbol_address = sym.st_value; + } + } + + } + } + + return symbol_address; +} + +#ifdef __UNITTEST__ + +int main(int argc, char *argv[]) +{ + int fd; + unsigned int flag_addr, ptr_addr, file_addr, len_addr; + + fd = elfsym_open(argv[1]); + flag_addr = elfsym_get_symbol_address(fd, "syscalls_gdb_flag"); + ptr_addr = elfsym_get_symbol_address(fd, "syscalls_gdb_ptr"); + file_addr = elfsym_get_symbol_address(fd, "syscalls_gdb_file"); + len_addr = elfsym_get_symbol_address(fd, "syscalls_gdb_len"); + elfsym_close(fd); + + printf("flag_addr: 0x%x\n", flag_addr); + printf("ptr_addr: 0x%x\n", ptr_addr); + printf("file_addr: 0x%x\n", file_addr); + printf("len_addr: 0x%x\n", len_addr); + + return 0; +} + +#endif diff --git a/codec2-dev/stm32/stlink/elfsym.h b/codec2-dev/stm32/stlink/elfsym.h new file mode 100644 index 00000000..fcd287ab --- /dev/null +++ b/codec2-dev/stm32/stlink/elfsym.h @@ -0,0 +1,14 @@ +/* + elfsym.h + + Read symbol adresses from a .elf file. +*/ + +#ifndef __ELFSYM__ +#define __ELFSYM__ + +int elfsym_open(char file[]); +void elfsym_close(int fd); +unsigned int elfsym_get_symbol_address(int fd, char symbol_name[]); + +#endif diff --git a/codec2-dev/stm32/stlink.patch b/codec2-dev/stm32/stlink/stlink.patch similarity index 100% rename from codec2-dev/stm32/stlink.patch rename to codec2-dev/stm32/stlink/stlink.patch -- 2.25.1