Skip to content

Commit 7cf8add

Browse files
authored
[TLOF][NFC] Make emitLinkerDirectives virtual and public. (llvm#123773)
Today, emitLinkerDirectives is private to TLOFCOFF-- it isolates parsing and processing of the linker options. Similar processing is also done by other TLOFs inline within emitModuleMetadata. This patch promotes emitLinkerDirectives to a virtual (public) method so that this handling is similarly isolated in the other TLOFs. This also enables downstream targets to override just this handling instead of the whole of emitModuleMetadata.
1 parent 6ab9daf commit 7cf8add

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
6060
const MCSymbol *Sym,
6161
const MachineModuleInfo *MMI) const;
6262

63+
void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const override;
64+
6365
/// Given a constant with the SectionKind, return a section that it should be
6466
/// placed in.
6567
MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
@@ -131,6 +133,8 @@ class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
131133
/// Emit the module flags that specify the garbage collection information.
132134
void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
133135

136+
void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const override;
137+
134138
MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
135139
const TargetMachine &TM) const override;
136140

@@ -192,6 +196,8 @@ class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
192196
/// Emit Obj-C garbage collection and linker options.
193197
void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
194198

199+
void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const override;
200+
195201
MCSection *getStaticCtorSection(unsigned Priority,
196202
const MCSymbol *KeySym) const override;
197203
MCSection *getStaticDtorSection(unsigned Priority,
@@ -206,9 +212,6 @@ class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
206212
MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
207213
const Constant *C,
208214
Align &Alignment) const override;
209-
210-
private:
211-
void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const;
212215
};
213216

214217
class TargetLoweringObjectFileWasm : public TargetLoweringObjectFile {

llvm/include/llvm/Target/TargetLoweringObjectFile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {
9191
/// Emit Call Graph Profile metadata.
9292
void emitCGProfileMetadata(MCStreamer &Streamer, Module &M) const;
9393

94+
/// Process linker options metadata and emit platform-specific bits.
95+
virtual void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const {}
96+
9497
/// Get the module-level metadata that the platform cares about.
9598
virtual void getModuleMetadata(Module &M) {}
9699

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -306,21 +306,7 @@ void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer,
306306
Module &M) const {
307307
auto &C = getContext();
308308

309-
if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
310-
auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS,
311-
ELF::SHF_EXCLUDE);
312-
313-
Streamer.switchSection(S);
314-
315-
for (const auto *Operand : LinkerOptions->operands()) {
316-
if (cast<MDNode>(Operand)->getNumOperands() != 2)
317-
report_fatal_error("invalid llvm.linker.options");
318-
for (const auto &Option : cast<MDNode>(Operand)->operands()) {
319-
Streamer.emitBytes(cast<MDString>(Option)->getString());
320-
Streamer.emitInt8(0);
321-
}
322-
}
323-
}
309+
emitLinkerDirectives(Streamer, M);
324310

325311
if (NamedMDNode *DependentLibraries = M.getNamedMetadata("llvm.dependent-libraries")) {
326312
auto *S = C.getELFSection(".deplibs", ELF::SHT_LLVM_DEPENDENT_LIBRARIES,
@@ -400,6 +386,26 @@ void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer,
400386
emitCGProfileMetadata(Streamer, M);
401387
}
402388

389+
void TargetLoweringObjectFileELF::emitLinkerDirectives(MCStreamer &Streamer,
390+
Module &M) const {
391+
auto &C = getContext();
392+
if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
393+
auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS,
394+
ELF::SHF_EXCLUDE);
395+
396+
Streamer.switchSection(S);
397+
398+
for (const auto *Operand : LinkerOptions->operands()) {
399+
if (cast<MDNode>(Operand)->getNumOperands() != 2)
400+
report_fatal_error("invalid llvm.linker.options");
401+
for (const auto &Option : cast<MDNode>(Operand)->operands()) {
402+
Streamer.emitBytes(cast<MDString>(Option)->getString());
403+
Streamer.emitInt8(0);
404+
}
405+
}
406+
}
407+
}
408+
403409
MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
404410
const GlobalValue *GV, const TargetMachine &TM,
405411
MachineModuleInfo *MMI) const {
@@ -1244,14 +1250,7 @@ MCSection *TargetLoweringObjectFileMachO::getStaticDtorSection(
12441250
void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer,
12451251
Module &M) const {
12461252
// Emit the linker options if present.
1247-
if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
1248-
for (const auto *Option : LinkerOptions->operands()) {
1249-
SmallVector<std::string, 4> StrOptions;
1250-
for (const auto &Piece : cast<MDNode>(Option)->operands())
1251-
StrOptions.push_back(std::string(cast<MDString>(Piece)->getString()));
1252-
Streamer.emitLinkerOptions(StrOptions);
1253-
}
1254-
}
1253+
emitLinkerDirectives(Streamer, M);
12551254

12561255
unsigned VersionVal = 0;
12571256
unsigned ImageInfoFlags = 0;
@@ -1285,6 +1284,18 @@ void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer,
12851284
Streamer.addBlankLine();
12861285
}
12871286

1287+
void TargetLoweringObjectFileMachO::emitLinkerDirectives(MCStreamer &Streamer,
1288+
Module &M) const {
1289+
if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
1290+
for (const auto *Option : LinkerOptions->operands()) {
1291+
SmallVector<std::string, 4> StrOptions;
1292+
for (const auto &Piece : cast<MDNode>(Option)->operands())
1293+
StrOptions.push_back(std::string(cast<MDString>(Piece)->getString()));
1294+
Streamer.emitLinkerOptions(StrOptions);
1295+
}
1296+
}
1297+
}
1298+
12881299
static void checkMachOComdat(const GlobalValue *GV) {
12891300
const Comdat *C = GV->getComdat();
12901301
if (!C)

0 commit comments

Comments
 (0)