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 const char* name() const { return _stubTo.name(); }
45 virtual uint64_t size() const { return 4; }
46 virtual uint64_t objectAddress() const { return 0; }
47 virtual void copyRawContent(uint8_t buffer[]) const { }
48 virtual void setScope(Scope) { }
49 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup1; }
50 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup2)[1]; }
53 const ld::Atom& _stubTo;
57 static ld::Section _s_section;
58 static ld::Section _s_sectionLazy;
61 ld::Section LazyPointerAtom::_s_section("__DATA", "__la_symbol_ptr", ld::Section::typeLazyPointer);
62 ld::Section LazyPointerAtom::_s_sectionLazy("__DATA", "__ld_symbol_ptr", ld::Section::typeLazyDylibPointer);
65 class StubPICAtom : public ld::Atom {
67 StubPICAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
68 bool forLazyDylib, bool weakImport)
69 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
70 ld::Atom::scopeLinkageUnit, ld::Atom::typeStub,
71 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
73 _lazyPointer(pass, stubTo, forLazyDylib, weakImport),
74 _fixup1(12, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, &_lazyPointer),
75 _fixup2(12, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, this),
76 _fixup3(12, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, 12),
77 _fixup4(12, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian32)
78 { pass.addAtom(*this); }
80 virtual const ld::File* file() const { return _stubTo.file(); }
81 virtual const char* name() const { return _stubTo.name(); }
82 virtual uint64_t size() const { return 16; }
83 virtual uint64_t objectAddress() const { return 0; }
84 virtual void copyRawContent(uint8_t buffer[]) const {
85 OSWriteLittleInt32(&buffer[ 0], 0, 0xe59fc004); // ldr ip, pc + 12
86 OSWriteLittleInt32(&buffer[ 4], 0, 0xe08fc00c); // add ip, pc, ip
87 OSWriteLittleInt32(&buffer[ 8], 0, 0xe59cf000); // ldr pc, [ip]
88 OSWriteLittleInt32(&buffer[12], 0, 0x00000000); // .long L_foo$lazy_ptr - (L1$scv + 8)
90 virtual void setScope(Scope) { }
91 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup1; }
92 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup4)[1]; }
95 const ld::Atom& _stubTo;
96 LazyPointerAtom _lazyPointer;
102 static ld::Section _s_section;
105 ld::Section StubPICAtom::_s_section("__TEXT", "__picsymbolstub4", ld::Section::typeStub);
109 class StubNoPICAtom : public ld::Atom {
111 StubNoPICAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
112 bool forLazyDylib, bool weakImport)
113 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
114 ld::Atom::scopeLinkageUnit, ld::Atom::typeStub,
115 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
117 _lazyPointer(pass, stubTo, forLazyDylib, weakImport),
118 _fixup(8, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, &_lazyPointer)
119 { pass.addAtom(*this); }
121 virtual const ld::File* file() const { return _stubTo.file(); }
122 virtual const char* name() const { return _stubTo.name(); }
123 virtual uint64_t size() const { return 12; }
124 virtual uint64_t objectAddress() const { return 0; }
125 virtual void copyRawContent(uint8_t buffer[]) const {
126 OSWriteLittleInt32(&buffer[ 0], 0, 0xe59fc000); // ldr ip, [pc, #0]
127 OSWriteLittleInt32(&buffer[ 4], 0, 0xe59cf000); // ldr pc, [ip]
128 OSWriteLittleInt32(&buffer[ 8], 0, 0x00000000); // .long L_foo$lazy_ptr
130 virtual void setScope(Scope) { }
131 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup; }
132 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup)[1]; }
135 const ld::Atom& _stubTo;
136 LazyPointerAtom _lazyPointer;
139 static ld::Section _s_section;
142 ld::Section StubNoPICAtom::_s_section("__TEXT", "__symbol_stub4", ld::Section::typeStub);
147 } // namespace classic