Skip to content

[TLOF][NFC] Make emitLinkerDirectives virtual and public. #123773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1238,14 +1244,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 @@ -1279,6 +1278,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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL there is a MachO-specific .linker_option assembler directive. Who knew.

}
}
}

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