Skip to content

Commit 7b23f41

Browse files
committed
MCAsmStreamer: Omit initial ".text"
llvm-mc --assemble prints an initial `.text` from `initSections`. This is weird for quick assembly tasks that do not specify `.text`. Omit the .text by moving section directive printing from `changeSection` to `switchSection`. switchSectionNoPrint now correctly calls the `changeSection` hook (needed by MachO). The initial directives of clang -S are now reordered. On ELF targets, we get `.file "a.c"; .text` instead of `.text; .file "a.c"`. If there is no function, `.text` will be omitted.
1 parent 158a600 commit 7b23f41

File tree

11 files changed

+45
-23
lines changed

11 files changed

+45
-23
lines changed

Diff for: llvm/include/llvm/MC/MCStreamer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ class MCStreamer {
424424
/// Calls changeSection as needed.
425425
///
426426
/// Returns false if the stack was empty.
427-
bool popSection();
427+
virtual bool popSection();
428428

429429
/// Set the current section where code is being emitted to \p Section. This
430430
/// is required to update CurSection.

Diff for: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,8 @@ bool AsmPrinter::doInitialization(Module &M) {
531531
if (TM.getTargetTriple().isOSBinFormatXCOFF()) {
532532
emitModuleCommandLines(M);
533533
// Now we can generate section information.
534-
OutStreamer->initSections(false, *TM.getMCSubtargetInfo());
534+
OutStreamer->switchSection(
535+
OutContext.getObjectFileInfo()->getTextSection());
535536

536537
// To work around an AIX assembler and/or linker bug, generate
537538
// a rename for the default text-section symbol name. This call has

Diff for: llvm/lib/MC/MCAsmStreamer.cpp

+25-9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class MCAsmStreamer final : public MCStreamer {
5555
raw_svector_ostream CommentStream;
5656
raw_null_ostream NullStream;
5757

58+
bool EmittedSectionDirective = false;
59+
5860
bool IsVerboseAsm = false;
5961
bool ShowInst = false;
6062
bool UseDwarfDirectory = false;
@@ -160,7 +162,8 @@ class MCAsmStreamer final : public MCStreamer {
160162
/// @name MCStreamer Interface
161163
/// @{
162164

163-
void changeSection(MCSection *Section, uint32_t Subsection) override;
165+
void switchSection(MCSection *Section, uint32_t Subsection) override;
166+
bool popSection() override;
164167

165168
void emitELFSymverDirective(const MCSymbol *OriginalSym, StringRef Name,
166169
bool KeepOriginalSym) override;
@@ -532,14 +535,27 @@ void MCAsmStreamer::emitExplicitComments() {
532535
ExplicitCommentToEmit.clear();
533536
}
534537

535-
void MCAsmStreamer::changeSection(MCSection *Section, uint32_t Subsection) {
536-
if (MCTargetStreamer *TS = getTargetStreamer()) {
537-
TS->changeSection(getCurrentSection().first, Section, Subsection, OS);
538-
} else {
539-
Section->printSwitchToSection(*MAI, getContext().getTargetTriple(), OS,
540-
Subsection);
538+
void MCAsmStreamer::switchSection(MCSection *Section, uint32_t Subsection) {
539+
MCSectionSubPair Cur = getCurrentSection();
540+
if (!EmittedSectionDirective ||
541+
MCSectionSubPair(Section, Subsection) != Cur) {
542+
EmittedSectionDirective = true;
543+
if (MCTargetStreamer *TS = getTargetStreamer()) {
544+
TS->changeSection(Cur.first, Section, Subsection, OS);
545+
} else {
546+
Section->printSwitchToSection(*MAI, getContext().getTargetTriple(), OS,
547+
Subsection);
548+
}
541549
}
542-
MCStreamer::changeSection(Section, Subsection);
550+
MCStreamer::switchSection(Section, Subsection);
551+
}
552+
553+
bool MCAsmStreamer::popSection() {
554+
if (!MCStreamer::popSection())
555+
return false;
556+
auto [Sec, Subsec] = getCurrentSection();
557+
Sec->printSwitchToSection(*MAI, getContext().getTargetTriple(), OS, Subsec);
558+
return true;
543559
}
544560

545561
void MCAsmStreamer::emitELFSymverDirective(const MCSymbol *OriginalSym,
@@ -2543,7 +2559,7 @@ void MCAsmStreamer::finishImpl() {
25432559
if (!Tables.empty()) {
25442560
assert(Tables.size() == 1 && "asm output only supports one line table");
25452561
if (auto *Label = Tables.begin()->second.getLabel()) {
2546-
switchSection(getContext().getObjectFileInfo()->getDwarfLineSection());
2562+
switchSection(getContext().getObjectFileInfo()->getDwarfLineSection(), 0);
25472563
emitLabel(Label);
25482564
}
25492565
}

Diff for: llvm/lib/MC/MCStreamer.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ void MCStreamer::emitEHSymAttributes(const MCSymbol *Symbol,
414414
}
415415

416416
void MCStreamer::initSections(bool NoExecStack, const MCSubtargetInfo &STI) {
417-
switchSection(getContext().getObjectFileInfo()->getTextSection());
417+
switchSectionNoPrint(getContext().getObjectFileInfo()->getTextSection());
418418
}
419419

420420
void MCStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
@@ -1332,7 +1332,10 @@ bool MCStreamer::switchSection(MCSection *Section, const MCExpr *SubsecExpr) {
13321332
void MCStreamer::switchSectionNoPrint(MCSection *Section) {
13331333
SectionStack.back().second = SectionStack.back().first;
13341334
SectionStack.back().first = MCSectionSubPair(Section, 0);
1335-
CurFrag = &Section->getDummyFragment();
1335+
changeSection(Section, 0);
1336+
MCSymbol *Sym = Section->getBeginSymbol();
1337+
if (Sym && !Sym->isInSection())
1338+
emitLabel(Sym);
13361339
}
13371340

13381341
MCSymbol *MCStreamer::endSection(MCSection *Section) {

Diff for: llvm/test/CodeGen/PowerPC/check-cpu.ll

-2
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,3 @@
2020
; Test -mcpu=[pwr9|pwr10|pwr11|future] is recognized on PowerPC.
2121

2222
; CHECK-NOT: is not a recognized processor for this target
23-
; CHECK: .text
24-

Diff for: llvm/test/CodeGen/X86/coff-alias-type.ll

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ entry:
1313
; CHECK-NEXT: .scl 2
1414
; CHECK-NEXT: .type 32
1515
; CHECK-NEXT: .endef
16+
; CHECK-NEXT: .text
1617
; CHECK-NEXT: .globl _ZN8MyStructC2Ev
1718
; CHECK: {{^}}_ZN8MyStructC2Ev:
1819

Diff for: llvm/test/DebugInfo/ARM/header.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
; This is particularly important on ARM MachO as a change in section order can
55
; cause a change the relaxation of the instructions used.
66

7-
; CHECK: .section __TEXT,__text,regular,pure_instructions
8-
; CHECK-NEXT: .syntax unified
7+
; CHECK: .syntax unified
8+
; CHECK-NEXT: .section __TEXT,__text,regular,pure_instructions
99
; CHECK-NEXT: .globl _f
1010
; CHECK-NEXT: .p2align 2
1111
; CHECK-NEXT: .code 32 @ @f

Diff for: llvm/test/DebugInfo/X86/header.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
; Test that we don't pollute the start of the file with debug sections
44

5-
; CHECK: .text
6-
; CHECK-NEXT: .file "<stdin>"
5+
; CHECK: .file "<stdin>"
6+
; CHECK-NEXT: .text
77
; CHECK-NEXT: .globl f
88
; CHECK-NEXT: .p2align 4
99
; CHECK-NEXT: .type f,@function

Diff for: llvm/test/MC/AArch64/armv8.3a-signed-pointer.s

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// RUN: FileCheck --check-prefixes=NO83,ALL %s < %t.1
66
// RUN: FileCheck --check-prefix=CHECK-REQ %s < %t.2
77

8-
// ALL: .text
98
.text
109
mrs x0, apiakeylo_el1
1110
mrs x0, apiakeyhi_el1
@@ -17,8 +16,7 @@
1716
mrs x0, apdbkeyhi_el1
1817
mrs x0, apgakeylo_el1
1918
mrs x0, apgakeyhi_el1
20-
// ALL-EMPTY:
21-
// ALL-EMPTY:
19+
// ALL: .text
2220
// CHECK-NEXT: mrs x0, APIAKeyLo_EL1 // encoding: [0x00,0x21,0x38,0xd5]
2321
// CHECK-NEXT: mrs x0, APIAKeyHi_EL1 // encoding: [0x20,0x21,0x38,0xd5]
2422
// CHECK-NEXT: mrs x0, APIBKeyLo_EL1 // encoding: [0x40,0x21,0x38,0xd5]

Diff for: llvm/test/MC/ELF/section.s

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | llvm-readobj -S - | FileCheck %s
22
// RUN: llvm-mc -filetype=asm -triple x86_64-pc-linux-gnu %s -o - | FileCheck %s --check-prefix=ASM
33

4+
/// The second .text has no effect, therefore it is not printed.
5+
// ASM: .text
6+
// ASM-EMPTY:
7+
.text
8+
.text
9+
410
// Test that these names are accepted.
511

612
.section .note.GNU-stack,"",@progbits

Diff for: llvm/test/MC/Hexagon/hexagon_attributes.s

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ v1:0.sf=vadd(v0.bf,v0.bf) // hvxv73, hvx-ieee-fp
7878
// ASM-NEXT: .attribute 8, 1 // Tag_zreg
7979
// ASM-NEXT: .attribute 9, 1 // Tag_audio
8080
// ASM-NEXT: .attribute 10, 1 // Tag_cabac
81-
// ASM-NEXT: .text
8281
// ASM-EMPTY:
8382
// ASM-NEXT: {
8483
// ASM-NEXT: q0 &= vcmp.gt(v0.bf,v0.bf)

0 commit comments

Comments
 (0)