Keil Logo

Removing variables used in unused functions

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Author
Bru No
Posted
2-Aug-2018 10:00 GMT
Toolset
ARM
New! Removing variables used in unused functions

Hello,

I'm having issue with removing unused variables, when they are used in unused functions.

I'm using Keil 5.25.2.0, armcc.exe and armlink.exe V5.06 update 6 build 750. My project options are set for space optimization:
- Use cross-module optimization
- One ELF section per function
- -O2 -Ospace
- --remove
My MCU is a STM32F0, Cortex M0.

Here is a sample of what I do :

int unused_data[100];

main(){

some code...

my_unused_function();

some more code...

}

void my_unused_function(void){

int i;
for(i=0; i<100; i++) unused_data[i] = i;

}

When adding unused_data and my_unused_function(), I can see the code increasing, and 100 bytes of data being added to ZI-data. So far so good.

When commenting the call to my_unused_function(), the code decreases as expected. I get the original code size (when the function was not there), which means it has been successfully removed.

But ZI-data doesn't decrease, the variable is still there. And only if I comment "unused_data[i] = i;", then unused_data is successfully removed.

So I have this variable, used only in an unused function, which is virtually unused. But the linker doesn't seem to see this, and doesn't remove it. Which is quite a problem, this actually prevents my code to fit into the RAM of my MCU.

So how can I tell the linker to remove this (and all the other) unused variables ?

Author
Andrew Neil
Posted
2-Aug-2018 10:20 GMT
Toolset
None
New! How To Properly Post Source Code

Look at the picture:


http://www.danlhenry.com/caps/keil_code.png

Author
Bru No
Posted
2-Aug-2018 10:26 GMT
Toolset
None
New! RE: How To Properly Post Source Code

My bad. I don't see how to edit my message however.

But to be fair, this is not really about the code, is it ?

Author
Andrew Neil
Posted
2-Aug-2018 11:07 GMT
Toolset
None
New! RE: I don't see how to edit my message

You can't.

:(

You just have to re-post it, I'm afraid.

Author
Bru No
Posted
2-Aug-2018 13:27 GMT
Toolset
None
New! RE: I don't see how to edit my message

Ok, I'll do that :)

Author
Andrew Neil
Posted
2-Aug-2018 14:07 GMT
Toolset
None
New! RE: I don't see how to edit my message

You could've just continued here - rather than start a whole new thread:

http://www.keil.com/forum/63799

Author
Westonsupermare Pier
Posted
2-Aug-2018 18:32 GMT
Toolset
None
New! RE: I don't see how to edit my message

I think we can read the code as presented, lets just bring the circus when it is a wall of intractable text.

Try defining both the function and data as "static" so their scope is limited to the file, and the linker can discard more readily.

Author
Bru No
Posted
3-Aug-2018 13:07 GMT
Toolset
None
New! RE: I don't see how to edit my message

Well, the whole thing here is more complicated:

I have a full program, that builds OK using another IDE, and I want to build it with ARM-MDK. It's not that big, but still is something like a few dozens source files and at least as many include files, spread over several libraries + our own files.

I'm indeed considering monitoring everything and manually finding the biggest variables that are not needed. This is a long and tedious process, but more importantly, it is a recurring process. I'm sure I'll manage to modify what's needed to build now, but what I want is to be able to do this automatically in my future developments.

Last but not least, I like not to modify the libraries I use, to allow easier updates. So automatic removal really is the right option.

Next Thread | Thread List | Previous Thread Start a Thread | Settings

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.