0

In my project, I first compile aa_1.c, aa_2.c... from folder A, then compile bb_1.c bb_2.c... from folder B. Then I use gcc-ar to generate libapps.a. At last, I link with other static libraries.

Now I want to calculate text, rodata, data and bss section of folder A.

My method is to execute gcc-nm -S --size-sort folder/*.o, and accumulate text, rodata, data and bss sections. But some functions may be optimized away because they're never called.

So how can I calculate text, data, rodata and bss sizes?

I also have another question, using gcc-nm -S --size-sort a.o there is no 'b' type. But using gcc-size a.o shows bss section is 8 bytes. So which one is right?

Toribio
  • 3,963
  • 3
  • 34
  • 48
Emily
  • 3
  • 2
  • Have a look at `readelf -S`. It will report the address for each of the sections that you can use to calculate the size. – David C. Rankin Nov 02 '19 at 01:39
  • `But some functions may be optimized away because they're never called.` - you only know that _after_ you do the final link when creating the executable. Ergo, such calculation has to be done after the executable is generated. – KamilCuk Nov 02 '19 at 02:04

1 Answers1

0

As per Get list of static libraries used in an executable, the library names are discarded during the linking process.

You can add -Xlinker -Map=file.map to the link command, and then try to extract information from the 'file.map'.

dash-o
  • 13,723
  • 1
  • 10
  • 37