xref: /openbsd-src/gnu/llvm/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c (revision 810390e339a5425391477d5d41c78d7cab2424ac)
13cab2bb3Spatrick /*===- InstrProfilingPlatformLinux.c - Profile data Linux platform ------===*\
23cab2bb3Spatrick |*
33cab2bb3Spatrick |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick |* See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick |*
73cab2bb3Spatrick \*===----------------------------------------------------------------------===*/
83cab2bb3Spatrick 
93cab2bb3Spatrick #if defined(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
10*810390e3Srobert     (defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__) || \
11*810390e3Srobert     defined(_AIX)
123cab2bb3Spatrick 
13*810390e3Srobert #if !defined(_AIX)
14d89ec533Spatrick #include <elf.h>
15d89ec533Spatrick #include <link.h>
16*810390e3Srobert #endif
173cab2bb3Spatrick #include <stdlib.h>
18d89ec533Spatrick #include <string.h>
193cab2bb3Spatrick 
203cab2bb3Spatrick #include "InstrProfiling.h"
21d89ec533Spatrick #include "InstrProfilingInternal.h"
22d89ec533Spatrick 
23d89ec533Spatrick #if defined(__FreeBSD__) && !defined(ElfW)
24d89ec533Spatrick /*
25d89ec533Spatrick  * FreeBSD's elf.h and link.h headers do not define the ElfW(type) macro yet.
26d89ec533Spatrick  * If this is added to all supported FreeBSD versions in the future, this
27d89ec533Spatrick  * compatibility macro can be removed.
28d89ec533Spatrick  */
29d89ec533Spatrick #define ElfW(type) __ElfN(type)
30d89ec533Spatrick #endif
313cab2bb3Spatrick 
323cab2bb3Spatrick #define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON)
333cab2bb3Spatrick #define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON)
343cab2bb3Spatrick #define PROF_NAME_START INSTR_PROF_SECT_START(INSTR_PROF_NAME_COMMON)
353cab2bb3Spatrick #define PROF_NAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_NAME_COMMON)
363cab2bb3Spatrick #define PROF_CNTS_START INSTR_PROF_SECT_START(INSTR_PROF_CNTS_COMMON)
373cab2bb3Spatrick #define PROF_CNTS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_CNTS_COMMON)
383cab2bb3Spatrick #define PROF_ORDERFILE_START INSTR_PROF_SECT_START(INSTR_PROF_ORDERFILE_COMMON)
393cab2bb3Spatrick #define PROF_VNODES_START INSTR_PROF_SECT_START(INSTR_PROF_VNODES_COMMON)
403cab2bb3Spatrick #define PROF_VNODES_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNODES_COMMON)
413cab2bb3Spatrick 
423cab2bb3Spatrick /* Declare section start and stop symbols for various sections
433cab2bb3Spatrick  * generated by compiler instrumentation.
443cab2bb3Spatrick  */
45d89ec533Spatrick extern __llvm_profile_data PROF_DATA_START COMPILER_RT_VISIBILITY
46d89ec533Spatrick     COMPILER_RT_WEAK;
47d89ec533Spatrick extern __llvm_profile_data PROF_DATA_STOP COMPILER_RT_VISIBILITY
48d89ec533Spatrick     COMPILER_RT_WEAK;
49*810390e3Srobert extern char PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
50*810390e3Srobert extern char PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
51d89ec533Spatrick extern uint32_t PROF_ORDERFILE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
52d89ec533Spatrick extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
53d89ec533Spatrick extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
54d89ec533Spatrick extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
55d89ec533Spatrick extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
563cab2bb3Spatrick 
573cab2bb3Spatrick COMPILER_RT_VISIBILITY const __llvm_profile_data *
__llvm_profile_begin_data(void)583cab2bb3Spatrick __llvm_profile_begin_data(void) {
593cab2bb3Spatrick   return &PROF_DATA_START;
603cab2bb3Spatrick }
613cab2bb3Spatrick COMPILER_RT_VISIBILITY const __llvm_profile_data *
__llvm_profile_end_data(void)623cab2bb3Spatrick __llvm_profile_end_data(void) {
633cab2bb3Spatrick   return &PROF_DATA_STOP;
643cab2bb3Spatrick }
__llvm_profile_begin_names(void)653cab2bb3Spatrick COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_names(void) {
663cab2bb3Spatrick   return &PROF_NAME_START;
673cab2bb3Spatrick }
__llvm_profile_end_names(void)683cab2bb3Spatrick COMPILER_RT_VISIBILITY const char *__llvm_profile_end_names(void) {
693cab2bb3Spatrick   return &PROF_NAME_STOP;
703cab2bb3Spatrick }
__llvm_profile_begin_counters(void)71*810390e3Srobert COMPILER_RT_VISIBILITY char *__llvm_profile_begin_counters(void) {
723cab2bb3Spatrick   return &PROF_CNTS_START;
733cab2bb3Spatrick }
__llvm_profile_end_counters(void)74*810390e3Srobert COMPILER_RT_VISIBILITY char *__llvm_profile_end_counters(void) {
753cab2bb3Spatrick   return &PROF_CNTS_STOP;
763cab2bb3Spatrick }
__llvm_profile_begin_orderfile(void)773cab2bb3Spatrick COMPILER_RT_VISIBILITY uint32_t *__llvm_profile_begin_orderfile(void) {
783cab2bb3Spatrick   return &PROF_ORDERFILE_START;
793cab2bb3Spatrick }
803cab2bb3Spatrick 
813cab2bb3Spatrick COMPILER_RT_VISIBILITY ValueProfNode *
__llvm_profile_begin_vnodes(void)823cab2bb3Spatrick __llvm_profile_begin_vnodes(void) {
833cab2bb3Spatrick   return &PROF_VNODES_START;
843cab2bb3Spatrick }
__llvm_profile_end_vnodes(void)853cab2bb3Spatrick COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) {
863cab2bb3Spatrick   return &PROF_VNODES_STOP;
873cab2bb3Spatrick }
883cab2bb3Spatrick COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START;
893cab2bb3Spatrick COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP;
903cab2bb3Spatrick 
91d89ec533Spatrick #ifdef NT_GNU_BUILD_ID
RoundUp(size_t size,size_t align)92d89ec533Spatrick static size_t RoundUp(size_t size, size_t align) {
93d89ec533Spatrick   return (size + align - 1) & ~(align - 1);
94d89ec533Spatrick }
95d89ec533Spatrick 
96d89ec533Spatrick /*
97d89ec533Spatrick  * Write binary id length and then its data, because binary id does not
98d89ec533Spatrick  * have a fixed length.
99d89ec533Spatrick  */
WriteOneBinaryId(ProfDataWriter * Writer,uint64_t BinaryIdLen,const uint8_t * BinaryIdData,uint64_t BinaryIdPadding)100d89ec533Spatrick static int WriteOneBinaryId(ProfDataWriter *Writer, uint64_t BinaryIdLen,
101*810390e3Srobert                             const uint8_t *BinaryIdData,
102*810390e3Srobert                             uint64_t BinaryIdPadding) {
103d89ec533Spatrick   ProfDataIOVec BinaryIdIOVec[] = {
104d89ec533Spatrick       {&BinaryIdLen, sizeof(uint64_t), 1, 0},
105*810390e3Srobert       {BinaryIdData, sizeof(uint8_t), BinaryIdLen, 0},
106*810390e3Srobert       {NULL, sizeof(uint8_t), BinaryIdPadding, 1},
107*810390e3Srobert   };
108d89ec533Spatrick   if (Writer->Write(Writer, BinaryIdIOVec,
109d89ec533Spatrick                     sizeof(BinaryIdIOVec) / sizeof(*BinaryIdIOVec)))
110d89ec533Spatrick     return -1;
111d89ec533Spatrick 
112d89ec533Spatrick   /* Successfully wrote binary id, report success. */
113d89ec533Spatrick   return 0;
114d89ec533Spatrick }
115d89ec533Spatrick 
116d89ec533Spatrick /*
117d89ec533Spatrick  * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID
118d89ec533Spatrick  * that contains build id. If build id exists, write binary id.
119d89ec533Spatrick  *
120d89ec533Spatrick  * Each note in notes section starts with a struct which includes
121d89ec533Spatrick  * n_namesz, n_descsz, and n_type members. It is followed by the name
122d89ec533Spatrick  * (whose length is defined in n_namesz) and then by the descriptor
123d89ec533Spatrick  * (whose length is defined in n_descsz).
124d89ec533Spatrick  *
125d89ec533Spatrick  * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned
126d89ec533Spatrick  * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes.
127d89ec533Spatrick  */
WriteBinaryIdForNote(ProfDataWriter * Writer,const ElfW (Nhdr)* Note)128d89ec533Spatrick static int WriteBinaryIdForNote(ProfDataWriter *Writer,
129d89ec533Spatrick                                 const ElfW(Nhdr) * Note) {
130d89ec533Spatrick   int BinaryIdSize = 0;
131d89ec533Spatrick   const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr));
132d89ec533Spatrick   if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 &&
133d89ec533Spatrick       memcmp(NoteName, "GNU\0", 4) == 0) {
134d89ec533Spatrick     uint64_t BinaryIdLen = Note->n_descsz;
135d89ec533Spatrick     const uint8_t *BinaryIdData =
136d89ec533Spatrick         (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4));
137*810390e3Srobert     uint8_t BinaryIdPadding = __llvm_profile_get_num_padding_bytes(BinaryIdLen);
138*810390e3Srobert     if (Writer != NULL && WriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData,
139*810390e3Srobert                                            BinaryIdPadding) == -1)
140d89ec533Spatrick       return -1;
141d89ec533Spatrick 
142*810390e3Srobert     BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen + BinaryIdPadding;
143d89ec533Spatrick   }
144d89ec533Spatrick 
145d89ec533Spatrick   return BinaryIdSize;
146d89ec533Spatrick }
147d89ec533Spatrick 
148d89ec533Spatrick /*
149d89ec533Spatrick  * Helper function that iterates through notes section and find build ids.
150d89ec533Spatrick  * If writer is given, write binary ids into profiles.
151d89ec533Spatrick  * If an error happens while writing, return -1.
152d89ec533Spatrick  */
WriteBinaryIds(ProfDataWriter * Writer,const ElfW (Nhdr)* Note,const ElfW (Nhdr)* NotesEnd)153d89ec533Spatrick static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note,
154d89ec533Spatrick                           const ElfW(Nhdr) * NotesEnd) {
155*810390e3Srobert   int BinaryIdsSize = 0;
156d89ec533Spatrick   while (Note < NotesEnd) {
157*810390e3Srobert     int OneBinaryIdSize = WriteBinaryIdForNote(Writer, Note);
158*810390e3Srobert     if (OneBinaryIdSize == -1)
159d89ec533Spatrick       return -1;
160*810390e3Srobert     BinaryIdsSize += OneBinaryIdSize;
161d89ec533Spatrick 
162d89ec533Spatrick     /* Calculate the offset of the next note in notes section. */
163d89ec533Spatrick     size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) +
164d89ec533Spatrick                         RoundUp(Note->n_descsz, 4);
165d89ec533Spatrick     Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset);
166d89ec533Spatrick   }
167d89ec533Spatrick 
168*810390e3Srobert   return BinaryIdsSize;
169d89ec533Spatrick }
170d89ec533Spatrick 
171d89ec533Spatrick /*
172d89ec533Spatrick  * Write binary ids into profiles if writer is given.
173d89ec533Spatrick  * Return the total size of binary ids.
174d89ec533Spatrick  * If an error happens while writing, return -1.
175d89ec533Spatrick  */
__llvm_write_binary_ids(ProfDataWriter * Writer)176d89ec533Spatrick COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
177d89ec533Spatrick   extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden")));
178d89ec533Spatrick   const ElfW(Ehdr) *ElfHeader = &__ehdr_start;
179d89ec533Spatrick   const ElfW(Phdr) *ProgramHeader =
180d89ec533Spatrick       (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff);
181d89ec533Spatrick 
182*810390e3Srobert   int TotalBinaryIdsSize = 0;
183d89ec533Spatrick   uint32_t I;
184d89ec533Spatrick   /* Iterate through entries in the program header. */
185d89ec533Spatrick   for (I = 0; I < ElfHeader->e_phnum; I++) {
186*810390e3Srobert     /* Look for the notes segment in program header entries. */
187d89ec533Spatrick     if (ProgramHeader[I].p_type != PT_NOTE)
188d89ec533Spatrick       continue;
189d89ec533Spatrick 
190*810390e3Srobert     /* There can be multiple notes segment, and examine each of them. */
191*810390e3Srobert     const ElfW(Nhdr) * Note;
192*810390e3Srobert     const ElfW(Nhdr) * NotesEnd;
193*810390e3Srobert     /*
194*810390e3Srobert      * When examining notes in file, use p_offset, which is the offset within
195*810390e3Srobert      * the elf file, to find the start of notes.
196*810390e3Srobert      */
197*810390e3Srobert     if (ProgramHeader[I].p_memsz == 0 ||
198*810390e3Srobert         ProgramHeader[I].p_memsz == ProgramHeader[I].p_filesz) {
199*810390e3Srobert       Note = (const ElfW(Nhdr) *)((uintptr_t)ElfHeader +
200*810390e3Srobert                                   ProgramHeader[I].p_offset);
201*810390e3Srobert       NotesEnd = (const ElfW(Nhdr) *)((const char *)(Note) +
202*810390e3Srobert                                       ProgramHeader[I].p_filesz);
203*810390e3Srobert     } else {
204*810390e3Srobert       /*
205*810390e3Srobert        * When examining notes in memory, use p_vaddr, which is the address of
206*810390e3Srobert        * section after loaded to memory, to find the start of notes.
207*810390e3Srobert        */
208*810390e3Srobert       Note =
209*810390e3Srobert           (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_vaddr);
210*810390e3Srobert       NotesEnd =
211*810390e3Srobert           (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_memsz);
212d89ec533Spatrick     }
213d89ec533Spatrick 
214*810390e3Srobert     int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd);
215*810390e3Srobert     if (TotalBinaryIdsSize == -1)
216*810390e3Srobert       return -1;
217*810390e3Srobert 
218*810390e3Srobert     TotalBinaryIdsSize += BinaryIdsSize;
219*810390e3Srobert   }
220*810390e3Srobert 
221*810390e3Srobert   return TotalBinaryIdsSize;
222d89ec533Spatrick }
223d89ec533Spatrick #else /* !NT_GNU_BUILD_ID */
224d89ec533Spatrick /*
225d89ec533Spatrick  * Fallback implementation for targets that don't support the GNU
226d89ec533Spatrick  * extensions NT_GNU_BUILD_ID and __ehdr_start.
227d89ec533Spatrick  */
__llvm_write_binary_ids(ProfDataWriter * Writer)228d89ec533Spatrick COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
229d89ec533Spatrick   return 0;
230d89ec533Spatrick }
231d89ec533Spatrick #endif
232d89ec533Spatrick 
233*810390e3Srobert #if defined(_AIX)
234*810390e3Srobert // Empty stubs to allow linking object files using the registration-based scheme
235*810390e3Srobert COMPILER_RT_VISIBILITY
__llvm_profile_register_function(void * Data_)236*810390e3Srobert void __llvm_profile_register_function(void *Data_) {}
237*810390e3Srobert 
238*810390e3Srobert COMPILER_RT_VISIBILITY
__llvm_profile_register_names_function(void * NamesStart,uint64_t NamesSize)239*810390e3Srobert void __llvm_profile_register_names_function(void *NamesStart,
240*810390e3Srobert                                             uint64_t NamesSize) {}
241*810390e3Srobert 
242*810390e3Srobert // The __start_SECNAME and __stop_SECNAME symbols (for SECNAME \in
243*810390e3Srobert // {"__llvm_prf_cnts", "__llvm_prf_data", "__llvm_prf_name", "__llvm_prf_vnds"})
244*810390e3Srobert // are always live when linking on AIX, regardless if the .o's being linked
245*810390e3Srobert // reference symbols from the profile library (for example when no files were
246*810390e3Srobert // compiled with -fprofile-generate). That's because these symbols are kept
247*810390e3Srobert // alive through references in constructor functions that are always live in the
248*810390e3Srobert // default linking model on AIX (-bcdtors:all). The __start_SECNAME and
249*810390e3Srobert // __stop_SECNAME symbols are only resolved by the linker when the SECNAME
250*810390e3Srobert // section exists. So for the scenario where the user objects have no such
251*810390e3Srobert // section (i.e. when they are compiled with -fno-profile-generate), we always
252*810390e3Srobert // define these zero length variables in each of the above 4 sections.
253*810390e3Srobert static int dummy_cnts[0] COMPILER_RT_SECTION(
254*810390e3Srobert     COMPILER_RT_SEG INSTR_PROF_CNTS_SECT_NAME);
255*810390e3Srobert static int dummy_data[0] COMPILER_RT_SECTION(
256*810390e3Srobert     COMPILER_RT_SEG INSTR_PROF_DATA_SECT_NAME);
257*810390e3Srobert static const int dummy_name[0] COMPILER_RT_SECTION(
258*810390e3Srobert     COMPILER_RT_SEG INSTR_PROF_NAME_SECT_NAME);
259*810390e3Srobert static int dummy_vnds[0] COMPILER_RT_SECTION(
260*810390e3Srobert     COMPILER_RT_SEG INSTR_PROF_VNODES_SECT_NAME);
261*810390e3Srobert 
262*810390e3Srobert // To avoid GC'ing of the dummy variables by the linker, reference them in an
263*810390e3Srobert // array and reference the array in the runtime registration code
264*810390e3Srobert // (InstrProfilingRuntime.cpp)
265*810390e3Srobert COMPILER_RT_VISIBILITY
266*810390e3Srobert void *__llvm_profile_keep[] = {(void *)&dummy_cnts, (void *)&dummy_data,
267*810390e3Srobert                                (void *)&dummy_name, (void *)&dummy_vnds};
268*810390e3Srobert #endif
269*810390e3Srobert 
2703cab2bb3Spatrick #endif
271