LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 04-16-2024, 10:25 AM   #1
speranza
LQ Newbie
 
Registered: Apr 2024
Posts: 1

Rep: Reputation: 0
Question 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.

Last edited by speranza; 04-16-2024 at 11:46 AM.
 
Old 04-16-2024, 03:01 PM   #3
murugesandins
Member
 
Registered: Apr 2024
Location: Bangalore Karnataka India
Distribution: CYGWIN_NT
Posts: 46

Rep: Reputation: 0
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'
 
Old 04-16-2024, 03:06 PM   #4
murugesandins
Member
 
Registered: Apr 2024
Location: Bangalore Karnataka India
Distribution: CYGWIN_NT
Posts: 46

Rep: Reputation: 0
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Physical link vs Relative link vs Symbolic link hack3rcon Linux - Newbie 13 01-13-2018 10:48 AM
Restored files symbolically link to backup files, & can't boot using linked files mazinoz Linux Mint 1 01-31-2015 05:10 AM
in copy files or ls files the command want to invert select some files how to?? hocheetiong Linux - Newbie 3 06-27-2008 06:32 AM
How to use samba implementing some files can visible and some files can not visible willie118 Linux - Server 7 09-18-2007 12:27 AM
skge: eth0: Link is up... Link is down... Link is up.... sugar2 Linux - Hardware 5 07-03-2007 03:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 03:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration