1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2009 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
25 // already in ld::passes::stubs namespace
30 class LazyPointerAtom : public ld::Atom {
32 LazyPointerAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
33 bool forLazyDylib, bool weakImport)
34 : ld::Atom(forLazyDylib ? _s_sectionLazy : _s_section, ld::Atom::definitionRegular,
35 ld::Atom::combineNever, ld::Atom::scopeLinkageUnit, ld::Atom::typeLazyPointer,
36 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
38 _fixup1(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32,
39 forLazyDylib ? pass.internal()->lazyBindingHelper : pass.internal()->classicBindingHelper),
40 _fixup2(0, ld::Fixup::k1of1, ld::Fixup::kindLazyTarget, &stubTo)
41 { _fixup2.weakImport = weakImport; pass.addAtom(*this); }
43 virtual const ld::File* file() const { return _stubTo.file(); }
44 virtual bool translationUnitSource(const char** dir, const char** nm) const
46 virtual const char* name() const { return _stubTo.name(); }
47 virtual uint64_t size() const { return 4; }
48 virtual uint64_t objectAddress() const { return 0; }
49 virtual void copyRawContent(uint8_t buffer[]) const { }
50 virtual void setScope(Scope) { }
51 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup1; }
52 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup2)[1]; }
55 const ld::Atom& _stubTo;
59 static ld::Section _s_section;
60 static ld::Section _s_sectionLazy;
63 ld::Section LazyPointerAtom::_s_section("__DATA", "__la_symbol_ptr", ld::Section::typeLazyPointer);
64 ld::Section LazyPointerAtom::_s_sectionLazy("__DATA", "__ld_symbol_ptr", ld::Section::typeLazyDylibPointer);
67 class StubPICAtom : public ld::Atom {
69 StubPICAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
70 bool forLazyDylib, bool weakImport)
71 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
72 ld::Atom::scopeLinkageUnit, ld::Atom::typeStub,
73 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
75 _lazyPointer(pass, stubTo, forLazyDylib, weakImport),
76 _fixup1(12, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, &_lazyPointer),
77 _fixup2(12, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, this),
78 _fixup3(12, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, 12),
79 _fixup4(12, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian32)
80 { pass.addAtom(*this); }
82 virtual const ld::File* file() const { return _stubTo.file(); }
83 virtual bool translationUnitSource(const char** dir, const char** nm) const
85 virtual const char* name() const { return _stubTo.name(); }
86 virtual uint64_t size() const { return 16; }
87 virtual uint64_t objectAddress() const { return 0; }
88 virtual void copyRawContent(uint8_t buffer[]) const {
89 OSWriteLittleInt32(&buffer[ 0], 0, 0xe59fc004); // ldr ip, pc + 12
90 OSWriteLittleInt32(&buffer[ 4], 0, 0xe08fc00c); // add ip, pc, ip
91 OSWriteLittleInt32(&buffer[ 8], 0, 0xe59cf000); // ldr pc, [ip]
92 OSWriteLittleInt32(&buffer[12], 0, 0x00000000); // .long L_foo$lazy_ptr - (L1$scv + 8)
94 virtual void setScope(Scope) { }
95 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup1; }
96 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup4)[1]; }
99 const ld::Atom& _stubTo;
100 LazyPointerAtom _lazyPointer;
106 static ld::Section _s_section;
109 ld::Section StubPICAtom::_s_section("__TEXT", "__picsymbolstub4", ld::Section::typeStub);
113 class StubNoPICAtom : public ld::Atom {
115 StubNoPICAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
116 bool forLazyDylib, bool weakImport)
117 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
118 ld::Atom::scopeLinkageUnit, ld::Atom::typeStub,
119 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
121 _lazyPointer(pass, stubTo, forLazyDylib, weakImport),
122 _fixup(8, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, &_lazyPointer)
123 { pass.addAtom(*this); }
125 virtual const ld::File* file() const { return _stubTo.file(); }
126 virtual bool translationUnitSource(const char** dir, const char** nm) const
128 virtual const char* name() const { return _stubTo.name(); }
129 virtual uint64_t size() const { return 12; }
130 virtual uint64_t objectAddress() const { return 0; }
131 virtual void copyRawContent(uint8_t buffer[]) const {
132 OSWriteLittleInt32(&buffer[ 0], 0, 0xe59fc000); // ldr ip, [pc, #0]
133 OSWriteLittleInt32(&buffer[ 4], 0, 0xe59cf000); // ldr pc, [ip]
134 OSWriteLittleInt32(&buffer[ 8], 0, 0x00000000); // .long L_foo$lazy_ptr
136 virtual void setScope(Scope) { }
137 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup; }
138 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup)[1]; }
141 const ld::Atom& _stubTo;
142 LazyPointerAtom _lazyPointer;
145 static ld::Section _s_section;
148 ld::Section StubNoPICAtom::_s_section("__TEXT", "__symbol_stub4", ld::Section::typeStub);
153 } // namespace classic