Our application is embedded. And in addition to cold boot (reload everything; start over from scratch), we support warm boot. As part of supporting warm boot, read-write data that needs to be initialized, is initialized by code. And we ensure at link time that the traditional initialized read-write data sections (.data, .ldata, .sdata) are empty.
This presents a problem when attempting to use GCC based profiling as it creates read-write data in the aforementioned data sections. This patch adds a new command line option that allows you to specify the name of the section where GCC puts the instrumentation data. If the new option (-fprofile-data-section) is not specified, GCC behaves as before. What's missing? Testsuite changes. I haven't yet figured out how to do automated testing of this. To test it, I built our software, several thousand files, and then did an 'objdump --headers', verified that sections .data / .ldata / .sdata were either absent of empty, and that the instrumentation section had the name that I specified. We have a copyright assignment on file from before EMC was acquired by Dell. Our company lawyers assure me that it survived the acquisition and is still valid. I'm sending this from GNU/Linux rather than from Windows (to avoid having the patch mangled), so I'm not sure what the headers will show for my return address. If you wish to email me, I can be reached at dtaylor at emc dot com or David dot Taylor at dell dot com. Or... you can just send to the gcc-patches list as I'll be reading it. Enough verbiage, here's the ChangeLog entry and the patch... 2019-10-23 David Taylor <dtay...@emc.com> * common.opt (fprofile-data-section): New command line switch. * coverage.c (build_var): Add support for -fprofile-data-section. (coverage_obj_finish): Ditto. * toplev.c (process_options): Issue warning if -fprofile-data-section is specified when it is not supported. * doc/invoke.texi (Option Summary): List -fprofile-data-section. (Instrumentation Options): Document -fprofile-data-section. Index: gcc/common.opt =================================================================== --- gcc/common.opt (revision 277133) +++ gcc/common.opt (working copy) @@ -2124,6 +2124,10 @@ Common Joined RejectNegative Var(profile_note_location) Select the name for storing the profile note file. +fprofile-data-section= +Common Joined RejectNegative Var(profile_data_section_name) +Specify the section name for initialized profile data. + fprofile-correction Common Report Var(flag_profile_correction) Enable correction of flow inconsistent profile data input. Index: gcc/coverage.c =================================================================== --- gcc/coverage.c (revision 277133) +++ gcc/coverage.c (working copy) @@ -749,6 +749,9 @@ fn_name_len = strlen (fn_name); buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3); + TREE_STATIC (var) = 1; + if (profile_data_section_name) + set_decl_section_name (var, profile_data_section_name); if (counter < 0) strcpy (buf, "__gcov__"); else @@ -757,7 +760,6 @@ buf[len - 1] = symbol_table::symbol_suffix_separator (); memcpy (buf + len, fn_name, fn_name_len + 1); DECL_NAME (var) = get_identifier (buf); - TREE_STATIC (var) = 1; TREE_ADDRESSABLE (var) = 1; DECL_NONALIASED (var) = 1; SET_DECL_ALIGN (var, TYPE_ALIGN (type)); @@ -1188,10 +1190,14 @@ ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1); DECL_NAME (fn_info_ary) = get_identifier (name_buf); DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor); + if (profile_data_section_name) + set_decl_section_name (fn_info_ary, profile_data_section_name); varpool_node::finalize_decl (fn_info_ary); DECL_INITIAL (gcov_info_var) = build_info (TREE_TYPE (gcov_info_var), fn_info_ary); + if (profile_data_section_name) + set_decl_section_name (gcov_info_var, profile_data_section_name); varpool_node::finalize_decl (gcov_info_var); } Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 277133) +++ gcc/doc/invoke.texi (working copy) @@ -496,7 +496,7 @@ @item Program Instrumentation Options @xref{Instrumentation Options,,Program Instrumentation Options}. @gccoptlist{-p -pg -fprofile-arcs --coverage -ftest-coverage @gol --fprofile-abs-path @gol +-fprofile-abs-path -fprofile-data-section=@var{name} @gol -fprofile-dir=@var{path} -fprofile-generate -fprofile-generate=@var{path} @gol -fprofile-note=@var{path} -fprofile-update=@var{method} @gol -fprofile-filter-files=@var{regex} -fprofile-exclude-files=@var{regex} @gol @@ -12483,6 +12483,14 @@ generate test coverage data. Coverage data matches the source files more closely if you do not optimize. +@item -fprofile-data-section +@opindex fprofile-data-section +When profiling, sets the name of the section where GCC places the +profiling data. If not specified, then where the data is placed is +target dependent, but is typically either @code{.data} or +@code{.ldata}. Only supported for targets that support named +sections. + @item -fprofile-abs-path @opindex fprofile-abs-path Automatically convert relative source file names to absolute path names Index: gcc/toplev.c =================================================================== --- gcc/toplev.c (revision 277133) +++ gcc/toplev.c (working copy) @@ -1665,6 +1665,12 @@ "%<-fdata-sections%> not supported for this target"); flag_data_sections = 0; } + if (profile_data_section_name) + { + warning_at (UNKNOWN_LOCATION, 0, + "-fprofile-data-section= not supported for this target"); + profile_data_section_name = NULL; + } } if (flag_prefetch_loop_arrays > 0 && !targetm.code_for_prefetch)