| 1 | /*===- InstrProfilingPlatformLinux.c - Profile data Linux platform ------===*\ |
| 2 | |* |
| 3 | |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | |* See https://llvm.org/LICENSE.txt for license information. |
| 5 | |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | |* |
| 7 | \*===----------------------------------------------------------------------===*/ |
| 8 | |
| 9 | // This file defines profile data symbols for ELF, wasm, XCOFF. It assumes |
| 10 | // __start_ and __stop_ symbols for profile data point at the beginning and |
| 11 | // end of the sections in question. (This is technically a linker feature, |
| 12 | // not a file format feature, but linkers for these targets support it.) |
| 13 | // |
| 14 | // MachO (MacOS/iOS) and PE-COFF (Windows) have a similar support, but the |
| 15 | // identifiers are different, so the support is in separate files. |
| 16 | // |
| 17 | // Support for targets which don't have linker support is in |
| 18 | // InstrProfilingPlatformOther.c. |
| 19 | // |
| 20 | // This file also contains code to extract ELF build IDs from the ELF file, |
| 21 | // to identify the build which generated the file. |
| 22 | |
| 23 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \ |
| 24 | (defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__) || \ |
| 25 | defined(_AIX) || defined(__wasm__) || defined(__HAIKU__) || \ |
| 26 | defined(COMPILER_RT_PROFILE_BAREMETAL) |
| 27 | |
| 28 | #if !defined(_AIX) && !defined(__wasm__) && \ |
| 29 | !defined(COMPILER_RT_PROFILE_BAREMETAL) |
| 30 | // Includes for non-baremetal ELF targets, used to output build IDs. |
| 31 | #include <elf.h> |
| 32 | #include <link.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #endif |
| 36 | |
| 37 | #include "InstrProfiling.h" |
| 38 | #include "InstrProfilingInternal.h" |
| 39 | |
| 40 | #define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON) |
| 41 | #define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON) |
| 42 | #define PROF_NAME_START INSTR_PROF_SECT_START(INSTR_PROF_NAME_COMMON) |
| 43 | #define PROF_NAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_NAME_COMMON) |
| 44 | #define PROF_VNAME_START INSTR_PROF_SECT_START(INSTR_PROF_VNAME_COMMON) |
| 45 | #define PROF_VNAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNAME_COMMON) |
| 46 | #define PROF_CNTS_START INSTR_PROF_SECT_START(INSTR_PROF_CNTS_COMMON) |
| 47 | #define PROF_CNTS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_CNTS_COMMON) |
| 48 | #define PROF_VTABLE_START INSTR_PROF_SECT_START(INSTR_PROF_VTAB_COMMON) |
| 49 | #define PROF_VTABLE_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VTAB_COMMON) |
| 50 | #define PROF_BITS_START INSTR_PROF_SECT_START(INSTR_PROF_BITS_COMMON) |
| 51 | #define PROF_BITS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_BITS_COMMON) |
| 52 | #define PROF_VNODES_START INSTR_PROF_SECT_START(INSTR_PROF_VNODES_COMMON) |
| 53 | #define PROF_VNODES_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNODES_COMMON) |
| 54 | #define PROF_COVINIT_START INSTR_PROF_SECT_START(INSTR_PROF_COVINIT_COMMON) |
| 55 | #define PROF_COVINIT_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_COVINIT_COMMON) |
| 56 | |
| 57 | /* Declare section start and stop symbols for various sections |
| 58 | * generated by compiler instrumentation. |
| 59 | */ |
| 60 | extern __llvm_profile_data PROF_DATA_START COMPILER_RT_VISIBILITY |
| 61 | COMPILER_RT_WEAK; |
| 62 | extern __llvm_profile_data PROF_DATA_STOP COMPILER_RT_VISIBILITY |
| 63 | COMPILER_RT_WEAK; |
| 64 | extern char PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; |
| 65 | extern char PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; |
| 66 | extern VTableProfData PROF_VTABLE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; |
| 67 | extern VTableProfData PROF_VTABLE_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; |
| 68 | extern char PROF_VNAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; |
| 69 | extern char PROF_VNAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; |
| 70 | extern char PROF_BITS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; |
| 71 | extern char PROF_BITS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; |
| 72 | extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; |
| 73 | extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; |
| 74 | extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; |
| 75 | extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; |
| 76 | extern __llvm_gcov_init_func_struct PROF_COVINIT_START COMPILER_RT_VISIBILITY |
| 77 | COMPILER_RT_WEAK; |
| 78 | extern __llvm_gcov_init_func_struct PROF_COVINIT_STOP COMPILER_RT_VISIBILITY |
| 79 | COMPILER_RT_WEAK; |
| 80 | |
| 81 | COMPILER_RT_VISIBILITY const __llvm_profile_data * |
| 82 | __llvm_profile_begin_data(void) { |
| 83 | return &PROF_DATA_START; |
| 84 | } |
| 85 | COMPILER_RT_VISIBILITY const __llvm_profile_data * |
| 86 | __llvm_profile_end_data(void) { |
| 87 | return &PROF_DATA_STOP; |
| 88 | } |
| 89 | COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_names(void) { |
| 90 | return &PROF_NAME_START; |
| 91 | } |
| 92 | COMPILER_RT_VISIBILITY const char *__llvm_profile_end_names(void) { |
| 93 | return &PROF_NAME_STOP; |
| 94 | } |
| 95 | COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_vtabnames(void) { |
| 96 | return &PROF_VNAME_START; |
| 97 | } |
| 98 | COMPILER_RT_VISIBILITY const char *__llvm_profile_end_vtabnames(void) { |
| 99 | return &PROF_VNAME_STOP; |
| 100 | } |
| 101 | COMPILER_RT_VISIBILITY const VTableProfData * |
| 102 | __llvm_profile_begin_vtables(void) { |
| 103 | return &PROF_VTABLE_START; |
| 104 | } |
| 105 | COMPILER_RT_VISIBILITY const VTableProfData *__llvm_profile_end_vtables(void) { |
| 106 | return &PROF_VTABLE_STOP; |
| 107 | } |
| 108 | COMPILER_RT_VISIBILITY char *__llvm_profile_begin_counters(void) { |
| 109 | return &PROF_CNTS_START; |
| 110 | } |
| 111 | COMPILER_RT_VISIBILITY char *__llvm_profile_end_counters(void) { |
| 112 | return &PROF_CNTS_STOP; |
| 113 | } |
| 114 | COMPILER_RT_VISIBILITY char *__llvm_profile_begin_bitmap(void) { |
| 115 | return &PROF_BITS_START; |
| 116 | } |
| 117 | COMPILER_RT_VISIBILITY char *__llvm_profile_end_bitmap(void) { |
| 118 | return &PROF_BITS_STOP; |
| 119 | } |
| 120 | |
| 121 | COMPILER_RT_VISIBILITY ValueProfNode * |
| 122 | __llvm_profile_begin_vnodes(void) { |
| 123 | return &PROF_VNODES_START; |
| 124 | } |
| 125 | COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) { |
| 126 | return &PROF_VNODES_STOP; |
| 127 | } |
| 128 | COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START; |
| 129 | COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP; |
| 130 | |
| 131 | COMPILER_RT_VISIBILITY const __llvm_gcov_init_func_struct * |
| 132 | __llvm_profile_begin_covinit() { |
| 133 | return &PROF_COVINIT_START; |
| 134 | } |
| 135 | |
| 136 | COMPILER_RT_VISIBILITY const __llvm_gcov_init_func_struct * |
| 137 | __llvm_profile_end_covinit() { |
| 138 | return &PROF_COVINIT_STOP; |
| 139 | } |
| 140 | |
| 141 | #ifdef NT_GNU_BUILD_ID |
| 142 | static size_t RoundUp(size_t size, size_t align) { |
| 143 | return (size + align - 1) & ~(align - 1); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID |
| 148 | * that contains build id. If build id exists, write binary id. |
| 149 | * |
| 150 | * Each note in notes section starts with a struct which includes |
| 151 | * n_namesz, n_descsz, and n_type members. It is followed by the name |
| 152 | * (whose length is defined in n_namesz) and then by the descriptor |
| 153 | * (whose length is defined in n_descsz). |
| 154 | * |
| 155 | * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned |
| 156 | * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes. |
| 157 | */ |
| 158 | static int WriteBinaryIdForNote(ProfDataWriter *Writer, |
| 159 | const ElfW(Nhdr) * Note) { |
| 160 | int BinaryIdSize = 0; |
| 161 | const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr)); |
| 162 | if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 && |
| 163 | memcmp(s1: NoteName, s2: "GNU\0" , n: 4) == 0) { |
| 164 | uint64_t BinaryIdLen = Note->n_descsz; |
| 165 | const uint8_t *BinaryIdData = |
| 166 | (const uint8_t *)(NoteName + RoundUp(size: Note->n_namesz, align: 4)); |
| 167 | uint8_t BinaryIdPadding = __llvm_profile_get_num_padding_bytes(SizeInBytes: BinaryIdLen); |
| 168 | if (Writer != NULL && |
| 169 | lprofWriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData, |
| 170 | BinaryIdPadding) == -1) |
| 171 | return -1; |
| 172 | |
| 173 | BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen + BinaryIdPadding; |
| 174 | } |
| 175 | |
| 176 | return BinaryIdSize; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Helper function that iterates through notes section and find build ids. |
| 181 | * If writer is given, write binary ids into profiles. |
| 182 | * If an error happens while writing, return -1. |
| 183 | */ |
| 184 | static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note, |
| 185 | const ElfW(Nhdr) * NotesEnd) { |
| 186 | int BinaryIdsSize = 0; |
| 187 | while (Note < NotesEnd) { |
| 188 | int OneBinaryIdSize = WriteBinaryIdForNote(Writer, Note); |
| 189 | if (OneBinaryIdSize == -1) |
| 190 | return -1; |
| 191 | BinaryIdsSize += OneBinaryIdSize; |
| 192 | |
| 193 | /* Calculate the offset of the next note in notes section. */ |
| 194 | size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(size: Note->n_namesz, align: 4) + |
| 195 | RoundUp(size: Note->n_descsz, align: 4); |
| 196 | Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset); |
| 197 | } |
| 198 | |
| 199 | return BinaryIdsSize; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Write binary ids into profiles if writer is given. |
| 204 | * Return the total size of binary ids. |
| 205 | * If an error happens while writing, return -1. |
| 206 | */ |
| 207 | COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) { |
| 208 | extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden" ))); |
| 209 | extern ElfW(Dyn) _DYNAMIC[] __attribute__((weak, visibility("hidden" ))); |
| 210 | |
| 211 | const ElfW(Ehdr) * = &__ehdr_start; |
| 212 | const ElfW(Phdr) * = |
| 213 | (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff); |
| 214 | |
| 215 | /* Compute the added base address in case of position-independent code. */ |
| 216 | uintptr_t Base = 0; |
| 217 | for (uint32_t I = 0; I < ElfHeader->e_phnum; I++) { |
| 218 | if (ProgramHeader[I].p_type == PT_PHDR) |
| 219 | Base = (uintptr_t)ProgramHeader - ProgramHeader[I].p_vaddr; |
| 220 | if (ProgramHeader[I].p_type == PT_DYNAMIC && _DYNAMIC) |
| 221 | Base = (uintptr_t)_DYNAMIC - ProgramHeader[I].p_vaddr; |
| 222 | } |
| 223 | |
| 224 | int TotalBinaryIdsSize = 0; |
| 225 | /* Iterate through entries in the program header. */ |
| 226 | for (uint32_t I = 0; I < ElfHeader->e_phnum; I++) { |
| 227 | /* Look for the notes segment in program header entries. */ |
| 228 | if (ProgramHeader[I].p_type != PT_NOTE) |
| 229 | continue; |
| 230 | |
| 231 | /* There can be multiple notes segment, and examine each of them. */ |
| 232 | const ElfW(Nhdr) *Note = |
| 233 | (const ElfW(Nhdr) *)(Base + ProgramHeader[I].p_vaddr); |
| 234 | const ElfW(Nhdr) *NotesEnd = |
| 235 | (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_memsz); |
| 236 | |
| 237 | int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd); |
| 238 | if (TotalBinaryIdsSize == -1) |
| 239 | return -1; |
| 240 | |
| 241 | TotalBinaryIdsSize += BinaryIdsSize; |
| 242 | } |
| 243 | |
| 244 | return TotalBinaryIdsSize; |
| 245 | } |
| 246 | #elif !defined(_AIX) /* !NT_GNU_BUILD_ID */ |
| 247 | /* |
| 248 | * Fallback implementation for targets that don't support the GNU |
| 249 | * extensions NT_GNU_BUILD_ID and __ehdr_start. |
| 250 | */ |
| 251 | COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) { |
| 252 | return 0; |
| 253 | } |
| 254 | #endif |
| 255 | |
| 256 | #endif |
| 257 | |