#2020征文-发展板# Those things between SYS_RUN() and MODULE_INIT()

I have been in contact with Hongmeng equipment development for a while, and it is time to dig into the startup process of the Hongmeng equipment program.

 

Icebreaking question: Where did the Hongmeng equipment program start?

 

I believe everyone is already very clear that the Hongmeng device program needs to specify the entry function, which is specified by the statement SYS_RUN(app_entry); at the code level , where app_entry is the name of the device program entry function; and the startup process of the entire Hongmeng device can also be logical. Dig it out. As shown below:

This looks perfect and solves all problems! However, I think there is still something unclear, namely: What did MODULE_INIT(run) do? Why is the entry function app_entry() called eventually?

 

Next, we solve the problems one by one!

 

Essential question: What did MODULE_INIT(run) do?

 

To clarify this problem, we must first talk about what SYS_RUN() is? ! Some students may think that SYS_RUN(app_entry); is a function call statement, register the device program entry address in the system, and then call it. It's right to understand it in principle, but it's not the same in details! SYS_RUN() is very similar to a function in usage, but is essentially a macro! It must be emphasized: In C language, function calls cannot be made outside of functions, and SYS_RUN(app_entry); does not appear in any function, so it cannot be a function call. What could it be? There is only one truth, only one definition (declaration) statement. In order to prove this conclusion, we stripped the SYS_RUN() macro completely and looked at it thoroughly. as follows:

Analysis:

In the end, we can know: SYS_RUN(app_entry); defines a function pointer named __zinitcall_run_app_entry, its type is InitCall, no matter whether it is used or not, it will not compile and report an error, and it will be forced to compile to be finally stored in the name .zinitcall. In the section of run2.init.

 

it is good! Then you can directly analyze MODULE_INIT(run).

After MODULE_INIT(run) is unfolded, there is nothing to do with app_entry at all! We used a lot of power to break the macro apart, but the result seemed to be nothing! ! ! The relationship between MODULE_INIT() and SYS_RUN() is still very unclear, what should we do next?

 

Carefully observe the symbols spliced ​​by these two macros!

It can be found that they are all related to zinitcall, and we also know that the global pointer defined by SYS_RUN(app_entry) is placed in  the section named  .zinitcall.run2.init , so we can speculate: the relationship between these two macros is through the link script Associated.

 

Next, use the tool to view the segment information and symbol information of the target file.

It can be known from the output that the symbol __zinitcall_run_app_entry does exist in the section named .zinitcall.run2.init.

After that, go through the source code. . . .

After hard work, we can find the \code-1.0\vendor\hisi\hi3861\hi3861\build\build_tmp\scripts\link.lds file, and find the following script code:


The follow-up content and attachments of the article can click on the original link below to learn

Original link:https://harmonyos.51cto.com/posts/2017#bkwz


To learn more, please visit:

Hongmeng technology community built by 51CTO and Huawei official strategic cooperation

https://harmonyos.51cto.com/#bkwz


Guess you like

Origin blog.51cto.com/14901125/2562298