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

function pointer with pic #3

Open
anjiahao1 opened this issue Nov 30, 2023 · 3 comments
Open

function pointer with pic #3

anjiahao1 opened this issue Nov 30, 2023 · 3 comments

Comments

@anjiahao1
Copy link

Hello,
I have a writing problem when using pic,
An error occurs when using a pointer to access a function in a global variable. The reason is that the function address in the global variable has been determined, but when using a pointer to access it, pic will not add the offset for me.

If you update the contents of .data during the system initialization phase, you cannot accurately distinguish which ones are absolute addresses and which ones are data.

example:

typedef void (*func)(void);

struct ops
{
  uint32_t a;
  func b;
};
void test(void)
{
  //do nothing
}

struct ops g_ops =
{
  12345,
  test
};

int main(void)
{
  struct ops *ops = &g_ops;

  ops->b();
  return 0;
}

do ops->b() will get a bad address.

@usvi
Copy link
Owner

usvi commented Nov 30, 2023

Yes, notoriously function pointers are a problem at least with basic forms of PIC and should be avoided. Some globals are a problem also.

Dunno if you have read my writeup but here it is: https://techblog.paalijarvi.fi/2022/01/16/portable-position-independent-code-pic-bootloader-and-firmware-for-arm-cortex-m0-and-cortex-m4/

I don't know if these aspects can be solved in a proper way at all. One hack I have heard of was to put the function pointers to a struct and use somehow with that. But I don't remember/know the details.

I'm very sorry.

@anjiahao1
Copy link
Author

Thank you for your reply. I found this code repository after reading your article. The article is very well written.
So for now, there is no way to access function pointers and certain global variables? Can such an operation be avoided only manually?

@anjiahao1
Copy link
Author

anjiahao1 commented Nov 30, 2023

i am do PIC on Nuttx rtos. just function pointer and some globals var is badaddress, other code is run well, i mark some function pointer,can run well.
However, mark all fuctions pointer is a verify lager work.

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

2 participants