Removing padding from structure in kernel module - gcc

I am compiling a kernel module, containing a structure of size 34, using the standard command.
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
The sizeof(some_structure) is coming as 36 instead of 34 i.e. the compiler is padding the structure.
How do I remove this padding?
Running make V=1 shows the gcc compiler options passed as
make -I../inc -C /lib/modules/2.6.29.4-167.fc11.i686.PAE/build M=/home/vishal/20100426_eth_vishal/organised_eth/src modules
make[1]: Entering directory `/usr/src/kernels/2.6.29.4-167.fc11.i686.PAE'
test -e include/linux/autoconf.h -a -e include/config/auto.conf || ( \
echo; \
echo " ERROR: Kernel configuration is invalid."; \
echo " include/linux/autoconf.h or include/config/auto.conf are missing."; \
echo " Run 'make oldconfig && make prepare' on kernel src to fix it."; \
echo; \
/bin/false)
mkdir -p /home/vishal/20100426_eth_vishal/organised_eth/src/.tmp_versions ; rm -f /home/vishal/20100426_eth_vishal/organised_eth/src/.tmp_versions/*
make -f scripts/Makefile.build obj=/home/vishal/20100426_eth_vishal/organised_eth/src
gcc -Wp,-MD,/home/vishal/20100426_eth_vishal/organised_eth/src/.eth_main.o.d -nostdinc -isystem /usr/lib/gcc/i586-redhat-linux/4.4.0/include -Iinclude -I/usr/src/kernels/2.6.29.4-167.fc11.i686.PAE/arch/x86/include -include include/linux/autoconf.h -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Os -m32 -msoft-float -mregparm=3 -freg-struct-return -mpreferred-stack-boundary=2 -march=i686 -mtune=generic -Wa,-mtune=generic32 -ffreestanding -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -Iarch/x86/include/asm/mach-generic -Iarch/x86/include/asm/mach-default -Wframe-larger-than=1024 -fno-stack-protector -fno-omit-frame-pointer -fno-optimize-sibling-calls -g -pg -Wdeclaration-after-statement -Wno-pointer-sign -fwrapv -fno-dwarf2-cfi-asm -DTX_DESCRIPTOR_IN_SYSTEM_MEMORY -DRX_DESCRIPTOR_IN_SYSTEM_MEMORY -DTX_BUFFER_IN_SYSTEM_MEMORY -DRX_BUFFER_IN_SYSTEM_MEMORY -DALTERNATE_DESCRIPTORS -DEXT_8_BYTE_DESCRIPTOR -O0 -Wall -DT_ETH_1588_051 -DALTERNATE_DESCRIPTORS -DEXT_8_BYTE_DESCRIPTOR -DNETHERNET_INTERRUPTS -DETH_IEEE1588_TESTS -DSNAPTYPSEL_TMSTRENA_TEVENTENA_TESTS -DT_ETH_1588_140_147 -DLOW_DEBUG_PRINTS -DMEDIUM_DEBUG_PRINTS -DHIGH_DEBUG_PRINTS -DMODULE -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(eth_main)" -D"KBUILD_MODNAME=KBUILD_STR(conxt_eth)" -c -o /home/vishal/20100426_eth_vishal/organised_eth/src/eth_main.o /home/vishal/20100426_eth_vishal/organised_eth/src/eth_main.c

If using GCC, you can use the packed attribute on your structure to prevent padding:
struct foo
{
void * bar;
}
__attribute__( ( packed ) );

#pragma pack might work

I suspect that GCC is forcing the total structure to be aligned onto a 32 bit boundary, so its size is a multiple of 4.
Imagine the following.
struct foo
{
void * bar ;
some other stuff .....
};
struct foo my_foo_array[10];
Then if the sizeof(struct foo) is not a multiple of 4 then.
my_foo_array[0].bar has a different memory alignment to my_foo_array[1].bar. The processor would need to perform 2 32 bit memory accesses in order to access all four bytes of my_foo_array[1].bar. x86 processors will do this reassembly of misaligned 32 bit values but most other processors will throw some form of bus error exception which is not good.
The packed attribute signals how the elements of the structure are packed with respect to each other, but in normal operation the start of structure needs to placed on a 32 bit aligned address.
I hope this explains things a little better.

Related

Linux kernel: can't build module with non-retpoline compiler

OS - RedHat 8.5, kernel 4.18.0-348.20.1.el8_5.x86_64, gcc-8.5.0
I have installed kernel-devel and kernel-headers, and tried to build a very simple kernel module (just a pair of printk()).
Makefile:
obj-m += test_mod.o
MSRC ?= $(PWD)
KDIR ?= /lib/modules/`uname -r`/build/
all:
make -C $(KDIR) M=$(MSRC) modules
clean:
make -C $(KDIR) M=$(MSRC) clean
However I'm getting error:
% make V=1
make -C /lib/modules/`uname -r`/build M= modules
make[1]: Entering directory '/usr/src/kernels/4.18.0-348.20.1.el8_5.x86_64'
arch/x86/Makefile:249: *** You are building kernel with non-retpoline compiler, please update your compiler.. Stop.
make[1]: Leaving directory '/usr/src/kernels/4.18.0-348.20.1.el8_5.x86_64'
make: *** [Makefile:4: all] Error 2
%
Do I have to upgrade to a new compiler, if so which compiler version is needed? man gcc on my machine (RHEL8.5) says that -mindirect-branch= option is supported, and I believe this is the one enabling retpoline.
Should it be possible to disable retpoline support in the kernel? (however I would not like to rebuild the kernel if possible).
Thanks!
EDIT
I disabled retpoline as suggested by 0andriy via grub configuration file, (cat /proc/cmdline shows spectre_v2=off):
% grub2-editenv - set kernelopts=root=/dev/mapper/rhel_ps3cat5505k1-root ro crashkernel=auto resume=/dev/mapper/rhel_ps3cat5505k1-swap rd.lvm.lv=rhel_ps3cat5505k1/root rd.lvm.lv=rhel_ps3cat5505k1/swap rhgb quiet spectre_v2=off
% reboot
However the build still fails:
% make V=1
make -C /lib/modules/`uname -r`/build/ M=/home/mrv/tmp modules
make[1]: Entering directory '/usr/src/kernels/4.18.0-348.20.1.el8_5.x86_64'
test -e include/generated/autoconf.h -a -e include/config/auto.conf || ( \
echo >&2; \
echo >&2 " ERROR: Kernel configuration is invalid."; \
echo >&2 " include/generated/autoconf.h or include/config/auto.conf are missing.";\
echo >&2 " Run 'make oldconfig && make prepare' on kernel src to fix it."; \
echo >&2 ; \
/bin/false)
mkdir -p /home/mrv/tmp/.tmp_versions ; rm -f /home/mrv/tmp/.tmp_versions/*
make -f ./scripts/Makefile.build obj=/home/mrv/tmp
(cat /dev/null; echo kernel//home/mrv/tmp/test_mod.ko;) > /home/mrv/tmp/modules.order
gcc -Wp,-MD,/home/mrv/tmp/.test_mod.o.d -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/8/include -I./arch/x86/include -I./arch/x86/include/generated -I./include/drm-backport -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/kconfig.h -include ./include/linux/compiler_types.h -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -Werror-implicit-function-declaration -Wno-format-security -std=gnu89 -fno-PIE -DCC_HAVE_ASM_GOTO -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -funit-at-a-time -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_SSSE3=1 -DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -DCONFIG_AS_AVX512=1 -DCONFIG_AS_SHA1_NI=1 -DCONFIG_AS_SHA256_NI=1 -DCONFIG_TPAUSE=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -fno-jump-tables -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow -Wno-int-in-bool-context -O2 --param=allow-store-data-races=0 -Wframe-larger-than=2048 -fstack-protector-strong -Wno-unused-but-set-variable -Wno-unused-const-variable -g -gdwarf-4 -pg -mrecord-mcount -mfentry -DCC_USING_FENTRY -fno-inline-functions-called-once -Wdeclaration-after-statement -Wno-pointer-sign -Wno-stringop-truncation -fno-strict-overflow -fno-merge-all-constants -fmerge-constants -fno-stack-check -fconserve-stack -Werror=implicit-int -Werror=strict-prototypes -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -fmacro-prefix-map=./= -Wno-packed-not-aligned -DMODULE -DKBUILD_BASENAME='"test_mod"' -DKBUILD_MODNAME='"test_mod"' -c -o /home/mrv/tmp/.tmp_test_mod.o /home/mrv/tmp/test_mod.c
In file included from ./include/linux/module.h:18,
from /home/mrv/tmp/test_mod.c:1:
./include/linux/moduleparam.h:22:1: error: expected ‘,’ or ‘;’ before ‘static’
static const char __UNIQUE_ID(name)[] \
^~~~~~
./include/linux/module.h:158:32: note: in expansion of macro ‘__MODULE_INFO’
#define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
^~~~~~~~~~~~~
./include/linux/module.h:196:34: note: in expansion of macro ‘MODULE_INFO’
#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
^~~~~~~~~~~
/home/mrv/tmp/test_mod.c:19:1: note: in expansion of macro ‘MODULE_LICENSE’
MODULE_LICENSE("GPL")
^~~~~~~~~~~~~~
make[2]: *** [scripts/Makefile.build:322: /home/mrv/tmp/test_mod.o] Error 1
make[1]: *** [Makefile:1571: _module_/home/mrv/tmp] Error 2
make[1]: Leaving directory '/usr/src/kernels/4.18.0-348.20.1.el8_5.x86_64'
make: *** [Makefile:6: all] Error 2
%
So, running 'make oldconfig && make prepare' in kernel sources directory /usr/src/kernels/4.18.0-348.20.1.el8_5.x86_64 results in failure to locate tools/build/Build.include.
Not sure if RedHat allows to build kernel modules in RHEL8 at all.
EDIT2
Simple kernel module:
#include <linux/module.h>
#include <linux/kernel.h>
static __init int test_init(void)
{
printk("Init module\n");
return 0;
}
static __exit void test_exit(void)
{
printk("Cleanup module\n");
}
module_init(test_init)
module_exit(test_exit)
MODULE_DESCRIPTION("Test module");
MODULE_LICENSE("GPL");

Unable to insert kernel module: Unknown symbol net_namespace_list

I'm writing a kernel module that lists the network namespaces using the for_each_net and have written the following snippet which compiles fine:
rtnl_lock();
for_each_net(net)
printk("network ns: %p\n", net);
rtnl_unlock();
However when I try to insert the kernel module i get the following error:
insmod: ERROR: could not insert module kerNotification.ko: Unknown symbol in module
The dmesg gives the additional error information:
[3561461.418499] kerNotification: Unknown symbol net_namespace_list (err 0)
The kallsyms shows that it has the symbol of this kernel function:
[josh#dev kernel_prog(keystone_admin)]# cat /proc/kallsyms | grep net_namespace_list
ffffffff81a203e0 R __ksymtab_net_namespace_list
ffffffff81a32bc0 r __kcrctab_net_namespace_list
ffffffff81a55452 r __kstrtab_net_namespace_list
ffffffff81b6b000 D net_namespace_lis
The Module.symvers is included in the makefile as KBUILD_EXTRA_SYMBOLS. Here is the Makefile:
obj-m += kerNotification.o
SYMBOLA=/usr/src/kernels/4.4.57-1.el7.elrepo.x86_64/Module.symvers
KBUILD_EXTRA_SYMBOLS= $(SYMBOLA)
all:
make -C /lib/modules/$(shell uname -r)/build $(KBUILD_EXTRA_SYMBOLS) M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Here is the complete logs when the make file is built with V=1:
make -C /lib/modules/4.4.57-1.el7.elrepo.x86_64/build /usr/src/kernels/4.4.57-1.el7.elrepo.x86_64/Module.symvers M=/home/cmmdocker/kernel_prog modules
make[1]: Entering directory `/usr/src/kernels/4.4.57-1.el7.elrepo.x86_64'
test -e include/generated/autoconf.h -a -e include/config/auto.conf || ( \
echo >&2; \
echo >&2 " ERROR: Kernel configuration is invalid."; \
echo >&2 " include/generated/autoconf.h or include/config/auto.conf are missing.";\
echo >&2 " Run 'make oldconfig && make prepare' on kernel src to fix it."; \
echo >&2 ; \
/bin/false)
make[1]: Nothing to be done for `/usr/src/kernels/4.4.57-1.el7.elrepo.x86_64/Module.symvers'.
mkdir -p /home/cmmdocker/kernel_prog/.tmp_versions ; rm -f /home/cmmdocker/kernel_prog/.tmp_versions/*
make -f ./scripts/Makefile.build obj=/home/cmmdocker/kernel_prog
rm -f /home/cmmdocker/kernel_prog/built-in.o; ar rcsD /home/cmmdocker/kernel_prog/built-in.o
gcc -Wp,-MD,/home/cmmdocker/kernel_prog/.kerNotification.o.d -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.5/include -I./arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -Iinclude -I./arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I./include/uapi -Iinclude/generated/uapi -include ./include/linux/kconfig.h -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -std=gnu89 -fno-PIE -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mtune=generic -mno-red-zone -mcmodel=kernel -funit-at-a-time -maccumulate-outgoing-args -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_SSSE3=1 -DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -DCONFIG_AS_SHA1_NI=1 -DCONFIG_AS_SHA256_NI=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -fno-delete-null-pointer-checks -Wno-maybe-uninitialized -O2 --param=allow-store-data-races=0 -Wframe-larger-than=2048 -fstack-protector-strong -Wno-unused-but-set-variable -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-var-tracking-assignments -g -pg -mfentry -DCC_USING_FENTRY -fno-inline-functions-called-once -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fconserve-stack -Werror=implicit-int -Werror=strict-prototypes -DCC_HAVE_ASM_GOTO -DMODULE -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(kerNotification)" -D"KBUILD_MODNAME=KBUILD_STR(kerNotification)" -c -o /home/cmmdocker/kernel_prog/.tmp_kerNotification.o /home/cmmdocker/kernel_prog/kerNotification.c
/home/cmmdocker/kernel_prog/kerNotification.c: In function ‘kerNotification_init’:
/home/cmmdocker/kernel_prog/kerNotification.c:37:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
struct net *net;
^
if [ "-pg" = "-pg" ]; then if [ /home/cmmdocker/kernel_prog/kerNotification.o != "scripts/mod/empty.o" ]; then ./scripts/recordmcount "/home/cmmdocker/kernel_prog/kerNotification.o"; fi; fi;
(cat /dev/null; echo kernel//home/cmmdocker/kernel_prog/kerNotification.ko;) > /home/cmmdocker/kernel_prog/modules.order
make -f ./scripts/Makefile.modpost
find /home/cmmdocker/kernel_prog/.tmp_versions -name '*.mod' | xargs -r grep -h '\.ko$' | sort -u | sed 's/\.ko$/.o/' | scripts/mod/modpost -m -a -i ./Module.symvers -I /home/cmmdocker/kernel_prog/Module.symvers -e /usr/src/kernels/4.4.57-1.el7.elrepo.x86_64/Module.symvers -o /home/cmmdocker/kernel_prog/Module.symvers -w -s -T -
gcc -Wp,-MD,/home/cmmdocker/kernel_prog/.kerNotification.mod.o.d -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.5/include -I./arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -Iinclude -I./arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I./include/uapi -Iinclude/generated/uapi -include ./include/linux/kconfig.h -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -std=gnu89 -fno-PIE -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mtune=generic -mno-red-zone -mcmodel=kernel -funit-at-a-time -maccumulate-outgoing-args -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_SSSE3=1 -DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -DCONFIG_AS_SHA1_NI=1 -DCONFIG_AS_SHA256_NI=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -fno-delete-null-pointer-checks -Wno-maybe-uninitialized -O2 --param=allow-store-data-races=0 -Wframe-larger-than=2048 -fstack-protector-strong -Wno-unused-but-set-variable -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-var-tracking-assignments -g -pg -mfentry -DCC_USING_FENTRY -fno-inline-functions-called-once -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fconserve-stack -Werror=implicit-int -Werror=strict-prototypes -DCC_HAVE_ASM_GOTO -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(kerNotification.mod)" -D"KBUILD_MODNAME=KBUILD_STR(kerNotification)" -DMODULE -c -o /home/cmmdocker/kernel_prog/kerNotification.mod.o /home/cmmdocker/kernel_prog/kerNotification.mod.c
ld -r -m elf_x86_64 -T ./scripts/module-common.lds --build-id -o /home/cmmdocker/kernel_prog/kerNotification.ko /home/cmmdocker/kernel_prog/kerNotification.o /home/cmmdocker/kernel_prog/kerNotification.mod.o
make[1]: Leaving directory `/usr/src/kernels/4.4.57-1.el7.elrepo.x86_64'
Any idea how to fix this?
Issue was missing license. Below was added to get the expected result:
MODULE_LICENSE("GPL");
Referred the following answer from another query on stackoverflow for this: unknown-symbol-class-create-err-0
insmod: ERROR: could not insert module kerNotification.ko: Unknown
symbol in module
You get the above error because the net_namespace_list is missing in the kernel. So you need to make sure to insert the module which it is defined. looking at the source code its declared in net_namespace.c and looking at the Makefile it will when CONFIG_NET is enabled. Also when you have built your kernel image the System.map file should tell you if its declared in the kernel image which you have built, something like below:
cat System.map | grep net_namespace_list
8041a888 r __ksymtab_net_namespace_list
8042b80c r __kstrtab_net_namespace_list
80464c08 D net_namespace_list

Fortran 77 complains about common blocks

I'm using with gfortran 4.8.2 on FreeBSD 9.2 to create some executable files. There are three files, one C file and two Fortran 77 files where I'm using two routines with one common block.
The problem is that I receive an error of multiple definitions from gfortran compiler.
requests which I had sent to server:
autoreconf
./configure
make
di8810.c
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
void main(argc,argv)
int argc;
char *argv[];
{
if (argc != 4)
{
exit(99);
}
gds100(argv[1],argv[2],argv[3]);
}
gds100.f
SUBROUTINE GDS100(AUSGABE,FORMAT,FILENAME)
CHARACTER*4097 EBUF
CHARACTER*264 BUFFER
CHARACTER*1 CBUFFER(264)
CHARACTER*1 CEBUF(4097)
CHARACTER*1 FORMAT
INTEGER*2 INULL
CHARACTER*1 LTEXT(112)
COMMON /GDSCB2/ EBUF
EQUIVALENCE (EBUF,CEBUF(1))
EQUIVALENCE (CEBUF(4097),INULL)
DATA INULL /0/
...
END
gds102.f
SUBROUTINE GDS102
CHARACTER*264 BUFFER
CHARACTER*1 CBUFFER(264)
CHARACTER*4097 EBUF
CHARACTER*1 CEBUF(4097)
INTEGER*2 INULL
INTEGER POIADR
COMMON /GDSCB2/ EBUF
EQUIVALENCE (BUFFER,CBUFFER(1))
EQUIVALENCE (EBUF,CEBUF(1))
EQUIVALENCE (CEBUF(4097),INULL)
DATA IWOGRZ /4096/
DATA INULL /0/
ENTRY GDSUMS(N)
...
END
The error is:
make all-am
gcc -DHAVE_CONFIG_H -I. -DDI88xx -g -O2 -MT src/di8810-di8810.o -MD -MP -MF src/.deps/di8810-di8810.Tpo -c -o src/di8810-di8810.o `test -f 'src/di8810.c' || echo './'`src/di8810.c
mv -f src/.deps/di8810-di8810.Tpo src/.deps/di8810-di8810.Po
gfortran -cpp -fcheck=all -fno-underscoring -DDI88xx -g -O2 -c -o src/di8810-gds100.o `test -f 'src/gds100.f' || echo './'`src/gds100.f
gfortran -cpp -fcheck=all -fno-underscoring -DDI88xx -g -O2 -c -o src/di8810-gds102.o `test -f 'src/gds102.f' || echo './'`src/gds102.f
gfortran -cpp -fcheck=all -fno-underscoring -DDI88xx -g -O2 -o di8810 src/di8810-di8810.o src/di8810-gds100.o src/di8810-gds102.o
src/di8810-gds102.o: In function `gds102':
/.amd_mnt/blnn728x/home/sayik_bo/di8810_t/src/gds102.f:2: multiple definition of `gdscb2'
src/di8810-gds100.o:/.amd_mnt/blnn728x/home/sayik_bo/di8810_t/src/gds100.f:1: first defined here
collect2: Fehler: ld gab 1 als Ende-Status zurück
*** [di8810] Error code 1
Stop in /.amd_mnt/blnn728x/home/sayik_bo/di8810_t.
*** [all] Error code 1
Stop in /.amd_mnt/blnn728x/home/sayik_bo/di8810_t.
Stop in /.amd_mnt/blnn728x/home/sayik_bo/di8810_t.
*** [all] Error code 1
Stop in /.amd_mnt/blnn728x/home/sayik_bo/di8810_t.
It's driving me nuts. Any ideas?
elaborating on my comment
the common statement causes the compiler to allocate global storage for GDSCB2.
The symbols CEBUF,INULL are by Equivalence essentially pointers to the global storage.
Now the two data inull/0/ statements are redundantly initializing the same
location in global memory. I don't know if that a problem or not..just something to look at.
The other thing i see there is inull is 2 bytes (probably..or maybe more but certainly not 1)
yet it is equivalenced to the very last byte of the global character array. ie the initilization writes to data beyond the allocated space.
If at all feasible i would get rid of the common all together. Allocate the storage in the calling program and pass it as an argument to the subroutines.
in any case just do CEBUF(4097)=char(0) instead of using inull like that.

Build error when using cgo to wrap <dispatch/dispatch.h>

I'm playing around with XPC, GCD and go but I hit a quick wall when my code was failing to compile with the following error messages (which I don't understand):
main(__DATA/__const): unexpected reloc for dynamic symbol _NSConcreteGlobalBlock
main(__DATA/__const): unhandled relocation for _NSConcreteGlobalBlock (type 28 rtype 120)
I am compiling using go build the code that follows:
main.go
package main
/*
#include <xpc/xpc.h>
#include "wrapper.h"
*/
import "C"
import (
"fmt"
)
//export HandleXPCEvent
func HandleXPCEvent(event C.xpc_object_t) {
fmt.Println("Event was handled")
}
func main() {
name := C.CString("com.example.xpc")
queue := C.dispatch_queue_create(name, nil)
conn := C.xpc_connection_create(name, queue)
C.set_event_handler(conn)
//C.xpc_connection_resume(conn)
}
wrapper.h
#ifndef _WRAPPER_H_
#define _WRAPPER_H_
#include <stdlib.h>
#include <stdio.h>
#include <xpc/xpc.h>
xpc_connection_t connect( char* name);
void set_event_handler(xpc_connection_t connection);
#endif
wrapper.c
#include "wrapper.h"
#include <dispatch/dispatch.h>
extern void HandleXPCEvent(xpc_object_t);
xpc_connection_t connect( char* name) {
dispatch_queue_t queue = dispatch_queue_create(name,0);
return xpc_connection_create(name,queue);
}
void set_event_handler(xpc_connection_t connection) {
xpc_connection_set_event_handler(connection, ^(xpc_object_t event) {
xpc_retain(event);
// Call Go function
HandleXPCEvent(event);
});
}
I'm I doing something wrong? Is this some kind of go bug or how can this be fixed?
Update:
I ran go build -x -work on my project and I got the following output:
➣ go build -x -work
WORK=/var/folders/fb/bgfqk8wx5x16w7yh2cg50vrw0000gn/T/go-build524335717
mkdir -p $WORK/github.com/gabrielayuso/go-xpc/_obj/
mkdir -p $WORK/github.com/gabrielayuso/go-xpc/_obj/exe/
cd /Users/gabrielayuso/Documents/Workspace/Projects/go/src/github.com/gabrielayuso/go-xpc
/usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/github.com/gabrielayuso/go-xpc/_obj/ -- -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ main.go
/usr/local/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ -I /usr/local/go/pkg/darwin_amd64 -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_defun.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_defun.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -print-libgcc-file-name
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_main.o -c $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_main.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_export.o -c $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_export.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ -o $WORK/github.com/gabrielayuso/go-xpc/_obj/main.cgo2.o -c $WORK/github.com/gabrielayuso/go-xpc/_obj/main.cgo2.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ -o $WORK/github.com/gabrielayuso/go-xpc/_obj/wrapper.o -c ./wrapper.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_.o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_main.o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_export.o $WORK/github.com/gabrielayuso/go-xpc/_obj/main.cgo2.o $WORK/github.com/gabrielayuso/go-xpc/_obj/wrapper.o
/usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/github.com/gabrielayuso/go-xpc/_obj/ -dynimport $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_.o -dynout $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_import.c
/usr/local/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ -I /usr/local/go/pkg/darwin_amd64 -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_import.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_import.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_all.o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_export.o $WORK/github.com/gabrielayuso/go-xpc/_obj/main.cgo2.o $WORK/github.com/gabrielayuso/go-xpc/_obj/wrapper.o -Wl,-r -nostdlib /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/x86_64/libgcc.a
/usr/local/go/pkg/tool/darwin_amd64/6g -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_go_.6 -p github.com/gabrielayuso/go-xpc -D _/Users/gabrielayuso/Documents/Workspace/Projects/go/src/github.com/gabrielayuso/go-xpc -I $WORK $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_gotypes.go $WORK/github.com/gabrielayuso/go-xpc/_obj/main.cgo1.go
/usr/local/go/pkg/tool/darwin_amd64/pack grcP $WORK $WORK/github.com/gabrielayuso/go-xpc.a $WORK/github.com/gabrielayuso/go-xpc/_obj/_go_.6 $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_import.6 $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_defun.6 $WORK/github.com/gabrielayuso/go-xpc/_obj/_all.o
cd .
/usr/local/go/pkg/tool/darwin_amd64/6l -o $WORK/github.com/gabrielayuso/go-xpc/_obj/exe/a.out -L $WORK $WORK/github.com/gabrielayuso/go-xpc.a
# github.com/gabrielayuso/go-xpc
main(__DATA/__const): unexpected reloc for dynamic symbol _NSConcreteGlobalBlock
main(__DATA/__const): unhandled relocation for _NSConcreteGlobalBlock (type 28 rtype 120)
Content of $WORK dir as generated by go build -x -work: go-xpc_work.zip
I'm not very familiar with compiling and linking therefore I can't make much sense of this output. I just noticed that _cgo_import.c a file generated by cgo (with options -dynimport and -dynout) has #pragma cgo_import_dynamic _NSConcreteGlobalBlock _NSConcreteGlobalBlock "" in the first line which is related to the error message the linker gave.
Hope this information can help to find out what the problem is and how to solve it.
I don't know much about those libraries but nothing jumps out at me with the code you have here.
Some useful debug output can be obtained from go build -x -work which will print the commands and the working directory for you.
The working directory will be left untouched so you can go look at the code cgo generates for you. That plus the commands it will print for you should get you started on tracking down the problem.
I have a wrapper written up for using Go and XPC in Cocoa Applications.
It's located here:
https://github.com/aventurella/go-xpc

inline functions have "first defined here" and "multiple defenition"

Like two of my previous questions (inline-asm-with-gcc & arm7tdmi-does-not-support-requested-special-purpose-register, I have some build problem when converting code compiled with ARMASM to gcc(code sourcery GCC-4.6.2 eabi).
This time is at linking process: I get a lot of "first defined here" and "multiple definition" to inline functions. Example :
inline U16 ByteSwap16(U16 uData) {
return ( (uData >> 8) | (uData << 8) );
}
I get " multiple definition of `ByteSwap16' " and "first defined here" on the first line.
Here's the linking parameter that I use for the file with the error :
arm-none-eabi-ld -T".\linker.ld" -Map=BootLoad.map -o BootLoad.elf InitMain.o tsk_main.o ecp.o memalloc.o tsk_ecp.o firmdesc.o crc.o flash.o eth.o firmflash.o firmdest.o bcfg.o bootdownload.o cinit.o serial.o cpu.o mmu.o ngucos.o cdbini.o cs712sio.o cs712eth.o nginit.o MmuSdram0.o ../../OS/ngos/lib/rtstub/arm/gcc/libngosd4m32l.a ../../OS/ngip/lib/rtstub/arm/gcc/libngipd4m32l.a
In case the error comes in compilation process :
arm-none-eabi-gcc -c -H -Wall -Wa,-adhlns="tsk_main.o.lst" -fmessage-length=0 -MMD -MP -MF"tsk_main.d" -MT"tsk_main.d" -fpic -o"tsk_main.o" -march=armv4t -mcpu=arm7tdmi -mlittle-endian -g3 -gdwarf-2 -O0 -I"../../OS/ngos/hw/cdb89712" -I"../../OS/ngos" -I"../../OS/ngos/include" -I"../../OS/ngos/rtos/ucosii" -I"./" -I"src/" -I"../../Common/inc" -I"../../OS/uCOS-II/SOURCE" -I"../../OS/ngos/drivers/arm" -I"../../OS/ngos/include/ngos" -I"../../OS/ngip/include" -I"../../OS/ngip/include/ngip" -I"../../Dvcscomponent/Inc" -I"../../Inc" "src/tsk_main.c"
Any idea why the inline function generate theses errors?
Thanks in advance!

Resources