InstrProfilingPlatformLinux.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #if defined(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
  9. (defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__) || \
  10. defined(_AIX)
  11. #if !defined(_AIX)
  12. #include <elf.h>
  13. #include <link.h>
  14. #endif
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "InstrProfiling.h"
  18. #include "InstrProfilingInternal.h"
  19. #define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON)
  20. #define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON)
  21. #define PROF_NAME_START INSTR_PROF_SECT_START(INSTR_PROF_NAME_COMMON)
  22. #define PROF_NAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_NAME_COMMON)
  23. #define PROF_CNTS_START INSTR_PROF_SECT_START(INSTR_PROF_CNTS_COMMON)
  24. #define PROF_CNTS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_CNTS_COMMON)
  25. #define PROF_BITS_START INSTR_PROF_SECT_START(INSTR_PROF_BITS_COMMON)
  26. #define PROF_BITS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_BITS_COMMON)
  27. #define PROF_ORDERFILE_START INSTR_PROF_SECT_START(INSTR_PROF_ORDERFILE_COMMON)
  28. #define PROF_VNODES_START INSTR_PROF_SECT_START(INSTR_PROF_VNODES_COMMON)
  29. #define PROF_VNODES_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNODES_COMMON)
  30. /* Declare section start and stop symbols for various sections
  31. * generated by compiler instrumentation.
  32. */
  33. extern __llvm_profile_data PROF_DATA_START COMPILER_RT_VISIBILITY
  34. COMPILER_RT_WEAK;
  35. extern __llvm_profile_data PROF_DATA_STOP COMPILER_RT_VISIBILITY
  36. COMPILER_RT_WEAK;
  37. extern char PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
  38. extern char PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
  39. extern char PROF_BITS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
  40. extern char PROF_BITS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
  41. extern uint32_t PROF_ORDERFILE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
  42. extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
  43. extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
  44. extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
  45. extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
  46. COMPILER_RT_VISIBILITY const __llvm_profile_data *
  47. __llvm_profile_begin_data(void) {
  48. return &PROF_DATA_START;
  49. }
  50. COMPILER_RT_VISIBILITY const __llvm_profile_data *
  51. __llvm_profile_end_data(void) {
  52. return &PROF_DATA_STOP;
  53. }
  54. COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_names(void) {
  55. return &PROF_NAME_START;
  56. }
  57. COMPILER_RT_VISIBILITY const char *__llvm_profile_end_names(void) {
  58. return &PROF_NAME_STOP;
  59. }
  60. COMPILER_RT_VISIBILITY char *__llvm_profile_begin_counters(void) {
  61. return &PROF_CNTS_START;
  62. }
  63. COMPILER_RT_VISIBILITY char *__llvm_profile_end_counters(void) {
  64. return &PROF_CNTS_STOP;
  65. }
  66. COMPILER_RT_VISIBILITY char *__llvm_profile_begin_bitmap(void) {
  67. return &PROF_BITS_START;
  68. }
  69. COMPILER_RT_VISIBILITY char *__llvm_profile_end_bitmap(void) {
  70. return &PROF_BITS_STOP;
  71. }
  72. COMPILER_RT_VISIBILITY uint32_t *__llvm_profile_begin_orderfile(void) {
  73. return &PROF_ORDERFILE_START;
  74. }
  75. COMPILER_RT_VISIBILITY ValueProfNode *
  76. __llvm_profile_begin_vnodes(void) {
  77. return &PROF_VNODES_START;
  78. }
  79. COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) {
  80. return &PROF_VNODES_STOP;
  81. }
  82. COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START;
  83. COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP;
  84. #ifdef NT_GNU_BUILD_ID
  85. static size_t RoundUp(size_t size, size_t align) {
  86. return (size + align - 1) & ~(align - 1);
  87. }
  88. /*
  89. * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID
  90. * that contains build id. If build id exists, write binary id.
  91. *
  92. * Each note in notes section starts with a struct which includes
  93. * n_namesz, n_descsz, and n_type members. It is followed by the name
  94. * (whose length is defined in n_namesz) and then by the descriptor
  95. * (whose length is defined in n_descsz).
  96. *
  97. * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned
  98. * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes.
  99. */
  100. static int WriteBinaryIdForNote(ProfDataWriter *Writer,
  101. const ElfW(Nhdr) * Note) {
  102. int BinaryIdSize = 0;
  103. const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr));
  104. if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 &&
  105. memcmp(NoteName, "GNU\0", 4) == 0) {
  106. uint64_t BinaryIdLen = Note->n_descsz;
  107. const uint8_t *BinaryIdData =
  108. (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4));
  109. uint8_t BinaryIdPadding = __llvm_profile_get_num_padding_bytes(BinaryIdLen);
  110. if (Writer != NULL &&
  111. lprofWriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData,
  112. BinaryIdPadding) == -1)
  113. return -1;
  114. BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen + BinaryIdPadding;
  115. }
  116. return BinaryIdSize;
  117. }
  118. /*
  119. * Helper function that iterates through notes section and find build ids.
  120. * If writer is given, write binary ids into profiles.
  121. * If an error happens while writing, return -1.
  122. */
  123. static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note,
  124. const ElfW(Nhdr) * NotesEnd) {
  125. int BinaryIdsSize = 0;
  126. while (Note < NotesEnd) {
  127. int OneBinaryIdSize = WriteBinaryIdForNote(Writer, Note);
  128. if (OneBinaryIdSize == -1)
  129. return -1;
  130. BinaryIdsSize += OneBinaryIdSize;
  131. /* Calculate the offset of the next note in notes section. */
  132. size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) +
  133. RoundUp(Note->n_descsz, 4);
  134. Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset);
  135. }
  136. return BinaryIdsSize;
  137. }
  138. /*
  139. * Write binary ids into profiles if writer is given.
  140. * Return the total size of binary ids.
  141. * If an error happens while writing, return -1.
  142. */
  143. COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
  144. extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden")));
  145. const ElfW(Ehdr) *ElfHeader = &__ehdr_start;
  146. const ElfW(Phdr) *ProgramHeader =
  147. (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff);
  148. int TotalBinaryIdsSize = 0;
  149. uint32_t I;
  150. /* Iterate through entries in the program header. */
  151. for (I = 0; I < ElfHeader->e_phnum; I++) {
  152. /* Look for the notes segment in program header entries. */
  153. if (ProgramHeader[I].p_type != PT_NOTE)
  154. continue;
  155. /* There can be multiple notes segment, and examine each of them. */
  156. const ElfW(Nhdr) * Note;
  157. const ElfW(Nhdr) * NotesEnd;
  158. /*
  159. * When examining notes in file, use p_offset, which is the offset within
  160. * the elf file, to find the start of notes.
  161. */
  162. if (ProgramHeader[I].p_memsz == 0 ||
  163. ProgramHeader[I].p_memsz == ProgramHeader[I].p_filesz) {
  164. Note = (const ElfW(Nhdr) *)((uintptr_t)ElfHeader +
  165. ProgramHeader[I].p_offset);
  166. NotesEnd = (const ElfW(Nhdr) *)((const char *)(Note) +
  167. ProgramHeader[I].p_filesz);
  168. } else {
  169. /*
  170. * When examining notes in memory, use p_vaddr, which is the address of
  171. * section after loaded to memory, to find the start of notes.
  172. */
  173. Note =
  174. (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_vaddr);
  175. NotesEnd =
  176. (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_memsz);
  177. }
  178. int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd);
  179. if (TotalBinaryIdsSize == -1)
  180. return -1;
  181. TotalBinaryIdsSize += BinaryIdsSize;
  182. }
  183. return TotalBinaryIdsSize;
  184. }
  185. #elif !defined(_AIX) /* !NT_GNU_BUILD_ID */
  186. /*
  187. * Fallback implementation for targets that don't support the GNU
  188. * extensions NT_GNU_BUILD_ID and __ehdr_start.
  189. */
  190. COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
  191. return 0;
  192. }
  193. #endif
  194. #endif