]> git.saurik.com Git - apple/ld64.git/blob - src/ld/HeaderAndLoadCommands.hpp
395fc99e95054958857413a2f8e0e5821e3f0fd2
[apple/ld64.git] / src / ld / HeaderAndLoadCommands.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 #ifndef __HEADER_LOAD_COMMANDS_HPP__
26 #define __HEADER_LOAD_COMMANDS_HPP__
27
28 #include <stdlib.h>
29 #include <limits.h>
30 #include <unistd.h>
31 #include <mach-o/loader.h>
32
33 #include <vector>
34
35 #include "MachOFileAbstraction.hpp"
36 #include "Options.h"
37 #include "ld.hpp"
38
39 namespace ld {
40 namespace tool {
41
42 class HeaderAndLoadCommandsAbtract : public ld::Atom
43 {
44 public:
45 HeaderAndLoadCommandsAbtract(const ld::Section& sect, ld::Atom::Definition d,
46 ld::Atom::Combine c, ld::Atom::Scope s, ld::Atom::ContentType ct,
47 ld::Atom::SymbolTableInclusion i, bool dds, bool thumb, bool al,
48 ld::Atom::Alignment a) : ld::Atom(sect, d, c, s, ct, i, dds, thumb, al, a) { }
49
50 virtual void setUUID(const uint8_t digest[16]) = 0;
51 virtual void recopyUUIDCommand() = 0;
52 };
53
54 template <typename A>
55 class HeaderAndLoadCommandsAtom : public HeaderAndLoadCommandsAbtract
56 {
57 public:
58 HeaderAndLoadCommandsAtom(const Options& opts, ld::Internal& state,
59 OutputFile& writer);
60
61 // overrides of ld::Atom
62 virtual ld::File* file() const { return NULL; }
63 virtual bool translationUnitSource(const char** dir, const char** nm) const
64 { return false; }
65 virtual const char* name() const { return "mach-o header and load commands"; }
66 virtual uint64_t size() const;
67 virtual uint64_t objectAddress() const { return _address; }
68 virtual void copyRawContent(uint8_t buffer[]) const;
69
70 // overrides of HeaderAndLoadCommandsAbtract
71 virtual void setUUID(const uint8_t digest[16]) { memcpy(_uuid, digest, 16); }
72 virtual void recopyUUIDCommand();
73
74 private:
75 typedef typename A::P P;
76 typedef typename A::P::E E;
77 typedef typename A::P::uint_t pint_t;
78
79 unsigned int nonHiddenSectionCount() const;
80 unsigned int segmentCount() const;
81 static uint32_t alignedSize(uint32_t x);
82 uint32_t magic() const;
83 uint32_t cpuType() const;
84 uint32_t cpuSubType() const;
85 uint32_t flags() const;
86 uint32_t fileType() const;
87 uint32_t commandsCount() const;
88 uint32_t threadLoadCommandSize() const;
89 uint8_t* copySingleSegmentLoadCommand(uint8_t* p) const;
90 uint8_t* copySegmentLoadCommands(uint8_t* p) const;
91 uint8_t* copyDyldInfoLoadCommand(uint8_t* p) const;
92 uint8_t* copySymbolTableLoadCommand(uint8_t* p) const;
93 uint8_t* copyDynamicSymbolTableLoadCommand(uint8_t* p) const;
94 uint8_t* copyDyldLoadCommand(uint8_t* p) const;
95 uint8_t* copyDylibIDLoadCommand(uint8_t* p) const;
96 uint8_t* copyRoutinesLoadCommand(uint8_t* p) const;
97 uint8_t* copyUUIDLoadCommand(uint8_t* p) const;
98 uint8_t* copyVersionLoadCommand(uint8_t* p) const;
99 uint8_t* copyThreadsLoadCommand(uint8_t* p) const;
100 uint8_t* copyEncryptionLoadCommand(uint8_t* p) const;
101 uint8_t* copySplitSegInfoLoadCommand(uint8_t* p) const;
102 uint8_t* copyDylibLoadCommand(uint8_t* p, const ld::dylib::File*) const;
103 uint8_t* copyRPathLoadCommand(uint8_t* p, const char*) const;
104 uint8_t* copySubFrameworkLoadCommand(uint8_t* p) const;
105 uint8_t* copyAllowableClientLoadCommand(uint8_t* p, const char* client) const;
106 uint8_t* copySubLibraryLoadCommand(uint8_t* p, const char* name) const;
107 uint8_t* copySubUmbrellaLoadCommand(uint8_t* p, const char* name) const;
108 uint8_t* copyFunctionStartsLoadCommand(uint8_t* p) const;
109 uint8_t* copyDyldEnvLoadCommand(uint8_t* p, const char* env) const;
110
111 uint32_t sectionFlags(ld::Internal::FinalSection* sect) const;
112 bool sectionTakesNoDiskSpace(ld::Internal::FinalSection* sect) const;
113
114
115 const Options& _options;
116 ld::Internal& _state;
117 OutputFile& _writer;
118 pint_t _address;
119 bool _hasDyldInfoLoadCommand;
120 bool _hasDyldLoadCommand;
121 bool _hasDylibIDLoadCommand;
122 bool _hasThreadLoadCommand;
123 bool _hasEncryptionLoadCommand;
124 bool _hasSplitSegInfoLoadCommand;
125 bool _hasRoutinesLoadCommand;
126 bool _hasUUIDLoadCommand;
127 bool _hasSymbolTableLoadCommand;
128 bool _hasDynamicSymbolTableLoadCommand;
129 bool _hasRPathLoadCommands;
130 bool _hasSubFrameworkLoadCommand;
131 bool _hasVersionLoadCommand;
132 bool _hasFunctionStartsLoadCommand;
133 uint32_t _dylibLoadCommmandsCount;
134 uint32_t _allowableClientLoadCommmandsCount;
135 uint32_t _dyldEnvironExrasCount;
136 std::vector<const char*> _subLibraryNames;
137 std::vector<const char*> _subUmbrellaNames;
138 uint8_t _uuid[16];
139 mutable macho_uuid_command<P>* _uuidCmdInOutputBuffer;
140
141 static ld::Section _s_section;
142 static ld::Section _s_preload_section;
143 };
144
145 template <typename A>
146 ld::Section HeaderAndLoadCommandsAtom<A>::_s_section("__TEXT", "__mach_header", ld::Section::typeMachHeader, true);
147 template <typename A>
148 ld::Section HeaderAndLoadCommandsAtom<A>::_s_preload_section("__HEADER", "__mach_header", ld::Section::typeMachHeader, true);
149
150
151 template <typename A>
152 HeaderAndLoadCommandsAtom<A>::HeaderAndLoadCommandsAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
153 : HeaderAndLoadCommandsAbtract((opts.outputKind() == Options::kPreload) ? _s_preload_section : _s_section,
154 ld::Atom::definitionRegular, ld::Atom::combineNever,
155 ld::Atom::scopeTranslationUnit, ld::Atom::typeUnclassified,
156 ld::Atom::symbolTableNotIn, false, false, false,
157 (opts.outputKind() == Options::kPreload) ? ld::Atom::Alignment(0) : ld::Atom::Alignment(12) ),
158 _options(opts), _state(state), _writer(writer), _address(0), _uuidCmdInOutputBuffer(NULL)
159 {
160 bzero(_uuid, 16);
161 _hasDyldInfoLoadCommand = opts.makeCompressedDyldInfo();
162 _hasDyldLoadCommand = ((opts.outputKind() == Options::kDynamicExecutable) || (_options.outputKind() == Options::kDyld));
163 _hasDylibIDLoadCommand = (opts.outputKind() == Options::kDynamicLibrary);
164 _hasThreadLoadCommand = _hasDyldLoadCommand || (opts.outputKind() == Options::kStaticExecutable)
165 || (opts.outputKind() == Options::kPreload)
166 || (opts.outputKind() == Options::kDyld);
167 _hasEncryptionLoadCommand = opts.makeEncryptable();
168 _hasSplitSegInfoLoadCommand = opts.sharedRegionEligible();
169 _hasRoutinesLoadCommand = (opts.initFunctionName() != NULL);
170 _hasSymbolTableLoadCommand = true;
171 _hasUUIDLoadCommand = (opts.UUIDMode() != Options::kUUIDNone);
172 switch ( opts.outputKind() ) {
173 case Options::kDynamicExecutable:
174 case Options::kDynamicLibrary:
175 case Options::kDynamicBundle:
176 case Options::kDyld:
177 case Options::kKextBundle:
178 _hasDynamicSymbolTableLoadCommand = true;
179 break;
180 case Options::kObjectFile:
181 if ( ! state.someObjectFileHasDwarf )
182 _hasUUIDLoadCommand = false;
183 _hasDynamicSymbolTableLoadCommand = false;
184 for (std::vector<ld::Internal::FinalSection*>::iterator it = _state.sections.begin(); it != _state.sections.end(); ++it) {
185 if ( (*it)->type() == ld::Section::typeNonLazyPointer ) {
186 _hasDynamicSymbolTableLoadCommand = true;
187 break;
188 }
189 }
190 break;
191 case Options::kStaticExecutable:
192 _hasDynamicSymbolTableLoadCommand = false;
193 break;
194 case Options::kPreload:
195 _hasDynamicSymbolTableLoadCommand = opts.positionIndependentExecutable();
196 break;
197 }
198 _hasRPathLoadCommands = (_options.rpaths().size() != 0);
199 _hasSubFrameworkLoadCommand = (_options.umbrellaName() != NULL);
200 _hasVersionLoadCommand = _options.addVersionLoadCommand();
201 _hasFunctionStartsLoadCommand = _options.addFunctionStarts();
202 _dylibLoadCommmandsCount = _writer.dylibCount();
203 _allowableClientLoadCommmandsCount = _options.allowableClients().size();
204 _dyldEnvironExrasCount = _options.dyldEnvironExtras().size();
205 if ( ! _options.useSimplifiedDylibReExports() ) {
206 // target OS does not support LC_REEXPORT_DYLIB, so use old complicated load commands
207 for(uint32_t ord=1; ord <= _writer.dylibCount(); ++ord) {
208 const ld::dylib::File* dylib = _writer.dylibByOrdinal(ord);
209 if ( dylib->willBeReExported() ) {
210 // if child says it is an sub-framework of the image being created, then nothing to do here
211 bool isSubFramework = false;
212 const char* childInUmbrella = dylib->parentUmbrella();
213 if ( childInUmbrella != NULL ) {
214 const char* myLeaf = strrchr(_options.installPath(), '/');
215 if ( myLeaf != NULL ) {
216 if ( strcmp(childInUmbrella, &myLeaf[1]) == 0 )
217 isSubFramework = true;
218 }
219 }
220 // LC_SUB_FRAMEWORK is in child, so do nothing in parent
221 if ( ! isSubFramework ) {
222 // this dylib also needs a sub_x load command
223 bool isFrameworkReExport = false;
224 const char* lastSlash = strrchr(dylib->installPath(), '/');
225 if ( lastSlash != NULL ) {
226 char frameworkName[strlen(lastSlash)+20];
227 sprintf(frameworkName, "/%s.framework/", &lastSlash[1]);
228 isFrameworkReExport = (strstr(dylib->installPath(), frameworkName) != NULL);
229 }
230 if ( isFrameworkReExport ) {
231 // needs a LC_SUB_UMBRELLA command
232 _subUmbrellaNames.push_back(&lastSlash[1]);
233 }
234 else {
235 // needs a LC_SUB_LIBRARY command
236 const char* nameStart = &lastSlash[1];
237 if ( lastSlash == NULL )
238 nameStart = dylib->installPath();
239 int len = strlen(nameStart);
240 const char* dot = strchr(nameStart, '.');
241 if ( dot != NULL )
242 len = dot - nameStart;
243 char* subLibName = new char[len+1];
244 strlcpy(subLibName, nameStart, len+1);
245 _subLibraryNames.push_back(subLibName);
246 }
247 }
248 }
249 }
250 }
251 }
252
253 template <typename A>
254 uint32_t HeaderAndLoadCommandsAtom<A>::alignedSize(uint32_t size)
255 {
256 if ( sizeof(pint_t) == 4 )
257 return ((size+3) & (-4)); // 4-byte align all load commands for 32-bit mach-o
258 else
259 return ((size+7) & (-8)); // 8-byte align all load commands for 64-bit mach-o
260 }
261
262
263 template <typename A>
264 unsigned int HeaderAndLoadCommandsAtom<A>::nonHiddenSectionCount() const
265 {
266 unsigned int count = 0;
267 for (std::vector<ld::Internal::FinalSection*>::iterator it = _state.sections.begin(); it != _state.sections.end(); ++it) {
268 if ( ! (*it)->isSectionHidden() && ((*it)->type() != ld::Section::typeTentativeDefs) )
269 ++count;
270 }
271 return count;
272 }
273
274 template <typename A>
275 unsigned int HeaderAndLoadCommandsAtom<A>::segmentCount() const
276 {
277 if ( _options.outputKind() == Options::kObjectFile ) {
278 // .o files have one anonymous segment that contains all sections
279 return 1;
280 }
281
282 unsigned int count = 0;
283 const char* lastSegName = "";
284 for (std::vector<ld::Internal::FinalSection*>::iterator it = _state.sections.begin(); it != _state.sections.end(); ++it) {
285 if ( _options.outputKind() == Options::kPreload ) {
286 if ( (*it)->type() == ld::Section::typeMachHeader )
287 continue; // for -preload, don't put hidden __HEADER segment into output
288 if ( (*it)->type() == ld::Section::typeLinkEdit )
289 continue; // for -preload, don't put hidden __LINKEDIT segment into output
290 }
291 if ( strcmp(lastSegName, (*it)->segmentName()) != 0 ) {
292 lastSegName = (*it)->segmentName();
293 ++count;
294 }
295 }
296 return count;
297 }
298
299
300 template <typename A>
301 uint64_t HeaderAndLoadCommandsAtom<A>::size() const
302 {
303 uint32_t sz = sizeof(macho_header<P>);
304
305 sz += sizeof(macho_segment_command<P>) * this->segmentCount();
306 sz += sizeof(macho_section<P>) * this->nonHiddenSectionCount();
307
308 if ( _hasDylibIDLoadCommand )
309 sz += alignedSize(sizeof(macho_dylib_command<P>) + strlen(_options.installPath()) + 1);
310
311 if ( _hasDyldInfoLoadCommand )
312 sz += sizeof(macho_dyld_info_command<P>);
313
314 if ( _hasSymbolTableLoadCommand )
315 sz += sizeof(macho_symtab_command<P>);
316
317 if ( _hasDynamicSymbolTableLoadCommand )
318 sz += sizeof(macho_dysymtab_command<P>);
319
320 if ( _hasDyldLoadCommand )
321 sz += alignedSize(sizeof(macho_dylinker_command<P>) + strlen(_options.dyldInstallPath()) + 1);
322
323 if ( _hasRoutinesLoadCommand )
324 sz += sizeof(macho_routines_command<P>);
325
326 if ( _hasUUIDLoadCommand )
327 sz += sizeof(macho_uuid_command<P>);
328
329 if ( _hasVersionLoadCommand )
330 sz += sizeof(macho_version_min_command<P>);
331
332 if ( _hasThreadLoadCommand )
333 sz += this->threadLoadCommandSize();
334
335 if ( _hasEncryptionLoadCommand )
336 sz += sizeof(macho_encryption_info_command<P>);
337
338 if ( _hasSplitSegInfoLoadCommand )
339 sz += sizeof(macho_linkedit_data_command<P>);
340
341 for(uint32_t ord=1; ord <= _writer.dylibCount(); ++ord) {
342 sz += alignedSize(sizeof(macho_dylib_command<P>) + strlen(_writer.dylibByOrdinal(ord)->installPath()) + 1);
343 }
344
345 if ( _hasRPathLoadCommands ) {
346 const std::vector<const char*>& rpaths = _options.rpaths();
347 for (std::vector<const char*>::const_iterator it = rpaths.begin(); it != rpaths.end(); ++it) {
348 sz += alignedSize(sizeof(macho_rpath_command<P>) + strlen(*it) + 1);
349 }
350 }
351
352 if ( _hasSubFrameworkLoadCommand )
353 sz += alignedSize(sizeof(macho_sub_framework_command<P>) + strlen(_options.umbrellaName()) + 1);
354
355 for (std::vector<const char*>::const_iterator it = _subLibraryNames.begin(); it != _subLibraryNames.end(); ++it) {
356 sz += alignedSize(sizeof(macho_sub_library_command<P>) + strlen(*it) + 1);
357 }
358
359 for (std::vector<const char*>::const_iterator it = _subUmbrellaNames.begin(); it != _subUmbrellaNames.end(); ++it) {
360 sz += alignedSize(sizeof(macho_sub_umbrella_command<P>) + strlen(*it) + 1);
361 }
362
363 if ( _allowableClientLoadCommmandsCount != 0 ) {
364 const std::vector<const char*>& clients = _options.allowableClients();
365 for (std::vector<const char*>::const_iterator it = clients.begin(); it != clients.end(); ++it) {
366 sz += alignedSize(sizeof(macho_sub_client_command<P>) + strlen(*it) + 1);
367 }
368 }
369
370 if ( _dyldEnvironExrasCount != 0 ) {
371 const std::vector<const char*>& extras = _options.dyldEnvironExtras();
372 for (std::vector<const char*>::const_iterator it = extras.begin(); it != extras.end(); ++it) {
373 sz += alignedSize(sizeof(macho_dylinker_command<P>) + strlen(*it) + 1);
374 }
375 }
376
377 if ( _hasFunctionStartsLoadCommand )
378 sz += sizeof(macho_linkedit_data_command<P>);
379
380 return sz;
381 }
382
383 template <typename A>
384 uint32_t HeaderAndLoadCommandsAtom<A>::commandsCount() const
385 {
386 uint32_t count = this->segmentCount();
387
388 if ( _hasDylibIDLoadCommand )
389 ++count;
390
391 if ( _hasDyldInfoLoadCommand )
392 ++count;
393
394 if ( _hasSymbolTableLoadCommand )
395 ++count;
396
397 if ( _hasDynamicSymbolTableLoadCommand )
398 ++count;
399
400 if ( _hasDyldLoadCommand )
401 ++count;
402
403 if ( _hasRoutinesLoadCommand )
404 ++count;
405
406 if ( _hasUUIDLoadCommand )
407 ++count;
408
409 if ( _hasVersionLoadCommand )
410 ++count;
411
412 if ( _hasThreadLoadCommand )
413 ++count;
414
415 if ( _hasEncryptionLoadCommand )
416 ++count;
417
418 if ( _hasSplitSegInfoLoadCommand )
419 ++count;
420
421 count += _dylibLoadCommmandsCount;
422
423 count += _options.rpaths().size();
424
425 if ( _hasSubFrameworkLoadCommand )
426 ++count;
427
428 count += _subLibraryNames.size();
429
430 count += _subUmbrellaNames.size();
431
432 count += _allowableClientLoadCommmandsCount;
433
434 count += _dyldEnvironExrasCount;
435
436 if ( _hasFunctionStartsLoadCommand )
437 ++count;
438
439 return count;
440 }
441
442 template <typename A>
443 uint32_t HeaderAndLoadCommandsAtom<A>::fileType() const
444 {
445 switch ( _options.outputKind() ) {
446 case Options::kDynamicExecutable:
447 case Options::kStaticExecutable:
448 return MH_EXECUTE;
449 case Options::kDynamicLibrary:
450 return MH_DYLIB;
451 case Options::kDynamicBundle:
452 return MH_BUNDLE;
453 case Options::kObjectFile:
454 return MH_OBJECT;
455 case Options::kDyld:
456 return MH_DYLINKER;
457 case Options::kPreload:
458 return MH_PRELOAD;
459 case Options::kKextBundle:
460 return MH_KEXT_BUNDLE;
461 }
462 throw "unknonwn mach-o file type";
463 }
464
465 template <typename A>
466 uint32_t HeaderAndLoadCommandsAtom<A>::flags() const
467 {
468 uint32_t bits = 0;
469 if ( _options.outputKind() == Options::kObjectFile ) {
470 if ( _state.allObjectFilesScatterable )
471 bits = MH_SUBSECTIONS_VIA_SYMBOLS;
472 }
473 else {
474 if ( _options.outputKind() == Options::kStaticExecutable ) {
475 bits |= MH_NOUNDEFS;
476 }
477 else if ( _options.outputKind() == Options::kPreload ) {
478 bits |= MH_NOUNDEFS;
479 if ( _options.positionIndependentExecutable() )
480 bits |= MH_PIE;
481 }
482 else {
483 bits = MH_DYLDLINK;
484 switch ( _options.nameSpace() ) {
485 case Options::kTwoLevelNameSpace:
486 bits |= MH_TWOLEVEL | MH_NOUNDEFS;
487 break;
488 case Options::kFlatNameSpace:
489 break;
490 case Options::kForceFlatNameSpace:
491 bits |= MH_FORCE_FLAT;
492 break;
493 }
494 if ( _writer.hasWeakExternalSymbols || _writer.overridesWeakExternalSymbols )
495 bits |= MH_WEAK_DEFINES;
496 if ( _writer.usesWeakExternalSymbols || _writer.hasWeakExternalSymbols )
497 bits |= MH_BINDS_TO_WEAK;
498 if ( _options.prebind() )
499 bits |= MH_PREBOUND;
500 if ( _options.splitSeg() )
501 bits |= MH_SPLIT_SEGS;
502 if ( (_options.outputKind() == Options::kDynamicLibrary)
503 && _writer._noReExportedDylibs
504 && _options.useSimplifiedDylibReExports() ) {
505 bits |= MH_NO_REEXPORTED_DYLIBS;
506 }
507 if ( _options.positionIndependentExecutable() && ! _writer.pieDisabled )
508 bits |= MH_PIE;
509 if ( _options.markAutoDeadStripDylib() )
510 bits |= MH_DEAD_STRIPPABLE_DYLIB;
511 if ( _writer.hasThreadLocalVariableDefinitions )
512 bits |= MH_HAS_TLV_DESCRIPTORS;
513 if ( _options.hasNonExecutableHeap() )
514 bits |= MH_NO_HEAP_EXECUTION;
515 }
516 if ( _options.hasExecutableStack() )
517 bits |= MH_ALLOW_STACK_EXECUTION;
518 }
519 return bits;
520 }
521
522 template <> uint32_t HeaderAndLoadCommandsAtom<ppc>::magic() const { return MH_MAGIC; }
523 template <> uint32_t HeaderAndLoadCommandsAtom<ppc64>::magic() const { return MH_MAGIC_64; }
524 template <> uint32_t HeaderAndLoadCommandsAtom<x86>::magic() const { return MH_MAGIC; }
525 template <> uint32_t HeaderAndLoadCommandsAtom<x86_64>::magic() const { return MH_MAGIC_64; }
526 template <> uint32_t HeaderAndLoadCommandsAtom<arm>::magic() const { return MH_MAGIC; }
527
528 template <> uint32_t HeaderAndLoadCommandsAtom<ppc>::cpuType() const { return CPU_TYPE_POWERPC; }
529 template <> uint32_t HeaderAndLoadCommandsAtom<ppc64>::cpuType() const { return CPU_TYPE_POWERPC64; }
530 template <> uint32_t HeaderAndLoadCommandsAtom<x86>::cpuType() const { return CPU_TYPE_I386; }
531 template <> uint32_t HeaderAndLoadCommandsAtom<x86_64>::cpuType() const { return CPU_TYPE_X86_64; }
532 template <> uint32_t HeaderAndLoadCommandsAtom<arm>::cpuType() const { return CPU_TYPE_ARM; }
533
534
535 template <>
536 uint32_t HeaderAndLoadCommandsAtom<ppc>::cpuSubType() const
537 {
538 return _state.cpuSubType;
539 }
540
541 template <>
542 uint32_t HeaderAndLoadCommandsAtom<ppc64>::cpuSubType() const
543 {
544 if ( (_options.outputKind() == Options::kDynamicExecutable) && (_options.macosxVersionMin() >= ld::mac10_5) )
545 return (CPU_SUBTYPE_POWERPC_ALL | 0x80000000);
546 else
547 return CPU_SUBTYPE_POWERPC_ALL;
548 }
549
550 template <>
551 uint32_t HeaderAndLoadCommandsAtom<x86>::cpuSubType() const
552 {
553 return CPU_SUBTYPE_I386_ALL;
554 }
555
556 template <>
557 uint32_t HeaderAndLoadCommandsAtom<x86_64>::cpuSubType() const
558 {
559 if ( (_options.outputKind() == Options::kDynamicExecutable) && (_options.macosxVersionMin() >= ld::mac10_5) )
560 return (CPU_SUBTYPE_X86_64_ALL | 0x80000000);
561 else
562 return CPU_SUBTYPE_X86_64_ALL;
563 }
564
565 template <>
566 uint32_t HeaderAndLoadCommandsAtom<arm>::cpuSubType() const
567 {
568 return _state.cpuSubType;
569 }
570
571
572
573 template <typename A>
574 uint8_t* HeaderAndLoadCommandsAtom<A>::copySingleSegmentLoadCommand(uint8_t* p) const
575 {
576 // in .o files there is just one segment load command with a blank name
577 // and all sections under it
578 macho_segment_command<P>* cmd = (macho_segment_command<P>*)p;
579 cmd->set_cmd(macho_segment_command<P>::CMD);
580 cmd->set_segname("");
581 cmd->set_vmaddr(_options.baseAddress());
582 cmd->set_vmsize(0); // updated after sections set
583 cmd->set_fileoff(0); // updated after sections set
584 cmd->set_filesize(0); // updated after sections set
585 cmd->set_maxprot(VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
586 cmd->set_initprot(VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
587 cmd->set_nsects(this->nonHiddenSectionCount());
588 cmd->set_flags(0);
589 // add sections array
590 macho_section<P>* msect = (macho_section<P>*)&p[sizeof(macho_segment_command<P>)];
591 for (std::vector<ld::Internal::FinalSection*>::iterator sit = _state.sections.begin(); sit != _state.sections.end(); ++sit) {
592 ld::Internal::FinalSection* fsect = *sit;
593 if ( fsect->isSectionHidden() )
594 continue;
595 if ( fsect->type() == ld::Section::typeTentativeDefs )
596 continue;
597 msect->set_sectname(fsect->sectionName());
598 msect->set_segname(fsect->segmentName());
599 msect->set_addr(fsect->address);
600 msect->set_size(fsect->size);
601 msect->set_offset(fsect->fileOffset);
602 msect->set_align(fsect->alignment);
603 msect->set_reloff((fsect->relocCount == 0) ? 0 : _writer.sectionRelocationsSection->fileOffset + fsect->relocStart * sizeof(macho_relocation_info<P>));
604 msect->set_nreloc(fsect->relocCount);
605 msect->set_flags(sectionFlags(fsect));
606 msect->set_reserved1(fsect->indirectSymTabStartIndex);
607 msect->set_reserved2(fsect->indirectSymTabElementSize);
608 // update segment info
609 if ( cmd->fileoff() == 0 )
610 cmd->set_fileoff(fsect->fileOffset);
611 cmd->set_vmsize(fsect->address + fsect->size - cmd->vmaddr());
612 if ( (fsect->type() != ld::Section::typeZeroFill) && (fsect->type() != ld::Section::typeTentativeDefs) )
613 cmd->set_filesize(fsect->fileOffset + fsect->size - cmd->fileoff());
614 ++msect;
615 }
616 cmd->set_cmdsize(sizeof(macho_segment_command<P>) + cmd->nsects()*sizeof(macho_section<P>));
617 return p + cmd->cmdsize();
618 }
619
620 struct SegInfo {
621 SegInfo(const char* n, const Options&);
622 const char* segName;
623 uint32_t nonHiddenSectionCount;
624 uint32_t maxProt;
625 uint32_t initProt;
626 std::vector<ld::Internal::FinalSection*> sections;
627 };
628
629
630 SegInfo::SegInfo(const char* n, const Options& opts)
631 : segName(n), nonHiddenSectionCount(0), maxProt(opts.maxSegProtection(n)), initProt(opts.initialSegProtection(n))
632 {
633 }
634
635
636 template <typename A>
637 uint32_t HeaderAndLoadCommandsAtom<A>::sectionFlags(ld::Internal::FinalSection* sect) const
638 {
639 uint32_t bits;
640 switch ( sect->type() ) {
641 case ld::Section::typeUnclassified:
642 if ( strcmp(sect->segmentName(), "__OBJC") == 0 )
643 return S_REGULAR | S_ATTR_NO_DEAD_STRIP;
644 else if ( (strcmp(sect->sectionName(), "__objc_classlist") == 0) && (strcmp(sect->segmentName(), "__DATA") == 0) )
645 return S_REGULAR | S_ATTR_NO_DEAD_STRIP;
646 else if ( (strcmp(sect->sectionName(), "__objc_catlist") == 0) && (strcmp(sect->segmentName(), "__DATA") == 0) )
647 return S_REGULAR | S_ATTR_NO_DEAD_STRIP;
648 else if ( (strncmp(sect->sectionName(), "__objc_superrefs", 16) == 0) && (strcmp(sect->segmentName(), "__DATA") == 0) )
649 return S_REGULAR | S_ATTR_NO_DEAD_STRIP;
650 else if ( (strncmp(sect->sectionName(), "__objc_nlclslist", 16) == 0) && (strcmp(sect->segmentName(), "__DATA") == 0) )
651 return S_REGULAR | S_ATTR_NO_DEAD_STRIP;
652 else
653 return S_REGULAR;
654 case ld::Section::typeCode:
655 bits = S_REGULAR | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
656 if ( sect->hasLocalRelocs && ! _writer.pieDisabled )
657 bits |= S_ATTR_LOC_RELOC;
658 if ( sect->hasExternalRelocs )
659 bits |= S_ATTR_EXT_RELOC;
660 return bits;
661 case ld::Section::typePageZero:
662 return S_REGULAR;
663 case ld::Section::typeImportProxies:
664 return S_REGULAR;
665 case ld::Section::typeLinkEdit:
666 return S_REGULAR;
667 case ld::Section::typeMachHeader:
668 return S_REGULAR;
669 case ld::Section::typeStack:
670 return S_REGULAR;
671 case ld::Section::typeLiteral4:
672 return S_4BYTE_LITERALS;
673 case ld::Section::typeLiteral8:
674 return S_8BYTE_LITERALS;
675 case ld::Section::typeLiteral16:
676 return S_16BYTE_LITERALS;
677 case ld::Section::typeConstants:
678 return S_REGULAR;
679 case ld::Section::typeTempLTO:
680 assert(0 && "typeTempLTO should not make it to final linked image");
681 return S_REGULAR;
682 case ld::Section::typeAbsoluteSymbols:
683 assert(0 && "typeAbsoluteSymbols should not make it to final linked image");
684 return S_REGULAR;
685 case ld::Section::typeCString:
686 case ld::Section::typeNonStdCString:
687 return S_CSTRING_LITERALS;
688 case ld::Section::typeCStringPointer:
689 return S_LITERAL_POINTERS | S_ATTR_NO_DEAD_STRIP;
690 case ld::Section::typeUTF16Strings:
691 return S_REGULAR;
692 case ld::Section::typeCFString:
693 return S_REGULAR;
694 case ld::Section::typeObjC1Classes:
695 return S_REGULAR | S_ATTR_NO_DEAD_STRIP;
696 case ld::Section::typeCFI:
697 return S_REGULAR;
698 case ld::Section::typeLSDA:
699 return S_REGULAR;
700 case ld::Section::typeDtraceDOF:
701 return S_DTRACE_DOF;
702 case ld::Section::typeUnwindInfo:
703 return S_REGULAR;
704 case ld::Section::typeObjCClassRefs:
705 case ld::Section::typeObjC2CategoryList:
706 return S_REGULAR | S_ATTR_NO_DEAD_STRIP;
707 case ld::Section::typeZeroFill:
708 if ( _options.optimizeZeroFill() )
709 return S_ZEROFILL;
710 else
711 return S_REGULAR;
712 case ld::Section::typeTentativeDefs:
713 assert(0 && "typeTentativeDefs should not make it to final linked image");
714 return S_REGULAR;
715 case ld::Section::typeLazyPointer:
716 case ld::Section::typeLazyPointerClose:
717 return S_LAZY_SYMBOL_POINTERS;
718 case ld::Section::typeStubClose:
719 case ld::Section::typeStub:
720 if ( sect->hasLocalRelocs )
721 return S_SYMBOL_STUBS | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS | S_ATTR_LOC_RELOC;
722 else
723 return S_SYMBOL_STUBS | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
724 case ld::Section::typeNonLazyPointer:
725 return S_NON_LAZY_SYMBOL_POINTERS;
726 case ld::Section::typeDyldInfo:
727 return S_REGULAR;
728 case ld::Section::typeLazyDylibPointer:
729 return S_LAZY_DYLIB_SYMBOL_POINTERS;
730 case ld::Section::typeStubHelper:
731 if ( sect->hasLocalRelocs )
732 return S_REGULAR | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS | S_ATTR_LOC_RELOC;
733 else
734 return S_REGULAR | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
735 case ld::Section::typeInitializerPointers:
736 return S_MOD_INIT_FUNC_POINTERS;
737 case ld::Section::typeTerminatorPointers:
738 return S_MOD_TERM_FUNC_POINTERS;
739 case ld::Section::typeTLVInitialValues:
740 return S_THREAD_LOCAL_REGULAR;
741 case ld::Section::typeTLVZeroFill:
742 return S_THREAD_LOCAL_ZEROFILL;
743 case ld::Section::typeTLVDefs:
744 return S_THREAD_LOCAL_VARIABLES;
745 case ld::Section::typeTLVInitializerPointers:
746 return S_THREAD_LOCAL_INIT_FUNCTION_POINTERS;
747 case ld::Section::typeTLVPointers:
748 return S_THREAD_LOCAL_VARIABLE_POINTERS;
749 case ld::Section::typeFirstSection:
750 assert(0 && "typeFirstSection should not make it to final linked image");
751 return S_REGULAR;
752 case ld::Section::typeLastSection:
753 assert(0 && "typeLastSection should not make it to final linked image");
754 return S_REGULAR;
755 case ld::Section::typeDebug:
756 return S_REGULAR | S_ATTR_DEBUG;
757 }
758 return S_REGULAR;
759 }
760
761
762 template <typename A>
763 bool HeaderAndLoadCommandsAtom<A>::sectionTakesNoDiskSpace(ld::Internal::FinalSection* sect) const
764 {
765 switch ( sect->type() ) {
766 case ld::Section::typeZeroFill:
767 case ld::Section::typeTLVZeroFill:
768 return _options.optimizeZeroFill();
769 case ld::Section::typeAbsoluteSymbols:
770 case ld::Section::typeTentativeDefs:
771 case ld::Section::typeLastSection:
772 return true;
773 default:
774 break;
775 }
776 return false;
777 }
778
779
780 template <typename A>
781 uint8_t* HeaderAndLoadCommandsAtom<A>::copySegmentLoadCommands(uint8_t* p) const
782 {
783 // group sections into segments
784 std::vector<SegInfo> segs;
785 const char* lastSegName = "";
786 for (std::vector<ld::Internal::FinalSection*>::iterator it = _state.sections.begin(); it != _state.sections.end(); ++it) {
787 ld::Internal::FinalSection* sect = *it;
788 if ( _options.outputKind() == Options::kPreload ) {
789 if ( (*it)->type() == ld::Section::typeMachHeader )
790 continue; // for -preload, don't put hidden __HEADER segment into output
791 if ( (*it)->type() == ld::Section::typeLinkEdit )
792 continue; // for -preload, don't put hidden __LINKEDIT segment into output
793 }
794 if ( strcmp(lastSegName, sect->segmentName()) != 0 ) {
795 SegInfo si(sect->segmentName(), _options);
796 segs.push_back(si);
797 lastSegName = sect->segmentName();
798 }
799 if ( ! sect->isSectionHidden() )
800 segs.back().nonHiddenSectionCount++;
801 segs.back().sections.push_back(sect);
802 }
803 // write out segment load commands for each section with trailing sections
804 for (std::vector<SegInfo>::iterator it = segs.begin(); it != segs.end(); ++it) {
805 SegInfo& si = *it;
806 ld::Internal::FinalSection* lastNonZeroFillSection = NULL;
807 for (int i=si.sections.size()-1; i >= 0; --i) {
808 if ( !sectionTakesNoDiskSpace(si.sections[i]) ) {
809 lastNonZeroFillSection = si.sections[i];
810 break;
811 }
812 }
813 uint64_t vmsize = si.sections.back()->address + si.sections.back()->size - si.sections.front()->address;
814 vmsize = ((vmsize+_options.segmentAlignment()-1) & (-_options.segmentAlignment()));
815 uint64_t filesize = 0;
816 if ( lastNonZeroFillSection != NULL ) {
817 filesize = lastNonZeroFillSection->address + lastNonZeroFillSection->size - si.sections.front()->address;
818 // round up all segments to page aligned, except __LINKEDIT
819 if ( (si.sections[0]->type() != ld::Section::typeLinkEdit) && (si.sections[0]->type() != ld::Section::typeImportProxies) )
820 filesize = (filesize + _options.segmentAlignment()-1) & (-_options.segmentAlignment());
821 }
822 if ( si.sections.front()->type() == ld::Section::typePageZero )
823 filesize = 0;
824 else if ( si.sections.front()->type() == ld::Section::typeStack )
825 filesize = 0;
826 macho_segment_command<P>* segCmd = (macho_segment_command<P>*)p;
827 segCmd->set_cmd(macho_segment_command<P>::CMD);
828 segCmd->set_cmdsize(sizeof(macho_segment_command<P>) + si.nonHiddenSectionCount*sizeof(macho_section<P>));
829 segCmd->set_segname(si.sections.front()->segmentName());
830 segCmd->set_vmaddr(si.sections.front()->address);
831 segCmd->set_vmsize(vmsize);
832 segCmd->set_fileoff(si.sections.front()->fileOffset);
833 segCmd->set_filesize(filesize);
834 segCmd->set_maxprot(si.maxProt);
835 segCmd->set_initprot(si.initProt);
836 segCmd->set_nsects(si.nonHiddenSectionCount);
837 segCmd->set_flags(0);
838 p += sizeof(macho_segment_command<P>);
839 macho_section<P>* msect = (macho_section<P>*)p;
840 for (std::vector<ld::Internal::FinalSection*>::iterator sit = si.sections.begin(); sit != si.sections.end(); ++sit) {
841 ld::Internal::FinalSection* fsect = *sit;
842 if ( ! fsect->isSectionHidden() ) {
843 msect->set_sectname(fsect->sectionName());
844 msect->set_segname(fsect->segmentName());
845 msect->set_addr(fsect->address);
846 msect->set_size(fsect->size);
847 msect->set_offset(sectionTakesNoDiskSpace(fsect) ? 0 : fsect->fileOffset);
848 msect->set_align(fsect->alignment);
849 msect->set_reloff(0);
850 msect->set_nreloc(0);
851 msect->set_flags(sectionFlags(fsect));
852 msect->set_reserved1(fsect->indirectSymTabStartIndex);
853 msect->set_reserved2(fsect->indirectSymTabElementSize);
854 p += sizeof(macho_section<P>);
855 ++msect;
856 }
857 }
858 }
859
860 return p;
861 }
862
863
864 template <typename A>
865 uint8_t* HeaderAndLoadCommandsAtom<A>::copySymbolTableLoadCommand(uint8_t* p) const
866 {
867 // build LC_SYMTAB command
868 macho_symtab_command<P>* symbolTableCmd = (macho_symtab_command<P>*)p;
869 symbolTableCmd->set_cmd(LC_SYMTAB);
870 symbolTableCmd->set_cmdsize(sizeof(macho_symtab_command<P>));
871 symbolTableCmd->set_nsyms(_writer.symbolTableSection->size/sizeof(macho_nlist<P>));
872 symbolTableCmd->set_symoff(_writer.symbolTableSection->size == 0 ? 0 : _writer.symbolTableSection->fileOffset);
873 symbolTableCmd->set_stroff(_writer.stringPoolSection->size == 0 ? 0 : _writer.stringPoolSection->fileOffset );
874 symbolTableCmd->set_strsize(_writer.stringPoolSection->size);
875 return p + sizeof(macho_symtab_command<P>);
876 }
877
878 template <typename A>
879 uint8_t* HeaderAndLoadCommandsAtom<A>::copyDynamicSymbolTableLoadCommand(uint8_t* p) const
880 {
881 // build LC_SYMTAB command
882 macho_dysymtab_command<P>* dynamicSymbolTableCmd = (macho_dysymtab_command<P>*)p;
883 dynamicSymbolTableCmd->set_cmd(LC_DYSYMTAB);
884 dynamicSymbolTableCmd->set_cmdsize(sizeof(macho_dysymtab_command<P>));
885 dynamicSymbolTableCmd->set_ilocalsym(0);
886 dynamicSymbolTableCmd->set_nlocalsym(_writer._localSymbolsCount);
887 dynamicSymbolTableCmd->set_iextdefsym(dynamicSymbolTableCmd->ilocalsym()+dynamicSymbolTableCmd->nlocalsym());
888 dynamicSymbolTableCmd->set_nextdefsym(_writer._globalSymbolsCount);
889 dynamicSymbolTableCmd->set_iundefsym(dynamicSymbolTableCmd->iextdefsym()+dynamicSymbolTableCmd->nextdefsym());
890 dynamicSymbolTableCmd->set_nundefsym(_writer._importSymbolsCount);
891
892 // FIX ME: support for 10.3 dylibs which need modules
893 //if ( fWriter.fModuleInfoAtom != NULL ) {
894 // dynamicSymbolTableCmd->set_tocoff(fWriter.fModuleInfoAtom->getTableOfContentsFileOffset());
895 // dynamicSymbolTableCmd->set_ntoc(fWriter.fSymbolTableExportCount);
896 // dynamicSymbolTableCmd->set_modtaboff(fWriter.fModuleInfoAtom->getModuleTableFileOffset());
897 // dynamicSymbolTableCmd->set_nmodtab(1);
898 // dynamicSymbolTableCmd->set_extrefsymoff(fWriter.fModuleInfoAtom->getReferencesFileOffset());
899 // dynamicSymbolTableCmd->set_nextrefsyms(fWriter.fModuleInfoAtom->getReferencesCount());
900 //}
901
902 bool hasIndirectSymbols = ( (_writer.indirectSymbolTableSection != NULL) && (_writer.indirectSymbolTableSection->size != 0) );
903 dynamicSymbolTableCmd->set_indirectsymoff(hasIndirectSymbols ? _writer.indirectSymbolTableSection->fileOffset : 0);
904 dynamicSymbolTableCmd->set_nindirectsyms( hasIndirectSymbols ? _writer.indirectSymbolTableSection->size/sizeof(uint32_t) : 0);
905
906 // FIX ME: support for classic relocations
907 if ( _options.outputKind() != Options::kObjectFile ) {
908 bool hasExternalRelocs = ( (_writer.externalRelocationsSection != NULL) && (_writer.externalRelocationsSection->size != 0) );
909 dynamicSymbolTableCmd->set_extreloff(hasExternalRelocs ? _writer.externalRelocationsSection->fileOffset : 0);
910 dynamicSymbolTableCmd->set_nextrel( hasExternalRelocs ? _writer.externalRelocationsSection->size/8 : 0);
911 bool hasLocalRelocs = ( (_writer.localRelocationsSection != NULL) && (_writer.localRelocationsSection->size != 0) );
912 dynamicSymbolTableCmd->set_locreloff(hasLocalRelocs ? _writer.localRelocationsSection->fileOffset : 0);
913 dynamicSymbolTableCmd->set_nlocrel (hasLocalRelocs ? _writer.localRelocationsSection->size/8 : 0);
914 }
915 return p + sizeof(macho_dysymtab_command<P>);
916 }
917
918
919 template <typename A>
920 uint8_t* HeaderAndLoadCommandsAtom<A>::copyDyldInfoLoadCommand(uint8_t* p) const
921 {
922 // build LC_DYLD_INFO command
923 macho_dyld_info_command<P>* cmd = (macho_dyld_info_command<P>*)p;
924
925 cmd->set_cmd(LC_DYLD_INFO_ONLY);
926 cmd->set_cmdsize(sizeof(macho_dyld_info_command<P>));
927 if ( _writer.rebaseSection->size != 0 ) {
928 cmd->set_rebase_off(_writer.rebaseSection->fileOffset);
929 cmd->set_rebase_size(_writer.rebaseSection->size);
930 }
931 if ( _writer.bindingSection->size != 0 ) {
932 cmd->set_bind_off(_writer.bindingSection->fileOffset);
933 cmd->set_bind_size(_writer.bindingSection->size);
934 }
935 if ( _writer.weakBindingSection->size != 0 ) {
936 cmd->set_weak_bind_off(_writer.weakBindingSection->fileOffset);
937 cmd->set_weak_bind_size(_writer.weakBindingSection->size);
938 }
939 if ( _writer.lazyBindingSection->size != 0 ) {
940 cmd->set_lazy_bind_off(_writer.lazyBindingSection->fileOffset);
941 cmd->set_lazy_bind_size(_writer.lazyBindingSection->size);
942 }
943 if ( _writer.exportSection->size != 0 ) {
944 cmd->set_export_off(_writer.exportSection->fileOffset);
945 cmd->set_export_size(_writer.exportSection->size);
946 }
947 return p + sizeof(macho_dyld_info_command<P>);
948 }
949
950
951 template <typename A>
952 uint8_t* HeaderAndLoadCommandsAtom<A>::copyDyldLoadCommand(uint8_t* p) const
953 {
954 uint32_t sz = alignedSize(sizeof(macho_dylinker_command<P>) + strlen(_options.dyldInstallPath()) + 1);
955 macho_dylinker_command<P>* cmd = (macho_dylinker_command<P>*)p;
956 if ( _options.outputKind() == Options::kDyld )
957 cmd->set_cmd(LC_ID_DYLINKER);
958 else
959 cmd->set_cmd(LC_LOAD_DYLINKER);
960 cmd->set_cmdsize(sz);
961 cmd->set_name_offset();
962 strcpy((char*)&p[sizeof(macho_dylinker_command<P>)], _options.dyldInstallPath());
963 return p + sz;
964 }
965
966
967 template <typename A>
968 uint8_t* HeaderAndLoadCommandsAtom<A>::copyDylibIDLoadCommand(uint8_t* p) const
969 {
970 uint32_t sz = alignedSize(sizeof(macho_dylib_command<P>) + strlen(_options.installPath()) + 1);
971 macho_dylib_command<P>* cmd = (macho_dylib_command<P>*)p;
972 cmd->set_cmd(LC_ID_DYLIB);
973 cmd->set_cmdsize(sz);
974 cmd->set_name_offset();
975 cmd->set_timestamp(1); // needs to be some constant value that is different than DylibLoadCommandsAtom uses
976 cmd->set_current_version(_options.currentVersion());
977 cmd->set_compatibility_version(_options.compatibilityVersion());
978 strcpy((char*)&p[sizeof(macho_dylib_command<P>)], _options.installPath());
979 return p + sz;
980 }
981
982 template <typename A>
983 uint8_t* HeaderAndLoadCommandsAtom<A>::copyRoutinesLoadCommand(uint8_t* p) const
984 {
985 pint_t initAddr = _state.entryPoint->finalAddress();
986 if ( _state.entryPoint->isThumb() )
987 initAddr |= 1ULL;
988 macho_routines_command<P>* cmd = (macho_routines_command<P>*)p;
989 cmd->set_cmd(macho_routines_command<P>::CMD);
990 cmd->set_cmdsize(sizeof(macho_routines_command<P>));
991 cmd->set_init_address(initAddr);
992 return p + sizeof(macho_routines_command<P>);
993 }
994
995
996 template <typename A>
997 void HeaderAndLoadCommandsAtom<A>::recopyUUIDCommand()
998 {
999 assert(_uuidCmdInOutputBuffer != NULL);
1000 _uuidCmdInOutputBuffer->set_uuid(_uuid);
1001 }
1002
1003
1004 template <typename A>
1005 uint8_t* HeaderAndLoadCommandsAtom<A>::copyUUIDLoadCommand(uint8_t* p) const
1006 {
1007 macho_uuid_command<P>* cmd = (macho_uuid_command<P>*)p;
1008 cmd->set_cmd(LC_UUID);
1009 cmd->set_cmdsize(sizeof(macho_uuid_command<P>));
1010 cmd->set_uuid(_uuid);
1011 _uuidCmdInOutputBuffer = cmd; // save for later re-write by recopyUUIDCommand()
1012 return p + sizeof(macho_uuid_command<P>);
1013 }
1014
1015
1016 template <typename A>
1017 uint8_t* HeaderAndLoadCommandsAtom<A>::copyVersionLoadCommand(uint8_t* p) const
1018 {
1019 macho_version_min_command<P>* cmd = (macho_version_min_command<P>*)p;
1020 ld::MacVersionMin macVersion = _options.macosxVersionMin();
1021 ld::IOSVersionMin iOSVersion = _options.iOSVersionMin();
1022 assert( (macVersion != ld::macVersionUnset) || (iOSVersion != ld::iOSVersionUnset) );
1023 if ( macVersion != ld::macVersionUnset ) {
1024 cmd->set_cmd(LC_VERSION_MIN_MACOSX);
1025 cmd->set_cmdsize(sizeof(macho_version_min_command<P>));
1026 cmd->set_version((uint32_t)macVersion);
1027 cmd->set_reserved(0);
1028 }
1029 else {
1030 cmd->set_cmd(LC_VERSION_MIN_IPHONEOS);
1031 cmd->set_cmdsize(sizeof(macho_version_min_command<P>));
1032 cmd->set_version((uint32_t)iOSVersion);
1033 cmd->set_reserved(0);
1034 }
1035 return p + sizeof(macho_version_min_command<P>);
1036 }
1037
1038
1039 template <>
1040 uint32_t HeaderAndLoadCommandsAtom<ppc>::threadLoadCommandSize() const
1041 {
1042 return this->alignedSize(16 + 40*4); // base size + PPC_THREAD_STATE_COUNT * 4
1043 }
1044
1045
1046 template <>
1047 uint8_t* HeaderAndLoadCommandsAtom<ppc>::copyThreadsLoadCommand(uint8_t* p) const
1048 {
1049 assert(_state.entryPoint != NULL);
1050 pint_t start = _state.entryPoint->finalAddress();
1051 macho_thread_command<ppc::P>* cmd = (macho_thread_command<ppc::P>*)p;
1052 cmd->set_cmd(LC_UNIXTHREAD);
1053 cmd->set_cmdsize(threadLoadCommandSize());
1054 cmd->set_flavor(1); // PPC_THREAD_STATE
1055 cmd->set_count(40); // PPC_THREAD_STATE_COUNT;
1056 cmd->set_thread_register(0, start);
1057 if ( _options.hasCustomStack() )
1058 cmd->set_thread_register(3, _options.customStackAddr()); // r1
1059 return p + threadLoadCommandSize();
1060 }
1061
1062 template <>
1063 uint32_t HeaderAndLoadCommandsAtom<ppc64>::threadLoadCommandSize() const
1064 {
1065 return this->alignedSize(16 + 76*4); // base size + PPC_THREAD_STATE64_COUNT * 4
1066 }
1067
1068 template <>
1069 uint8_t* HeaderAndLoadCommandsAtom<ppc64>::copyThreadsLoadCommand(uint8_t* p) const
1070 {
1071 assert(_state.entryPoint != NULL);
1072 pint_t start = _state.entryPoint->finalAddress();
1073 macho_thread_command<ppc::P>* cmd = (macho_thread_command<ppc::P>*)p;
1074 cmd->set_cmd(LC_UNIXTHREAD);
1075 cmd->set_cmdsize(threadLoadCommandSize());
1076 cmd->set_flavor(5); // PPC_THREAD_STATE64
1077 cmd->set_count(76); // PPC_THREAD_STATE64_COUNT;
1078 cmd->set_thread_register(0, start);
1079 if ( _options.hasCustomStack() )
1080 cmd->set_thread_register(3, _options.customStackAddr()); // r1
1081 return p + threadLoadCommandSize();
1082 }
1083
1084 template <>
1085 uint32_t HeaderAndLoadCommandsAtom<x86>::threadLoadCommandSize() const
1086 {
1087 return this->alignedSize(16 + 16*4); // base size + i386_THREAD_STATE_COUNT * 4
1088 }
1089
1090 template <>
1091 uint8_t* HeaderAndLoadCommandsAtom<x86>::copyThreadsLoadCommand(uint8_t* p) const
1092 {
1093 assert(_state.entryPoint != NULL);
1094 pint_t start = _state.entryPoint->finalAddress();
1095 macho_thread_command<P>* cmd = (macho_thread_command<P>*)p;
1096 cmd->set_cmd(LC_UNIXTHREAD);
1097 cmd->set_cmdsize(threadLoadCommandSize());
1098 cmd->set_flavor(1); // i386_THREAD_STATE
1099 cmd->set_count(16); // i386_THREAD_STATE_COUNT;
1100 cmd->set_thread_register(10, start);
1101 if ( _options.hasCustomStack() )
1102 cmd->set_thread_register(7, _options.customStackAddr()); // r1
1103 return p + threadLoadCommandSize();
1104 }
1105
1106 template <>
1107 uint32_t HeaderAndLoadCommandsAtom<x86_64>::threadLoadCommandSize() const
1108 {
1109 return this->alignedSize(16 + x86_THREAD_STATE64_COUNT * 4);
1110 }
1111
1112 template <>
1113 uint8_t* HeaderAndLoadCommandsAtom<x86_64>::copyThreadsLoadCommand(uint8_t* p) const
1114 {
1115 assert(_state.entryPoint != NULL);
1116 pint_t start = _state.entryPoint->finalAddress();
1117 macho_thread_command<P>* cmd = (macho_thread_command<P>*)p;
1118 cmd->set_cmd(LC_UNIXTHREAD);
1119 cmd->set_cmdsize(threadLoadCommandSize());
1120 cmd->set_flavor(x86_THREAD_STATE64);
1121 cmd->set_count(x86_THREAD_STATE64_COUNT);
1122 cmd->set_thread_register(16, start); // rip
1123 if ( _options.hasCustomStack() )
1124 cmd->set_thread_register(7, _options.customStackAddr()); // r1
1125 return p + threadLoadCommandSize();
1126 }
1127
1128 template <>
1129 uint32_t HeaderAndLoadCommandsAtom<arm>::threadLoadCommandSize() const
1130 {
1131 return this->alignedSize(16 + 17 * 4); // base size + ARM_THREAD_STATE_COUNT * 4
1132 }
1133
1134 template <>
1135 uint8_t* HeaderAndLoadCommandsAtom<arm>::copyThreadsLoadCommand(uint8_t* p) const
1136 {
1137 assert(_state.entryPoint != NULL);
1138 pint_t start = _state.entryPoint->finalAddress();
1139 if ( _state.entryPoint->isThumb() )
1140 start |= 1ULL;
1141 macho_thread_command<P>* cmd = (macho_thread_command<P>*)p;
1142 cmd->set_cmd(LC_UNIXTHREAD);
1143 cmd->set_cmdsize(threadLoadCommandSize());
1144 cmd->set_flavor(1);
1145 cmd->set_count(17);
1146 cmd->set_thread_register(15, start); // pc
1147 if ( _options.hasCustomStack() )
1148 cmd->set_thread_register(13, _options.customStackAddr()); // sp
1149 return p + threadLoadCommandSize();
1150 }
1151
1152 template <typename A>
1153 uint8_t* HeaderAndLoadCommandsAtom<A>::copyEncryptionLoadCommand(uint8_t* p) const
1154 {
1155 macho_encryption_info_command<P>* cmd = (macho_encryption_info_command<P>*)p;
1156 cmd->set_cmd(LC_ENCRYPTION_INFO);
1157 cmd->set_cmdsize(sizeof(macho_encryption_info_command<P>));
1158 assert(_writer.encryptedTextStartOffset() != 0);
1159 assert(_writer.encryptedTextEndOffset() != 0);
1160 cmd->set_cryptoff(_writer.encryptedTextStartOffset());
1161 cmd->set_cryptsize(_writer.encryptedTextEndOffset()-_writer.encryptedTextStartOffset());
1162 cmd->set_cryptid(0);
1163 return p + sizeof(macho_encryption_info_command<P>);
1164 }
1165
1166
1167 template <typename A>
1168 uint8_t* HeaderAndLoadCommandsAtom<A>::copySplitSegInfoLoadCommand(uint8_t* p) const
1169 {
1170 macho_linkedit_data_command<P>* cmd = (macho_linkedit_data_command<P>*)p;
1171 cmd->set_cmd(LC_SEGMENT_SPLIT_INFO);
1172 cmd->set_cmdsize(sizeof(macho_linkedit_data_command<P>));
1173 cmd->set_dataoff(_writer.splitSegInfoSection->fileOffset);
1174 cmd->set_datasize(_writer.splitSegInfoSection->size);
1175 return p + sizeof(macho_linkedit_data_command<P>);
1176 }
1177
1178
1179 template <typename A>
1180 uint8_t* HeaderAndLoadCommandsAtom<A>::copyDylibLoadCommand(uint8_t* p, const ld::dylib::File* dylib) const
1181 {
1182 uint32_t sz = alignedSize(sizeof(macho_dylib_command<P>) + strlen(dylib->installPath()) + 1);
1183 macho_dylib_command<P>* cmd = (macho_dylib_command<P>*)p;
1184 if ( dylib->willBeLazyLoadedDylib() )
1185 cmd->set_cmd(LC_LAZY_LOAD_DYLIB);
1186 else if ( dylib->forcedWeakLinked() || dylib->allSymbolsAreWeakImported() )
1187 cmd->set_cmd(LC_LOAD_WEAK_DYLIB);
1188 else if ( dylib->willBeReExported() && _options.useSimplifiedDylibReExports() )
1189 cmd->set_cmd(LC_REEXPORT_DYLIB);
1190 else if ( dylib->willBeUpwardDylib() && _options.useUpwardDylibs() )
1191 cmd->set_cmd(LC_LOAD_UPWARD_DYLIB);
1192 else
1193 cmd->set_cmd(LC_LOAD_DYLIB);
1194 cmd->set_cmdsize(sz);
1195 cmd->set_timestamp(2); // needs to be some constant value that is different than DylibIDLoadCommandsAtom uses
1196 cmd->set_current_version(dylib->currentVersion());
1197 cmd->set_compatibility_version(dylib->compatibilityVersion());
1198 cmd->set_name_offset();
1199 strcpy((char*)&p[sizeof(macho_dylib_command<P>)], dylib->installPath());
1200 return p + sz;
1201 }
1202
1203 template <typename A>
1204 uint8_t* HeaderAndLoadCommandsAtom<A>::copyRPathLoadCommand(uint8_t* p, const char* path) const
1205 {
1206 uint32_t sz = alignedSize(sizeof(macho_rpath_command<P>) + strlen(path) + 1);
1207 macho_rpath_command<P>* cmd = (macho_rpath_command<P>*)p;
1208 cmd->set_cmd(LC_RPATH);
1209 cmd->set_cmdsize(sz);
1210 cmd->set_path_offset();
1211 strcpy((char*)&p[sizeof(macho_rpath_command<P>)], path);
1212 return p + sz;
1213 }
1214
1215 template <typename A>
1216 uint8_t* HeaderAndLoadCommandsAtom<A>::copySubFrameworkLoadCommand(uint8_t* p) const
1217 {
1218 const char* umbrellaName = _options.umbrellaName();
1219 uint32_t sz = alignedSize(sizeof(macho_sub_framework_command<P>) + strlen(umbrellaName) + 1);
1220 macho_sub_framework_command<P>* cmd = (macho_sub_framework_command<P>*)p;
1221 cmd->set_cmd(LC_SUB_FRAMEWORK);
1222 cmd->set_cmdsize(sz);
1223 cmd->set_umbrella_offset();
1224 strcpy((char*)&p[sizeof(macho_sub_framework_command<P>)], umbrellaName);
1225 return p + sz;
1226 }
1227
1228
1229 template <typename A>
1230 uint8_t* HeaderAndLoadCommandsAtom<A>::copyAllowableClientLoadCommand(uint8_t* p, const char* client) const
1231 {
1232 uint32_t sz = alignedSize(sizeof(macho_sub_client_command<P>) + strlen(client) + 1);
1233 macho_sub_client_command<P>* cmd = (macho_sub_client_command<P>*)p;
1234 cmd->set_cmd(LC_SUB_CLIENT);
1235 cmd->set_cmdsize(sz);
1236 cmd->set_client_offset();
1237 strcpy((char*)&p[sizeof(macho_sub_client_command<P>)], client);
1238 return p + sz;
1239 }
1240
1241 template <typename A>
1242 uint8_t* HeaderAndLoadCommandsAtom<A>::copyDyldEnvLoadCommand(uint8_t* p, const char* env) const
1243 {
1244 uint32_t sz = alignedSize(sizeof(macho_dylinker_command<P>) + strlen(env) + 1);
1245 macho_dylinker_command<P>* cmd = (macho_dylinker_command<P>*)p;
1246 cmd->set_cmd(LC_DYLD_ENVIRONMENT);
1247 cmd->set_cmdsize(sz);
1248 cmd->set_name_offset();
1249 strcpy((char*)&p[sizeof(macho_dylinker_command<P>)], env);
1250 return p + sz;
1251 }
1252
1253 template <typename A>
1254 uint8_t* HeaderAndLoadCommandsAtom<A>::copySubUmbrellaLoadCommand(uint8_t* p, const char* nm) const
1255 {
1256 uint32_t sz = alignedSize(sizeof(macho_sub_umbrella_command<P>) + strlen(nm) + 1);
1257 macho_sub_umbrella_command<P>* cmd = (macho_sub_umbrella_command<P>*)p;
1258 cmd->set_cmd(LC_SUB_UMBRELLA);
1259 cmd->set_cmdsize(sz);
1260 cmd->set_sub_umbrella_offset();
1261 strcpy((char*)&p[sizeof(macho_sub_umbrella_command<P>)], nm);
1262 return p + sz;
1263 }
1264
1265 template <typename A>
1266 uint8_t* HeaderAndLoadCommandsAtom<A>::copySubLibraryLoadCommand(uint8_t* p, const char* nm) const
1267 {
1268 uint32_t sz = alignedSize(sizeof(macho_sub_library_command<P>) + strlen(nm) + 1);
1269 macho_sub_library_command<P>* cmd = (macho_sub_library_command<P>*)p;
1270 cmd->set_cmd(LC_SUB_LIBRARY);
1271 cmd->set_cmdsize(sz);
1272 cmd->set_sub_library_offset();
1273 strcpy((char*)&p[sizeof(macho_sub_library_command<P>)], nm);
1274 return p + sz;
1275 }
1276
1277 template <typename A>
1278 uint8_t* HeaderAndLoadCommandsAtom<A>::copyFunctionStartsLoadCommand(uint8_t* p) const
1279 {
1280 macho_linkedit_data_command<P>* cmd = (macho_linkedit_data_command<P>*)p;
1281 cmd->set_cmd(LC_FUNCTION_STARTS);
1282 cmd->set_cmdsize(sizeof(macho_linkedit_data_command<P>));
1283 cmd->set_dataoff(_writer.functionStartsSection->fileOffset);
1284 cmd->set_datasize(_writer.functionStartsSection->size);
1285 return p + sizeof(macho_linkedit_data_command<P>);
1286 }
1287
1288
1289 template <typename A>
1290 void HeaderAndLoadCommandsAtom<A>::copyRawContent(uint8_t buffer[]) const
1291 {
1292 macho_header<P>* mh = (macho_header<P>*)buffer;
1293 bzero(buffer, this->size());
1294
1295 // copy mach_header
1296 mh->set_magic(this->magic());
1297 mh->set_cputype(this->cpuType());
1298 mh->set_cpusubtype(this->cpuSubType());
1299 mh->set_filetype(this->fileType());
1300 mh->set_ncmds(this->commandsCount());
1301 mh->set_sizeofcmds(this->size()-sizeof(macho_header<P>));
1302 mh->set_flags(this->flags());
1303
1304 // copy load commands
1305 uint8_t* p = &buffer[sizeof(macho_header<P>)];
1306
1307 if ( _options.outputKind() == Options::kObjectFile )
1308 p = this->copySingleSegmentLoadCommand(p);
1309 else
1310 p = this->copySegmentLoadCommands(p);
1311
1312 if ( _hasDylibIDLoadCommand )
1313 p = this->copyDylibIDLoadCommand(p);
1314
1315 if ( _hasDyldInfoLoadCommand )
1316 p = this->copyDyldInfoLoadCommand(p);
1317
1318 if ( _hasSymbolTableLoadCommand )
1319 p = this->copySymbolTableLoadCommand(p);
1320
1321 if ( _hasDynamicSymbolTableLoadCommand )
1322 p = this->copyDynamicSymbolTableLoadCommand(p);
1323
1324 if ( _hasDyldLoadCommand )
1325 p = this->copyDyldLoadCommand(p);
1326
1327 if ( _hasRoutinesLoadCommand )
1328 p = this->copyRoutinesLoadCommand(p);
1329
1330 if ( _hasUUIDLoadCommand )
1331 p = this->copyUUIDLoadCommand(p);
1332
1333 if ( _hasVersionLoadCommand )
1334 p = this->copyVersionLoadCommand(p);
1335
1336 if ( _hasThreadLoadCommand )
1337 p = this->copyThreadsLoadCommand(p);
1338
1339 if ( _hasEncryptionLoadCommand )
1340 p = this->copyEncryptionLoadCommand(p);
1341
1342 if ( _hasSplitSegInfoLoadCommand )
1343 p = this->copySplitSegInfoLoadCommand(p);
1344
1345 for(uint32_t ord=1; ord <= _writer.dylibCount(); ++ord) {
1346 p = this->copyDylibLoadCommand(p, _writer.dylibByOrdinal(ord));
1347 }
1348
1349 if ( _hasRPathLoadCommands ) {
1350 const std::vector<const char*>& rpaths = _options.rpaths();
1351 for (std::vector<const char*>::const_iterator it = rpaths.begin(); it != rpaths.end(); ++it) {
1352 p = this->copyRPathLoadCommand(p, *it);
1353 }
1354 }
1355
1356 if ( _hasSubFrameworkLoadCommand )
1357 p = this->copySubFrameworkLoadCommand(p);
1358
1359 for (std::vector<const char*>::const_iterator it = _subLibraryNames.begin(); it != _subLibraryNames.end(); ++it) {
1360 p = this->copySubLibraryLoadCommand(p, *it);
1361 }
1362
1363 for (std::vector<const char*>::const_iterator it = _subUmbrellaNames.begin(); it != _subUmbrellaNames.end(); ++it) {
1364 p = this->copySubUmbrellaLoadCommand(p, *it);
1365 }
1366
1367 if ( _allowableClientLoadCommmandsCount != 0 ) {
1368 const std::vector<const char*>& clients = _options.allowableClients();
1369 for (std::vector<const char*>::const_iterator it = clients.begin(); it != clients.end(); ++it) {
1370 p = this->copyAllowableClientLoadCommand(p, *it);
1371 }
1372 }
1373
1374 if ( _dyldEnvironExrasCount != 0 ) {
1375 const std::vector<const char*>& extras = _options.dyldEnvironExtras();
1376 for (std::vector<const char*>::const_iterator it = extras.begin(); it != extras.end(); ++it) {
1377 p = this->copyDyldEnvLoadCommand(p, *it);
1378 }
1379 }
1380
1381 if ( _hasFunctionStartsLoadCommand )
1382 p = this->copyFunctionStartsLoadCommand(p);
1383
1384 }
1385
1386
1387
1388 } // namespace tool
1389 } // namespace ld
1390
1391 #endif // __HEADER_LOAD_COMMANDS_HPP__