]> git.saurik.com Git - apple/ld64.git/blame - src/ld/OutputFile.h
ld64-127.2.tar.gz
[apple/ld64.git] / src / ld / OutputFile.h
CommitLineData
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
49namespace ld {
50namespace tool {
51
52class OutputFile
53{
54public:
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);
68
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
139private:
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);
213 void rangeCheckPPCBranch24(int64_t delta, ld::Internal& state, const ld::Atom* atom,
214 const ld::Fixup* fixup);
215 void rangeCheckPPCBranch14(int64_t delta, ld::Internal& state, const ld::Atom* atom,
216 const ld::Fixup* fixup);
217 uint64_t sectionOffsetOf(const ld::Internal& state, const ld::Fixup* fixup);
218 uint64_t tlvTemplateOffsetOf(const ld::Internal& state, const ld::Fixup* fixup);
219 void dumpAtomsBySection(ld::Internal& state, bool);
220 void synthesizeDebugNotes(ld::Internal& state);
221 const char* assureFullPath(const char* path);
222 void noteTextReloc(const ld::Atom* atom, const ld::Atom* target);
223
224
225 static uint16_t get16LE(uint8_t* loc);
226 static void set16LE(uint8_t* loc, uint16_t value);
227 static uint32_t get32LE(uint8_t* loc);
228 static void set32LE(uint8_t* loc, uint32_t value);
229 static uint64_t get64LE(uint8_t* loc);
230 static void set64LE(uint8_t* loc, uint64_t value);
231
232 static uint16_t get16BE(uint8_t* loc);
233 static void set16BE(uint8_t* loc, uint16_t value);
234 static uint32_t get32BE(uint8_t* loc);
235 static void set32BE(uint8_t* loc, uint32_t value);
236 static uint64_t get64BE(uint8_t* loc);
237 static void set64BE(uint8_t* loc, uint64_t value);
238
239
240
241 const Options& _options;
242 std::map<const ld::dylib::File*, int> _dylibToOrdinal;
243 std::vector<const ld::dylib::File*> _dylibsToLoad;
244 std::vector<const char*> _dylibOrdinalPaths;
245 const bool _hasDyldInfo;
246 const bool _hasSymbolTable;
247 const bool _hasSectionRelocations;
248 const bool _hasSplitSegInfo;
249 const bool _hasFunctionStartsInfo;
250 bool _hasDynamicSymbolTable;
251 bool _hasLocalRelocations;
252 bool _hasExternalRelocations;
253 uint64_t _fileSize;
254 std::map<uint64_t, uint32_t> _lazyPointerAddressToInfoOffset;
255 uint32_t _encryptedTEXTstartOffset;
256 uint32_t _encryptedTEXTendOffset;
257public:
258 std::vector<const ld::Atom*> _localAtoms;
259 std::vector<const ld::Atom*> _exportedAtoms;
260 std::vector<const ld::Atom*> _importedAtoms;
261 uint32_t _localSymbolsStartIndex;
262 uint32_t _localSymbolsCount;
263 uint32_t _globalSymbolsStartIndex;
264 uint32_t _globalSymbolsCount;
265 uint32_t _importSymbolsStartIndex;
266 uint32_t _importSymbolsCount;
267 std::map<const ld::Atom*, uint32_t> _atomToSymbolIndex;
268 std::vector<RebaseInfo> _rebaseInfo;
269 std::vector<BindingInfo> _bindingInfo;
270 std::vector<BindingInfo> _lazyBindingInfo;
271 std::vector<BindingInfo> _weakBindingInfo;
272 std::vector<SplitSegInfoEntry> _splitSegInfos;
273 class HeaderAndLoadCommandsAbtract* _headersAndLoadCommandAtom;
274 class RelocationsAtomAbstract* _sectionsRelocationsAtom;
275 class RelocationsAtomAbstract* _localRelocsAtom;
276 class RelocationsAtomAbstract* _externalRelocsAtom;
277 class ClassicLinkEditAtom* _symbolTableAtom;
278 class ClassicLinkEditAtom* _indirectSymbolTableAtom;
279 class StringPoolAtom* _stringPoolAtom;
280 class LinkEditAtom* _rebasingInfoAtom;
281 class LinkEditAtom* _bindingInfoAtom;
282 class LinkEditAtom* _lazyBindingInfoAtom;
283 class LinkEditAtom* _weakBindingInfoAtom;
284 class LinkEditAtom* _exportInfoAtom;
285 class LinkEditAtom* _splitSegInfoAtom;
286 class LinkEditAtom* _functionStartsAtom;
287};
288
289} // namespace tool
290} // namespace ld
291
292#endif // __OUTPUT_FILE_H__