By Richard

2008-10-14 07:48:55 8 Comments

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

  1. So I've run 'size' on it to determine how big the hex file is.
  2. 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.
  3. Then I used 'readelf' to see which functions take up the most memory.
  4. 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


@odarcy 2015-07-24 13:21:54

To answer this specific need:

•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.

If you want to analyze your code base to see who calls what, by whom a given function is being called and things like that, there is a great tool out there called "Understand C" provided by SciTools.

I have used it very often in the past to perform static code analysis. It can really help to determine library dependency tree. It allows to easily browse up and down the calling tree among other things.

They provide a limited time evaluation, then you must purchase a license.

@Russ 2012-04-26 19:37:16

Andrew EdgeCombe has a great list, but if you really want to scrape every last byte, sstrip is a good tool that is missing from the list and and can shave off a few more kB.

For example, when run on strip itself, it can shave off ~2kB.

From an old README (see the comments at the top of this indirect source file):

sstrip is a small utility that removes the contents at the end of an ELF file that are not part of the program's memory image.

Most ELF executables are built with both a program header table and a section header table. However, only the former is required in order for the OS to load, link and execute a program. sstrip attempts to extract the ELF header, the program header table, and its contents, leaving everything else in the bit bucket. It can only remove parts of the file that occur at the end, after the parts to be saved. However, this almost always includes the section header table, and occasionally a few random sections that are not used when running a program.

Note that due to some of the information that it removes, a sstrip'd executable is rumoured to have issues with some tools. This is discussed more in the comments of the source.

Also... for an entertaining/crazy read on how to make the smallest possible executable, this article is worth a read.

@Seika 2008-12-09 20:19:17

On deeply embedded projects I always try to avoid using any standard library functions. Even simple functions like "strtol()" blow up the binary size. If possible just simply avoid those calls.

In most deeply embedded projects you don't need a versatile "printf()" or dynamic memory allocation (many controllers have 32kb or less RAM).

Instead of just using "printf()" I use a very simple custom "printf()", this function can only print numbers in hexadecimal or decimal format not more. Most data structures are preallocated at compile time.

@MSalters 2008-10-14 11:24:58

Another optimization that might save you work is -ffunction-sections, -Wl,--gc-sections, assuming you're using GCC. A good toolchain will not need to be told that, though.

Explanation: GNU ld links sections, and GCC emits one section per translation unit unless you tell it otherwise. But in C++, the nodes in the dependecy graph are objects and functions.

@Andrew Edgecombe 2008-10-14 10:04:58

General list:

  • Make sure that you have the compiler and linker debug options disabled
  • Compile and link with all size options turned on (-Os in gcc)
  • Run strip on the executable
  • Generate a map file and check your function sizes. You can either get your linker to generate your map file (-M when 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.
  • Use nm to 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. gcc will 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.

@Tarski 2008-10-14 12:16:47

If you are going to investigate symbols then you DO want to be compiling with debug options ON and not stripping the exe.

@strager 2008-11-28 01:51:48

I would also suggest fiddling around with -mthumb, since thumb executables are generally smaller than arm ones.

@Richard 2008-10-14 09:37:25

Ok so in the end I just reduced the project to it's simplest form, then slowly added files one by one until the function that I wanted to remove appeared in the 'readelf' file. Then when I had the file I commented everything out and slowly add things back in until the function popped up again. So in the end I found out what called it and removed all those calls...Now it works as desired...sweet!

Must be a better way to do it though.

@MSalters 2008-10-14 08:15:58

Just to double-check and document for future reference, but do you use Thumb instructions? They're 16 bit versions of the normal instructions. Sometimes you might need 2 16 bit instructions, so it won't save 50% in code space.

A decent linker should take just the functions needed. However, you might need compiler & linke settings to package functions for individual linking.

@Richard 2008-10-14 08:27:33

Thanks, hadn't thought about the Thumb stuff. To be honest I don't know if it is being used, I think so but will investigate further. Yes the linker only adds the functions used, i'm pretty sure. But I just can't work out what is calling the function? I need like a map of who calls what maybe?

@TraumaPony 2008-10-14 08:02:39

You could look at something like executable compression.

@Anders Hansson 2008-10-14 11:38:54

That wouldn't help if the running program is not supposed to be more than 32K.

@TraumaPony 2008-10-14 14:00:35

It would if it only decompressed 32k at a time.

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] How to run code from RAM on ARM architecture

  • 2013-02-28 13:51:02
  • morcillo
  • 13731 View
  • 8 Score
  • 3 Answer
  • Tags:   arm

1 Answered Questions

[SOLVED] How to affect Delphi XEx code generation for Android/ARM targets?

2 Answered Questions

[SOLVED] why my executable is bigger after linking

  • 2016-08-26 18:08:48
  • mjspier
  • 124 View
  • 2 Score
  • 2 Answer
  • Tags:   c arm embedded

2 Answered Questions

[SOLVED] Code size with const objects

1 Answered Questions

[SOLVED] What is the correct jmp_buf size?

  • 2013-07-10 07:52:59
  • harper
  • 901 View
  • 0 Score
  • 1 Answer
  • Tags:   gcc arm

2 Answered Questions

[SOLVED] Reducing the size of .rodata

  • 2012-12-25 18:43:18
  • templar
  • 3550 View
  • 7 Score
  • 2 Answer
  • Tags:   c++ linker embedded

2 Answered Questions

[SOLVED] Newlib optimized for code size

Sponsored Content