Index: compiler-rt/lib/profile/InstrProfilingPlatformFuchsia.c =================================================================== --- compiler-rt/lib/profile/InstrProfilingPlatformFuchsia.c +++ compiler-rt/lib/profile/InstrProfilingPlatformFuchsia.c @@ -20,7 +20,9 @@ #if defined(__Fuchsia__) +#include #include +#include #include #include #include @@ -103,6 +105,27 @@ This->WriterCtx = Ctx; } +extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden"))); + +static size_t getBuildId(char *Buffer, size_t N) { + const ElfW(Ehdr) *Ehdr = &__ehdr_start; + const ElfW(Phdr) *Phdr = + (const ElfW(Phdr) *)((uintptr_t)Ehdr + Ehdr->e_phoff); + while (Phdr->p_type != PT_NOTE) + ++Phdr; + const ElfW(Nhdr) *Nhdr = + (const ElfW(Nhdr) *)((uintptr_t)Ehdr + Phdr->p_offset); + while (Nhdr->n_type != NT_GNU_BUILD_ID) + Nhdr = (const ElfW(Nhdr) *)((uintptr_t)Nhdr + sizeof(ElfW(Nhdr)) + + Nhdr->n_namesz + Nhdr->n_descsz); + uint8_t *BuildId = + (uint8_t *)((uintptr_t)Nhdr + sizeof(ElfW(Nhdr)) + Nhdr->n_namesz); + int L = 0; + for (size_t I = 0; I < Nhdr->n_descsz; I++) + L += snprintf(Buffer + (I * 2), N - (I * 2), "%02x", BuildId[I]); + return L; +} + /* This method is invoked by the runtime initialization hook * InstrProfilingRuntime.o if it is linked in. */ COMPILER_RT_VISIBILITY @@ -143,8 +166,8 @@ /* Give the VMO a name that includes the module signature. */ char VmoName[ZX_MAX_NAME_LEN]; - snprintf(VmoName, sizeof(VmoName), "%" PRIu64 ".profraw", - lprofGetLoadModuleSignature()); + size_t BuildIdLen = getBuildId(VmoName, ZX_MAX_NAME_LEN); + snprintf(VmoName + BuildIdLen, sizeof(VmoName) - BuildIdLen, ".profraw"); _zx_object_set_property(Vmo, ZX_PROP_NAME, VmoName, strlen(VmoName)); /* Write the profile data into the mapped region. */