Skip to content

Commit 4b1254e

Browse files
[AIX] In assembly file, create a dummy text renamed to an empty string (#73052)
This works around an AIX assembler and linker bug. If the -fno-integrated-as and -frecord-command-line options are used but there's no actual code in the source file, the assembler creates an object file with only an .info section. The AIX linker rejects such an object file.
1 parent 3d718d0 commit 4b1254e

21 files changed

+68
-39
lines changed

llvm/include/llvm/MC/MCSectionXCOFF.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class MCSectionXCOFF final : public MCSection {
115115
bool useCodeAlign() const override;
116116
bool isVirtualSection() const override;
117117
StringRef getSymbolTableName() const { return SymbolTableName; }
118+
void setSymbolTableName(StringRef STN) { SymbolTableName = STN; }
118119
bool isMultiSymbolsAllowed() const { return MultiSymbolsAllowed; }
119120
bool isCsect() const { return CsectProp.has_value(); }
120121
bool isDwarfSect() const { return DwarfSubtypeFlags.has_value(); }

llvm/include/llvm/MC/MCSymbolXCOFF.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ class MCSymbolXCOFF : public MCSymbol {
5252

5353
XCOFF::VisibilityType getVisibilityType() const { return VisibilityType; }
5454

55-
bool hasRename() const { return !SymbolTableName.empty(); }
55+
bool hasRename() const { return HasRename; }
5656

57-
void setSymbolTableName(StringRef STN) { SymbolTableName = STN; }
57+
void setSymbolTableName(StringRef STN) {
58+
SymbolTableName = STN;
59+
HasRename = true;
60+
}
5861

5962
StringRef getSymbolTableName() const {
6063
if (hasRename())
@@ -67,6 +70,7 @@ class MCSymbolXCOFF : public MCSymbol {
6770
MCSectionXCOFF *RepresentedCsect = nullptr;
6871
XCOFF::VisibilityType VisibilityType = XCOFF::SYM_V_UNSPECIFIED;
6972
StringRef SymbolTableName;
73+
bool HasRename = false;
7074
};
7175

7276
} // end namespace llvm

llvm/include/llvm/MC/MCXCOFFStreamer.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ class MCXCOFFStreamer : public MCObjectStreamer {
3333
MCSymbolAttr Visibility) override;
3434
void emitXCOFFRefDirective(const MCSymbol *Symbol) override;
3535
void emitXCOFFRenameDirective(const MCSymbol *Name,
36-
StringRef Rename) override {
37-
report_fatal_error("emitXCOFFRenameDirective is not implemented yet on "
38-
"object generation path");
39-
}
36+
StringRef Rename) override;
4037
void emitXCOFFExceptDirective(const MCSymbol *Symbol, const MCSymbol *Trap,
4138
unsigned Lang, unsigned Reason,
4239
unsigned FunctionSize, bool hasDebug) override;

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#include "llvm/MC/MCSectionCOFF.h"
9494
#include "llvm/MC/MCSectionELF.h"
9595
#include "llvm/MC/MCSectionMachO.h"
96+
#include "llvm/MC/MCSectionXCOFF.h"
9697
#include "llvm/MC/MCStreamer.h"
9798
#include "llvm/MC/MCSubtargetInfo.h"
9899
#include "llvm/MC/MCSymbol.h"
@@ -444,7 +445,7 @@ bool AsmPrinter::doInitialization(Module &M) {
444445
.getModuleMetadata(M);
445446

446447
// On AIX, we delay emitting any section information until
447-
// after emitting the .file pseudo-op. This allows additional
448+
// after emitting the .file pseudo-op. This allows additional
448449
// information (such as the embedded command line) to be associated
449450
// with all sections in the object file rather than a single section.
450451
if (!TM.getTargetTriple().isOSBinFormatXCOFF())
@@ -496,8 +497,18 @@ bool AsmPrinter::doInitialization(Module &M) {
496497
// C_INFO symbol is preserved if any csect is kept by the linker.
497498
if (TM.getTargetTriple().isOSBinFormatXCOFF()) {
498499
emitModuleCommandLines(M);
499-
// Now we can generate section information
500+
// Now we can generate section information.
500501
OutStreamer->initSections(false, *TM.getMCSubtargetInfo());
502+
503+
// To work around an AIX assembler and/or linker bug, generate
504+
// a rename for the default text-section symbol name. This call has
505+
// no effect when generating object code directly.
506+
MCSection *TextSection =
507+
OutStreamer->getContext().getObjectFileInfo()->getTextSection();
508+
MCSymbolXCOFF *XSym =
509+
static_cast<MCSectionXCOFF *>(TextSection)->getQualNameSymbol();
510+
if (XSym->hasRename())
511+
OutStreamer->emitXCOFFRenameDirective(XSym, XSym->getSymbolTableName());
501512
}
502513

503514
GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();

llvm/lib/MC/MCObjectFileInfo.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,10 +933,16 @@ void MCObjectFileInfo::initXCOFFMCObjectFileInfo(const Triple &T) {
933933
// the ABI or object file format, but various tools rely on the section
934934
// name being empty (considering named symbols to be "user symbol names").
935935
TextSection = Ctx->getXCOFFSection(
936-
"", SectionKind::getText(),
936+
"..text..", // Use a non-null name to work around an AIX assembler bug...
937+
SectionKind::getText(),
937938
XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_PR, XCOFF::XTY_SD),
938939
/* MultiSymbolsAllowed*/ true);
939940

941+
// ... but use a null name when generating the symbol table.
942+
MCSectionXCOFF *TS = static_cast<MCSectionXCOFF *>(TextSection);
943+
TS->getQualNameSymbol()->setSymbolTableName("");
944+
TS->setSymbolTableName("");
945+
940946
DataSection = Ctx->getXCOFFSection(
941947
".data", SectionKind::getData(),
942948
XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RW, XCOFF::XTY_SD),

llvm/lib/MC/MCStreamer.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,10 +1190,7 @@ void MCStreamer::emitXCOFFSymbolLinkageWithVisibility(MCSymbol *Symbol,
11901190
}
11911191

11921192
void MCStreamer::emitXCOFFRenameDirective(const MCSymbol *Name,
1193-
StringRef Rename) {
1194-
llvm_unreachable("emitXCOFFRenameDirective is only supported on "
1195-
"XCOFF targets");
1196-
}
1193+
StringRef Rename) {}
11971194

11981195
void MCStreamer::emitXCOFFRefDirective(const MCSymbol *Symbol) {
11991196
llvm_unreachable("emitXCOFFRefDirective is only supported on XCOFF targets");

llvm/lib/MC/MCXCOFFStreamer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ void MCXCOFFStreamer::emitXCOFFRefDirective(const MCSymbol *Symbol) {
9696
DF->getFixups().push_back(Fixup);
9797
}
9898

99+
void MCXCOFFStreamer::emitXCOFFRenameDirective(const MCSymbol *Name,
100+
StringRef Rename) {
101+
const MCSymbolXCOFF *Symbol = cast<const MCSymbolXCOFF>(Name);
102+
if (!Symbol->hasRename())
103+
report_fatal_error("Only explicit .rename is supported for XCOFF.");
104+
}
105+
99106
void MCXCOFFStreamer::emitXCOFFExceptDirective(const MCSymbol *Symbol,
100107
const MCSymbol *Trap,
101108
unsigned Lang, unsigned Reason,

llvm/test/CodeGen/PowerPC/aix-alias.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ entry:
5555
; ASM-NEXT: .csect fun[DS]
5656
; ASM-NEXT: fun_weak: # @fun
5757
; ASM-NEXT: fun_hidden:
58-
; ASM: .csect [PR],5
58+
; ASM: .csect ..text..[PR],5
5959
; ASM-NEXT: .fun:
6060
; ASM-NEXT: .fun_weak:
6161
; ASM-NEXT: .fun_hidden:
6262
; ASM-NEXT: # %bb.0:
6363
; ASM-NEXT: li 3, 0
6464
; ASM-NEXT: blr
6565
; ASM-NEXT: # -- End function
66-
; ASM: .csect [PR],5
66+
; ASM: .csect ..text..[PR],5
6767
; ASM-NEXT: .test:
6868
; ASM-NEXT: # %bb.0: # %entry
6969
; ASM: bl .fun

llvm/test/CodeGen/PowerPC/aix-emit-tracebacktable-clobber-register.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ entry:
9999
; COMMON-NEXT: .align 2
100100
; COMMON-NEXT: .vbyte 4, 0
101101
; COMMON-NEXT: .vbyte 4, 0
102-
; CHECK-ASM-NEXT: .csect [PR],5
102+
; CHECK-ASM-NEXT: .csect ..text..[PR],5
103103
; CHECK-FUNC-NEXT: .csect .foov[PR],5
104104
; COMMON-NEXT: # -- End function
105105
; COMMON: .toc

llvm/test/CodeGen/PowerPC/aix-extern-weak.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ declare extern_weak void @foo_ext_weak(ptr)
4141
; BIT64-NEXT: .vbyte 8, .main # @main
4242
; BIT64-NEXT: .vbyte 8, TOC[TC0]
4343
; BIT64-NEXT: .vbyte 8, 0
44-
; COMMON-NEXT: .csect [PR]
44+
; COMMON-NEXT: .csect ..text..[PR]
4545
; COMMON-NEXT: .main:
4646

4747
; COMMON: .csect .data[RW]

0 commit comments

Comments
 (0)