Skip to content

Commit 58ecfcc

Browse files
committed
[profile] Add __attribute__((used)) to zero size dummy sections
D14468 added these dummy sections. This patch adds `__attribute__((used))` so that when compiled by GCC>=11 or (expected, D96838) Clang>=13 on some ELF platforms, these sections will get SHF_GNU_RETAIN to make sure they will not be discarded by ld --gc-sections. We are trying to get rid of LLD's "__start_/__stop_ references retain C identifier name sections" rule. If LLD drops the rule in the future (we will retain compatibility for `__llvm_prf_*` for a while), `__llvm_prf_*` will need to have the SHF_GNU_RETAIN flag, otherwise: ``` // __llvm_prf_cnts/__llvm_prf_data usually exist, but {names,vnds} may not exist. // Such diagnostics will happen with {cnts,data} as well if no input object file is instrumented. % clang++ -fprofile-generate a.cc -fuse-ld=lld -Wl,--gc-sections ld.lld: error: undefined hidden symbol: __start___llvm_prf_names >>> referenced by InstrProfilingPlatformLinux.c >>> InstrProfilingPlatformLinux.c.o:(__llvm_profile_begin_names) in archive /tmp/RelA/lib/clang/13.0.0/lib/linux/libclang_rt.profile-x86_64.a ... ``` Differential Revision: https://reviews.llvm.org/D96902
1 parent c3a3d20 commit 58ecfcc

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

compiler-rt/lib/profile/InstrProfilingPlatformLinux.c

+13-9
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,19 @@ extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY;
3636
extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY;
3737
extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY;
3838

39-
/* Add dummy data to ensure the section is always created. */
40-
__llvm_profile_data
41-
__prof_data_sect_data[0] COMPILER_RT_SECTION(INSTR_PROF_DATA_SECT_NAME);
42-
uint64_t
43-
__prof_cnts_sect_data[0] COMPILER_RT_SECTION(INSTR_PROF_CNTS_SECT_NAME);
44-
uint32_t
45-
__prof_orderfile_sect_data[0] COMPILER_RT_SECTION(INSTR_PROF_ORDERFILE_SECT_NAME);
46-
const char __prof_nms_sect_data[0] COMPILER_RT_SECTION(INSTR_PROF_NAME_SECT_NAME);
47-
ValueProfNode __prof_vnodes_sect_data[0] COMPILER_RT_SECTION(INSTR_PROF_VNODES_SECT_NAME);
39+
/* Add dummy data to ensure the section is always created. Add used attribute so
40+
* that they are linker GC roots on supported ELF platforms.
41+
*/
42+
__llvm_profile_data __prof_data_sect_data[0] COMPILER_RT_SECTION(
43+
INSTR_PROF_DATA_SECT_NAME) COMPILER_RT_USED;
44+
uint64_t __prof_cnts_sect_data[0] COMPILER_RT_SECTION(INSTR_PROF_CNTS_SECT_NAME)
45+
COMPILER_RT_USED;
46+
uint32_t __prof_orderfile_sect_data[0] COMPILER_RT_SECTION(
47+
INSTR_PROF_ORDERFILE_SECT_NAME) COMPILER_RT_USED;
48+
const char __prof_nms_sect_data[0] COMPILER_RT_SECTION(
49+
INSTR_PROF_NAME_SECT_NAME) COMPILER_RT_USED;
50+
ValueProfNode __prof_vnodes_sect_data[0] COMPILER_RT_SECTION(
51+
INSTR_PROF_VNODES_SECT_NAME) COMPILER_RT_USED;
4852

4953
COMPILER_RT_VISIBILITY const __llvm_profile_data *
5054
__llvm_profile_begin_data(void) {

compiler-rt/lib/profile/InstrProfilingPort.h

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define COMPILER_RT_FTRUNCATE(f,l) _chsize(_fileno(f),l)
2424
#define COMPILER_RT_ALWAYS_INLINE __forceinline
2525
#define COMPILER_RT_CLEANUP(x)
26+
#define COMPILER_RT_USED
2627
#elif __GNUC__
2728
#ifdef _WIN32
2829
#define COMPILER_RT_FTRUNCATE(f, l) _chsize(fileno(f), l)
@@ -37,6 +38,7 @@
3738
#define COMPILER_RT_ALLOCA __builtin_alloca
3839
#define COMPILER_RT_ALWAYS_INLINE inline __attribute((always_inline))
3940
#define COMPILER_RT_CLEANUP(x) __attribute__((cleanup(x)))
41+
#define COMPILER_RT_USED __attribute__((used))
4042
#endif
4143

4244
#if defined(__APPLE__)

0 commit comments

Comments
 (0)