LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   How to link some .o files in kernel? (https://www.linuxquestions.org/questions/linux-kernel-70/how-to-link-some-o-files-in-kernel-4175736091/)

speranza 04-16-2024 10:25 AM

How to link some .o files in kernel?
 
Hi friends,I have a question.It has been bothering me for a while.Here is details:
I'm adding some ebpf helpers into the kernel.My kernel version is 5.15.153
I modify /kernel/bpf/helpers.c like that:

#include "../../security/dir/header.h"

BPF_CALL1(func1,argType,argName){
func2(argName);
return 0;
}


The function "func2" is in another .c file.That file can't be compiled with kernel because it uses some standard lib such as stdio,stdlib.So I compile it in gcc and get the .o file.I think maybe that .o file can be linked with "helpers.o".But I don't get any idea after I check the Makefile of the kernel and not find any information for this question.

Actually I find two similar question in stackoverflow and here:
https://stackoverflow.com/questions/...-kernel-binary
https://www.linuxquestions.org/quest...kbuild-694396/
But I don't think these can resolve my question.

I want to ask that is it possible to link a .o file with kernel?I think that I need to modify the Makefile in somewhere.

teckk 04-16-2024 02:37 PM

https://docs.kernel.org/kbuild/makefiles.html
https://docs.kernel.org/5.10/kbuild/modules.html
https://www.kernel.org/doc/html/v5.1...-syscalls.html

murugesandins 04-16-2024 03:01 PM

I do not have your OS here.
I don't have your code here.
Hence I tried simulating your sample code at bash.exe/gcc.exe using CYGWIN_NT at windows.
cat CommonMakefile
Code:

all:
        @echo inside $@;\
        echo call /usr/bin/make ACTION=kernel CompileAtAnyOS;\
        /usr/bin/make ACTION=kernel CompileAtAnyOS
CompileAtAnyOS:
        @echo inside $@;\
        OS=$$(uname -s | sed "s/-[0-9]*\.[0-9]*-[0-9]*//;");\
        echo call /usr/bin/make ACTION=$$ACTION $$OS;\
        /usr/bin/make ACTION=$$ACTION $$OS
CYGWIN_NT:
        @echo inside $@;\
        echo calling /usr/bin/make $(ACTION);\
        CFLAGS=-O3 \
        CC=/usr/bin/g++ \
        cc=/usr/bin/gcc \
        INCLUDE_DIR=../../security/dir \
        /usr/bin/make $(ACTION)
kernel:$(BUILD)
        @echo inside $@

cat Makefile
Code:

BUILD        =        ERP
include CommonMakefile
ERP:
        @if [[ ! -f helpers.o ]];\
        then\
                echo $(cc) -c $(CFLAGS) helpers.c;\
                $(cc) -c $(CFLAGS) helpers.c;\
        else\
                /usr/bin/ls -tr helpers.c \
                helpers.o 2>&1 |\
                /usr/bin/tail -1 |\
                /usr/bin/grep helpers.o >/dev/null 2>&1;\
                Ret=$$?;\
                if [[ 0 -eq $$Ret ]];\
                then\
                        echo helpers.o is up to date;\
                else\
                        echo $(cc) -c $(CFLAGS) helpers.c -I$(INCLUDE_DIR);\
                        $(cc) -c $(CFLAGS) helpers.c -I$(INCLUDE_DIR);\
                fi;\
        fi;\
        if [ ! -f ./a.out ];\
        then\
                echo $(cc)  $(CFLAGS) mymain.c helpers.o  -I$(INCLUDE_DIR) -o ./a.out;\
                $(cc)  $(CFLAGS) mymain.c helpers.o  -I$(INCLUDE_DIR) -o ./a.out;\
        else\
                /usr/bin/ls -tr ./a.out helpers.o mymain.c 2>&1 |\
                /usr/bin/grep a.out >/dev/null 2>&1;\
                Ret=$$?;\
                if [[ 0 -eq $$Ret ]];\
                then\
                        echo "./a.out is up to date";\
                else\
                        echo $(cc)  $(CFLAGS) mymain.c helpers.o  -I$(INCLUDE_DIR) -o ./a.out;\
                        $(cc)  $(CFLAGS) mymain.c helpers.o  -I$(INCLUDE_DIR) -o ./a.out;\
                fi;\
        fi

cat helpers.c
Code:

#include "header.h"
// I am defining macro since I am not sure about actual definition of func1/argType/argName at your code.
#define func1 int arg1
#define argType int arg2
#define argName int arg3
#define BPF_CALL1 void bpf_call1
#define func2(argName) extern void func2(argName)
// I am defining func2 based on same reason
void fun2(int parameter )
{
        return;
}
BPF_CALL1(func1,argType,argName)
{
        func2(argName);
        return;
}

cat mymain.c
Code:

#include <stdio.h>
#include <header.h>
void func2(int arg)
{
        printf( "%d\n", arg);
        return;
}
int main()
{
        func2(12);
}

I created empty header file ..\..\security\dir\header.h
Code:

$ file ../../security/dir/header.h
../../security/dir/header.h: empty

Sample compilation using bash.exe/make.exe at cygwin_nt
Code:

$ /usr/bin/make.exe
inside all
call /usr/bin/make ACTION=kernel CompileAtAnyOS
make[1]: Entering directory '/home/murugesan_openssl'
inside CompileAtAnyOS
call /usr/bin/make ACTION=kernel CYGWIN_NT
make[2]: Entering directory '/home/murugesan_openssl'
inside CYGWIN_NT
calling /usr/bin/make kernel
make[3]: Entering directory '/home/murugesan_openssl'
/usr/bin/gcc -c -O3 helpers.c -I../../security/dir
/usr/bin/gcc -O3 mymain.c helpers.o -I../../security/dir -o ./a.out
inside kernel
make[3]: Leaving directory '/home/murugesan_openssl'
make[2]: Leaving directory '/home/murugesan_openssl'
make[1]: Leaving directory '/home/murugesan_openssl'
$ make
inside all
call /usr/bin/make ACTION=kernel CompileAtAnyOS
make[1]: Entering directory '/home/murugesan_openssl'
inside CompileAtAnyOS
call /usr/bin/make ACTION=kernel CYGWIN_NT
make[2]: Entering directory '/home/murugesan_openssl'
inside CYGWIN_NT
calling /usr/bin/make kernel
make[3]: Entering directory '/home/murugesan_openssl'
helpers.o is up to date
./a.out is up to date
inside kernel
make[3]: Leaving directory '/home/murugesan_openssl'
make[2]: Leaving directory '/home/murugesan_openssl'
make[1]: Leaving directory '/home/murugesan_openssl'
$ /usr/bin/touch.exe helpers.c
$ /usr/bin/make
inside all
call /usr/bin/make ACTION=kernel CompileAtAnyOS
make[1]: Entering directory '/home/murugesan_openssl'
inside CompileAtAnyOS
call /usr/bin/make ACTION=kernel CYGWIN_NT
make[2]: Entering directory '/home/murugesan_openssl'
inside CYGWIN_NT
calling /usr/bin/make kernel
make[3]: Entering directory '/home/murugesan_openssl'
/usr/bin/gcc -c -O3 helpers.c -I../../security/dir
/usr/bin/gcc -O3 mymain.c helpers.o -I../../security/dir -o ./a.out
inside kernel
make[3]: Leaving directory '/home/murugesan_openssl'
make[2]: Leaving directory '/home/murugesan_openssl'
make[1]: Leaving directory '/home/murugesan_openssl'
$ make
inside all
call /usr/bin/make ACTION=kernel CompileAtAnyOS
make[1]: Entering directory '/home/murugesan_openssl'
inside CompileAtAnyOS
call /usr/bin/make ACTION=kernel CYGWIN_NT
make[2]: Entering directory '/home/murugesan_openssl'
inside CYGWIN_NT
calling /usr/bin/make kernel
make[3]: Entering directory '/home/murugesan_openssl'
helpers.o is up to date
./a.out is up to date
inside kernel
make[3]: Leaving directory '/home/murugesan_openssl'
make[2]: Leaving directory '/home/murugesan_openssl'
make[1]: Leaving directory '/home/murugesan_openssl'


murugesandins 04-16-2024 03:06 PM

I am feeling much happy after writing above comment :)
enjoy maadi => enjoy => at kannadaa
nanukku kannada solpha solpha barathu => I knew only few words at kannada(Karnataka Kannada language) even though I am staying there from 2004 to till now.


All times are GMT -5. The time now is 11:31 PM.