Commit 16a354f920f3959ed847bd917bdfbc7eba48cf1e

Authored by Wolfgang Denk
1 parent 553f09823c

include/asm-offsets.h: automatically generate assembler constants

A recurrent issue is that certain C level constructs like sizeof() or
offsetof() cannot be used in assembler files, which is inconvenient
when such constructs are used in the definition of macro names etc.

To avoid duplication of such definitions (and thus another cause of
problems), we adapt the Linux way to automatically generate the
respective definitions from the respective C header files.

In Linux, this is implemented in include/linux/kbuild.h, Kbuild, and
arch/*/kernel/asm-offsets.c; we adapt the code from the Linux v2.6.36
kernel tree.

We also copy the concept of the include/generated/ directory which can
be used to hold other automatically generated files as well.

We start with an architecture-independent lib/asm-offsets.c which
generates include/generated/generic-asm-offsets.h (included by
include/asm-offsets.h, which is what will be referred to in the actual
source code).  Later this may be extended by architecture-specific
arch/*/lib/asm-offsets.c files that will generate a
include/generated/asm-offsets.h.

Signed-off-by: Wolfgang Denk <wd@denx.de>
Acked-by: Kumar Gala <galak@kernel.crashing.org>

Showing 6 changed files with 96 additions and 2 deletions Side-by-side Diff

... ... @@ -40,6 +40,9 @@
40 40 /errlog
41 41 /reloc_off
42 42  
  43 +/include/generated/
  44 +/lib/asm-offsets.s
  45 +
43 46 # stgit generated dirs
44 47 patches-*
45 48 .stgit-edit.txt
... ... @@ -372,7 +372,8 @@
372 372 cd $(LNDIR) && $(LD) $(LDFLAGS) $$UNDEF_SYM $(__OBJS) \
373 373 --start-group $(__LIBS) --end-group $(PLATFORM_LIBS) \
374 374 -Map u-boot.map -o u-boot
375   -$(obj)u-boot: depend $(SUBDIRS) $(OBJS) $(LIBBOARD) $(LIBS) $(LDSCRIPT) $(obj)u-boot.lds
  375 +$(obj)u-boot: depend \
  376 + $(SUBDIRS) $(OBJS) $(LIBBOARD) $(LIBS) $(LDSCRIPT) $(obj)u-boot.lds
376 377 $(GEN_UBOOT)
377 378 ifeq ($(CONFIG_KALLSYMS),y)
378 379 smap=`$(call SYSTEM_MAP,u-boot) | \
... ... @@ -426,7 +427,9 @@
426 427  
427 428 # Explicitly make _depend in subdirs containing multiple targets to prevent
428 429 # parallel sub-makes creating .depend files simultaneously.
429   -depend dep: $(TIMESTAMP_FILE) $(VERSION_FILE) $(obj)include/autoconf.mk
  430 +depend dep: $(TIMESTAMP_FILE) $(VERSION_FILE) \
  431 + $(obj)include/autoconf.mk \
  432 + $(obj)include/generated/generic-asm-offsets.h
430 433 for dir in $(SUBDIRS) $(CPUDIR) $(dir $(LDSCRIPT)) ; do \
431 434 $(MAKE) -C $$dir _depend ; done
432 435  
... ... @@ -473,6 +476,18 @@
473 476 sed -n -f tools/scripts/define2mk.sed > $@.tmp && \
474 477 mv $@.tmp $@
475 478  
  479 +$(obj)include/generated/generic-asm-offsets.h: $(obj)include/autoconf.mk.dep \
  480 + $(obj)lib/asm-offsets.s
  481 + @$(XECHO) Generating $@
  482 + tools/scripts/make-asm-offsets $(obj)lib/asm-offsets.s $@
  483 +
  484 +$(obj)lib/asm-offsets.s: $(obj)include/autoconf.mk.dep \
  485 + $(src)lib/asm-offsets.c
  486 + @mkdir -p $(obj)lib
  487 + $(CC) -DDO_DEPS_ONLY \
  488 + $(CFLAGS) $(CFLAGS_$(BCURDIR)/$(@F)) $(CFLAGS_$(BCURDIR)) \
  489 + -o $@ $(src)lib/asm-offsets.c -c -S
  490 +
476 491 #########################################################################
477 492 else # !config.mk
478 493 all $(obj)u-boot.hex $(obj)u-boot.srec $(obj)u-boot.bin \
... ... @@ -1206,6 +1221,7 @@
1206 1221 $(obj)u-boot.lds \
1207 1222 $(obj)arch/blackfin/cpu/bootrom-asm-offsets.[chs]
1208 1223 @rm -f $(obj)include/bmp_logo.h
  1224 + @rm -f $(obj)lib/asm-offsets.s
1209 1225 @rm -f $(obj)nand_spl/{u-boot.lds,u-boot-spl,u-boot-spl.map,System.map}
1210 1226 @rm -f $(obj)onenand_ipl/onenand-{ipl,ipl.bin,ipl.map}
1211 1227 @rm -f $(ONENAND_BIN)
... ... @@ -1229,6 +1245,7 @@
1229 1245 @rm -f $(obj)tools/{env/crc32.c,inca-swap-bytes}
1230 1246 @rm -f $(obj)arch/powerpc/cpu/mpc824x/bedbug_603e.c
1231 1247 @rm -f $(obj)include/asm/proc $(obj)include/asm/arch $(obj)include/asm
  1248 + @rm -fr $(obj)include/generated
1232 1249 @[ ! -d $(obj)nand_spl ] || find $(obj)nand_spl -name "*" -type l -print | xargs rm -f
1233 1250 @[ ! -d $(obj)onenand_ipl ] || find $(obj)onenand_ipl -name "*" -type l -print | xargs rm -f
1234 1251  
include/asm-offsets.h
  1 +#include <generated/generic-asm-offsets.h>
  2 +/* #include <generated/asm-offsets.h> */
include/linux/kbuild.h
  1 +/*
  2 + * Copied from Linux:
  3 + * commit 37487a56523d402e25650da16c337acf4cecd13d
  4 + * Author: Christoph Lameter <clameter@sgi.com>
  5 + */
  6 +#ifndef __LINUX_KBUILD_H
  7 +#define __LINUX_KBUILD_H
  8 +
  9 +#define DEFINE(sym, val) \
  10 + asm volatile("\n->" #sym " %0 " #val : : "i" (val))
  11 +
  12 +#define BLANK() asm volatile("\n->" : : )
  13 +
  14 +#define OFFSET(sym, str, mem) \
  15 + DEFINE(sym, offsetof(struct str, mem))
  16 +
  17 +#define COMMENT(x) \
  18 + asm volatile("\n->#" x)
  19 +
  20 +#endif
  1 +/*
  2 + * Adapted from Linux v2.6.36 kernel: arch/powerpc/kernel/asm-offsets.c
  3 + *
  4 + * This program is used to generate definitions needed by
  5 + * assembly language modules.
  6 + *
  7 + * We use the technique used in the OSF Mach kernel code:
  8 + * generate asm statements containing #defines,
  9 + * compile this file to assembler, and then extract the
  10 + * #defines from the assembly-language output.
  11 + *
  12 + * This program is free software; you can redistribute it and/or
  13 + * modify it under the terms of the GNU General Public License
  14 + * as published by the Free Software Foundation; either version
  15 + * 2 of the License, or (at your option) any later version.
  16 + */
  17 +
  18 +#include <common.h>
  19 +
  20 +#include <linux/kbuild.h>
  21 +
  22 +int main(void)
  23 +{
  24 + return 0;
  25 +}
tools/scripts/make-asm-offsets
  1 +#!/bin/sh
  2 +
  3 +# Adapted from Linux kernel's "Kbuild":
  4 +# commit 1cdf25d704f7951d02a04064c97db547d6021872
  5 +# Author: Christoph Lameter <clameter@sgi.com>
  6 +
  7 +mkdir -p $(dirname $2)
  8 +
  9 +# Default sed regexp - multiline due to syntax constraints
  10 +SED_CMD="/^->/{s:->#\(.*\):/* \1 */:; \
  11 + s:^->\([^ ]*\) [\$$#]*\([-0-9]*\) \(.*\):#define \1 (\2) /* \3 */:; \
  12 + s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
  13 + s:->::; p;}"
  14 +
  15 +(set -e
  16 + echo "#ifndef __ASM_OFFSETS_H__"
  17 + echo "#define __ASM_OFFSETS_H__"
  18 + echo "/*"
  19 + echo " * DO NOT MODIFY."
  20 + echo " *"
  21 + echo " * This file was generated by $(basename $0)"
  22 + echo " *"
  23 + echo " */"
  24 + echo ""
  25 + sed -ne "${SED_CMD}" $1
  26 + echo ""
  27 + echo "#endif" ) > $2