]>
Commit | Line | Data |
---|---|---|
a645023d A |
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 | #ifndef __OUTPUT_FILE_H__ | |
26 | #define __OUTPUT_FILE_H__ | |
27 | ||
28 | #include <stdlib.h> | |
29 | #include <sys/types.h> | |
30 | #include <sys/stat.h> | |
31 | #include <sys/mman.h> | |
32 | #include <sys/sysctl.h> | |
33 | #include <fcntl.h> | |
34 | #include <errno.h> | |
35 | #include <limits.h> | |
36 | #include <unistd.h> | |
37 | #include <mach/mach_time.h> | |
38 | #include <mach/vm_statistics.h> | |
39 | #include <mach/mach_init.h> | |
40 | #include <mach/mach_host.h> | |
41 | #include <dlfcn.h> | |
42 | #include <mach-o/dyld.h> | |
43 | ||
44 | #include <vector> | |
45 | ||
46 | #include "Options.h" | |
47 | #include "ld.hpp" | |
48 | ||
49 | namespace ld { | |
50 | namespace tool { | |
51 | ||
52 | class OutputFile | |
53 | { | |
54 | public: | |
55 | OutputFile(const Options& opts); | |
56 | ||
57 | ||
58 | // iterates all atoms in initial files | |
59 | void write(ld::Internal&); | |
60 | bool findSegment(ld::Internal& state, uint64_t addr, uint64_t* start, uint64_t* end, uint32_t* index); | |
61 | void setLazyBindingInfoOffset(uint64_t lpAddress, uint32_t lpInfoOffset); | |
62 | uint32_t dylibCount(); | |
63 | const ld::dylib::File* dylibByOrdinal(unsigned int ordinal); | |
64 | uint32_t dylibToOrdinal(const ld::dylib::File*); | |
65 | uint32_t encryptedTextStartOffset() { return _encryptedTEXTstartOffset; } | |
66 | uint32_t encryptedTextEndOffset() { return _encryptedTEXTendOffset; } | |
67 | int compressedOrdinalForAtom(const ld::Atom* target); | |
b2fa67a8 | 68 | uint64_t fileSize() const { return _fileSize; } |
a645023d A |
69 | |
70 | ||
71 | bool hasWeakExternalSymbols; | |
72 | bool usesWeakExternalSymbols; | |
73 | bool overridesWeakExternalSymbols; | |
74 | bool _noReExportedDylibs; | |
75 | bool hasThreadLocalVariableDefinitions; | |
76 | bool pieDisabled; | |
77 | ld::Internal::FinalSection* headerAndLoadCommandsSection; | |
78 | ld::Internal::FinalSection* rebaseSection; | |
79 | ld::Internal::FinalSection* bindingSection; | |
80 | ld::Internal::FinalSection* weakBindingSection; | |
81 | ld::Internal::FinalSection* lazyBindingSection; | |
82 | ld::Internal::FinalSection* exportSection; | |
83 | ld::Internal::FinalSection* splitSegInfoSection; | |
84 | ld::Internal::FinalSection* functionStartsSection; | |
85 | ld::Internal::FinalSection* symbolTableSection; | |
86 | ld::Internal::FinalSection* stringPoolSection; | |
87 | ld::Internal::FinalSection* localRelocationsSection; | |
88 | ld::Internal::FinalSection* externalRelocationsSection; | |
89 | ld::Internal::FinalSection* sectionRelocationsSection; | |
90 | ld::Internal::FinalSection* indirectSymbolTableSection; | |
91 | ||
92 | struct RebaseInfo { | |
93 | RebaseInfo(uint8_t t, uint64_t addr) : _type(t), _address(addr) {} | |
94 | uint8_t _type; | |
95 | uint64_t _address; | |
96 | // for sorting | |
97 | int operator<(const RebaseInfo& rhs) const { | |
98 | // sort by type, then address | |
99 | if ( this->_type != rhs._type ) | |
100 | return (this->_type < rhs._type ); | |
101 | return (this->_address < rhs._address ); | |
102 | } | |
103 | }; | |
104 | ||
105 | struct BindingInfo { | |
106 | BindingInfo(uint8_t t, int ord, const char* sym, bool weak_import, uint64_t addr, int64_t add) | |
107 | : _type(t), _flags(weak_import ? BIND_SYMBOL_FLAGS_WEAK_IMPORT : 0 ), _libraryOrdinal(ord), | |
108 | _symbolName(sym), _address(addr), _addend(add) {} | |
109 | BindingInfo(uint8_t t, const char* sym, bool non_weak_definition, uint64_t addr, int64_t add) | |
110 | : _type(t), _flags(non_weak_definition ? BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION : 0 ), | |
111 | _libraryOrdinal(0), _symbolName(sym), _address(addr), _addend(add) {} | |
112 | uint8_t _type; | |
113 | uint8_t _flags; | |
114 | int _libraryOrdinal; | |
115 | const char* _symbolName; | |
116 | uint64_t _address; | |
117 | int64_t _addend; | |
118 | ||
119 | // for sorting | |
120 | int operator<(const BindingInfo& rhs) const { | |
121 | // sort by library, symbol, type, then address | |
122 | if ( this->_libraryOrdinal != rhs._libraryOrdinal ) | |
123 | return (this->_libraryOrdinal < rhs._libraryOrdinal ); | |
124 | if ( this->_symbolName != rhs._symbolName ) | |
125 | return ( strcmp(this->_symbolName, rhs._symbolName) < 0 ); | |
126 | if ( this->_type != rhs._type ) | |
127 | return (this->_type < rhs._type ); | |
128 | return (this->_address < rhs._address ); | |
129 | } | |
130 | }; | |
131 | ||
132 | struct SplitSegInfoEntry { | |
afe874b1 | 133 | SplitSegInfoEntry(uint64_t a, ld::Fixup::Kind k, uint32_t e=0) : address(a), kind(k), extra(e) {} |
a645023d A |
134 | uint64_t address; |
135 | ld::Fixup::Kind kind; | |
afe874b1 | 136 | uint32_t extra; |
a645023d A |
137 | }; |
138 | ||
139 | private: | |
140 | void buildDylibOrdinalMapping(ld::Internal&); | |
141 | bool hasOrdinalForInstallPath(const char* path, int* ordinal); | |
142 | void addLoadCommands(ld::Internal& state); | |
143 | void addLinkEdit(ld::Internal& state); | |
144 | void addPreloadLinkEdit(ld::Internal& state); | |
145 | void generateLinkEditInfo(ld::Internal& state); | |
146 | void buildSymbolTable(ld::Internal& state); | |
147 | void writeOutputFile(ld::Internal& state); | |
148 | void assignFileOffsets(ld::Internal& state); | |
149 | void setSectionSizesAndAlignments(ld::Internal& state); | |
150 | void addSectionRelocs(ld::Internal& state, ld::Internal::FinalSection* sect, | |
151 | const ld::Atom* atom, ld::Fixup* fixupWithTarget, | |
152 | ld::Fixup* fixupWithMinusTarget, ld::Fixup* fixupWithStore, | |
153 | const ld::Atom* target, const ld::Atom* minusTarget, | |
154 | uint64_t targetAddend, uint64_t minusTargetAddend); | |
155 | void addDyldInfo(ld::Internal& state, ld::Internal::FinalSection* sect, | |
156 | const ld::Atom* atom, ld::Fixup* fixupWithTarget, | |
157 | ld::Fixup* fixupWithMinusTarget, ld::Fixup* fixupWithStore, | |
158 | const ld::Atom* target, const ld::Atom* minusTarget, | |
159 | uint64_t targetAddend, uint64_t minusTargetAddend); | |
160 | void addClassicRelocs(ld::Internal& state, ld::Internal::FinalSection* sect, | |
161 | const ld::Atom* atom, ld::Fixup* fixupWithTarget, | |
162 | ld::Fixup* fixupWithMinusTarget, ld::Fixup* fixupWithStore, | |
163 | const ld::Atom* target, const ld::Atom* minusTarget, | |
164 | uint64_t targetAddend, uint64_t minusTargetAddend); | |
165 | bool useExternalSectionReloc(const ld::Atom* atom, const ld::Atom* target, | |
166 | ld::Fixup* fixupWithTarget); | |
167 | uint64_t pageAlign(uint64_t addr); | |
168 | uint64_t pageAlign(uint64_t addr, uint64_t pageSize); | |
169 | void setLoadCommandsPadding(ld::Internal& state); | |
170 | void assignAtomAddresses(ld::Internal& state); | |
171 | void addRebaseInfo(const ld::Atom* atom, const ld::Fixup* fixup, const ld::Atom* target); | |
172 | void makeRebasingInfo(ld::Internal& state); | |
173 | void makeBindingInfo(ld::Internal& state); | |
174 | void updateLINKEDITAddresses(ld::Internal& state); | |
175 | void applyFixUps(ld::Internal& state, uint64_t mhAddress, const ld::Atom* atom, uint8_t* buffer); | |
176 | uint64_t addressOf(const ld::Internal& state, const ld::Fixup* fixup, const ld::Atom** target); | |
177 | bool targetIsThumb(ld::Internal& state, const ld::Fixup* fixup); | |
178 | uint32_t lazyBindingInfoOffsetForLazyPointerAddress(uint64_t lpAddress); | |
afe874b1 | 179 | void copyNoOps(uint8_t* from, uint8_t* to, bool thumb); |
a645023d A |
180 | bool isPointerToTarget(ld::Fixup::Kind kind); |
181 | bool isPointerFromTarget(ld::Fixup::Kind kind); | |
182 | bool isPcRelStore(ld::Fixup::Kind kind); | |
183 | bool isStore(ld::Fixup::Kind kind); | |
184 | bool storeAddendOnly(const ld::Atom* inAtom, const ld::Atom* target, bool pcRel=false); | |
185 | bool setsTarget(ld::Fixup::Kind kind); | |
186 | void addFixupOutInfo(ld::Internal& state); | |
187 | void makeRelocations(ld::Internal& state); | |
188 | void makeSectionRelocations(ld::Internal& state); | |
189 | void makeDyldInfo(ld::Internal& state); | |
190 | void makeSplitSegInfo(ld::Internal& state); | |
191 | void writeMapFile(ld::Internal& state); | |
192 | uint64_t lookBackAddend(ld::Fixup::iterator fit); | |
193 | bool takesNoDiskSpace(const ld::Section* sect); | |
194 | bool hasZeroForFileOffset(const ld::Section* sect); | |
195 | ||
196 | void printSectionLayout(ld::Internal& state); | |
197 | void rangeCheck8(int64_t delta, ld::Internal& state, const ld::Atom* atom, | |
198 | const ld::Fixup* fixup); | |
199 | void rangeCheck16(int64_t delta, ld::Internal& state, const ld::Atom* atom, | |
200 | const ld::Fixup* fixup); | |
201 | void rangeCheckBranch32(int64_t delta, ld::Internal& state, const ld::Atom* atom, | |
202 | const ld::Fixup* fixup); | |
afe874b1 A |
203 | void rangeCheckAbsolute32(int64_t delta, ld::Internal& state, const ld::Atom* atom, |
204 | const ld::Fixup* fixup); | |
a645023d A |
205 | void rangeCheckRIP32(int64_t delta, ld::Internal& state, const ld::Atom* atom, |
206 | const ld::Fixup* fixup); | |
207 | void rangeCheckARM12(int64_t delta, ld::Internal& state, const ld::Atom* atom, | |
208 | const ld::Fixup* fixup); | |
209 | void rangeCheckARMBranch24(int64_t delta, ld::Internal& state, const ld::Atom* atom, | |
210 | const ld::Fixup* fixup); | |
211 | void rangeCheckThumbBranch22(int64_t delta, ld::Internal& state, const ld::Atom* atom, | |
212 | const ld::Fixup* fixup); | |
a645023d A |
213 | uint64_t sectionOffsetOf(const ld::Internal& state, const ld::Fixup* fixup); |
214 | uint64_t tlvTemplateOffsetOf(const ld::Internal& state, const ld::Fixup* fixup); | |
215 | void dumpAtomsBySection(ld::Internal& state, bool); | |
216 | void synthesizeDebugNotes(ld::Internal& state); | |
217 | const char* assureFullPath(const char* path); | |
218 | void noteTextReloc(const ld::Atom* atom, const ld::Atom* target); | |
219 | ||
220 | ||
221 | static uint16_t get16LE(uint8_t* loc); | |
222 | static void set16LE(uint8_t* loc, uint16_t value); | |
223 | static uint32_t get32LE(uint8_t* loc); | |
224 | static void set32LE(uint8_t* loc, uint32_t value); | |
225 | static uint64_t get64LE(uint8_t* loc); | |
226 | static void set64LE(uint8_t* loc, uint64_t value); | |
227 | ||
228 | static uint16_t get16BE(uint8_t* loc); | |
229 | static void set16BE(uint8_t* loc, uint16_t value); | |
230 | static uint32_t get32BE(uint8_t* loc); | |
231 | static void set32BE(uint8_t* loc, uint32_t value); | |
232 | static uint64_t get64BE(uint8_t* loc); | |
233 | static void set64BE(uint8_t* loc, uint64_t value); | |
234 | ||
235 | ||
236 | ||
237 | const Options& _options; | |
238 | std::map<const ld::dylib::File*, int> _dylibToOrdinal; | |
239 | std::vector<const ld::dylib::File*> _dylibsToLoad; | |
240 | std::vector<const char*> _dylibOrdinalPaths; | |
241 | const bool _hasDyldInfo; | |
242 | const bool _hasSymbolTable; | |
243 | const bool _hasSectionRelocations; | |
244 | const bool _hasSplitSegInfo; | |
245 | const bool _hasFunctionStartsInfo; | |
246 | bool _hasDynamicSymbolTable; | |
247 | bool _hasLocalRelocations; | |
248 | bool _hasExternalRelocations; | |
249 | uint64_t _fileSize; | |
250 | std::map<uint64_t, uint32_t> _lazyPointerAddressToInfoOffset; | |
251 | uint32_t _encryptedTEXTstartOffset; | |
252 | uint32_t _encryptedTEXTendOffset; | |
253 | public: | |
254 | std::vector<const ld::Atom*> _localAtoms; | |
255 | std::vector<const ld::Atom*> _exportedAtoms; | |
256 | std::vector<const ld::Atom*> _importedAtoms; | |
257 | uint32_t _localSymbolsStartIndex; | |
258 | uint32_t _localSymbolsCount; | |
259 | uint32_t _globalSymbolsStartIndex; | |
260 | uint32_t _globalSymbolsCount; | |
261 | uint32_t _importSymbolsStartIndex; | |
262 | uint32_t _importSymbolsCount; | |
263 | std::map<const ld::Atom*, uint32_t> _atomToSymbolIndex; | |
264 | std::vector<RebaseInfo> _rebaseInfo; | |
265 | std::vector<BindingInfo> _bindingInfo; | |
266 | std::vector<BindingInfo> _lazyBindingInfo; | |
267 | std::vector<BindingInfo> _weakBindingInfo; | |
268 | std::vector<SplitSegInfoEntry> _splitSegInfos; | |
269 | class HeaderAndLoadCommandsAbtract* _headersAndLoadCommandAtom; | |
270 | class RelocationsAtomAbstract* _sectionsRelocationsAtom; | |
271 | class RelocationsAtomAbstract* _localRelocsAtom; | |
272 | class RelocationsAtomAbstract* _externalRelocsAtom; | |
273 | class ClassicLinkEditAtom* _symbolTableAtom; | |
274 | class ClassicLinkEditAtom* _indirectSymbolTableAtom; | |
275 | class StringPoolAtom* _stringPoolAtom; | |
276 | class LinkEditAtom* _rebasingInfoAtom; | |
277 | class LinkEditAtom* _bindingInfoAtom; | |
278 | class LinkEditAtom* _lazyBindingInfoAtom; | |
279 | class LinkEditAtom* _weakBindingInfoAtom; | |
280 | class LinkEditAtom* _exportInfoAtom; | |
281 | class LinkEditAtom* _splitSegInfoAtom; | |
282 | class LinkEditAtom* _functionStartsAtom; | |
283 | }; | |
284 | ||
285 | } // namespace tool | |
286 | } // namespace ld | |
287 | ||
288 | #endif // __OUTPUT_FILE_H__ |