Skip to content

Commit

Permalink
[TLOF][NFC] Make emitLinkerDirectives virtual and public. (llvm#123773)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nvjle authored Jan 22, 2025
1 parent 6ab9daf commit 7cf8add
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
9 changes: 6 additions & 3 deletions llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
const MCSymbol *Sym,
const MachineModuleInfo *MMI) const;

void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const override;

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

void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const override;

MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
const TargetMachine &TM) const override;

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

void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const override;

MCSection *getStaticCtorSection(unsigned Priority,
const MCSymbol *KeySym) const override;
MCSection *getStaticDtorSection(unsigned Priority,
Expand All @@ -206,9 +212,6 @@ class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
const Constant *C,
Align &Alignment) const override;

private:
void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const;
};

class TargetLoweringObjectFileWasm : public TargetLoweringObjectFile {
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/Target/TargetLoweringObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {
/// Emit Call Graph Profile metadata.
void emitCGProfileMetadata(MCStreamer &Streamer, Module &M) const;

/// Process linker options metadata and emit platform-specific bits.
virtual void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const {}

/// Get the module-level metadata that the platform cares about.
virtual void getModuleMetadata(Module &M) {}

Expand Down
57 changes: 34 additions & 23 deletions llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,7 @@ void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer,
Module &M) const {
auto &C = getContext();

if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS,
ELF::SHF_EXCLUDE);

Streamer.switchSection(S);

for (const auto *Operand : LinkerOptions->operands()) {
if (cast<MDNode>(Operand)->getNumOperands() != 2)
report_fatal_error("invalid llvm.linker.options");
for (const auto &Option : cast<MDNode>(Operand)->operands()) {
Streamer.emitBytes(cast<MDString>(Option)->getString());
Streamer.emitInt8(0);
}
}
}
emitLinkerDirectives(Streamer, M);

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

void TargetLoweringObjectFileELF::emitLinkerDirectives(MCStreamer &Streamer,
Module &M) const {
auto &C = getContext();
if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS,
ELF::SHF_EXCLUDE);

Streamer.switchSection(S);

for (const auto *Operand : LinkerOptions->operands()) {
if (cast<MDNode>(Operand)->getNumOperands() != 2)
report_fatal_error("invalid llvm.linker.options");
for (const auto &Option : cast<MDNode>(Operand)->operands()) {
Streamer.emitBytes(cast<MDString>(Option)->getString());
Streamer.emitInt8(0);
}
}
}
}

MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
const GlobalValue *GV, const TargetMachine &TM,
MachineModuleInfo *MMI) const {
Expand Down Expand Up @@ -1244,14 +1250,7 @@ MCSection *TargetLoweringObjectFileMachO::getStaticDtorSection(
void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer,
Module &M) const {
// Emit the linker options if present.
if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
for (const auto *Option : LinkerOptions->operands()) {
SmallVector<std::string, 4> StrOptions;
for (const auto &Piece : cast<MDNode>(Option)->operands())
StrOptions.push_back(std::string(cast<MDString>(Piece)->getString()));
Streamer.emitLinkerOptions(StrOptions);
}
}
emitLinkerDirectives(Streamer, M);

unsigned VersionVal = 0;
unsigned ImageInfoFlags = 0;
Expand Down Expand Up @@ -1285,6 +1284,18 @@ void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer,
Streamer.addBlankLine();
}

void TargetLoweringObjectFileMachO::emitLinkerDirectives(MCStreamer &Streamer,
Module &M) const {
if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
for (const auto *Option : LinkerOptions->operands()) {
SmallVector<std::string, 4> StrOptions;
for (const auto &Piece : cast<MDNode>(Option)->operands())
StrOptions.push_back(std::string(cast<MDString>(Piece)->getString()));
Streamer.emitLinkerOptions(StrOptions);
}
}
}

static void checkMachOComdat(const GlobalValue *GV) {
const Comdat *C = GV->getComdat();
if (!C)
Expand Down

0 comments on commit 7cf8add

Please sign in to comment.