Skip to content

Commit 7354caa

Browse files
committed
[MC][ELF] Fix printing group signature symbols.
They may be not known. Change-Id: Ie69060a2f7d049a11afa7b5e7c7f132ccb0a592d
1 parent df05512 commit 7354caa

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

llvm/lib/MC/MCSectionELF.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ void MCSectionELF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
191191
}
192192

193193
if (Flags & ELF::SHF_GROUP) {
194-
OS << ",";
195-
printName(OS, Group.getPointer()->getName());
194+
if (const MCSymbolELF *Signature = Group.getPointer()) {
195+
OS << ",";
196+
printName(OS, Signature->getName());
197+
}
196198
if (isComdat())
197199
OS << ",comdat";
198200
}

llvm/unittests/MC/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_llvm_unittest(MCTests
1818
DwarfLineTables.cpp
1919
DwarfLineTableHeaders.cpp
2020
MCInstPrinter.cpp
21+
MCStreamer.cpp
2122
StringTableBuilderTest.cpp
2223
TargetRegistry.cpp
2324
MCDisassemblerTest.cpp

llvm/unittests/MC/MCStreamer.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//===- llvm/unittest/MC/MCStreamer.cpp ------------------------------------===//
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+
#include "llvm/MC/MCStreamer.h"
10+
#include "llvm/MC/MCAsmBackend.h"
11+
#include "llvm/MC/MCAsmInfo.h"
12+
#include "llvm/MC/MCCodeEmitter.h"
13+
#include "llvm/MC/MCContext.h"
14+
#include "llvm/MC/MCInstPrinter.h"
15+
#include "llvm/MC/MCInstrInfo.h"
16+
#include "llvm/MC/MCRegisterInfo.h"
17+
#include "llvm/MC/MCSectionELF.h"
18+
#include "llvm/MC/MCSubtargetInfo.h"
19+
#include "llvm/MC/TargetRegistry.h"
20+
#include "llvm/Support/TargetSelect.h"
21+
#include "gtest/gtest.h"
22+
23+
using namespace llvm;
24+
25+
namespace {
26+
27+
class MCStreamerTest : public ::testing::Test {
28+
public:
29+
static constexpr char TripleName[] = "x86_64-pc-linux";
30+
std::unique_ptr<MCRegisterInfo> MRI;
31+
std::unique_ptr<MCAsmInfo> MAI;
32+
std::unique_ptr<const MCSubtargetInfo> STI;
33+
const Target *TheTarget;
34+
35+
struct StreamerContext {
36+
std::unique_ptr<MCContext> Ctx;
37+
std::unique_ptr<const MCInstrInfo> MII;
38+
std::unique_ptr<MCInstPrinter> Printer;
39+
std::unique_ptr<MCStreamer> Streamer;
40+
};
41+
42+
MCStreamerTest() {
43+
llvm::InitializeAllTargetInfos();
44+
llvm::InitializeAllTargetMCs();
45+
llvm::InitializeAllDisassemblers();
46+
47+
// If we didn't build the target, do not run the test.
48+
std::string Error;
49+
TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
50+
if (!TheTarget)
51+
return;
52+
53+
MRI.reset(TheTarget->createMCRegInfo(TripleName));
54+
MCTargetOptions MCOptions;
55+
MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
56+
STI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
57+
}
58+
59+
/// Create all data structures necessary to operate an assembler.
60+
StreamerContext createStreamer(raw_pwrite_stream &OS) {
61+
StreamerContext Res;
62+
Res.Ctx =
63+
std::make_unique<MCContext>(Triple(TripleName), MAI.get(), MRI.get(),
64+
/*MSTI=*/nullptr);
65+
Res.MII.reset(TheTarget->createMCInstrInfo());
66+
67+
Res.Printer.reset(TheTarget->createMCInstPrinter(
68+
Triple(TripleName), MAI->getAssemblerDialect(), *MAI, *Res.MII, *MRI));
69+
70+
MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*Res.MII, *Res.Ctx);
71+
MCAsmBackend *MAB =
72+
TheTarget->createMCAsmBackend(*STI, *MRI, MCTargetOptions());
73+
std::unique_ptr<formatted_raw_ostream> Out(new formatted_raw_ostream(OS));
74+
Res.Streamer.reset(TheTarget->createAsmStreamer(
75+
*Res.Ctx, std::move(Out), Res.Printer.get(),
76+
std::unique_ptr<MCCodeEmitter>(MCE),
77+
std::unique_ptr<MCAsmBackend>(MAB)));
78+
return Res;
79+
}
80+
};
81+
} // namespace
82+
83+
TEST_F(MCStreamerTest, AnonymousGroup) {
84+
if (!MRI)
85+
GTEST_SKIP();
86+
87+
SmallString<0> Out;
88+
raw_svector_ostream S(Out);
89+
StreamerContext C = createStreamer(S);
90+
91+
// Test that switching to a group section with no associated signature name
92+
// doesn't crash.
93+
C.Streamer->switchSection(
94+
C.Ctx->getELFSection("foo", ELF::SHT_PROGBITS, ELF::SHF_GROUP));
95+
C.Streamer->finish();
96+
}

0 commit comments

Comments
 (0)