]> git.saurik.com Git - apple/ld64.git/blob - src/ld/passes/stubs/stub_ppc_classic.hpp
6862b603b6550f4aa35a573aeb71a808e6c5b52f
[apple/ld64.git] / src / ld / passes / stubs / stub_ppc_classic.hpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2009 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
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
12 * file.
13 *
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.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25
26 // already in ld::passes::stubs namespace
27 namespace ppc {
28 namespace classic {
29
30
31
32 class LazyPointerAtom : public ld::Atom {
33 public:
34 LazyPointerAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
35 bool forLazyDylib, bool for64, bool weakImport)
36 : ld::Atom( forLazyDylib ? _s_sectionLazy : _s_section,
37 ld::Atom::definitionRegular, ld::Atom::combineNever,
38 ld::Atom::scopeTranslationUnit,
39 forLazyDylib ? ld::Atom::typeLazyDylibPointer : ld::Atom::typeLazyPointer,
40 symbolTableNotIn, false, false, false, for64 ? ld::Atom::Alignment(3) : ld::Atom::Alignment(2)),
41 _stubTo(stubTo),
42 _fixup1(0, ld::Fixup::k1of1,
43 for64 ? ld::Fixup::kindStoreTargetAddressBigEndian64 : ld::Fixup::kindStoreTargetAddressBigEndian32,
44 forLazyDylib ? pass.internal()->lazyBindingHelper : pass.internal()->classicBindingHelper),
45 _fixup2(0, ld::Fixup::k1of1, ld::Fixup::kindLazyTarget, &stubTo),
46 _for64(for64)
47 { _fixup2.weakImport = weakImport; pass.addAtom(*this); }
48
49 virtual const ld::File* file() const { return _stubTo.file(); }
50 virtual bool translationUnitSource(const char** dir, const char** ) const
51 { return false; }
52 virtual const char* name() const { return _stubTo.name(); }
53 virtual uint64_t size() const { return _for64 ? 8 : 4; }
54 virtual uint64_t objectAddress() const { return 0; }
55 virtual void copyRawContent(uint8_t buffer[]) const { }
56 virtual void setScope(Scope) { }
57 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
58 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup2)[1]; }
59
60 private:
61 const ld::Atom& _stubTo;
62 mutable ld::Fixup _fixup1;
63 mutable ld::Fixup _fixup2;
64 const bool _for64;
65
66 static ld::Section _s_section;
67 static ld::Section _s_sectionLazy;
68 };
69
70 ld::Section LazyPointerAtom::_s_section("__DATA", "__la_symbol_ptr", ld::Section::typeLazyPointer);
71 ld::Section LazyPointerAtom::_s_sectionLazy("__DATA", "__ld_symbol_ptr", ld::Section::typeLazyDylibPointer);
72
73
74
75 class StubPICAtom : public ld::Atom {
76 public:
77 StubPICAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
78 bool forLazyDylib, bool for64, bool weakImport)
79 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
80 ld::Atom::scopeLinkageUnit, ld::Atom::typeStub,
81 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
82 _stubTo(stubTo),
83 _lazyPointer(pass, stubTo, forLazyDylib, for64, weakImport),
84 _fixup1(12, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, &_lazyPointer),
85 _fixup2(12, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, this),
86 _fixup3(12, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, 8),
87 _fixup4(12, ld::Fixup::k4of4, ld::Fixup::kindStorePPCPicHigh16AddLow),
88 _fixup5(20, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, &_lazyPointer),
89 _fixup6(20, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, this),
90 _fixup7(20, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, 8),
91 _fixup8(20, ld::Fixup::k4of4, for64 ? ld::Fixup::kindStorePPCPicLow14 : ld::Fixup::kindStorePPCPicLow16),
92 _for64(for64)
93 { pass.addAtom(*this); }
94
95 virtual const ld::File* file() const { return _stubTo.file(); }
96 virtual bool translationUnitSource(const char** dir, const char** ) const
97 { return false; }
98 virtual const char* name() const { return _stubTo.name(); }
99 virtual uint64_t size() const { return 32; }
100 virtual uint64_t objectAddress() const { return 0; }
101 virtual void copyRawContent(uint8_t buffer[]) const {
102 OSWriteBigInt32(&buffer[ 0], 0, 0x7c0802a6); // mflr r0
103 OSWriteBigInt32(&buffer[ 4], 0, 0x429f0005); // bcl 20,31,Lpicbase
104 OSWriteBigInt32(&buffer[ 8], 0, 0x7d6802a6); // Lpicbase: mflr r11
105 OSWriteBigInt32(&buffer[12], 0, 0x3d6b0000); // addis r11,r11,ha16(L_fwrite$lazy_ptr-Lpicbase)
106 OSWriteBigInt32(&buffer[16], 0, 0x7c0803a6); // mtlr r0
107 if ( _for64 )
108 OSWriteBigInt32(&buffer[20], 0, 0xe98b0001);// ldu r12,lo16(L_fwrite$lazy_ptr-Lpicbase)(r11)
109 else
110 OSWriteBigInt32(&buffer[20], 0, 0x858b0000);// lwzu r12,lo16(L_fwrite$lazy_ptr-Lpicbase)(r11)
111 OSWriteBigInt32(&buffer[24], 0, 0x7d8903a6); // mtctr r12
112 OSWriteBigInt32(&buffer[28], 0, 0x4e800420); // bctr
113 }
114 virtual void setScope(Scope) { }
115 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
116 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup8)[1]; }
117
118 private:
119 const ld::Atom& _stubTo;
120 LazyPointerAtom _lazyPointer;
121 mutable ld::Fixup _fixup1;
122 mutable ld::Fixup _fixup2;
123 mutable ld::Fixup _fixup3;
124 mutable ld::Fixup _fixup4;
125 mutable ld::Fixup _fixup5;
126 mutable ld::Fixup _fixup6;
127 mutable ld::Fixup _fixup7;
128 mutable ld::Fixup _fixup8;
129 const bool _for64;
130
131 static ld::Section _s_section;
132 };
133
134 ld::Section StubPICAtom::_s_section("__TEXT", "__picsymbolstub1", ld::Section::typeStub);
135
136
137
138 class StubNoPICAtom : public ld::Atom {
139 public:
140 StubNoPICAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
141 bool forLazyDylib, bool for64, bool weakImport)
142 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
143 ld::Atom::scopeLinkageUnit, ld::Atom::typeStub,
144 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
145 _stubTo(stubTo),
146 _lazyPointer(pass, stubTo, forLazyDylib, for64, weakImport),
147 _fixup1(0, ld::Fixup::k1of2, ld::Fixup::kindSetTargetAddress, &_lazyPointer),
148 _fixup2(0, ld::Fixup::k2of2, ld::Fixup::kindStorePPCAbsHigh16AddLow),
149 _fixup3(4, ld::Fixup::k1of2, ld::Fixup::kindSetTargetAddress, &_lazyPointer),
150 _fixup4(4, ld::Fixup::k2of2, for64 ? ld::Fixup::kindStorePPCAbsLow14 : ld::Fixup::kindStorePPCAbsLow16),
151 _for64(for64)
152 { pass.addAtom(*this); }
153
154 virtual const ld::File* file() const { return _stubTo.file(); }
155 virtual bool translationUnitSource(const char** dir, const char** ) const
156 { return false; }
157 virtual const char* name() const { return _stubTo.name(); }
158 virtual uint64_t size() const { return 16; }
159 virtual uint64_t objectAddress() const { return 0; }
160 virtual void copyRawContent(uint8_t buffer[]) const {
161 OSWriteBigInt32(&buffer[ 0], 0, 0x3d600000); // lis r11,ha16(L_foo$lazy_ptr)
162 if ( _for64 )
163 OSWriteBigInt32(&buffer[ 4], 0, 0xe98b0001);// ldu r12,lo16(L_foo$lazy_ptr)(r11)
164 else
165 OSWriteBigInt32(&buffer[ 4], 0, 0x858b0000);// lwzu r12,lo16(L_foo$lazy_ptr)(r11)
166 OSWriteBigInt32(&buffer[ 8], 0, 0x7d8903a6); // mtctr r12
167 OSWriteBigInt32(&buffer[12], 0, 0x4e800420); // bctr
168 }
169 virtual void setScope(Scope) { }
170 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
171 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup4)[1]; }
172
173 private:
174 const ld::Atom& _stubTo;
175 LazyPointerAtom _lazyPointer;
176 mutable ld::Fixup _fixup1;
177 mutable ld::Fixup _fixup2;
178 mutable ld::Fixup _fixup3;
179 mutable ld::Fixup _fixup4;
180 const bool _for64;
181
182 static ld::Section _s_section;
183 };
184
185 ld::Section StubNoPICAtom::_s_section("__TEXT", "__symbol_stub1", ld::Section::typeStub);
186
187
188 } // namespace classic
189 } // namespace ppc
190