Skip to content

Commit b1acb7a

Browse files
[XCOFF] Add compiler version to an auxiliary symbol table entry (#80162)
C_FILE symbols. To match the behavior of the assembler and the legacy compiler, this includes using the generic ".file" name for the C_FILE symbol and generating the actual file name in an auxiliary entry.
1 parent 364f781 commit b1acb7a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1043
-936
lines changed

llvm/include/llvm/BinaryFormat/XCOFF.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace XCOFF {
2727

2828
constexpr size_t FileNamePadSize = 6;
2929
constexpr size_t NameSize = 8;
30+
constexpr size_t AuxFileEntNameSize = 14;
3031
constexpr size_t FileHeaderSize32 = 20;
3132
constexpr size_t FileHeaderSize64 = 24;
3233
constexpr size_t AuxFileHeaderSize32 = 72;

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ class MCAssembler {
133133

134134
/// List of declared file names
135135
std::vector<std::pair<std::string, size_t>> FileNames;
136+
// Optional compiler version.
137+
std::string CompilerVersion;
136138

137139
MCDwarfLineTableParams LTParams;
138140

@@ -486,6 +488,12 @@ class MCAssembler {
486488
FileNames.emplace_back(std::string(FileName), Symbols.size());
487489
}
488490

491+
void setCompilerVersion(std::string CompilerVers) {
492+
if (CompilerVersion.empty())
493+
CompilerVersion = std::move(CompilerVers);
494+
}
495+
StringRef getCompilerVersion() { return CompilerVersion; }
496+
489497
/// Write the necessary bundle padding to \p OS.
490498
/// Expects a fragment \p F containing instructions and its size \p FSize.
491499
void writeFragmentPadding(raw_ostream &OS, const MCEncodedFragment &F,

llvm/lib/MC/MCAsmStreamer.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,17 +1590,22 @@ void MCAsmStreamer::emitFileDirective(StringRef Filename,
15901590
assert(MAI->hasFourStringsDotFile());
15911591
OS << "\t.file\t";
15921592
PrintQuotedString(Filename, OS);
1593-
OS << ",";
1594-
if (!CompilerVersion.empty()) {
1595-
PrintQuotedString(CompilerVersion, OS);
1596-
}
1597-
if (!TimeStamp.empty()) {
1598-
OS << ",";
1599-
PrintQuotedString(TimeStamp, OS);
1600-
}
1601-
if (!Description.empty()) {
1593+
bool useTimeStamp = !TimeStamp.empty();
1594+
bool useCompilerVersion = !CompilerVersion.empty();
1595+
bool useDescription = !Description.empty();
1596+
if (useTimeStamp || useCompilerVersion || useDescription) {
16021597
OS << ",";
1603-
PrintQuotedString(Description, OS);
1598+
if (useTimeStamp)
1599+
PrintQuotedString(TimeStamp, OS);
1600+
if (useCompilerVersion || useDescription) {
1601+
OS << ",";
1602+
if (useCompilerVersion)
1603+
PrintQuotedString(CompilerVersion, OS);
1604+
if (useDescription) {
1605+
OS << ",";
1606+
PrintQuotedString(Description, OS);
1607+
}
1608+
}
16041609
}
16051610
EmitEOL();
16061611
}

llvm/lib/MC/MCObjectStreamer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,9 @@ void MCObjectStreamer::emitFileDirective(StringRef Filename,
902902
StringRef TimeStamp,
903903
StringRef Description) {
904904
getAssembler().addFileName(Filename);
905-
// TODO: add additional info to integrated assembler.
905+
getAssembler().setCompilerVersion(CompilerVerion.str());
906+
// TODO: add TimeStamp and Description to .file symbol table entry
907+
// with the integrated assembler.
906908
}
907909

908910
void MCObjectStreamer::emitAddrsig() {

llvm/lib/MC/XCOFFObjectWriter.cpp

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ class XCOFFObjectWriter : public MCObjectWriter {
361361
bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
362362
bool nameShouldBeInStringTable(const StringRef &);
363363
void writeSymbolName(const StringRef &);
364+
bool auxFileSymNameShouldBeInStringTable(const StringRef &);
365+
void writeAuxFileSymName(const StringRef &);
364366

365367
void writeSymbolEntryForCsectMemberLabel(const Symbol &SymbolRef,
366368
const XCOFFSection &CSectionRef,
@@ -391,7 +393,8 @@ class XCOFFObjectWriter : public MCObjectWriter {
391393
const MCAsmLayout &Layout,
392394
CInfoSymSectionEntry &CInfoSymEntry,
393395
uint64_t &CurrentAddressLocation);
394-
void writeSymbolTable(const MCAsmLayout &Layout);
396+
void writeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout);
397+
void writeSymbolAuxFileEntry(StringRef &Name, uint8_t ftype);
395398
void writeSymbolAuxDwarfEntry(uint64_t LengthOfSectionPortion,
396399
uint64_t NumberOfRelocEnt = 0);
397400
void writeSymbolAuxCsectEntry(uint64_t SectionOrLength,
@@ -416,7 +419,7 @@ class XCOFFObjectWriter : public MCObjectWriter {
416419
// *) Assigns symbol table indices.
417420
// *) Builds up the section header table by adding any non-empty sections to
418421
// `Sections`.
419-
void assignAddressesAndIndices(const MCAsmLayout &);
422+
void assignAddressesAndIndices(MCAssembler &Asm, const MCAsmLayout &);
420423
// Called after relocations are recorded.
421424
void finalizeSectionInfo();
422425
void finalizeRelocationInfo(SectionEntry *Sec, uint64_t RelCount);
@@ -640,12 +643,20 @@ void XCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
640643
if (FileNames.empty())
641644
FileNames.emplace_back(".file", 0);
642645
for (const std::pair<std::string, size_t> &F : FileNames) {
643-
if (nameShouldBeInStringTable(F.first))
646+
if (auxFileSymNameShouldBeInStringTable(F.first))
644647
Strings.add(F.first);
645648
}
646649

650+
// Always add ".file" to the symbol table. The actual file name will be in
651+
// the AUX_FILE auxiliary entry.
652+
if (nameShouldBeInStringTable(".file"))
653+
Strings.add(".file");
654+
StringRef Vers = Asm.getCompilerVersion();
655+
if (auxFileSymNameShouldBeInStringTable(Vers))
656+
Strings.add(Vers);
657+
647658
Strings.finalize();
648-
assignAddressesAndIndices(Layout);
659+
assignAddressesAndIndices(Asm, Layout);
649660
}
650661

651662
void XCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
@@ -818,7 +829,7 @@ uint64_t XCOFFObjectWriter::writeObject(MCAssembler &Asm,
818829
writeSectionHeaderTable();
819830
writeSections(Asm, Layout);
820831
writeRelocations();
821-
writeSymbolTable(Layout);
832+
writeSymbolTable(Asm, Layout);
822833
// Write the string table.
823834
Strings.write(W.OS);
824835

@@ -878,6 +889,36 @@ void XCOFFObjectWriter::writeSymbolAuxCsectEntry(uint64_t SectionOrLength,
878889
}
879890
}
880891

892+
bool XCOFFObjectWriter::auxFileSymNameShouldBeInStringTable(
893+
const StringRef &SymbolName) {
894+
return SymbolName.size() > XCOFF::AuxFileEntNameSize;
895+
}
896+
897+
void XCOFFObjectWriter::writeAuxFileSymName(const StringRef &SymbolName) {
898+
// Magic, Offset or SymbolName.
899+
if (auxFileSymNameShouldBeInStringTable(SymbolName)) {
900+
W.write<int32_t>(0);
901+
W.write<uint32_t>(Strings.getOffset(SymbolName));
902+
W.OS.write_zeros(XCOFF::FileNamePadSize);
903+
} else {
904+
char Name[XCOFF::AuxFileEntNameSize + 1];
905+
std::strncpy(Name, SymbolName.data(), XCOFF::AuxFileEntNameSize);
906+
ArrayRef<char> NameRef(Name, XCOFF::AuxFileEntNameSize);
907+
W.write(NameRef);
908+
}
909+
}
910+
911+
void XCOFFObjectWriter::writeSymbolAuxFileEntry(StringRef &Name,
912+
uint8_t ftype) {
913+
writeAuxFileSymName(Name);
914+
W.write<uint8_t>(ftype);
915+
W.OS.write_zeros(2);
916+
if (is64Bit())
917+
W.write<uint8_t>(XCOFF::AUX_FILE);
918+
else
919+
W.OS.write_zeros(1);
920+
}
921+
881922
void XCOFFObjectWriter::writeSymbolAuxDwarfEntry(
882923
uint64_t LengthOfSectionPortion, uint64_t NumberOfRelocEnt) {
883924
writeWord(LengthOfSectionPortion);
@@ -1109,8 +1150,11 @@ void XCOFFObjectWriter::writeRelocations() {
11091150
writeRelocation(Reloc, *DwarfSection.DwarfSect);
11101151
}
11111152

1112-
void XCOFFObjectWriter::writeSymbolTable(const MCAsmLayout &Layout) {
1153+
void XCOFFObjectWriter::writeSymbolTable(MCAssembler &Asm,
1154+
const MCAsmLayout &Layout) {
11131155
// Write C_FILE symbols.
1156+
StringRef Vers = Asm.getCompilerVersion();
1157+
11141158
for (const std::pair<std::string, size_t> &F : FileNames) {
11151159
// The n_name of a C_FILE symbol is the source file's name when no auxiliary
11161160
// entries are present.
@@ -1139,9 +1183,15 @@ void XCOFFObjectWriter::writeSymbolTable(const MCAsmLayout &Layout) {
11391183
else
11401184
CpuID = XCOFF::TCPU_COM;
11411185

1142-
writeSymbolEntry(FileName, /*Value=*/0, XCOFF::ReservedSectionNum::N_DEBUG,
1186+
int NumberOfFileAuxEntries = 1;
1187+
if (!Vers.empty())
1188+
++NumberOfFileAuxEntries;
1189+
writeSymbolEntry(".file", /*Value=*/0, XCOFF::ReservedSectionNum::N_DEBUG,
11431190
/*SymbolType=*/(LangID << 8) | CpuID, XCOFF::C_FILE,
1144-
/*NumberOfAuxEntries=*/0);
1191+
NumberOfFileAuxEntries);
1192+
writeSymbolAuxFileEntry(FileName, XCOFF::XFT_FN);
1193+
if (!Vers.empty())
1194+
writeSymbolAuxFileEntry(Vers, XCOFF::XFT_CV);
11451195
}
11461196

11471197
if (CInfoSymSection.Entry)
@@ -1357,9 +1407,12 @@ void XCOFFObjectWriter::addCInfoSymEntry(StringRef Name, StringRef Metadata) {
13571407
std::make_unique<CInfoSymInfo>(Name.str(), Metadata.str()));
13581408
}
13591409

1360-
void XCOFFObjectWriter::assignAddressesAndIndices(const MCAsmLayout &Layout) {
1361-
// The symbol table starts with all the C_FILE symbols.
1362-
uint32_t SymbolTableIndex = FileNames.size();
1410+
void XCOFFObjectWriter::assignAddressesAndIndices(MCAssembler &Asm,
1411+
const MCAsmLayout &Layout) {
1412+
// The symbol table starts with all the C_FILE symbols. Each C_FILE symbol
1413+
// requires 1 or 2 auxiliary entries.
1414+
uint32_t SymbolTableIndex =
1415+
(2 + (Asm.getCompilerVersion().empty() ? 0 : 1)) * FileNames.size();
13631416

13641417
if (CInfoSymSection.Entry)
13651418
SymbolTableIndex++;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ define void @foo3(%struct.B %a1) {
5858
; ASM-NEXT: .vbyte 4, 34
5959

6060
; SYM: SYMBOL TABLE:
61-
; SYM-NEXT: 00000000 df *DEBUG* 00000000 <stdin>
61+
; SYM-NEXT: 00000000 df *DEBUG* 00000000 .file
6262
; SYM-NEXT: 00000000 l .text 0000008a
6363
; SYM-NEXT: 00000000 g F .text (csect: ) 00000000 .foo1
6464
; SYM-NEXT: 00000030 g F .text (csect: ) 00000000 .foo2

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ define void @foo(i32 %a1, i32 %a2, i32 %a3) {
6161
; OBJ-NEXT: c: 4e 80 00 20 blr
6262

6363
; SYM: SYMBOL TABLE:
64-
; SYM-NEXT: 00000000 df *DEBUG* 00000000 <stdin>
64+
; SYM-NEXT: 00000000 df *DEBUG* 00000000 .file
6565
; SYM-NEXT: 00000000 l .text 00000029
6666
; SYM-NEXT: 00000000 g F .text (csect: ) 00000000 .foo
6767
; SYM-NEXT: 0000002c l .data 00000008 .data

llvm/test/CodeGen/PowerPC/aix-available-externally-linkage-fun.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
; RUN: llc -verify-machineinstrs -mtriple powerpc-ibm-aix-xcoff -mcpu=pwr4 \
88
; RUN: -mattr=-altivec -filetype=obj -o %t.o < %s
9-
; RUN: llvm-readobj --symbols %t.o | FileCheck --check-prefix=OBJ %s
9+
; RUN: llvm-readobj --symbols %t.o | FileCheck -D#NFA=2 --check-prefix=OBJ %s
1010

1111
; RUN: llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff -mcpu=pwr4 \
1212
; RUN: -mattr=-altivec -filetype=obj -o %t64.o < %s
13-
; RUN: llvm-readobj --symbols %t64.o | FileCheck --check-prefix=OBJ %s
13+
; RUN: llvm-readobj --symbols %t64.o | FileCheck -D#NFA=2 --check-prefix=OBJ %s
1414

1515
define available_externally i32 @foo(i32 %a) {
1616
entry:
@@ -27,7 +27,7 @@ entry:
2727
; OBJ-NEXT: StorageClass: C_EXT (0x2)
2828
; OBJ-NEXT: NumberOfAuxEntries: 1
2929
; OBJ-NEXT: CSECT Auxiliary Entry {
30-
; OBJ-NEXT: Index: 2
30+
; OBJ-NEXT: Index: [[#NFA+2]]
3131
; OBJ-NEXT: SectionLen: 0
3232
; OBJ-NEXT: ParameterHashIndex: 0x0
3333
; OBJ-NEXT: TypeChkSectNum: 0x0
@@ -42,7 +42,7 @@ entry:
4242
; OBJ-NEXT: StorageClass: C_EXT (0x2)
4343
; OBJ-NEXT: NumberOfAuxEntries: 1
4444
; OBJ-NEXT: CSECT Auxiliary Entry {
45-
; OBJ-NEXT: Index: 4
45+
; OBJ-NEXT: Index: [[#NFA+4]]
4646
; OBJ-NEXT: SectionLen: 0
4747
; OBJ-NEXT: ParameterHashIndex: 0x0
4848
; OBJ-NEXT: TypeChkSectNum: 0x0

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,15 @@ declare extern_weak void @foo_ext_weak(ptr)
6565
; CHECKSYM: Symbols [
6666
; CHECKSYM-NEXT: Symbol {
6767
; CHECKSYM-NEXT: Index: 0
68-
; CHECKSYM-NEXT: Name: <stdin>
68+
; CHECKSYM-NEXT: Name: .file
6969
; CHECKSYM-NEXT: Value (SymbolTableIndex): 0x0
7070
; CHECKSYM-NEXT: Section: N_DEBUG
7171
; CHECKSYM-NEXT: Source Language ID: TB_CPLUSPLUS (0x9)
7272
; CHECKSYM32-NEXT: CPU Version ID: TCPU_COM (0x3)
7373
; CHECKSYM64-NEXT: CPU Version ID: TCPU_PPC64 (0x2)
7474
; CHECKSYM-NEXT: StorageClass: C_FILE (0x67)
75-
; CHECKSYM-NEXT: NumberOfAuxEntries: 0
76-
; CHECKSYM-NEXT: }
77-
; CHECKSYM-NEXT: Symbol {
75+
; CHECKSYM-NEXT: NumberOfAuxEntries: 2
76+
; CHECKSYM: Symbol {
7877
; CHECKSYM-NEXT: Index: [[#Index:]]
7978
; CHECKSYM-NEXT: Name: .foo_ext_weak
8079
; CHECKSYM-NEXT: Value (RelocatableAddress): 0x0

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,15 @@ declare i32 @bar_extern(ptr)
8888
; CHECKSYM: Symbols [
8989
; CHECKSYM-NEXT: Symbol {
9090
; CHECKSYM-NEXT: Index: 0
91-
; CHECKSYM-NEXT: Name: <stdin>
91+
; CHECKSYM-NEXT: Name: .file
9292
; CHECKSYM-NEXT: Value (SymbolTableIndex): 0x0
9393
; CHECKSYM-NEXT: Section: N_DEBUG
9494
; CHECKSYM-NEXT: Source Language ID: TB_CPLUSPLUS (0x9)
9595
; CHECKSYM32-NEXT: CPU Version ID: TCPU_COM (0x3)
9696
; CHECKSYM64-NEXT: CPU Version ID: TCPU_PPC64 (0x2)
9797
; CHECKSYM-NEXT: StorageClass: C_FILE (0x67)
98-
; CHECKSYM-NEXT: NumberOfAuxEntries: 0
99-
; CHECKSYM-NEXT: }
100-
; CHECKSYM-NEXT: Symbol {
98+
; CHECKSYM-NEXT: NumberOfAuxEntries: 2
99+
; CHECKSYM: Symbol {
101100
; CHECKSYM-NEXT: Index: [[#Index:]]
102101
; CHECKSYM-NEXT: Name: .bar_extern
103102
; CHECKSYM-NEXT: Value (RelocatableAddress): 0x0

llvm/test/CodeGen/PowerPC/aix-filename-c.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
source_filename = "1.c"
77

8-
; OBJ: Name: 1.c
8+
; OBJ: Name: .file
99
; OBJ: Source Language ID: TB_C (0x0)
1010
; OBJ32: CPU Version ID: TCPU_COM (0x3)
1111
; OBJ64: CPU Version ID: TCPU_PPC64 (0x2)
12+
; OBJ: Name: 1.c

llvm/test/CodeGen/PowerPC/aix-filename-cpp.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
source_filename = "1.cpp"
77

8-
; OBJ: Name: 1.cpp
8+
; OBJ: Name: .file
99
; OBJ: Source Language ID: TB_CPLUSPLUS (0x9)
1010
; OBJ32: CPU Version ID: TCPU_COM (0x3)
1111
; OBJ64: CPU Version ID: TCPU_PPC64 (0x2)
12+
; OBJ: Name: 1.cpp

llvm/test/CodeGen/PowerPC/aix-filename-f.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
source_filename = "1.f95"
77

8-
; OBJ: Name: 1.f95
8+
; OBJ: Name: .file
99
; OBJ: Source Language ID: TB_Fortran (0x1)
1010
; OBJ32: CPU Version ID: TCPU_COM (0x3)
1111
; OBJ64: CPU Version ID: TCPU_PPC64 (0x2)
12+
; OBJ: Name: 1.f95

llvm/test/CodeGen/PowerPC/aix-func-dsc-gen.ll

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,23 @@ entry:
1313
; CHECK-NEXT: AddressSize: 32bit
1414
; CHECK: Symbol {
1515
; CHECK-NEXT: Index: 0
16-
; CHECK-NEXT: Name: <stdin>
16+
; CHECK-NEXT: Name: .file
1717
; CHECK-NEXT: Value (SymbolTableIndex): 0x0
1818
; CHECK-NEXT: Section: N_DEBUG
1919
; CHECK-NEXT: Source Language ID: TB_CPLUSPLUS (0x9)
2020
; CHECK-NEXT: CPU Version ID: TCPU_COM (0x3)
2121
; CHECK-NEXT: StorageClass: C_FILE (0x67)
22-
; CHECK-NEXT: NumberOfAuxEntries: 0
22+
; CHECK-NEXT: NumberOfAuxEntries: 2
23+
; CHECK-NEXT: File Auxiliary Entry {
24+
; CHECK-NEXT: Index: 1
25+
; CHECK-NEXT: Name:
26+
; CHECK-NEXT: Type: XFT_FN (0x0)
27+
; CHECK-NEXT: }
28+
; CHECK-NEXT: File Auxiliary Entry {
29+
; CHECK-NEXT: Index: 2
30+
; CHECK-NEXT: Name: LLVM
31+
; CHECK-NEXT: Type: XFT_CV (0x2)
32+
; CHECK-NEXT: }
2333
; CHECK-NEXT: }
2434
; CHECK-NEXT: Symbol {
2535
; CHECK-NEXT: Index: [[#Index:]]

0 commit comments

Comments
 (0)