Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lesson 01, How CPU find kernel_main in kernel.c from boot.S #225

Open
xubenji opened this issue Jan 23, 2021 · 8 comments
Open

lesson 01, How CPU find kernel_main in kernel.c from boot.S #225

xubenji opened this issue Jan 23, 2021 · 8 comments

Comments

@xubenji
Copy link

xubenji commented Jan 23, 2021

Hi, developers. I am a beginner in developing operating systems. Before, I have learned how to develop OS in X86. Each time, in assembly files, if I want the CPU to call a certain function in .c files from assembly files, I have to include the keyword 'extern'. If I want the CPU to call a function in assembly files from C files, I have to include 'global'. But, in ARM assembly, I can't find any keyword to indicate the C function in .s files. So I really want to find the answer. Thank you.

@rockytriton
Copy link

Notice in this file, https://github.com/s-matyukevich/raspberry-pi-os/blob/master/src/lesson01/src/mm.S how the function memzero is defined, that can be called from C.

@Andre-Fonteles
Copy link

Andre-Fonteles commented Feb 3, 2021

I think xubenji question might actually be about calling C functions from assembly files (not the other way around). Most of the time one would need to include the C header in the assembly file in order to call a function.

That said, I don't know how the boot.S file (https://github.com/s-matyukevich/raspberry-pi-os/blob/master/src/lesson01/src/boot.S) is able to call kernel_main without including any header that contains a reference to it.

@rockytriton
Copy link

Ok yeah if that's the case, there isn't really any need to pre-define it for the assembly code to use it. It just references a symbol by the given name and then the linker will try to find it. For instance, if you rename the function kernel_main2 and then try to rebuild, it will fail during the linking process as the symbol won't be found.

@xubenji
Copy link
Author

xubenji commented Feb 3, 2021

Ok yeah if that's the case, there isn't really any need to pre-define it for the assembly code to use it. It just references a symbol by the given name and then the linker will try to find it. For instance, if you rename the function kernel_main2 and then try to rebuild, it will fail during the linking process as the symbol won't be found.

If I change the kernel_mian() to kernel_main2(). What I have to do to let the linker find the new function name?

@xubenji
Copy link
Author

xubenji commented Feb 3, 2021

I think xubenji question might actually be about calling C functions from assembly files (not the other way around). Most of the time one would need to include the C header in the assembly file in order to call a function.

That said, I don't know how the boot.S file (https://github.com/s-matyukevich/raspberry-pi-os/blob/master/src/lesson01/src/boot.S) is able to call kernel_main without including any header that contains a reference to it.

Yes, It is what I thought

@rockytriton
Copy link

Ok yeah if that's the case, there isn't really any need to pre-define it for the assembly code to use it. It just references a symbol by the given name and then the linker will try to find it. For instance, if you rename the function kernel_main2 and then try to rebuild, it will fail during the linking process as the symbol won't be found.

If I change the kernel_mian() to kernel_main2(). What I have to do to let the linker find the new function name?

The linker will know the name change because it sees the function name in the symbols list. So as long as you change it in your .c file and your .S file then you will be good.

@xubenji
Copy link
Author

xubenji commented Feb 3, 2021

Ok yeah if that's the case, there isn't really any need to pre-define it for the assembly code to use it. It just references a symbol by the given name and then the linker will try to find it. For instance, if you rename the function kernel_main2 and then try to rebuild, it will fail during the linking process as the symbol won't be found.

If I change the kernel_mian() to kernel_main2(). What I have to do to let the linker find the new function name?

The linker will know the name change because it sees the function name in the symbols list. So as long as you change it in your .c file and your .S file then you will be good.

It seems like I don't have to do too many operations. But, If the CPU wants to call some functions in .S files from .c files, we need to add the necessary header files, right? In this files, https://github.com/s-matyukevich/raspberry-pi-os/blob/master/src/lesson01/src/mini_uart.c. I find it includes the function name which locals in utils.S

@Kristine1975
Copy link

If the CPU wants to call some functions in .S files from .c files, we need to add the necessary header files, right?

Yes. In C, the compiler needs to know how to call the functions. In assembler, you have to make sure yourself that you are calling the functions correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants