Index: lib/CodeGen/AsmPrinter/AsmPrinter.cpp =================================================================== --- lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -2790,10 +2790,10 @@ ELF::SHF_ALLOC | ELF::SHF_GROUP, 0, Fn->getComdat()->getName()); } else { - InstMap = OutContext.getELFSection("xray_instr_map", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC); - FnSledIndex = OutContext.getELFSection("xray_fn_idx", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC); + InstMap = OutContext.getELFSection( + "xray_instr_map", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); + FnSledIndex = OutContext.getELFSection( + "xray_fn_idx", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); } } else if (MF->getSubtarget().getTargetTriple().isOSBinFormatMachO()) { InstMap = OutContext.getMachOSection("__DATA", "xray_instr_map", 0, @@ -2820,20 +2820,39 @@ MCSymbol *SledsStart = OutContext.createTempSymbol("xray_sleds_start", true); OutStreamer->SwitchSection(InstMap); OutStreamer->EmitLabel(SledsStart); + if (MF->getSubtarget().getTargetTriple().isOSBinFormatELF()) { + OutStreamer->EmitSymbolAttribute(SledsStart, MCSymbolAttr::MCSA_Hidden); + OutStreamer->EmitSymbolAttribute(SledsStart, MCSymbolAttr::MCSA_Local); + OutStreamer->EmitSymbolAttribute(SledsStart, + MCSymbolAttr::MCSA_ELF_TypeNoType); + } for (const auto &Sled : Sleds) Sled.emit(WordSizeBytes, OutStreamer.get(), CurrentFnSym); MCSymbol *SledsEnd = OutContext.createTempSymbol("xray_sleds_end", true); OutStreamer->EmitLabel(SledsEnd); + if (MF->getSubtarget().getTargetTriple().isOSBinFormatELF()) { + OutStreamer->EmitSymbolAttribute(SledsEnd, MCSymbolAttr::MCSA_Hidden); + OutStreamer->EmitSymbolAttribute(SledsEnd, MCSymbolAttr::MCSA_Local); + OutStreamer->EmitSymbolAttribute(SledsEnd, + MCSymbolAttr::MCSA_ELF_TypeNoType); + } // We then emit a single entry in the index per function. We use the symbols // that bound the instrumentation map as the range for a specific function. // Each entry here will be 2 * word size aligned, as we're writing down two - // pointers. This should work for both 32-bit and 64-bit platforms. + // pointers. This should work for both 32-bit and 64-bit platforms. The index + // label/symbol we define we mark as "hidden" so they get removed by the + // linker when linking. OutStreamer->SwitchSection(FnSledIndex); OutStreamer->EmitCodeAlignment(2 * WordSizeBytes); OutStreamer->EmitLabel(IdxRef); - OutStreamer->EmitSymbolValue(SledsStart, WordSizeBytes); - OutStreamer->EmitSymbolValue(SledsEnd, WordSizeBytes); + if (MF->getSubtarget().getTargetTriple().isOSBinFormatELF()) { + OutStreamer->EmitSymbolAttribute(IdxRef, MCSymbolAttr::MCSA_Hidden); + OutStreamer->EmitSymbolAttribute(IdxRef, MCSymbolAttr::MCSA_Local); + OutStreamer->EmitSymbolAttribute(IdxRef, MCSymbolAttr::MCSA_ELF_TypeNoType); + } + OutStreamer->EmitSymbolValue(SledsStart, WordSizeBytes, false); + OutStreamer->EmitSymbolValue(SledsEnd, WordSizeBytes, false); OutStreamer->SwitchSection(PrevSection); Sleds.clear(); } Index: lib/Target/X86/X86MCInstLower.cpp =================================================================== --- lib/Target/X86/X86MCInstLower.cpp +++ lib/Target/X86/X86MCInstLower.cpp @@ -1068,6 +1068,12 @@ OutStreamer->AddComment("# XRay Custom Event Log"); OutStreamer->EmitCodeAlignment(2); OutStreamer->EmitLabel(CurSled); + if (MF->getSubtarget().getTargetTriple().isOSBinFormatELF()) { + OutStreamer->EmitSymbolAttribute(CurSled, MCSymbolAttr::MCSA_Hidden); + OutStreamer->EmitSymbolAttribute(CurSled, MCSymbolAttr::MCSA_Local); + OutStreamer->EmitSymbolAttribute(CurSled, + MCSymbolAttr::MCSA_ELF_TypeNoType); + } // Use a two-byte `jmp`. This version of JMP takes an 8-bit relative offset as // an operand (computed as an offset from the jmp instruction). @@ -1125,7 +1131,6 @@ // .Lxray_sled_N: // jmp .tmpN // # 9 bytes worth of noops - // .tmpN // // We need the 9 bytes because at runtime, we'd be patching over the full 11 // bytes with the following pattern: @@ -1136,14 +1141,18 @@ auto CurSled = OutContext.createTempSymbol("xray_sled_", true); OutStreamer->EmitCodeAlignment(2); OutStreamer->EmitLabel(CurSled); - auto Target = OutContext.createTempSymbol(); + if (MF->getSubtarget().getTargetTriple().isOSBinFormatELF()) { + OutStreamer->EmitSymbolAttribute(CurSled, MCSymbolAttr::MCSA_Hidden); + OutStreamer->EmitSymbolAttribute(CurSled, MCSymbolAttr::MCSA_Local); + OutStreamer->EmitSymbolAttribute(CurSled, + MCSymbolAttr::MCSA_ELF_TypeNoType); + } // Use a two-byte `jmp`. This version of JMP takes an 8-bit relative offset as // an operand (computed as an offset from the jmp instruction). // FIXME: Find another less hacky way do force the relative jump. OutStreamer->EmitBytes("\xeb\x09"); EmitNops(*OutStreamer, 9, Subtarget->is64Bit(), getSubtargetInfo()); - OutStreamer->EmitLabel(Target); recordSled(CurSled, MI, SledKind::FUNCTION_ENTER); } @@ -1166,6 +1175,12 @@ auto CurSled = OutContext.createTempSymbol("xray_sled_", true); OutStreamer->EmitCodeAlignment(2); OutStreamer->EmitLabel(CurSled); + if (MF->getSubtarget().getTargetTriple().isOSBinFormatELF()) { + OutStreamer->EmitSymbolAttribute(CurSled, MCSymbolAttr::MCSA_Hidden); + OutStreamer->EmitSymbolAttribute(CurSled, MCSymbolAttr::MCSA_Local); + OutStreamer->EmitSymbolAttribute(CurSled, + MCSymbolAttr::MCSA_ELF_TypeNoType); + } unsigned OpCode = MI.getOperand(0).getImm(); MCInst Ret; Ret.setOpcode(OpCode); Index: test/CodeGen/X86/xray-attribute-instrumentation.ll =================================================================== --- test/CodeGen/X86/xray-attribute-instrumentation.ll +++ test/CodeGen/X86/xray-attribute-instrumentation.ll @@ -1,4 +1,6 @@ ; RUN: llc -filetype=asm -o - -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s +; RUN: llc -filetype=asm -o - -mtriple=x86_64-unknown-linux-gnu \ +; RUN: -relocation-model=pic < %s | FileCheck %s ; RUN: llc -filetype=asm -o - -mtriple=x86_64-darwin-unknown < %s | FileCheck %s define i32 @foo() nounwind noinline uwtable "function-instrument"="xray-always" {