Skip to content

Commit 41a08e7

Browse files
committed
[MC] Move Mach-O specific getAtom and isSectionAtomizableBySymbols to Mach-O files
and devirtualize isSectionAtomizableBySymbols.
1 parent 4066a32 commit 41a08e7

File tree

12 files changed

+52
-46
lines changed

12 files changed

+52
-46
lines changed

llvm/include/llvm/MC/MCAsmInfo.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -580,12 +580,6 @@ class MCAsmInfo {
580580
return nullptr;
581581
}
582582

583-
/// True if the section is atomized using the symbols in it.
584-
/// This is false if the section is not atomized at all (most ELF sections) or
585-
/// if it is atomized based on its contents (MachO' __TEXT,__cstring for
586-
/// example).
587-
virtual bool isSectionAtomizableBySymbols(const MCSection &Section) const;
588-
589583
virtual const MCExpr *getExprForPersonalitySymbol(const MCSymbol *Sym,
590584
unsigned Encoding,
591585
MCStreamer &Streamer) const;

llvm/include/llvm/MC/MCAsmInfoDarwin.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class MCAsmInfoDarwin : public MCAsmInfo {
2222
public:
2323
explicit MCAsmInfoDarwin();
2424

25-
bool isSectionAtomizableBySymbols(const MCSection &Section) const override;
25+
/// True if the section is atomized using the symbols in it.
26+
/// This is false if the section is atomized based on its contents (MachO' __TEXT,__cstring for
27+
/// example).
28+
static bool isSectionAtomizableBySymbols(const MCSection &Section);
2629
};
2730

2831
} // end namespace llvm

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,6 @@ class MCAssembler {
251251
uint64_t computeFragmentSize(const MCAsmLayout &Layout,
252252
const MCFragment &F) const;
253253

254-
/// Find the symbol which defines the atom containing the given symbol, or
255-
/// null if there is no such symbol.
256-
const MCSymbol *getAtom(const MCSymbol &S) const;
257-
258254
/// Check whether a particular symbol is visible to the linker and is required
259255
/// in the symbol table, or whether it can be discarded by the assembler. This
260256
/// also effects whether the assembler treats the label as potentially

llvm/include/llvm/MC/MCMachObjectWriter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ class MachObjectWriter : public MCObjectWriter {
162162

163163
uint64_t getPaddingSize(const MCSection *SD, const MCAsmLayout &Layout) const;
164164

165+
const MCSymbol *getAtom(const MCSymbol &S) const;
166+
165167
bool doesSymbolRequireExternRelocation(const MCSymbol &S);
166168

167169
/// @}

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "llvm/IR/PseudoProbe.h"
4444
#include "llvm/IR/Type.h"
4545
#include "llvm/MC/MCAsmInfo.h"
46+
#include "llvm/MC/MCAsmInfoDarwin.h"
4647
#include "llvm/MC/MCContext.h"
4748
#include "llvm/MC/MCExpr.h"
4849
#include "llvm/MC/MCSectionCOFF.h"
@@ -1555,7 +1556,7 @@ const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
15551556

15561557
static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
15571558
const MCSection &Section) {
1558-
if (!AsmInfo.isSectionAtomizableBySymbols(Section))
1559+
if (!MCAsmInfoDarwin::isSectionAtomizableBySymbols(Section))
15591560
return true;
15601561

15611562
// FIXME: we should be able to use private labels for sections that can't be

llvm/lib/MC/MCAsmInfo.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ void MCAsmInfo::addInitialFrameState(const MCCFIInstruction &Inst) {
7676
InitialFrameState.push_back(Inst);
7777
}
7878

79-
bool MCAsmInfo::isSectionAtomizableBySymbols(const MCSection &Section) const {
80-
return false;
81-
}
82-
8379
const MCExpr *
8480
MCAsmInfo::getExprForPersonalitySymbol(const MCSymbol *Sym,
8581
unsigned Encoding,

llvm/lib/MC/MCAsmInfoDarwin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
using namespace llvm;
2020

2121
bool MCAsmInfoDarwin::isSectionAtomizableBySymbols(
22-
const MCSection &Section) const {
22+
const MCSection &Section) {
2323
const MCSectionMachO &SMO = static_cast<const MCSectionMachO &>(Section);
2424

2525
// Sections holding 1 byte strings are atomized based on the data they

llvm/lib/MC/MCAssembler.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,25 +172,6 @@ bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
172172
return false;
173173
}
174174

175-
const MCSymbol *MCAssembler::getAtom(const MCSymbol &S) const {
176-
// Linker visible symbols define atoms.
177-
if (isSymbolLinkerVisible(S))
178-
return &S;
179-
180-
// Absolute and undefined symbols have no defining atom.
181-
if (!S.isInSection())
182-
return nullptr;
183-
184-
// Non-linker visible symbols in sections which can't be atomized have no
185-
// defining atom.
186-
if (!getContext().getAsmInfo()->isSectionAtomizableBySymbols(
187-
*S.getFragment()->getParent()))
188-
return nullptr;
189-
190-
// Otherwise, return the atom for the containing fragment.
191-
return S.getFragment()->getAtom();
192-
}
193-
194175
bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
195176
const MCFragment *DF, MCValue &Target,
196177
const MCSubtargetInfo *STI, uint64_t &Value,

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/BinaryFormat/MachO.h"
1313
#include "llvm/MC/MCAsmBackend.h"
1414
#include "llvm/MC/MCAsmLayout.h"
15+
#include "llvm/MC/MCAsmInfoDarwin.h"
1516
#include "llvm/MC/MCAssembler.h"
1617
#include "llvm/MC/MCContext.h"
1718
#include "llvm/MC/MCDirectives.h"
@@ -132,6 +133,36 @@ uint64_t MachObjectWriter::getPaddingSize(const MCSection *Sec,
132133
return offsetToAlignment(EndAddr, NextSec.getAlign());
133134
}
134135

136+
static bool isSymbolLinkerVisible(const MCSymbol &Symbol) {
137+
// Non-temporary labels should always be visible to the linker.
138+
if (!Symbol.isTemporary())
139+
return true;
140+
141+
if (Symbol.isUsedInReloc())
142+
return true;
143+
144+
return false;
145+
}
146+
147+
const MCSymbol *MachObjectWriter::getAtom(const MCSymbol &S) const {
148+
// Linker visible symbols define atoms.
149+
if (isSymbolLinkerVisible(S))
150+
return &S;
151+
152+
// Absolute and undefined symbols have no defining atom.
153+
if (!S.isInSection())
154+
return nullptr;
155+
156+
// Non-linker visible symbols in sections which can't be atomized have no
157+
// defining atom.
158+
if (!MCAsmInfoDarwin::isSectionAtomizableBySymbols(
159+
*S.getFragment()->getParent()))
160+
return nullptr;
161+
162+
// Otherwise, return the atom for the containing fragment.
163+
return S.getFragment()->getAtom();
164+
}
165+
135166
void MachObjectWriter::writeHeader(MachO::HeaderFileType Type,
136167
unsigned NumLoadCommands,
137168
unsigned LoadCommandsSize,

llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/ADT/Twine.h"
1212
#include "llvm/BinaryFormat/MachO.h"
1313
#include "llvm/MC/MCAsmInfo.h"
14+
#include "llvm/MC/MCAsmInfoDarwin.h"
1415
#include "llvm/MC/MCAsmLayout.h"
1516
#include "llvm/MC/MCAssembler.h"
1617
#include "llvm/MC/MCContext.h"
@@ -216,10 +217,10 @@ void AArch64MachObjectWriter::recordRelocation(
216217
}
217218
} else if (Target.getSymB()) { // A - B + constant
218219
const MCSymbol *A = &Target.getSymA()->getSymbol();
219-
const MCSymbol *A_Base = Asm.getAtom(*A);
220+
const MCSymbol *A_Base = Writer->getAtom(*A);
220221

221222
const MCSymbol *B = &Target.getSymB()->getSymbol();
222-
const MCSymbol *B_Base = Asm.getAtom(*B);
223+
const MCSymbol *B_Base = Writer->getAtom(*B);
223224

224225
// Check for "_foo@got - .", which comes through here as:
225226
// Ltmp0:
@@ -313,11 +314,12 @@ void AArch64MachObjectWriter::recordRelocation(
313314
return;
314315
}
315316
const MCSection &Sec = Symbol->getSection();
316-
if (!Asm.getContext().getAsmInfo()->isSectionAtomizableBySymbols(Sec))
317+
if (!MCAsmInfoDarwin::isSectionAtomizableBySymbols(Sec))
317318
Symbol->setUsedInReloc();
318319
}
319320

320-
const MCSymbol *Base = Asm.getAtom(*Symbol);
321+
const MCSymbol *Base = Writer->getAtom(*Symbol);
322+
321323
// If the symbol is a variable it can either be in a section and
322324
// we have a base or it is absolute and should have been expanded.
323325
assert(!Symbol->isVariable() || Base);

llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/ADT/Twine.h"
1212
#include "llvm/BinaryFormat/MachO.h"
1313
#include "llvm/MC/MCAsmInfo.h"
14+
#include "llvm/MC/MCAsmInfoDarwin.h"
1415
#include "llvm/MC/MCAsmLayout.h"
1516
#include "llvm/MC/MCAssembler.h"
1617
#include "llvm/MC/MCContext.h"
@@ -145,12 +146,12 @@ void X86MachObjectWriter::RecordX86_64Relocation(
145146
const MCSymbol *A = &Target.getSymA()->getSymbol();
146147
if (A->isTemporary())
147148
A = &Writer->findAliasedSymbol(*A);
148-
const MCSymbol *A_Base = Asm.getAtom(*A);
149+
const MCSymbol *A_Base = Writer->getAtom(*A);
149150

150151
const MCSymbol *B = &Target.getSymB()->getSymbol();
151152
if (B->isTemporary())
152153
B = &Writer->findAliasedSymbol(*B);
153-
const MCSymbol *B_Base = Asm.getAtom(*B);
154+
const MCSymbol *B_Base = Writer->getAtom(*B);
154155

155156
// Neither symbol can be modified.
156157
if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None) {
@@ -217,10 +218,10 @@ void X86MachObjectWriter::RecordX86_64Relocation(
217218
const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
218219
if (Symbol->isTemporary() && Value) {
219220
const MCSection &Sec = Symbol->getSection();
220-
if (!Asm.getContext().getAsmInfo()->isSectionAtomizableBySymbols(Sec))
221+
if (!MCAsmInfoDarwin::isSectionAtomizableBySymbols(Sec))
221222
Symbol->setUsedInReloc();
222223
}
223-
RelSymbol = Asm.getAtom(*Symbol);
224+
RelSymbol = Writer->getAtom(*Symbol);
224225

225226
// Relocations inside debug sections always use local relocations when
226227
// possible. This seems to be done because the debugger doesn't fully

llvm/test/CodeGen/X86/osx-private-labels.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
; RUN: llc < %s -mtriple=x86_64-apple-darwin | FileCheck %s
22
; Test all the cases where a L label is safe. Removing any entry from
3-
; TargetLoweringObjectFileMachO::isSectionAtomizableBySymbols should cause
4-
; this to fail.
3+
; isSectionAtomizableBySymbols should cause this to fail.
54
; We also test some noteworthy cases that require an l label.
65

76
@private1 = private unnamed_addr constant [4 x i8] c"zed\00"

0 commit comments

Comments
 (0)