4

I'm on an embedded system and need to wring out an additional couple dozen bytes from the heap.

Once I've built the .elf, is there a utility to run so I can output the memory layout of my class objects and structs?

I need to see if there's a way to gain memory by simply changing the layout of members.

Bob
  • 4,576
  • 7
  • 39
  • 107
  • 2
    With gcc (at least) you can use the `-Wpadded` option to get a warning every time the compiler inserts padding into your `struct`/`class` - some of that can be avoided, some of it cannot. That's an easy way to find types where shuffling member order has potential to gain a few bytes.. – Jesper Juhl Jan 24 '19 at 17:27
  • How is seeing the memory layout of compiled binary off-topic??? What stackexchange site am I supposed to go for this question? – Bob Jan 24 '19 at 17:27
  • @JesperJuhl will it give a warning per struct/class? – Bob Jan 24 '19 at 17:28
  • This may help https://stackoverflow.com/questions/21092415/force-c-structure-to-pack-tightly – Kenny Ostrom Jan 24 '19 at 17:28
  • 1
    @Adrian Yes. Read the documentation yourself: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html – Jesper Juhl Jan 24 '19 at 17:29
  • You can probably also get some interesting info from the `objdump`, `readelf` & `nm` tools. – Jesper Juhl Jan 24 '19 at 17:31
  • 1
    Many compilers have an option to print out a *map* file, which is a cross reference of all the symbols in your program. Check your compiler for syntax for generating the *map* file. – Thomas Matthews Jan 24 '19 at 18:16
  • If it is an elf, you may be able to deduce the layout from the ABI specification: [itanium ABI - Chapter 2 Data layout](http://itanium-cxx-abi.github.io/cxx-abi/abi.html#layout) – Oliv Jan 24 '19 at 18:26
  • 1
    The rule is basically as much packed as allow the alignment. The vtable pointer is at the top of the struct – Oliv Jan 24 '19 at 18:29
  • 1
    Just for reference, if you're using clang the command line `clang -Xclang -fdump-record-layouts ` dumps this kind of information (and `-Xclang -fno-color-diagnostics` makes it more machine-processable). – Jeremy Jan 25 '19 at 13:32
  • How about viewing the layout on the target? see: https://stackoverflow.com/a/54384590/2785528 – 2785528 Jan 29 '19 at 00:29

0 Answers0