I’m producing a hex file to run on an ARM processor which I want to keep below 32K. It’s currently a lot larger than that and I wondered if someone might have some advice on what’s the best approach to slim it down?
Here’s what I’ve done so far
- So I’ve run ‘size’ on it to determine how big the hex file is.
- Then ‘size’ again to see how big each of the object files are that link to create the hex files. It seems the majority of the size comes from external libraries.
- Then I used ‘readelf’ to see which functions take up the most memory.
- I searched through the code to see if I could eliminate calls to those functions.
Here’s where I get stuck, there’s some functions which I don’t call directly (e.g. _vfprintf) and I can’t find what calls it so I can remove the call (as I think I don’t need it).
So what are the next steps?
Response to answers:
- As I can see there are functions being called which take up a lot of memory. I cannot however find what is calling it.
- I want to omit those functions (if possible) but I can’t find what’s calling them! Could be called from any number of library functions I guess.
- The linker is working as desired, I think, it only includes the relevant library files. How do you know if only the relevant functions are being included? Can you set a flag or something for that?
- I’m using GCC
General list:
stripon the executable-Mwhen using ld), or you can use objdump on the final executable (note that this will only work on an unstripped executable!) This won’t actually fix the problem, but it will let you know of the worst offenders.nmto investigate the symbols that are called from each of your object files. This should help in finding who’s calling functions that you don’t want called.In the original question was a sub-question about including only relevant functions.
gccwill include all functions within every object file that is used. To put that another way, if you have an object file that contains 10 functions, all 10 functions are included in your executable even if one 1 is actually called.The standard libraries (eg. libc) will split functions into many separate object files, which are then archived. The executable is then linked against the archive. By splitting into many object files the linker is able to include only the functions that are actually called. (this assumes that you’re statically linking)
There is no reason why you can’t do the same trick. Of course, you could argue that if the functions aren’t called the you can probably remove them yourself.
If you’re statically linking against other libraries you can run the tools listed above over them too to make sure that they’re following similar rules.