Skip to content

Commit 19e402d

Browse files
committed
[JITLink][MachO] Use full <segment>,<section> names for MachO jitlink::Sections.
JITLink now requires section names to be unique. In MachO section names are only guaranteed to be unique within their containing segment (e.g. a '__const' section in the '__DATA' segment does not clash with a '__const' section in the '__TEXT' segment), so we need to use the fully qualified <segment>,<section> section names (e.g. '__DATA,__const' or '__TEXT,__const') when constructing jitlink::Sections for MachO objects.
1 parent 594e0ba commit 19e402d

File tree

7 files changed

+61
-40
lines changed

7 files changed

+61
-40
lines changed

llvm/lib/ExecutionEngine/JITLink/EHFrameSupport.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ createEHFrameRecorderPass(const Triple &TT,
774774
StoreFrameRangeFunction StoreRangeAddress) {
775775
const char *EHFrameSectionName = nullptr;
776776
if (TT.getObjectFormat() == Triple::MachO)
777-
EHFrameSectionName = "__eh_frame";
777+
EHFrameSectionName = "__TEXT,__eh_frame";
778778
else
779779
EHFrameSectionName = ".eh_frame";
780780

@@ -791,8 +791,9 @@ createEHFrameRecorderPass(const Triple &TT,
791791
Size = R.getSize();
792792
}
793793
if (Addr == 0 && Size != 0)
794-
return make_error<JITLinkError>("__eh_frame section can not have zero "
795-
"address with non-zero size");
794+
return make_error<JITLinkError>(
795+
StringRef(EHFrameSectionName) +
796+
" section can not have zero address with non-zero size");
796797
StoreFrameRange(Addr, Size);
797798
return Error::success();
798799
};

llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ Error MachOLinkGraphBuilder::createNormalizedSections() {
116116

117117
auto SecIndex = Obj.getSectionIndex(SecRef.getRawDataRefImpl());
118118

119-
auto Name = SecRef.getName();
120-
if (!Name)
121-
return Name.takeError();
122-
123119
if (Obj.is64Bit()) {
124120
const MachO::section_64 &Sec64 =
125121
Obj.getSection64(SecRef.getRawDataRefImpl());
@@ -150,8 +146,9 @@ Error MachOLinkGraphBuilder::createNormalizedSections() {
150146
}
151147

152148
LLVM_DEBUG({
153-
dbgs() << " " << *Name << ": " << formatv("{0:x16}", NSec.Address)
154-
<< " -- " << formatv("{0:x16}", NSec.Address + NSec.Size)
149+
dbgs() << " " << NSec.SegName << "," << NSec.SectName << ": "
150+
<< formatv("{0:x16}", NSec.Address) << " -- "
151+
<< formatv("{0:x16}", NSec.Address + NSec.Size)
155152
<< ", align: " << NSec.Alignment << ", index: " << SecIndex
156153
<< "\n";
157154
});
@@ -182,10 +179,12 @@ Error MachOLinkGraphBuilder::createNormalizedSections() {
182179
sys::Memory::MF_WRITE);
183180

184181
if (!isDebugSection(NSec))
185-
NSec.GraphSection = &G->createSection(*Name, Prot);
182+
NSec.GraphSection = &G->createSection(
183+
G->allocateString(StringRef(NSec.SegName) + "," + NSec.SectName),
184+
Prot);
186185
else
187186
LLVM_DEBUG({
188-
dbgs() << " " << *Name
187+
dbgs() << " " << NSec.SegName << "," << NSec.SectName
189188
<< " is a debug section: No graph section will be created.\n";
190189
});
191190

llvm/lib/ExecutionEngine/JITLink/MachO_x86_64.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,10 +625,11 @@ void link_MachO_x86_64(std::unique_ptr<LinkGraph> G,
625625

626626
if (Ctx->shouldAddDefaultTargetPasses(G->getTargetTriple())) {
627627
// Add eh-frame passses.
628-
Config.PrePrunePasses.push_back(EHFrameSplitter("__eh_frame"));
628+
StringRef EHFrameSectionName = "__TEXT,__eh_frame";
629+
Config.PrePrunePasses.push_back(EHFrameSplitter(EHFrameSectionName));
629630
Config.PrePrunePasses.push_back(
630-
EHFrameEdgeFixer("__eh_frame", G->getPointerSize(), x86_64::Delta64,
631-
x86_64::Delta32, x86_64::NegDelta32));
631+
EHFrameEdgeFixer(EHFrameSectionName, G->getPointerSize(),
632+
x86_64::Delta64, x86_64::Delta32, x86_64::NegDelta32));
632633

633634
// Add a mark-live pass.
634635
if (auto MarkLive = Ctx->getMarkLivePass(G->getTargetTriple()))

llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,12 @@ void MachOPlatform::InitScraperPlugin::modifyPassConfig(
306306

307307
Config.PrePrunePasses.push_back([this, &MR](jitlink::LinkGraph &G) -> Error {
308308
JITLinkSymbolVector InitSectionSymbols;
309-
preserveInitSectionIfPresent(InitSectionSymbols, G, "__mod_init_func");
310-
preserveInitSectionIfPresent(InitSectionSymbols, G, "__objc_selrefs");
311-
preserveInitSectionIfPresent(InitSectionSymbols, G, "__objc_classlist");
309+
preserveInitSectionIfPresent(InitSectionSymbols, G,
310+
"__DATA,__mod_init_func");
311+
preserveInitSectionIfPresent(InitSectionSymbols, G,
312+
"__DATA,__objc_selrefs");
313+
preserveInitSectionIfPresent(InitSectionSymbols, G,
314+
"__DATA,__objc_classlist");
312315

313316
if (!InitSectionSymbols.empty()) {
314317
std::lock_guard<std::mutex> Lock(InitScraperMutex);
@@ -327,25 +330,27 @@ void MachOPlatform::InitScraperPlugin::modifyPassConfig(
327330
ObjCClassList;
328331

329332
JITTargetAddress ObjCImageInfoAddr = 0;
330-
if (auto *ObjCImageInfoSec = G.findSectionByName("__objc_image_info")) {
333+
if (auto *ObjCImageInfoSec =
334+
G.findSectionByName("__DATA,__objc_image_info")) {
331335
if (auto Addr = jitlink::SectionRange(*ObjCImageInfoSec).getStart())
332336
ObjCImageInfoAddr = Addr;
333337
}
334338

335339
// Record __mod_init_func.
336-
if (auto ModInitsOrErr = getSectionExtent(G, "__mod_init_func"))
340+
if (auto ModInitsOrErr = getSectionExtent(G, "__DATA,__mod_init_func"))
337341
ModInits = std::move(*ModInitsOrErr);
338342
else
339343
return ModInitsOrErr.takeError();
340344

341345
// Record __objc_selrefs.
342-
if (auto ObjCSelRefsOrErr = getSectionExtent(G, "__objc_selrefs"))
346+
if (auto ObjCSelRefsOrErr = getSectionExtent(G, "__DATA,__objc_selrefs"))
343347
ObjCSelRefs = std::move(*ObjCSelRefsOrErr);
344348
else
345349
return ObjCSelRefsOrErr.takeError();
346350

347351
// Record __objc_classlist.
348-
if (auto ObjCClassListOrErr = getSectionExtent(G, "__objc_classlist"))
352+
if (auto ObjCClassListOrErr =
353+
getSectionExtent(G, "__DATA,__objc_classlist"))
349354
ObjCClassList = std::move(*ObjCClassListOrErr);
350355
else
351356
return ObjCClassListOrErr.takeError();

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ class RuntimeDyldCheckerExprEval {
381381
RemainingExpr = RemainingExpr.substr(1).ltrim();
382382

383383
StringRef SectionName;
384-
std::tie(SectionName, RemainingExpr) = parseSymbol(RemainingExpr);
384+
size_t CloseParensIdx = RemainingExpr.find(')');
385+
SectionName = RemainingExpr.substr(0, CloseParensIdx).rtrim();
386+
RemainingExpr = RemainingExpr.substr(CloseParensIdx).ltrim();
385387

386388
if (!RemainingExpr.startswith(")"))
387389
return std::make_pair(

llvm/test/ExecutionEngine/JITLink/AArch64/MachO_arm64_relocations.s

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,15 @@ Lanon_data:
147147
# anonymous.
148148
#
149149
# Note: +8 offset in expression below to accounts for sizeof(Lanon_data).
150-
# jitlink-check: *{8}(section_addr(macho_reloc.o, __data) + 8) = (section_addr(macho_reloc.o, __data) + 8) - named_data + 2
150+
# jitlink-check: *{8}(section_addr(macho_reloc.o, __DATA,__data) + 8) = \
151+
# jitlink-check: (section_addr(macho_reloc.o, __DATA,__data) + 8) - named_data + 2
151152
.p2align 3
152153
Lanon_minuend_quad:
153154
.quad Lanon_minuend_quad - named_data + 2
154155

155156
# Note: +16 offset in expression below to accounts for sizeof(Lanon_data) + sizeof(Lanon_minuend_long).
156-
# jitlink-check: *{4}(section_addr(macho_reloc.o, __data) + 16) = ((section_addr(macho_reloc.o, __data) + 16) - named_data + 2)[31:0]
157+
# jitlink-check: *{4}(section_addr(macho_reloc.o, __DATA,__data) + 16) = \
158+
# jitlink-check: ((section_addr(macho_reloc.o, __DATA,__data) + 16) - named_data + 2)[31:0]
157159
.p2align 2
158160
Lanon_minuend_long:
159161
.long Lanon_minuend_long - named_data + 2
@@ -185,23 +187,26 @@ named_func_addr_quad:
185187
# Check ARM64_RELOC_UNSIGNED / quad / non-extern handling by putting the
186188
# address of a local anonymous function into a quad symbol.
187189
#
188-
# jitlink-check: *{8}anon_func_addr_quad = section_addr(macho_reloc.o, __text)
190+
# jitlink-check: *{8}anon_func_addr_quad = \
191+
# jitlink-check: section_addr(macho_reloc.o, __TEXT,__text)
189192
.globl anon_func_addr_quad
190193
.p2align 3
191194
anon_func_addr_quad:
192195
.quad Lanon_func
193196

194197
# ARM64_RELOC_SUBTRACTOR Quad/Long in named storage with anonymous minuend
195198
#
196-
# jitlink-check: *{8}anon_minuend_quad1 = section_addr(macho_reloc.o, __data) - anon_minuend_quad1 + 2
199+
# jitlink-check: *{8}anon_minuend_quad1 = \
200+
# jitlink-check: section_addr(macho_reloc.o, __DATA,__data) - anon_minuend_quad1 + 2
197201
# Only the form "B: .quad LA - B + C" is tested. The form "B: .quad B - LA + C" is
198202
# invalid because the subtrahend can not be local.
199203
.globl anon_minuend_quad1
200204
.p2align 3
201205
anon_minuend_quad1:
202206
.quad Lanon_data - anon_minuend_quad1 + 2
203207

204-
# jitlink-check: *{4}anon_minuend_long1 = (section_addr(macho_reloc.o, __data) - anon_minuend_long1 + 2)[31:0]
208+
# jitlink-check: *{4}anon_minuend_long1 = \
209+
# jitlink-check: (section_addr(macho_reloc.o, __DATA,__data) - anon_minuend_long1 + 2)[31:0]
205210
.globl anon_minuend_long1
206211
.p2align 2
207212
anon_minuend_long1:
@@ -308,14 +313,14 @@ test_got:
308313
# ORC responsibility set, which is automatically marked live and would couse
309314
# spurious passes.
310315
#
311-
# jitlink-check: *{8}section_addr(macho_reloc.o, __nds_test_sect) = 0
316+
# jitlink-check: *{8}section_addr(macho_reloc.o, __DATA,__nds_test_sect) = 0
312317
.section __DATA,__nds_test_sect,regular,no_dead_strip
313318
.quad 0
314319

315320
# Check that unreferenced local symbols that have been marked no-dead-strip are
316321
# not dead-striped.
317322
#
318-
# jitlink-check: *{8}section_addr(macho_reloc.o, __nds_test_nlst) = 0
323+
# jitlink-check: *{8}section_addr(macho_reloc.o, __DATA,__nds_test_nlst) = 0
319324
.section __DATA,__nds_test_nlst,regular
320325
.no_dead_strip no_dead_strip_test_symbol
321326
no_dead_strip_test_symbol:

llvm/test/ExecutionEngine/JITLink/X86/MachO_x86-64_relocations.s

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,26 @@ signed4:
106106
movl $0xAAAAAAAA, named_data(%rip)
107107

108108
.globl signedanon
109-
# jitlink-check: decode_operand(signedanon, 4) = section_addr(macho_reloc.o, __data) - next_pc(signedanon)
109+
# jitlink-check: decode_operand(signedanon, 4) = \
110+
# jitlink-check: section_addr(macho_reloc.o, __DATA,__data) - next_pc(signedanon)
110111
signedanon:
111112
movq Lanon_data(%rip), %rax
112113

113114
.globl signed1anon
114-
# jitlink-check: decode_operand(signed1anon, 3) = section_addr(macho_reloc.o, __data) - next_pc(signed1anon)
115+
# jitlink-check: decode_operand(signed1anon, 3) = \
116+
# jitlink-check: section_addr(macho_reloc.o, __DATA,__data) - next_pc(signed1anon)
115117
signed1anon:
116118
movb $0xAA, Lanon_data(%rip)
117119

118120
.globl signed2anon
119-
# jitlink-check: decode_operand(signed2anon, 3) = section_addr(macho_reloc.o, __data) - next_pc(signed2anon)
121+
# jitlink-check: decode_operand(signed2anon, 3) = \
122+
# jitlink-check: section_addr(macho_reloc.o, __DATA,__data) - next_pc(signed2anon)
120123
signed2anon:
121124
movw $0xAAAA, Lanon_data(%rip)
122125

123126
.globl signed4anon
124-
# jitlink-check: decode_operand(signed4anon, 3) = section_addr(macho_reloc.o, __data) - next_pc(signed4anon)
127+
# jitlink-check: decode_operand(signed4anon, 3) = \
128+
# jitlink-check: section_addr(macho_reloc.o, __DATA,__data) - next_pc(signed4anon)
125129
signed4anon:
126130
movl $0xAAAAAAAA, Lanon_data(%rip)
127131

@@ -140,13 +144,15 @@ Lanon_data:
140144
# anonymous.
141145
#
142146
# Note: +8 offset in expression below to accounts for sizeof(Lanon_data).
143-
# jitlink-check: *{8}(section_addr(macho_reloc.o, __data) + 8) = (section_addr(macho_reloc.o, __data) + 8) - named_data - 2
147+
# jitlink-check: *{8}(section_addr(macho_reloc.o, __DATA,__data) + 8) = \
148+
# jitlink-check: (section_addr(macho_reloc.o, __DATA,__data) + 8) - named_data - 2
144149
.p2align 3
145150
Lanon_minuend_quad:
146151
.quad Lanon_minuend_quad - named_data - 2
147152

148153
# Note: +16 offset in expression below to accounts for sizeof(Lanon_data) + sizeof(Lanon_minuend_long).
149-
# jitlink-check: *{4}(section_addr(macho_reloc.o, __data) + 16) = ((section_addr(macho_reloc.o, __data) + 16) - named_data - 2)[31:0]
154+
# jitlink-check: *{4}(section_addr(macho_reloc.o, __DATA,__data) + 16) = \
155+
# jitlink-check: ((section_addr(macho_reloc.o, __DATA,__data) + 16) - named_data - 2)[31:0]
150156
.p2align 2
151157
Lanon_minuend_long:
152158
.long Lanon_minuend_long - named_data - 2
@@ -185,23 +191,25 @@ named_func_addr_long:
185191
# Check X86_64_RELOC_UNSIGNED / quad / non-extern handling by putting the
186192
# address of a local anonymous function into a quad symbol.
187193
#
188-
# jitlink-check: *{8}anon_func_addr_quad = section_addr(macho_reloc.o, __text)
194+
# jitlink-check: *{8}anon_func_addr_quad = section_addr(macho_reloc.o, __TEXT,__text)
189195
.globl anon_func_addr_quad
190196
.p2align 3
191197
anon_func_addr_quad:
192198
.quad Lanon_func
193199

194200
# X86_64_RELOC_SUBTRACTOR Quad/Long in named storage with anonymous minuend
195201
#
196-
# jitlink-check: *{8}anon_minuend_quad1 = section_addr(macho_reloc.o, __data) - anon_minuend_quad1 - 2
202+
# jitlink-check: *{8}anon_minuend_quad1 = \
203+
# jitlink-check: section_addr(macho_reloc.o, __DATA,__data) - anon_minuend_quad1 - 2
197204
# Only the form "B: .quad LA - B + C" is tested. The form "B: .quad B - LA + C" is
198205
# invalid because the subtrahend can not be local.
199206
.globl anon_minuend_quad1
200207
.p2align 3
201208
anon_minuend_quad1:
202209
.quad Lanon_data - anon_minuend_quad1 - 2
203210

204-
# jitlink-check: *{4}anon_minuend_long1 = (section_addr(macho_reloc.o, __data) - anon_minuend_long1 - 2)[31:0]
211+
# jitlink-check: *{4}anon_minuend_long1 = \
212+
# jitlink-check: (section_addr(macho_reloc.o, __DATA,__data) - anon_minuend_long1 - 2)[31:0]
205213
.globl anon_minuend_long1
206214
.p2align 2
207215
anon_minuend_long1:
@@ -311,14 +319,14 @@ test_got:
311319
# ORC responsibility set, which is automatically marked live and would couse
312320
# spurious passes.
313321
#
314-
# jitlink-check: *{8}section_addr(macho_reloc.o, __nds_test_sect) = 0
322+
# jitlink-check: *{8}section_addr(macho_reloc.o, __DATA,__nds_test_sect) = 0
315323
.section __DATA,__nds_test_sect,regular,no_dead_strip
316324
.quad 0
317325

318326
# Check that unreferenced local symbols that have been marked no-dead-strip are
319327
# not dead-striped.
320328
#
321-
# jitlink-check: *{8}section_addr(macho_reloc.o, __nds_test_nlst) = 0
329+
# jitlink-check: *{8}section_addr(macho_reloc.o, __DATA,__nds_test_nlst) = 0
322330
.section __DATA,__nds_test_nlst,regular
323331
.no_dead_strip no_dead_strip_test_symbol
324332
no_dead_strip_test_symbol:

0 commit comments

Comments
 (0)