]> git.saurik.com Git - apple/ld64.git/blob - src/ld/passes/stubs/stub_arm_classic.hpp
ld64-127.2.tar.gz
[apple/ld64.git] / src / ld / passes / stubs / stub_arm_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 // already in ld::passes::stubs namespace
26 namespace arm {
27 namespace classic {
28
29
30 class LazyPointerAtom : public ld::Atom {
31 public:
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)),
37 _stubTo(stubTo),
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); }
42
43 virtual const ld::File* file() const { return _stubTo.file(); }
44 virtual bool translationUnitSource(const char** dir, const char** nm) const
45 { return false; }
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]; }
53
54 private:
55 const ld::Atom& _stubTo;
56 ld::Fixup _fixup1;
57 ld::Fixup _fixup2;
58
59 static ld::Section _s_section;
60 static ld::Section _s_sectionLazy;
61 };
62
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);
65
66
67 class StubPICAtom : public ld::Atom {
68 public:
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)),
74 _stubTo(stubTo),
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); }
81
82 virtual const ld::File* file() const { return _stubTo.file(); }
83 virtual bool translationUnitSource(const char** dir, const char** nm) const
84 { return false; }
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)
93 }
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]; }
97
98 private:
99 const ld::Atom& _stubTo;
100 LazyPointerAtom _lazyPointer;
101 ld::Fixup _fixup1;
102 ld::Fixup _fixup2;
103 ld::Fixup _fixup3;
104 ld::Fixup _fixup4;
105
106 static ld::Section _s_section;
107 };
108
109 ld::Section StubPICAtom::_s_section("__TEXT", "__picsymbolstub4", ld::Section::typeStub);
110
111
112
113 class StubNoPICAtom : public ld::Atom {
114 public:
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)),
120 _stubTo(stubTo),
121 _lazyPointer(pass, stubTo, forLazyDylib, weakImport),
122 _fixup(8, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, &_lazyPointer)
123 { pass.addAtom(*this); }
124
125 virtual const ld::File* file() const { return _stubTo.file(); }
126 virtual bool translationUnitSource(const char** dir, const char** nm) const
127 { return false; }
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
135 }
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]; }
139
140 private:
141 const ld::Atom& _stubTo;
142 LazyPointerAtom _lazyPointer;
143 ld::Fixup _fixup;
144
145 static ld::Section _s_section;
146 };
147
148 ld::Section StubNoPICAtom::_s_section("__TEXT", "__symbol_stub4", ld::Section::typeStub);
149
150
151
152
153 } // namespace classic
154 } // namespace arm
155