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 #ifndef __OUTPUT_FILE_H__
26 #define __OUTPUT_FILE_H__
29 #include <sys/types.h>
32 #include <sys/sysctl.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>
42 #include <mach-o/dyld.h>
55 OutputFile(const Options
& opts
);
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 uint64_t fileSize() const { return _fileSize
; }
71 bool hasWeakExternalSymbols
;
72 bool usesWeakExternalSymbols
;
73 bool overridesWeakExternalSymbols
;
74 bool _noReExportedDylibs
;
75 bool hasThreadLocalVariableDefinitions
;
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
;
93 RebaseInfo(uint8_t t
, uint64_t addr
) : _type(t
), _address(addr
) {}
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
);
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
) {}
115 const char* _symbolName
;
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
);
132 struct SplitSegInfoEntry
{
133 SplitSegInfoEntry(uint64_t a
, ld::Fixup::Kind k
, uint32_t e
=0) : address(a
), kind(k
), extra(e
) {}
135 ld::Fixup::Kind kind
;
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
);
179 void copyNoOps(uint8_t* from
, uint8_t* to
, bool thumb
);
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
);
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
);
203 void rangeCheckAbsolute32(int64_t delta
, ld::Internal
& state
, const ld::Atom
* atom
,
204 const ld::Fixup
* fixup
);
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 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
);
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
);
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
);
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
;
250 std::map
<uint64_t, uint32_t> _lazyPointerAddressToInfoOffset
;
251 uint32_t _encryptedTEXTstartOffset
;
252 uint32_t _encryptedTEXTendOffset
;
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
;
288 #endif // __OUTPUT_FILE_H__