Skip to content

Doc how to add new symbols to mp_fun_table for native modules #5463

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

Closed
wants to merge 4 commits into from

Conversation

tve
Copy link
Contributor

@tve tve commented Dec 27, 2019

See also #5458

Linker limitation: the native module is not linked against the symbol table of the
full micropython firmware. Rather, it is linked against an explicit table of exported
symbols found in ``mp_fun_table``. It is thus not possible to simply call some arbitrary function
in FreeRTOS, for example. Adding new symbols to the end of the table is relatively easy,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rephrase this to be a bit more general, eg something like "... some arbitrary HAL/OS/RTOS/system function, for exampe".

@@ -67,6 +67,14 @@ The known limitations are:
So, if your C code has writable data, make sure the data is defined globally,
without an initialiser, and only written to within functions.

Linker limitation: the native module is not linked against the symbol table of the
full micropython firmware. Rather, it is linked against an explicit table of exported
symbols found in ``mp_fun_table``. It is thus not possible to simply call some arbitrary function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a good idea to state the file this struct is found in.

in FreeRTOS, for example. Adding new symbols to the end of the table is relatively easy,
they then also need to be added to ``tools/mpy_ld.py``'s ``fun_table`` dict in the same location.
This allows ``mpy_ld.py`` to be able to pick the new symbols up and provide relocations
for them when the mpy is imported.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be a good idea to actually try this out first, to see if it does work (or not).

@cwalther
Copy link
Contributor

cwalther commented Feb 1, 2020

This may be a dumb question, as I have never attempted any of this and am not too well versed in the details of how linkers and loaders work, but one thing is unclear to me just from the added doc snippet: Is the modification of this table something that needs to be done when building the firmware that is going to load the module, or when building the module? In other words, can a module call any function that is present in the firmware, or only the ones that were explicitly exported at the time the firmware was built?

@dpgeorge dpgeorge added the docs label Feb 2, 2020
@dpgeorge
Copy link
Member

dpgeorge commented Feb 2, 2020

Is the modification of this table something that needs to be done when building the firmware that is going to load the module, or when building the module?

The former: new MicroPython firmware that loads modules needs to be rebuilt, so it includes the new functions in the mp_fun_table table.

In other words, can a module call any function that is present in the firmware, or only the ones that were explicitly exported at the time the firmware was built?

In short, a native module can only call functions explicitly exported / included in mp_fun_table, at the time the MicroPython firmware was built.

Technically it would be possible to call any MicroPython firmware function if you knew its address in ROM, but that would change across builds and ports, making it quite impractical (although still possible if you really wanted to minimise code size of the firmware, and never recompiled the main firmware).

@cwalther
Copy link
Contributor

cwalther commented Feb 2, 2020

Thanks for the clarification, that makes sense. @tve, do you think you could add a note to that effect to your proposed paragraph? Something along the lines of

- symbols found in ``mp_fun_table`` (in ``py/nativeglue.h``).
+ symbols found in ``mp_fun_table`` (in ``py/nativeglue.h``), fixed at firmware build time.

- Adding new symbols to the end of the table is relatively easy,
+ New symbols can be added to the end of the table and the firmware rebuilt,

I guess this also means that if mp_obj_get_array is considered a necessary or useful part of the module API (I have no idea if it is), @deshipu should eventually make a pull request for his addition from #5458 so that modules using it can in the future be loaded by standard firmware?

@dpgeorge
Copy link
Member

dpgeorge commented Feb 2, 2020

I guess this also means that if mp_obj_get_array is considered a necessary or useful part of the module API (I have no idea if it is), @deshipu should eventually make a pull request for his addition from #5458 so that modules using it can in the future be loaded by standard firmware?

The functionality of mp_obj_get_array could be implemented directly in a native module using the existing dynamic API (what's already in py/dynruntime.h), but it would come at a bit of cost in code size. So the question (for this and other API functions that are not in mp_fun_table) is whether it's a useful/common enough function that it should be added to mp_fun_table.

@carstenblt
Copy link

So the question (for this and other API functions that are not in mp_fun_table) is whether it's a useful/common enough function that it should be added to mp_fun_table.

Came here for this function. I believe manipulating larger data sets might be one of the main use cases for native modules.

@dpgeorge
Copy link
Member

@tve this looks good, is it ready to merge?

@tve
Copy link
Contributor Author

tve commented Feb 14, 2020

@dpgeorge yes, I think this is worth merging at this point. More docs are needed on recommendations for how to use this stuff, e.g. for drivers (see #5618), but that can come later.

@dpgeorge
Copy link
Member

Ok, rebased, squashed and merged in be92aac

@dpgeorge dpgeorge closed this Feb 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy