@@ -42,6 +42,11 @@ std::vector<SyntheticSection *> macho::syntheticSections;
42
42
43
43
SyntheticSection::SyntheticSection (const char *segname, const char *name)
44
44
: OutputSection(SyntheticKind, name), segname(segname) {
45
+ isec = make<InputSection>();
46
+ isec->segname = segname;
47
+ isec->name = name;
48
+ isec->parent = this ;
49
+ isec->outSecOff = 0 ;
45
50
syntheticSections.push_back (this );
46
51
}
47
52
@@ -118,12 +123,6 @@ void MachHeaderSection::writeTo(uint8_t *buf) const {
118
123
PageZeroSection::PageZeroSection ()
119
124
: SyntheticSection(segment_names::pageZero, section_names::pageZero) {}
120
125
121
- uint64_t Location::getVA () const {
122
- if (const auto *isec = section.dyn_cast <const InputSection *>())
123
- return isec->getVA () + offset;
124
- return section.get <const OutputSection *>()->addr + offset;
125
- }
126
-
127
126
RebaseSection::RebaseSection ()
128
127
: LinkEditSection(segment_names::linkEdit, section_names::rebase) {}
129
128
@@ -186,16 +185,11 @@ void RebaseSection::finalizeContents() {
186
185
os << static_cast <uint8_t >(REBASE_OPCODE_SET_TYPE_IMM | REBASE_TYPE_POINTER);
187
186
188
187
llvm::sort (locations, [](const Location &a, const Location &b) {
189
- return a.getVA () < b.getVA ();
188
+ return a.isec -> getVA () < b.isec -> getVA ();
190
189
});
191
- for (const Location &loc : locations) {
192
- if (const auto *isec = loc.section .dyn_cast <const InputSection *>()) {
193
- encodeRebase (isec->parent , isec->outSecOff + loc.offset , lastRebase, os);
194
- } else {
195
- const auto *osec = loc.section .get <const OutputSection *>();
196
- encodeRebase (osec, loc.offset , lastRebase, os);
197
- }
198
- }
190
+ for (const Location &loc : locations)
191
+ encodeRebase (loc.isec ->parent , loc.isec ->outSecOff + loc.offset , lastRebase,
192
+ os);
199
193
if (lastRebase.consecutiveCount != 0 )
200
194
encodeDoRebase (lastRebase, os);
201
195
@@ -218,7 +212,7 @@ void NonLazyPointerSectionBase::addEntry(Symbol *sym) {
218
212
assert (!sym->isInGot ());
219
213
sym->gotIndex = entries.size () - 1 ;
220
214
221
- addNonLazyBindingEntries (sym, this , sym->gotIndex * WordSize);
215
+ addNonLazyBindingEntries (sym, isec , sym->gotIndex * WordSize);
222
216
}
223
217
}
224
218
@@ -336,14 +330,9 @@ void BindingSection::finalizeContents() {
336
330
encodeDylibOrdinal (ordinal, os);
337
331
lastBinding.ordinal = ordinal;
338
332
}
339
- if (auto *isec = b.target .section .dyn_cast <const InputSection *>()) {
340
- encodeBinding (b.dysym , isec->parent , isec->outSecOff + b.target .offset ,
341
- b.addend , /* isWeakBinding=*/ false , lastBinding, os);
342
- } else {
343
- auto *osec = b.target .section .get <const OutputSection *>();
344
- encodeBinding (b.dysym , osec, b.target .offset , b.addend ,
345
- /* isWeakBinding=*/ false , lastBinding, os);
346
- }
333
+ encodeBinding (b.dysym , b.target .isec ->parent ,
334
+ b.target .isec ->outSecOff + b.target .offset , b.addend ,
335
+ /* isWeakBinding=*/ false , lastBinding, os);
347
336
}
348
337
if (!bindings.empty ())
349
338
os << static_cast <uint8_t >(BIND_OPCODE_DONE);
@@ -369,16 +358,10 @@ void WeakBindingSection::finalizeContents() {
369
358
[](const WeakBindingEntry &a, const WeakBindingEntry &b) {
370
359
return a.target .getVA () < b.target .getVA ();
371
360
});
372
- for (const WeakBindingEntry &b : bindings) {
373
- if (const auto *isec = b.target .section .dyn_cast <const InputSection *>()) {
374
- encodeBinding (b.symbol , isec->parent , isec->outSecOff + b.target .offset ,
375
- b.addend , /* isWeakBinding=*/ true , lastBinding, os);
376
- } else {
377
- const auto *osec = b.target .section .get <const OutputSection *>();
378
- encodeBinding (b.symbol , osec, b.target .offset , b.addend ,
379
- /* isWeakBinding=*/ true , lastBinding, os);
380
- }
381
- }
361
+ for (const WeakBindingEntry &b : bindings)
362
+ encodeBinding (b.symbol , b.target .isec ->parent ,
363
+ b.target .isec ->outSecOff + b.target .offset , b.addend ,
364
+ /* isWeakBinding=*/ true , lastBinding, os);
382
365
if (!bindings.empty () || !definitions.empty ())
383
366
os << static_cast <uint8_t >(BIND_OPCODE_DONE);
384
367
}
@@ -396,23 +379,21 @@ bool macho::needsBinding(const Symbol *sym) {
396
379
}
397
380
398
381
void macho::addNonLazyBindingEntries (const Symbol *sym,
399
- SectionPointerUnion section ,
400
- uint64_t offset, int64_t addend) {
382
+ const InputSection *isec, uint64_t offset ,
383
+ int64_t addend) {
401
384
if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
402
- in.binding ->addEntry (dysym, section , offset, addend);
385
+ in.binding ->addEntry (dysym, isec , offset, addend);
403
386
if (dysym->isWeakDef ())
404
- in.weakBinding ->addEntry (sym, section , offset, addend);
387
+ in.weakBinding ->addEntry (sym, isec , offset, addend);
405
388
} else if (auto *defined = dyn_cast<Defined>(sym)) {
406
- in.rebase ->addEntry (section , offset);
389
+ in.rebase ->addEntry (isec , offset);
407
390
if (defined->isExternalWeakDef ())
408
- in.weakBinding ->addEntry (sym, section , offset, addend);
409
- } else if (!isa<DSOHandle>(sym)) {
391
+ in.weakBinding ->addEntry (sym, isec , offset, addend);
392
+ } else {
410
393
// Undefined symbols are filtered out in scanRelocations(); we should never
411
394
// get here
412
395
llvm_unreachable (" cannot bind to an undefined symbol" );
413
396
}
414
- // TODO: understand the DSOHandle case better.
415
- // Is it bindable? Add a new test?
416
397
}
417
398
418
399
StubsSection::StubsSection ()
@@ -538,7 +519,7 @@ void LazyBindingSection::writeTo(uint8_t *buf) const {
538
519
void LazyBindingSection::addEntry (DylibSymbol *dysym) {
539
520
if (entries.insert (dysym)) {
540
521
dysym->stubsHelperIndex = entries.size () - 1 ;
541
- in.rebase ->addEntry (in.lazyPointers , dysym->stubsIndex * WordSize);
522
+ in.rebase ->addEntry (in.lazyPointers -> isec , dysym->stubsIndex * WordSize);
542
523
}
543
524
}
544
525
@@ -572,9 +553,9 @@ void macho::prepareBranchTarget(Symbol *sym) {
572
553
if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
573
554
if (in.stubs ->addEntry (dysym)) {
574
555
if (sym->isWeakDef ()) {
575
- in.binding ->addEntry (dysym, in.lazyPointers ,
556
+ in.binding ->addEntry (dysym, in.lazyPointers -> isec ,
576
557
sym->stubsIndex * WordSize);
577
- in.weakBinding ->addEntry (sym, in.lazyPointers ,
558
+ in.weakBinding ->addEntry (sym, in.lazyPointers -> isec ,
578
559
sym->stubsIndex * WordSize);
579
560
} else {
580
561
in.lazyBinding ->addEntry (dysym);
@@ -583,8 +564,8 @@ void macho::prepareBranchTarget(Symbol *sym) {
583
564
} else if (auto *defined = dyn_cast<Defined>(sym)) {
584
565
if (defined->isExternalWeakDef ()) {
585
566
if (in.stubs ->addEntry (sym)) {
586
- in.rebase ->addEntry (in.lazyPointers , sym->stubsIndex * WordSize);
587
- in.weakBinding ->addEntry (sym, in.lazyPointers ,
567
+ in.rebase ->addEntry (in.lazyPointers -> isec , sym->stubsIndex * WordSize);
568
+ in.weakBinding ->addEntry (sym, in.lazyPointers -> isec ,
588
569
sym->stubsIndex * WordSize);
589
570
}
590
571
}
@@ -786,9 +767,10 @@ void SymtabSection::finalizeContents() {
786
767
787
768
for (Symbol *sym : symtab->getSymbols ()) {
788
769
if (auto *defined = dyn_cast<Defined>(sym)) {
770
+ if (defined->linkerInternal )
771
+ continue ;
789
772
assert (defined->isExternal ());
790
- (void )defined;
791
- addSymbol (externalSymbols, sym);
773
+ addSymbol (externalSymbols, defined);
792
774
} else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
793
775
if (dysym->isReferenced ())
794
776
addSymbol (undefinedSymbols, sym);
0 commit comments