# endianess of target (automagically determined below)
ENDIAN=
-# don't do this if ELF format isn't used. it doesn't matter since offsets are
-# only needed for the asm parts (currently mips/arm32) and those have ELF
-check_elf ()
+# check which object format to dissect
+READELF=
+OBJDUMP=
+check_obj ()
{
+ # prepare an object file; as side effect dtermine the endianess
+ CROSS=$(echo $CC | sed 's/gcc.*//')
echo '#include <stdint.h>' >/tmp/getoffs.c
echo "const int32_t val = 1;" >>/tmp/getoffs.c
$CC $CFLAGS -I .. -c /tmp/getoffs.c -o /tmp/getoffs.o || exit 1
- if ! command -v readelf >/dev/null || ! file /tmp/getoffs.o | grep -q ELF; then
- echo "/* mkoffset.sh: no readelf or not ELF, offset table not created */" >$fn
- echo "WARNING: no readelf or not ELF, offset table not created"
- exit
+
+ # check for readelf; readelf is the only toolchain tool not using bfd,
+ # hence it works with ELF files for every target
+ if file /tmp/getoffs.o | grep -q ELF; then
+ if command -v readelf >/dev/null; then
+ READELF=readelf
+ elif command -v ${CROSS}readelf >/dev/null; then
+ READELF=${CROSS}readelf
+ fi
+ fi
+ if [ -n "$READELF" ]; then
+ # find the the .rodata section (in case -fdata-sections is used)
+ rosect=$($READELF -S /tmp/getoffs.o | grep '\.rodata\|\.sdata' |
+ sed 's/^[^.]*././;s/ .*//')
+ # read .rodata section as hex string (should be only 4 bytes)
+ ro=$($READELF -x $rosect /tmp/getoffs.o | grep '0x' | cut -c14-48 |
+ tr -d ' \n' | cut -c1-8)
+ # if no output could be read readelf isn't working
+ if [ -z "$ro" ]; then
+ READELF=
+ fi
+ fi
+ # if there is no working readelf try using objdump
+ if [ -z "$READELF" ]; then
+ # objdump is using bfd; try using the toolchain objdump first
+ # since this is likely working with the toolchain objects
+ if command -v ${CROSS}objdump >/dev/null; then
+ OBJDUMP=${CROSS}objdump
+ elif command -v objdump >/dev/null; then
+ OBJDUMP=objdump
+ fi
+ # find the start line of the .rodata section; read the next line
+ ro=$($OBJDUMP -s /tmp/getoffs.o | awk '\
+ /Contents of section.*(__const|.rodata|.sdata)/ {o=1; next} \
+ {if(o) { gsub(/ .*/,""); $1=""; gsub(/ /,""); print; o=0}}')
+ # no working tool for extracting the ro data; stop here
+ if [ -z "$ro" ]; then
+ echo "/* mkoffset.sh: no readelf or not ELF, offset table not created */" >$fn
+ echo "WARNING: no readelf or not ELF, offset table not created"
+ exit
+ fi
fi
+ # extract decimal value from ro
+ rodata=$(printf "%d" 0x$ro)
+ ENDIAN=$(if [ "$rodata" -eq 1 ]; then echo be; else echo le; fi)
}
# compile with target C compiler and extract value from .rodata section
compile_rodata ()
{
$CC $CFLAGS -I .. -c /tmp/getoffs.c -o /tmp/getoffs.o || exit 1
- # find the name of the .rodata section (in case -fdata-sections is used)
- rosect=$(readelf -S /tmp/getoffs.o | grep '\.rodata\|\.sdata' |
+ if [ -n "$READELF" ]; then
+ # find the .rodata section (in case -fdata-sections is used)
+ rosect=$(readelf -S /tmp/getoffs.o | grep '\.rodata\|\.sdata' |
sed 's/^[^.]*././;s/ .*//')
- # read out .rodata section as hex string (should be only 4 bytes)
- ro=$(readelf -x $rosect /tmp/getoffs.o | grep '0x' | cut -c14-48 |
+ # read .rodata section as hex string (should be only 4 bytes)
+ ro=$(readelf -x $rosect /tmp/getoffs.o | grep '0x' | cut -c14-48 |
tr -d ' \n' | cut -c1-8)
+ elif [ -n "$OBJDUMP" ]; then
+ # find the start line of the .rodata section; read the next line
+ ro=$($OBJDUMP -s /tmp/getoffs.o | awk '\
+ /Contents of section.*(__const|.rodata|.sdata)/ {o=1; next} \
+ {if(o) { gsub(/ .*/,""); $1=""; gsub(/ /,""); print; o=0}}')
+ fi
if [ "$ENDIAN" = "le" ]; then
# swap needed for le target
hex=""
fn="${1:-.}/pico_int_offs.h"
if echo $CFLAGS | grep -qe -flto; then CFLAGS="$CFLAGS -fno-lto"; fi
-check_elf
-# determine endianess
-echo '#include <stdint.h>' >/tmp/getoffs.c
-echo "const int32_t val = 1;" >>/tmp/getoffs.c
-compile_rodata
-ENDIAN=$(if [ "$rodata" -eq 1 ]; then echo be; else echo le; fi)
+check_obj
# output header
echo "/* autogenerated by mkoffset.sh, do not edit */" >$fn
echo "/* target endianess: $ENDIAN, compiled with: $CC $CFLAGS */" >>$fn