Skip to content

Commit 16a354f

Browse files
committed
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>
1 parent 553f098 commit 16a354f

6 files changed

Lines changed: 96 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
/errlog
4141
/reloc_off
4242

43+
/include/generated/
44+
/lib/asm-offsets.s
45+
4346
# stgit generated dirs
4447
patches-*
4548
.stgit-edit.txt

Makefile

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,8 @@ GEN_UBOOT = \
372372
cd $(LNDIR) && $(LD) $(LDFLAGS) $$UNDEF_SYM $(__OBJS) \
373373
--start-group $(__LIBS) --end-group $(PLATFORM_LIBS) \
374374
-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
376377
$(GEN_UBOOT)
377378
ifeq ($(CONFIG_KALLSYMS),y)
378379
smap=`$(call SYSTEM_MAP,u-boot) | \
@@ -426,7 +427,9 @@ updater:
426427

427428
# Explicitly make _depend in subdirs containing multiple targets to prevent
428429
# 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
430433
for dir in $(SUBDIRS) $(CPUDIR) $(dir $(LDSCRIPT)) ; do \
431434
$(MAKE) -C $$dir _depend ; done
432435

@@ -473,6 +476,18 @@ $(obj)include/autoconf.mk: $(obj)include/config.h
473476
sed -n -f tools/scripts/define2mk.sed > $@.tmp && \
474477
mv $@.tmp $@
475478

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+
476491
#########################################################################
477492
else # !config.mk
478493
all $(obj)u-boot.hex $(obj)u-boot.srec $(obj)u-boot.bin \
@@ -1206,6 +1221,7 @@ clean:
12061221
$(obj)u-boot.lds \
12071222
$(obj)arch/blackfin/cpu/bootrom-asm-offsets.[chs]
12081223
@rm -f $(obj)include/bmp_logo.h
1224+
@rm -f $(obj)lib/asm-offsets.s
12091225
@rm -f $(obj)nand_spl/{u-boot.lds,u-boot-spl,u-boot-spl.map,System.map}
12101226
@rm -f $(obj)onenand_ipl/onenand-{ipl,ipl.bin,ipl.map}
12111227
@rm -f $(ONENAND_BIN)
@@ -1229,6 +1245,7 @@ clobber: clean
12291245
@rm -f $(obj)tools/{env/crc32.c,inca-swap-bytes}
12301246
@rm -f $(obj)arch/powerpc/cpu/mpc824x/bedbug_603e.c
12311247
@rm -f $(obj)include/asm/proc $(obj)include/asm/arch $(obj)include/asm
1248+
@rm -fr $(obj)include/generated
12321249
@[ ! -d $(obj)nand_spl ] || find $(obj)nand_spl -name "*" -type l -print | xargs rm -f
12331250
@[ ! -d $(obj)onenand_ipl ] || find $(obj)onenand_ipl -name "*" -type l -print | xargs rm -f
12341251

include/asm-offsets.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include <generated/generic-asm-offsets.h>
2+
/* #include <generated/asm-offsets.h> */

include/linux/kbuild.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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

lib/asm-offsets.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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

0 commit comments

Comments
 (0)