Can anybody explain about following structure - c

What is the use of the below structure in linux kernel? I need to know it because I need to add a variable inside this structure.
static const struct modversion_info ____versions[]

It includes symbols referenced by the module and their checksums, when the module is inserted those symbols are checked to make sure the module is compiled for the running kernel.
On Ubnutu the kernel-headers package contains this file which has all the symbols exported by the kernel and their checksums, you should be able to find this file if you compiled your own kernel too
usr/src/linux-headers-2.6.38-generic/Module.symvers
Note, I'm not sure what you're trying to do but you shouldn't be adding symbols to your module, when you compile a module this structure is generated for you.

Related

How to solve "Undefined reference to function" in Eclipse CDT?

I did setup a C project with Eclipse Photon (4.8.0) for developing a program for the ESP-32. I did configure the IDE according to this official setup instructions.
Flashing the ESP-32 works fine. But as soon as I try to include header files from a sub folder, I run into troubles. I have set up a very simple project to illustrate the issue. The project consists of main.c, base/test.h and base/test.c, whereas the test.h and test.c files only contain one function with the signature void function1(void);.
When I try to call function1() in main.c, I get this error in main.c:
Undefined reference to function1()
Please compare to the attached screenshot, where everything is depicted.
How to solve this issue?
This is not a compiler, but rather a linker error.
Note, with #includeing a header file, you only make the external function known to the compiler. You also need to link to the external function during the linking stage. Make sure you include the compiled object file that contains function1 into the link.
Seems like you need to do proper linking.
If you are linking with a library, you need to specify:
The name of the library: Project\Settings\C C++ General\Paths and Symbols\Libraries
Location where the linker should search for this library:
Project\Settings\C C++ General\Paths and Symbols\Library Paths
Important: see Note.
If you are linking with object files, add those to:
Project\Settings\C C++ Build\Settings\Linker\Miscellaneous\Other objects
Note:
If your library name is, for example, libsomething.a, than you need to specify only something as the name; so omit lib prefix and .a suffix.
If your library is not prefixed with lib, then you need to add its name prefixed with :. For example, something.a should be added as :something.a.

Importing Modules — customized Modules in C

I am currently learning the C programming language and I'm having some issues with importing modules I created.
I created a small module to read with fgets and flush the buffer from stdin perfectly and I don't want to keep writing the code every single time. I just want to import this small module like I used to do in Python. I didn't knew how because I'm not using an IDE. I'm just compiling with gcc in terminal and using a text editor. I tried to search with Google,but in vain.
You should create a header for your module that declares the functions in the module – and any other information that a consumer of the module needs. You might call that header weekly.h, a pun on your name, but you can choose any name you like within reason.
You should create a library (shared or static — that's up to you) that contains the functions (and any global variables, if you're so gauche as to have any) defined by your module. You might call it libweekly.so or libweekly.a — or using the extensions appropriate to your machine (.dylib and .a on macOS, for example). The source files might or might not be weekly.c — if there's more than one function, you'll probably have multiple source files, so they won't all be weekly.c. You should put this code (the header and the source files and their makefile) into a separate source directory.
You should install the header(s) and the library in a well-known location (e.g. $HOME/include for headers and $HOME/lib for the library — or maybe in the corresponding directories under /usr/local), and then ensure that the right options are used when compiling (-I$HOME/include for the headers) or linking (-L$HOME/lib and -lweekly).
Your source code using the module would contain:
#include "weekly.h"
and your code would be available. With shared libraries in $HOME/lib, you would have to ensure that the runtime system knows where to find the library. If you install it in /usr/local, that is done for you already. If you install it in $HOME/lib, you have to investigate things like /etc/ld.so.conf or the LD_LIBRARY_PATH or DYLIB_LIBRARY_PATH environment variables, etc.
You need to create a header file (.h) with your function declarations types and extern variables. Then in the program where you want to use those functions include this .h file and and add the compiled .o file (with your functions) to the object file list. And you are done.

linking object files to a non-executable image using IAR toolchain

I'm using IAR toolchain to compile few source files and then link generated .o files.
However, I'm running into linking errors like below:
Error[Li005]: no definition for "main" [referenced from cmain.o(rt7M_tl.a)]
Error[Lc036]: no block or place matches the
pattern "ro code section .intvec in vector_table_M.o(rt7M_tl.a)"
As I understand, ILINK linker is trying to link object files as an executable image and in the process adding dependencies from standard libraries[ i.e looking for main() and interrupt vector table ].
What I'm looking for :
How to configure linker to not to add these system-library dependencies like main/start/interrupt-vector-table etc. ?
How to configure linker to output a non-executable image from bunch of object files - if that at all is possible ?
You can think of this non-executable image sort of configuration-table image which will be put in persistent memory to be read/write by main application image.
If you tell the linker that you don't have an entry point with the command line option '--no_entry' you will get rid of the reference to main and the .intvec data.
However you do need to tell the linker what it should keep.
--keep and/or __root can help you with that.

Linux kernel gives undefined reference for vmlinux but compiles the .o files

I have 2 driver files.
dr1.c
dr2.c
dr1.c does an EXPORT_SYMBOL(func1).
dr2.c uses that via a definition in dr1.h
Both the dr1 and dr2 are compiled and object files are created.
However, in th last stage of kernel compilation, I get an error
undefined reference for func1 in dr2 but the dr2 object file is created.
I do not understand why the linking is not happening.
Basically the linking doesn't happen because the address of func1() which is defined in dr1.c is unknown to dr2.c.
There are two solutions to this problem :
1. Make the address known manually to the second file :
In this case let us assume that the modules dr1.c and dr2.c are present in different directories and has got its own Makefile. Firstly, compile the dr1 module. After its done, copy the Modules.symvers file to the directory containing dr2.c and then build the dr2 module. The Modules.symvers basically contains the address of the "exported symbols", which, by copying it to the destination of the other module, you are making it "known" and thus finally the linking happens.
2. Use a common Makefile for both the modules :
Put both the dr1.c and dr2.c in the same directory and create a common kernel Makefile for it. Make the following entry in the Makefile :
obj-m := dr1.o dr2.o
When this is done, both the files will be compiled, produces a common Modules.symvers file and linked accordingly.
Hope this helped.
dr1 was being compiled as a module
dr2 was being compiled into the kernel.
As a result the linking could not happen since dr1 is not part of vmlinux even though dr1 got compiled into a module without any error. I made dr2 into a module as well.
Also, I had to make sure that the kernel modules option is enabled in the config.

Why do I need to manually link the C runtime library when creating an EXE out of static libraries without any object files?

I'm pretty new to working with libraries and I'm in the process of trying to understand some specifics regarding static libraries and object files.
Summary
The behavior I'm noticing is that I can link several objects to make an executable with no problem, but if I take an intermediate step of combining those objects into static libraries, I cannot link those static libraries to make an executable without additionally specifying the needed C Run-time library in the link command.
Also, or the record, I'm doing the compiling/linking with Visual Studio 2010 from the command line. More details of the process I'm following are below.
First, let's say I have four source files in a project: main.c, util1.c, util2.c, and util3.c.
What works
I can compile these sources with the following command:cl -c main.c util1.c util2.c util3.cAs a result, I now have four object files: main.obj, util1.obj, util2.obj, and util3.obj. These object files each contain a DEFAULTLIB statement intended to inform the linker that it should additionally check the static C Run-time library libcmt.lib for any unresolved external dependencies in these object files when linking them.
I can create an executable named "app_objs.exe" by linking these objects with the following command:
link -out:app_objs.exe main.obj util1.obj util2.obj util3.obj
As mentioned in step 1, the linker used the runtime library due to the compiler's step of adding a default library statement to the objects.
Where I'm confused
Let's say I want to have an intermediate step of combining these objects into static libraries, and then linking those resulting LIB files to create my executable. First, I can create these libraries with the following commands:
link -lib -out:main.lib main.obj
link -lib -out:util.lib util1.obj util2.obj util3.obj
Now, my original thought was that I could simply link these libraries and have the same executable that I created in step 2 of "What works". I tried the following command and received linker error LNK1561, which states that an entry point needs to be specified:
link -out:app_libs.exe main.lib util.lib
From Microsoft's documentation, it is evident that linking libraries without any object files may require entry points to be specified, so I modified the command to set the subsystem as "console" to specify that the executable in intended to be a console application (which seems to imply certain entry points, thereby resolving that error):link -out:app_libs.exe -subsystem:console main.lib util.libUnfortunately, now I get a linker error stating that mainCRTStartup is an unresolved external symbol. I understand that this is defined in the C runtime library, so I can resolve this issue by manually specifying that I want to link against libcmt.lib, and this gives me a functioning executable:link -out:app_libs.exe -subsystem:console main.lib util.lib libcmt.lib
What I'm not understanding is why the default library info that the compiler placed in each object file couldn't be used to resolve the dependency on libcmt.lib. If I can link object files without explicitly stating I want libcmt.lib, and I created static libraries that are containers for the object files, why can't I link those static libraries without having to explicitly state that I want libcmt.lib? Is this just the way things are, or is there some way I could create the static libraries so that the linker will know to check for unresolved symbols in the runtime library?
Thanks for your help. If I have some fundamentally incorrect ideas here, I'd love suggestions on good references to learn all of this correctly.
Well the answer to your misunderstanding is that .lib files are often a product in themselves, and the compiler can't make those assumptions safely. That's what "external" is for.
If I produce binaries for someone's platform because its users are totally helpless, and they want/need static linkage, I have to give them foo.h and libfoo.lib without tying them to a specific runtime entry point. They may very well have defined their own entry point already for their final product, whether DLL or EXE.
You either want the runtime, or you want your own .obj that contains your entry point. Be warned that declaring and defining mainCRTStartup on your own may mean you're not executing important instructions for the target platform.

Resources