Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions llvm/tools/llvm-profgen/ProfiledBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "MissingFrameInferrer.h"
#include "Options.h"
#include "ProfileGenerator.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/DebugInfo/PDB/IPDBSession.h"
#include "llvm/DebugInfo/PDB/PDB.h"
#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
Expand Down Expand Up @@ -246,6 +248,19 @@ void ProfiledBinary::load() {
// Find the preferred load address for text sections.
setPreferredTextSegmentAddresses(Obj);

// For shared libraries, read build ID to filter perfscript addresses
// in [buildid:]addr format. Main executables (including PIE) use empty
// FilterBuildID since their addresses have no buildid prefix.
// Both PIE executables and shared libraries are ET_DYN, but only PIE
// executables have a PT_INTERP program header.
file_magic Magic;
if (auto EC = identify_magic(Path, Magic);
!EC && Magic == file_magic::elf_shared_object && !HasInterp) {
auto BID = object::getBuildID(Obj);
if (!BID.empty())
FilterBuildID = llvm::toHex(BID, /*LowerCase=*/true);
}

// Load debug info of subprograms from DWARF section.
// If path of debug info binary is specified, use the debug info from it,
// otherwise use the debug info from the executable binary.
Expand Down Expand Up @@ -351,6 +366,8 @@ void ProfiledBinary::setPreferredTextSegmentAddresses(const ELFFile<ELFT> &Obj,
// because we may build the tools on non-linux.
uint64_t PageSize = 0x1000;
for (const typename ELFT::Phdr &Phdr : PhdrRange) {
if (Phdr.p_type == ELF::PT_INTERP)
HasInterp = true;
if (Phdr.p_type == ELF::PT_LOAD) {
if (!FirstLoadableAddress)
FirstLoadableAddress = Phdr.p_vaddr & ~(PageSize - 1U);
Expand Down
14 changes: 14 additions & 0 deletions llvm/tools/llvm-profgen/ProfiledBinary.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCTargetOptions.h"
#include "llvm/Object/BuildID.h"
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/ProfileData/SampleProf.h"
#include "llvm/Support/CommandLine.h"
Expand Down Expand Up @@ -337,6 +338,16 @@ class ProfiledBinary {

bool IsCOFF = false;

// Whether the binary has a PT_INTERP program header (PIE executables do,
// true shared libraries don't). Used to distinguish PIE from .so since
// both are ET_DYN.
bool HasInterp = false;

// Build ID used to filter perfscript addresses in [buildid:]addr format.
// For shared libraries, set to the binary's build ID.
// For main executables, kept empty (addresses have no buildid prefix).
std::string FilterBuildID;

void setPreferredTextSegmentAddresses(const object::ObjectFile *O);

// LLVMSymbolizer's symbolize{Code, Data} interfaces requires a section index
Expand Down Expand Up @@ -425,6 +436,9 @@ class ProfiledBinary {

bool isCOFF() const { return IsCOFF; }

// Return the build ID used for filtering perfscript addresses.
StringRef getFilterBuildID() const { return FilterBuildID; }

// Canonicalize to use preferred load address as base address.
uint64_t canonicalizeVirtualAddress(uint64_t Address) {
return Address - BaseAddress + getPreferredBaseAddress();
Expand Down