GCC Linker : How To Generate A Report Of Per File Contribution On Output Sections
Recently I meet a problem when trying to link my program. It report .text
can't fit in specified memory region. Obviously the source code grows too large to be linked in limited memory region.
What I want to do now is to analyze which file contribute most significantly to the ".text" section so that follow up code optimization can be performed. I tried many ways but don't make it.
nm -s output.elf
gives size of each symbol, but don't group the symbol under each source file.nm -s file.obj
go through every object file don't work because-fdata-section
-ffunction-section
-Wl
are specified, so not all content in an object file will get linked to final outputreadelf -s output.elf
gives information from file to file, but it simply list symbols under each file and their size. A script can be written to sum all the size under a file, but the total value seems wrong, an obvious error here is some symbols may point to the same memory location, so the same memory region may be calculated many times.
When gcc link do its work, it should know all the details of what is extracted from a object file and put to the output section, but seems it don't provide a switch to generate a detail report (Or am I missing something?)
Is there any tool which can do this job?
Answer
Perhaps --gc-sections
together with --print-gc-sections
and/or --print-map-discarded
?
If everything is in separate sections, then you have all your sections as input, and list of discarded sections. Then simple script shall produce list of used sections, their size and file mapping.
Related Questions
- → Cython Fatal Error: Python.h No such file or directory
- → Building Python kerberos extension on opensuse fails
- → Why does the pointer get printed twice in C?
- → how to switch between gcc versions to build torch7 or caffe with cudnn 5 acceleration (cuda 7.5) on manjaro linux?
- → Which gcc switch disables "left-hand operand of comma has no effect" warning?
- → Best practices for debugging linking errors
- → Portably handle exceptional errors in C++
- → Why Am I Getting Link Errors When Calling Function in Math.h?
- → How do I check if gcc is performing tail-recursion optimization?
- → Why doesn't GCC optimize structs?
- → Why is my C++ output not showing in VS Code Terminal (GCC)
- → How does GCC compiler find the header file corresponding to some implicitly declared functions?
- → CMAKE_C_COMPILER not set, after EnableLanguage