1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-*
3 * Copyright (c) 2009-2010 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 __LINKEDIT_HPP__
26 #define __LINKEDIT_HPP__
29 #include <sys/types.h>
38 #include "Architectures.hpp"
39 #include "MachOFileAbstraction.hpp"
46 std::vector<uint8_t> _data;
48 std::vector<uint8_t>& bytes() { return _data; }
49 unsigned long size() const { return _data.size(); }
50 void reserve(unsigned long l) { _data.reserve(l); }
51 const uint8_t* start() const { return &_data[0]; }
53 void append_uleb128(uint64_t value) {
60 _data.push_back(byte);
62 } while( byte >= 0x80 );
65 void append_sleb128(int64_t value) {
66 bool isNeg = ( value < 0 );
73 more = ( (value != -1) || ((byte & 0x40) == 0) );
75 more = ( (value != 0) || ((byte & 0x40) != 0) );
78 _data.push_back(byte);
83 void append_string(const char* str) {
84 for (const char* s = str; *s != '\0'; ++s)
86 _data.push_back('\0');
89 void append_byte(uint8_t byte) {
90 _data.push_back(byte);
93 static unsigned int uleb128_size(uint64_t value) {
98 } while ( value != 0 );
102 void pad_to_size(unsigned int alignment) {
103 while ( (_data.size() % alignment) != 0 )
109 class LinkEditAtom : public ld::Atom
113 // overrides of ld::Atom
114 virtual ld::File* file() const { return NULL; }
115 virtual bool translationUnitSource(const char** dir, const char** nm) const
117 virtual uint64_t objectAddress() const { return 0; }
118 virtual uint64_t size() const;
119 virtual void copyRawContent(uint8_t buffer[]) const;
121 virtual void encode() const = 0;
123 LinkEditAtom(const Options& opts, ld::Internal& state,
124 OutputFile& writer, const ld::Section& sect,
125 unsigned int pointerSize)
126 : ld::Atom(sect, ld::Atom::definitionRegular,
127 ld::Atom::combineNever, ld::Atom::scopeTranslationUnit,
128 ld::Atom::typeUnclassified, ld::Atom::symbolTableNotIn,
129 false, false, false, ld::Atom::Alignment(log2(pointerSize))),
130 _options(opts), _state(state), _writer(writer),
133 const Options& _options;
134 ld::Internal& _state;
136 mutable ByteStream _encodedData;
137 mutable bool _encoded;
140 uint64_t LinkEditAtom::size() const
143 return _encodedData.size();
146 void LinkEditAtom::copyRawContent(uint8_t buffer[]) const
149 memcpy(buffer, _encodedData.start(), _encodedData.size());
155 template <typename A>
156 class RebaseInfoAtom : public LinkEditAtom
159 RebaseInfoAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
160 : LinkEditAtom(opts, state, writer, _s_section, sizeof(pint_t)) { _encoded = true; }
162 // overrides of ld::Atom
163 virtual const char* name() const { return "rebase info"; }
164 // overrides of LinkEditAtom
165 virtual void encode() const;
170 rebase_tmp(uint8_t op, uint64_t p1, uint64_t p2=0) : opcode(op), operand1(p1), operand2(p2) {}
176 typedef typename A::P P;
177 typedef typename A::P::E E;
178 typedef typename A::P::uint_t pint_t;
180 static ld::Section _s_section;
183 template <typename A>
184 ld::Section RebaseInfoAtom<A>::_s_section("__LINKEDIT", "__rebase", ld::Section::typeLinkEdit, true);
187 template <typename A>
188 void RebaseInfoAtom<A>::encode() const
190 // omit relocs if this was supposed to be PIE but PIE not possible
191 if ( _options.positionIndependentExecutable() && this->_writer.pieDisabled )
194 // sort rebase info by type, then address
195 std::vector<OutputFile::RebaseInfo>& info = this->_writer._rebaseInfo;
196 std::sort(info.begin(), info.end());
198 // convert to temp encoding that can be more easily optimized
199 std::vector<rebase_tmp> mid;
200 uint64_t curSegStart = 0;
201 uint64_t curSegEnd = 0;
202 uint32_t curSegIndex = 0;
204 uint64_t address = (uint64_t)(-1);
205 for (std::vector<OutputFile::RebaseInfo>::iterator it = info.begin(); it != info.end(); ++it) {
206 if ( type != it->_type ) {
207 mid.push_back(rebase_tmp(REBASE_OPCODE_SET_TYPE_IMM, it->_type));
210 if ( address != it->_address ) {
211 if ( (it->_address < curSegStart) || ( it->_address >= curSegEnd) ) {
212 if ( ! this->_writer.findSegment(this->_state, it->_address, &curSegStart, &curSegEnd, &curSegIndex) )
213 throw "binding address outside range of any segment";
214 mid.push_back(rebase_tmp(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB, curSegIndex, it->_address - curSegStart));
217 mid.push_back(rebase_tmp(REBASE_OPCODE_ADD_ADDR_ULEB, it->_address-address));
219 address = it->_address;
221 mid.push_back(rebase_tmp(REBASE_OPCODE_DO_REBASE_ULEB_TIMES, 1));
222 address += sizeof(pint_t);
224 mid.push_back(rebase_tmp(REBASE_OPCODE_DONE, 0));
226 // optimize phase 1, compress packed runs of pointers
227 rebase_tmp* dst = &mid[0];
228 for (const rebase_tmp* src = &mid[0]; src->opcode != REBASE_OPCODE_DONE; ++src) {
229 if ( (src->opcode == REBASE_OPCODE_DO_REBASE_ULEB_TIMES) && (src->operand1 == 1) ) {
231 while (src->opcode == REBASE_OPCODE_DO_REBASE_ULEB_TIMES ) {
232 dst->operand1 += src->operand1;
242 dst->opcode = REBASE_OPCODE_DONE;
244 // optimize phase 2, combine rebase/add pairs
246 for (const rebase_tmp* src = &mid[0]; src->opcode != REBASE_OPCODE_DONE; ++src) {
247 if ( (src->opcode == REBASE_OPCODE_DO_REBASE_ULEB_TIMES)
248 && (src->operand1 == 1)
249 && (src[1].opcode == REBASE_OPCODE_ADD_ADDR_ULEB)) {
250 dst->opcode = REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB;
251 dst->operand1 = src[1].operand1;
259 dst->opcode = REBASE_OPCODE_DONE;
261 // optimize phase 3, compress packed runs of REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB with
262 // same addr delta into one REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB
264 for (const rebase_tmp* src = &mid[0]; src->opcode != REBASE_OPCODE_DONE; ++src) {
265 uint64_t delta = src->operand1;
266 if ( (src->opcode == REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB)
267 && (src[1].opcode == REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB)
268 && (src[2].opcode == REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB)
269 && (src[1].operand1 == delta)
270 && (src[2].operand1 == delta) ) {
271 // found at least three in a row, this is worth compressing
272 dst->opcode = REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB;
274 dst->operand2 = delta;
276 while ( (src->opcode == REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB)
277 && (src->operand1 == delta) ) {
288 dst->opcode = REBASE_OPCODE_DONE;
290 // optimize phase 4, use immediate encodings
291 for (rebase_tmp* p = &mid[0]; p->opcode != REBASE_OPCODE_DONE; ++p) {
292 if ( (p->opcode == REBASE_OPCODE_ADD_ADDR_ULEB)
293 && (p->operand1 < (15*sizeof(pint_t)))
294 && ((p->operand1 % sizeof(pint_t)) == 0) ) {
295 p->opcode = REBASE_OPCODE_ADD_ADDR_IMM_SCALED;
296 p->operand1 = p->operand1/sizeof(pint_t);
298 else if ( (p->opcode == REBASE_OPCODE_DO_REBASE_ULEB_TIMES) && (p->operand1 < 15) ) {
299 p->opcode = REBASE_OPCODE_DO_REBASE_IMM_TIMES;
303 // convert to compressed encoding
304 const static bool log = false;
305 this->_encodedData.reserve(info.size()*2);
307 for (typename std::vector<rebase_tmp>::iterator it = mid.begin(); !done && it != mid.end() ; ++it) {
308 switch ( it->opcode ) {
309 case REBASE_OPCODE_DONE:
310 if ( log ) fprintf(stderr, "REBASE_OPCODE_DONE()\n");
313 case REBASE_OPCODE_SET_TYPE_IMM:
314 if ( log ) fprintf(stderr, "REBASE_OPCODE_SET_TYPE_IMM(%lld)\n", it->operand1);
315 this->_encodedData.append_byte(REBASE_OPCODE_SET_TYPE_IMM | it->operand1);
317 case REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
318 if ( log ) fprintf(stderr, "REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB(%lld, 0x%llX)\n", it->operand1, it->operand2);
319 this->_encodedData.append_byte(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | it->operand1);
320 this->_encodedData.append_uleb128(it->operand2);
322 case REBASE_OPCODE_ADD_ADDR_ULEB:
323 if ( log ) fprintf(stderr, "REBASE_OPCODE_ADD_ADDR_ULEB(0x%llX)\n", it->operand1);
324 this->_encodedData.append_byte(REBASE_OPCODE_ADD_ADDR_ULEB);
325 this->_encodedData.append_uleb128(it->operand1);
327 case REBASE_OPCODE_ADD_ADDR_IMM_SCALED:
328 if ( log ) fprintf(stderr, "REBASE_OPCODE_ADD_ADDR_IMM_SCALED(%lld=0x%llX)\n", it->operand1, it->operand1*sizeof(pint_t));
329 this->_encodedData.append_byte(REBASE_OPCODE_ADD_ADDR_IMM_SCALED | it->operand1 );
331 case REBASE_OPCODE_DO_REBASE_IMM_TIMES:
332 if ( log ) fprintf(stderr, "REBASE_OPCODE_DO_REBASE_IMM_TIMES(%lld)\n", it->operand1);
333 this->_encodedData.append_byte(REBASE_OPCODE_DO_REBASE_IMM_TIMES | it->operand1);
335 case REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
336 if ( log ) fprintf(stderr, "REBASE_OPCODE_DO_REBASE_ULEB_TIMES(%lld)\n", it->operand1);
337 this->_encodedData.append_byte(REBASE_OPCODE_DO_REBASE_ULEB_TIMES);
338 this->_encodedData.append_uleb128(it->operand1);
340 case REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
341 if ( log ) fprintf(stderr, "REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB(0x%llX)\n", it->operand1);
342 this->_encodedData.append_byte(REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB);
343 this->_encodedData.append_uleb128(it->operand1);
345 case REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
346 if ( log ) fprintf(stderr, "REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB(%lld, %lld)\n", it->operand1, it->operand2);
347 this->_encodedData.append_byte(REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB);
348 this->_encodedData.append_uleb128(it->operand1);
349 this->_encodedData.append_uleb128(it->operand2);
355 // align to pointer size
356 this->_encodedData.pad_to_size(sizeof(pint_t));
358 this->_encoded = true;
360 if (log) fprintf(stderr, "total rebase info size = %ld\n", this->_encodedData.size());
364 template <typename A>
365 class BindingInfoAtom : public LinkEditAtom
368 BindingInfoAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
369 : LinkEditAtom(opts, state, writer, _s_section, sizeof(pint_t)) { }
371 // overrides of ld::Atom
372 virtual const char* name() const { return "binding info"; }
373 // overrides of LinkEditAtom
374 virtual void encode() const;
378 typedef typename A::P P;
379 typedef typename A::P::E E;
380 typedef typename A::P::uint_t pint_t;
384 binding_tmp(uint8_t op, uint64_t p1, uint64_t p2=0, const char* s=NULL)
385 : opcode(op), operand1(p1), operand2(p2), name(s) {}
392 static ld::Section _s_section;
395 template <typename A>
396 ld::Section BindingInfoAtom<A>::_s_section("__LINKEDIT", "__binding", ld::Section::typeLinkEdit, true);
399 template <typename A>
400 void BindingInfoAtom<A>::encode() const
402 // sort by library, symbol, type, then address
403 std::vector<OutputFile::BindingInfo>& info = this->_writer._bindingInfo;
404 std::sort(info.begin(), info.end());
406 // convert to temp encoding that can be more easily optimized
407 std::vector<binding_tmp> mid;
408 uint64_t curSegStart = 0;
409 uint64_t curSegEnd = 0;
410 uint32_t curSegIndex = 0;
411 int ordinal = 0x80000000;
412 const char* symbolName = NULL;
414 uint64_t address = (uint64_t)(-1);
416 for (std::vector<OutputFile::BindingInfo>::const_iterator it = info.begin(); it != info.end(); ++it) {
417 if ( ordinal != it->_libraryOrdinal ) {
418 if ( it->_libraryOrdinal <= 0 ) {
419 // special lookups are encoded as negative numbers in BindingInfo
420 mid.push_back(binding_tmp(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM, it->_libraryOrdinal));
423 mid.push_back(binding_tmp(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB, it->_libraryOrdinal));
425 ordinal = it->_libraryOrdinal;
427 if ( symbolName != it->_symbolName ) {
428 mid.push_back(binding_tmp(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM, it->_flags, 0, it->_symbolName));
429 symbolName = it->_symbolName;
431 if ( type != it->_type ) {
432 mid.push_back(binding_tmp(BIND_OPCODE_SET_TYPE_IMM, it->_type));
435 if ( address != it->_address ) {
436 if ( (it->_address < curSegStart) || ( it->_address >= curSegEnd) ) {
437 if ( ! this->_writer.findSegment(this->_state, it->_address, &curSegStart, &curSegEnd, &curSegIndex) )
438 throw "binding address outside range of any segment";
439 mid.push_back(binding_tmp(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB, curSegIndex, it->_address - curSegStart));
442 mid.push_back(binding_tmp(BIND_OPCODE_ADD_ADDR_ULEB, it->_address-address));
444 address = it->_address;
446 if ( addend != it->_addend ) {
447 mid.push_back(binding_tmp(BIND_OPCODE_SET_ADDEND_SLEB, it->_addend));
448 addend = it->_addend;
450 mid.push_back(binding_tmp(BIND_OPCODE_DO_BIND, 0));
451 address += sizeof(pint_t);
453 mid.push_back(binding_tmp(BIND_OPCODE_DONE, 0));
456 // optimize phase 1, combine bind/add pairs
457 binding_tmp* dst = &mid[0];
458 for (const binding_tmp* src = &mid[0]; src->opcode != BIND_OPCODE_DONE; ++src) {
459 if ( (src->opcode == BIND_OPCODE_DO_BIND)
460 && (src[1].opcode == BIND_OPCODE_ADD_ADDR_ULEB) ) {
461 dst->opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB;
462 dst->operand1 = src[1].operand1;
470 dst->opcode = BIND_OPCODE_DONE;
472 // optimize phase 2, compress packed runs of BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB with
473 // same addr delta into one BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB
475 for (const binding_tmp* src = &mid[0]; src->opcode != BIND_OPCODE_DONE; ++src) {
476 uint64_t delta = src->operand1;
477 if ( (src->opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB)
478 && (src[1].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB)
479 && (src[1].operand1 == delta) ) {
480 // found at least two in a row, this is worth compressing
481 dst->opcode = BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB;
483 dst->operand2 = delta;
485 while ( (src->opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB)
486 && (src->operand1 == delta) ) {
497 dst->opcode = BIND_OPCODE_DONE;
499 // optimize phase 3, use immediate encodings
500 for (binding_tmp* p = &mid[0]; p->opcode != REBASE_OPCODE_DONE; ++p) {
501 if ( (p->opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB)
502 && (p->operand1 < (15*sizeof(pint_t)))
503 && ((p->operand1 % sizeof(pint_t)) == 0) ) {
504 p->opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED;
505 p->operand1 = p->operand1/sizeof(pint_t);
507 else if ( (p->opcode == BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB) && (p->operand1 <= 15) ) {
508 p->opcode = BIND_OPCODE_SET_DYLIB_ORDINAL_IMM;
511 dst->opcode = BIND_OPCODE_DONE;
513 // convert to compressed encoding
514 const static bool log = false;
515 this->_encodedData.reserve(info.size()*2);
517 for (typename std::vector<binding_tmp>::iterator it = mid.begin(); !done && it != mid.end() ; ++it) {
518 switch ( it->opcode ) {
519 case BIND_OPCODE_DONE:
520 if ( log ) fprintf(stderr, "BIND_OPCODE_DONE()\n");
523 case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
524 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM(%lld)\n", it->operand1);
525 this->_encodedData.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | it->operand1);
527 case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
528 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB(%lld)\n", it->operand1);
529 this->_encodedData.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
530 this->_encodedData.append_uleb128(it->operand1);
532 case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
533 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM(%lld)\n", it->operand1);
534 this->_encodedData.append_byte(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM | (it->operand1 & BIND_IMMEDIATE_MASK));
536 case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
537 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM(0x%0llX, %s)\n", it->operand1, it->name);
538 this->_encodedData.append_byte(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | it->operand1);
539 this->_encodedData.append_string(it->name);
541 case BIND_OPCODE_SET_TYPE_IMM:
542 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_TYPE_IMM(%lld)\n", it->operand1);
543 this->_encodedData.append_byte(BIND_OPCODE_SET_TYPE_IMM | it->operand1);
545 case BIND_OPCODE_SET_ADDEND_SLEB:
546 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_ADDEND_SLEB(%lld)\n", it->operand1);
547 this->_encodedData.append_byte(BIND_OPCODE_SET_ADDEND_SLEB);
548 this->_encodedData.append_sleb128(it->operand1);
550 case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
551 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB(%lld, 0x%llX)\n", it->operand1, it->operand2);
552 this->_encodedData.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | it->operand1);
553 this->_encodedData.append_uleb128(it->operand2);
555 case BIND_OPCODE_ADD_ADDR_ULEB:
556 if ( log ) fprintf(stderr, "BIND_OPCODE_ADD_ADDR_ULEB(0x%llX)\n", it->operand1);
557 this->_encodedData.append_byte(BIND_OPCODE_ADD_ADDR_ULEB);
558 this->_encodedData.append_uleb128(it->operand1);
560 case BIND_OPCODE_DO_BIND:
561 if ( log ) fprintf(stderr, "BIND_OPCODE_DO_BIND()\n");
562 this->_encodedData.append_byte(BIND_OPCODE_DO_BIND);
564 case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
565 if ( log ) fprintf(stderr, "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB(0x%llX)\n", it->operand1);
566 this->_encodedData.append_byte(BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB);
567 this->_encodedData.append_uleb128(it->operand1);
569 case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
570 if ( log ) fprintf(stderr, "BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED(%lld=0x%llX)\n", it->operand1, it->operand1*sizeof(pint_t));
571 this->_encodedData.append_byte(BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED | it->operand1 );
573 case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
574 if ( log ) fprintf(stderr, "BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB(%lld, %lld)\n", it->operand1, it->operand2);
575 this->_encodedData.append_byte(BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB);
576 this->_encodedData.append_uleb128(it->operand1);
577 this->_encodedData.append_uleb128(it->operand2);
582 // align to pointer size
583 this->_encodedData.pad_to_size(sizeof(pint_t));
585 this->_encoded = true;
587 if (log) fprintf(stderr, "total binding info size = %ld\n", this->_encodedData.size());
592 template <typename A>
593 class WeakBindingInfoAtom : public LinkEditAtom
596 WeakBindingInfoAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
597 : LinkEditAtom(opts, state, writer, _s_section, sizeof(pint_t)) { _encoded = true; }
599 // overrides of ld::Atom
600 virtual const char* name() const { return "weak binding info"; }
601 // overrides of LinkEditAtom
602 virtual void encode() const;
605 typedef typename A::P P;
606 typedef typename A::P::E E;
607 typedef typename A::P::uint_t pint_t;
609 struct WeakBindingSorter
611 bool operator()(const OutputFile::BindingInfo& left, const OutputFile::BindingInfo& right)
613 // sort by symbol, type, address
614 if ( left._symbolName != right._symbolName )
615 return ( strcmp(left._symbolName, right._symbolName) < 0 );
616 if ( left._type != right._type )
617 return (left._type < right._type);
618 return (left._address < right._address);
624 binding_tmp(uint8_t op, uint64_t p1, uint64_t p2=0, const char* s=NULL)
625 : opcode(op), operand1(p1), operand2(p2), name(s) {}
632 static ld::Section _s_section;
635 template <typename A>
636 ld::Section WeakBindingInfoAtom<A>::_s_section("__LINKEDIT", "__weak_binding", ld::Section::typeLinkEdit, true);
639 template <typename A>
640 void WeakBindingInfoAtom<A>::encode() const
642 // sort by symbol, type, address
643 std::vector<OutputFile::BindingInfo>& info = this->_writer._weakBindingInfo;
644 if ( info.size() == 0 ) {
645 // short circuit if no weak binding needed
646 this->_encoded = true;
649 std::sort(info.begin(), info.end(), WeakBindingSorter());
651 // convert to temp encoding that can be more easily optimized
652 std::vector<binding_tmp> mid;
653 mid.reserve(info.size());
654 uint64_t curSegStart = 0;
655 uint64_t curSegEnd = 0;
656 uint32_t curSegIndex = 0;
657 const char* symbolName = NULL;
659 uint64_t address = (uint64_t)(-1);
661 for (typename std::vector<OutputFile::BindingInfo>::const_iterator it = info.begin(); it != info.end(); ++it) {
662 if ( symbolName != it->_symbolName ) {
663 mid.push_back(binding_tmp(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM, it->_flags, 0, it->_symbolName));
664 symbolName = it->_symbolName;
666 // non-weak symbols just have BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
667 // weak symbols have SET_SEG, ADD_ADDR, SET_ADDED, DO_BIND
668 if ( it->_type != BIND_TYPE_OVERRIDE_OF_WEAKDEF_IN_DYLIB ) {
669 if ( type != it->_type ) {
670 mid.push_back(binding_tmp(BIND_OPCODE_SET_TYPE_IMM, it->_type));
673 if ( address != it->_address ) {
674 if ( (it->_address < curSegStart) || ( it->_address >= curSegEnd) ) {
675 if ( ! this->_writer.findSegment(this->_state, it->_address, &curSegStart, &curSegEnd, &curSegIndex) )
676 throw "binding address outside range of any segment";
677 mid.push_back(binding_tmp(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB, curSegIndex, it->_address - curSegStart));
680 mid.push_back(binding_tmp(BIND_OPCODE_ADD_ADDR_ULEB, it->_address-address));
682 address = it->_address;
684 if ( addend != it->_addend ) {
685 mid.push_back(binding_tmp(BIND_OPCODE_SET_ADDEND_SLEB, it->_addend));
686 addend = it->_addend;
688 mid.push_back(binding_tmp(BIND_OPCODE_DO_BIND, 0));
689 address += sizeof(pint_t);
692 mid.push_back(binding_tmp(BIND_OPCODE_DONE, 0));
695 // optimize phase 1, combine bind/add pairs
696 binding_tmp* dst = &mid[0];
697 for (const binding_tmp* src = &mid[0]; src->opcode != BIND_OPCODE_DONE; ++src) {
698 if ( (src->opcode == BIND_OPCODE_DO_BIND)
699 && (src[1].opcode == BIND_OPCODE_ADD_ADDR_ULEB) ) {
700 dst->opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB;
701 dst->operand1 = src[1].operand1;
709 dst->opcode = BIND_OPCODE_DONE;
711 // optimize phase 2, compress packed runs of BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB with
712 // same addr delta into one BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB
714 for (const binding_tmp* src = &mid[0]; src->opcode != BIND_OPCODE_DONE; ++src) {
715 uint64_t delta = src->operand1;
716 if ( (src->opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB)
717 && (src[1].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB)
718 && (src[1].operand1 == delta) ) {
719 // found at least two in a row, this is worth compressing
720 dst->opcode = BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB;
722 dst->operand2 = delta;
724 while ( (src->opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB)
725 && (src->operand1 == delta) ) {
736 dst->opcode = BIND_OPCODE_DONE;
738 // optimize phase 3, use immediate encodings
739 for (binding_tmp* p = &mid[0]; p->opcode != REBASE_OPCODE_DONE; ++p) {
740 if ( (p->opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB)
741 && (p->operand1 < (15*sizeof(pint_t)))
742 && ((p->operand1 % sizeof(pint_t)) == 0) ) {
743 p->opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED;
744 p->operand1 = p->operand1/sizeof(pint_t);
747 dst->opcode = BIND_OPCODE_DONE;
750 // convert to compressed encoding
751 const static bool log = false;
752 this->_encodedData.reserve(info.size()*2);
754 for (typename std::vector<binding_tmp>::iterator it = mid.begin(); !done && it != mid.end() ; ++it) {
755 switch ( it->opcode ) {
756 case BIND_OPCODE_DONE:
757 if ( log ) fprintf(stderr, "BIND_OPCODE_DONE()\n");
758 this->_encodedData.append_byte(BIND_OPCODE_DONE);
761 case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
762 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM(%lld)\n", it->operand1);
763 this->_encodedData.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | it->operand1);
765 case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
766 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB(%lld)\n", it->operand1);
767 this->_encodedData.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
768 this->_encodedData.append_uleb128(it->operand1);
770 case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
771 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM(%lld)\n", it->operand1);
772 this->_encodedData.append_byte(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM | (it->operand1 & BIND_IMMEDIATE_MASK));
774 case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
775 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM(0x%0llX, %s)\n", it->operand1, it->name);
776 this->_encodedData.append_byte(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | it->operand1);
777 this->_encodedData.append_string(it->name);
779 case BIND_OPCODE_SET_TYPE_IMM:
780 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_TYPE_IMM(%lld)\n", it->operand1);
781 this->_encodedData.append_byte(BIND_OPCODE_SET_TYPE_IMM | it->operand1);
783 case BIND_OPCODE_SET_ADDEND_SLEB:
784 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_ADDEND_SLEB(%lld)\n", it->operand1);
785 this->_encodedData.append_byte(BIND_OPCODE_SET_ADDEND_SLEB);
786 this->_encodedData.append_sleb128(it->operand1);
788 case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
789 if ( log ) fprintf(stderr, "BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB(%lld, 0x%llX)\n", it->operand1, it->operand2);
790 this->_encodedData.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | it->operand1);
791 this->_encodedData.append_uleb128(it->operand2);
793 case BIND_OPCODE_ADD_ADDR_ULEB:
794 if ( log ) fprintf(stderr, "BIND_OPCODE_ADD_ADDR_ULEB(0x%llX)\n", it->operand1);
795 this->_encodedData.append_byte(BIND_OPCODE_ADD_ADDR_ULEB);
796 this->_encodedData.append_uleb128(it->operand1);
798 case BIND_OPCODE_DO_BIND:
799 if ( log ) fprintf(stderr, "BIND_OPCODE_DO_BIND()\n");
800 this->_encodedData.append_byte(BIND_OPCODE_DO_BIND);
802 case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
803 if ( log ) fprintf(stderr, "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB(0x%llX)\n", it->operand1);
804 this->_encodedData.append_byte(BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB);
805 this->_encodedData.append_uleb128(it->operand1);
807 case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
808 if ( log ) fprintf(stderr, "BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED(%lld=0x%llX)\n", it->operand1, it->operand1*sizeof(pint_t));
809 this->_encodedData.append_byte(BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED | it->operand1 );
811 case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
812 if ( log ) fprintf(stderr, "BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB(%lld, %lld)\n", it->operand1, it->operand2);
813 this->_encodedData.append_byte(BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB);
814 this->_encodedData.append_uleb128(it->operand1);
815 this->_encodedData.append_uleb128(it->operand2);
820 // align to pointer size
821 this->_encodedData.pad_to_size(sizeof(pint_t));
823 this->_encoded = true;
825 if (log) fprintf(stderr, "total weak binding info size = %ld\n", this->_encodedData.size());
831 template <typename A>
832 class LazyBindingInfoAtom : public LinkEditAtom
835 LazyBindingInfoAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
836 : LinkEditAtom(opts, state, writer, _s_section, sizeof(pint_t)) {_encoded = true; }
838 // overrides of ld::Atom
839 virtual const char* name() const { return "lazy binding info"; }
840 // overrides of LinkEditAtom
841 virtual void encode() const;
844 typedef typename A::P P;
845 typedef typename A::P::E E;
846 typedef typename A::P::uint_t pint_t;
848 static ld::Section _s_section;
851 template <typename A>
852 ld::Section LazyBindingInfoAtom<A>::_s_section("__LINKEDIT", "__lazy_binding", ld::Section::typeLinkEdit, true);
856 template <typename A>
857 void LazyBindingInfoAtom<A>::encode() const
859 // stream all lazy bindings and record start offsets
860 std::vector<OutputFile::BindingInfo>& info = this->_writer._lazyBindingInfo;
861 for (std::vector<OutputFile::BindingInfo>::const_iterator it = info.begin(); it != info.end(); ++it) {
862 // record start offset for use by stub helper
863 this->_writer.setLazyBindingInfoOffset(it->_address, this->_encodedData.size());
865 // write address to bind
866 uint64_t segStart = 0;
868 uint32_t segIndex = 0;
869 if ( ! this->_writer.findSegment(this->_state, it->_address, &segStart, &segEnd, &segIndex) )
870 throw "lazy binding address outside range of any segment";
871 this->_encodedData.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | segIndex);
872 this->_encodedData.append_uleb128(it->_address - segStart);
875 if ( it->_libraryOrdinal <= 0 ) {
876 // special lookups are encoded as negative numbers in BindingInfo
877 this->_encodedData.append_byte(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM | (it->_libraryOrdinal & BIND_IMMEDIATE_MASK) );
879 else if ( it->_libraryOrdinal <= 15 ) {
880 // small ordinals are encoded in opcode
881 this->_encodedData.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | it->_libraryOrdinal);
884 this->_encodedData.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
885 this->_encodedData.append_uleb128(it->_libraryOrdinal);
888 this->_encodedData.append_byte(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | it->_flags);
889 this->_encodedData.append_string(it->_symbolName);
891 this->_encodedData.append_byte(BIND_OPCODE_DO_BIND);
892 this->_encodedData.append_byte(BIND_OPCODE_DONE);
895 // align to pointer size
896 this->_encodedData.pad_to_size(sizeof(pint_t));
898 this->_encoded = true;
899 //fprintf(stderr, "lazy binding info size = %ld, for %ld entries\n", _encodedData.size(), allLazys.size());
904 template <typename A>
905 class ExportInfoAtom : public LinkEditAtom
908 ExportInfoAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
909 : LinkEditAtom(opts, state, writer, _s_section, sizeof(pint_t)) { _encoded = true; }
911 // overrides of ld::Atom
912 virtual const char* name() const { return "export info"; }
913 // overrides of LinkEditAtom
914 virtual void encode() const;
917 typedef typename A::P P;
918 typedef typename A::P::E E;
919 typedef typename A::P::uint_t pint_t;
921 const ld::Atom* stubForResolverFunction(const ld::Atom* resolver) const;
923 struct TrieEntriesSorter
925 TrieEntriesSorter(const Options& o) : _options(o) {}
927 bool operator()(const mach_o::trie::Entry& left, const mach_o::trie::Entry& right)
929 unsigned int leftOrder;
930 unsigned int rightOrder;
931 _options.exportedSymbolOrder(left.name, &leftOrder);
932 _options.exportedSymbolOrder(right.name, &rightOrder);
933 if ( leftOrder != rightOrder )
934 return (leftOrder < rightOrder);
936 return (left.address < right.address);
939 const Options& _options;
942 static ld::Section _s_section;
945 template <typename A>
946 ld::Section ExportInfoAtom<A>::_s_section("__LINKEDIT", "__export", ld::Section::typeLinkEdit, true);
948 template <typename A>
949 const ld::Atom* ExportInfoAtom<A>::stubForResolverFunction(const ld::Atom* resolver) const
951 for (std::vector<ld::Internal::FinalSection*>::iterator sit = _state.sections.begin(); sit != _state.sections.end(); ++sit) {
952 ld::Internal::FinalSection* sect = *sit;
953 if ( (sect->type() == ld::Section::typeStub) || (sect->type() == ld::Section::typeStubClose) ) {
954 for (std::vector<const ld::Atom*>::iterator ait = sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
955 const ld::Atom* atom = *ait;
956 if ( strcmp(atom->name(), resolver->name()) == 0 )
961 assert(0 && "no stub for resolver function");
966 template <typename A>
967 void ExportInfoAtom<A>::encode() const
969 // make vector of mach_o::trie::Entry for all exported symbols
970 std::vector<const ld::Atom*>& exports = this->_writer._exportedAtoms;
971 uint64_t imageBaseAddress = this->_writer.headerAndLoadCommandsSection->address;
972 std::vector<mach_o::trie::Entry> entries;
973 entries.reserve(exports.size());
974 for (std::vector<const ld::Atom*>::const_iterator it = exports.begin(); it != exports.end(); ++it) {
975 const ld::Atom* atom = *it;
976 mach_o::trie::Entry entry;
977 uint64_t flags = (atom->contentType() == ld::Atom::typeTLV) ? EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL : EXPORT_SYMBOL_FLAGS_KIND_REGULAR;
979 uint64_t address = atom->finalAddress() - imageBaseAddress;
980 if ( (atom->definition() == ld::Atom::definitionRegular) && (atom->combine() == ld::Atom::combineByName) )
981 flags |= EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;
982 if ( atom->definition() == ld::Atom::definitionProxy ) {
983 entry.name = atom->name();
984 entry.flags = flags | EXPORT_SYMBOL_FLAGS_REEXPORT;
985 entry.other = this->_writer.compressedOrdinalForAtom(atom);
986 if ( entry.other == BIND_SPECIAL_DYLIB_SELF ) {
987 warning("not adding explict export for symbol %s because it is already re-exported from dylib %s", entry.name, atom->file()->path());
990 if ( atom->isAlias() ) {
991 // alias proxy means symbol was re-exported with a name change
992 const ld::Atom* aliasOf = NULL;
993 for (ld::Fixup::iterator fit = atom->fixupsBegin(); fit != atom->fixupsEnd(); ++fit) {
994 if ( fit->kind == ld::Fixup::kindNoneFollowOn ) {
995 assert(fit->binding == ld::Fixup::bindingDirectlyBound);
996 aliasOf = fit->u.target;
999 assert(aliasOf != NULL);
1000 entry.importName = aliasOf->name();
1003 // symbol name stays same as re-export
1004 entry.importName = atom->name();
1006 entries.push_back(entry);
1007 //fprintf(stderr, "re-export %s from lib %llu as %s\n", entry.importName, entry.other, entry.name);
1010 if ( atom->isThumb() )
1012 if ( atom->contentType() == ld::Atom::typeResolver ) {
1013 flags |= EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER;
1014 // set normal lookup to return stub address
1015 // and add resolver function in new location that newer dyld's can access
1017 const ld::Atom* stub = stubForResolverFunction(atom);
1018 address = stub->finalAddress() - imageBaseAddress;
1019 if ( stub->isThumb() )
1022 entry.name = atom->name();
1023 entry.flags = flags;
1024 entry.address = address;
1025 entry.other = other;
1026 entry.importName = NULL;
1027 entries.push_back(entry);
1031 // sort vector by -exported_symbols_order, and any others by address
1032 std::sort(entries.begin(), entries.end(), TrieEntriesSorter(_options));
1035 mach_o::trie::makeTrie(entries, this->_encodedData.bytes());
1037 // align to pointer size
1038 this->_encodedData.pad_to_size(sizeof(pint_t));
1040 this->_encoded = true;
1044 template <typename A>
1045 class SplitSegInfoAtom : public LinkEditAtom
1048 SplitSegInfoAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
1049 : LinkEditAtom(opts, state, writer, _s_section, sizeof(pint_t)) { }
1051 // overrides of ld::Atom
1052 virtual const char* name() const { return "split seg info"; }
1053 // overrides of LinkEditAtom
1054 virtual void encode() const;
1057 typedef typename A::P P;
1058 typedef typename A::P::E E;
1059 typedef typename A::P::uint_t pint_t;
1061 void addSplitSegInfo(uint64_t address, ld::Fixup::Kind k) const;
1062 void uleb128EncodeAddresses(const std::vector<uint64_t>& locations) const;
1064 mutable std::vector<uint64_t> _32bitPointerLocations;
1065 mutable std::vector<uint64_t> _64bitPointerLocations;
1066 mutable std::vector<uint64_t> _ppcHi16Locations;
1069 static ld::Section _s_section;
1072 template <typename A>
1073 ld::Section SplitSegInfoAtom<A>::_s_section("__LINKEDIT", "__splitSegInfo", ld::Section::typeLinkEdit, true);
1076 void SplitSegInfoAtom<x86_64>::addSplitSegInfo(uint64_t address, ld::Fixup::Kind kind) const
1079 case ld::Fixup::kindStoreX86PCRel32:
1080 case ld::Fixup::kindStoreX86PCRel32_1:
1081 case ld::Fixup::kindStoreX86PCRel32_2:
1082 case ld::Fixup::kindStoreX86PCRel32_4:
1083 case ld::Fixup::kindStoreX86PCRel32GOTLoad:
1084 case ld::Fixup::kindStoreX86PCRel32GOTLoadNowLEA:
1085 case ld::Fixup::kindStoreX86PCRel32GOT:
1086 case ld::Fixup::kindStoreLittleEndian32:
1087 case ld::Fixup::kindStoreTargetAddressLittleEndian32:
1088 case ld::Fixup::kindStoreTargetAddressX86PCRel32:
1089 case ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad:
1090 case ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoadNowLEA:
1091 _32bitPointerLocations.push_back(address);
1093 case ld::Fixup::kindStoreLittleEndian64:
1094 case ld::Fixup::kindStoreTargetAddressLittleEndian64:
1095 _64bitPointerLocations.push_back(address);
1098 warning("codegen at address 0x%08llX prevents image from working in dyld shared cache", address);
1104 void SplitSegInfoAtom<x86>::addSplitSegInfo(uint64_t address, ld::Fixup::Kind kind) const
1107 case ld::Fixup::kindStoreLittleEndian32:
1108 case ld::Fixup::kindStoreTargetAddressLittleEndian32:
1109 _32bitPointerLocations.push_back(address);
1112 warning("codegen at address 0x%08llX prevents image from working in dyld shared cache", address);
1118 void SplitSegInfoAtom<arm>::addSplitSegInfo(uint64_t address, ld::Fixup::Kind kind) const
1121 case ld::Fixup::kindStoreLittleEndian32:
1122 _32bitPointerLocations.push_back(address);
1125 warning("codegen at address 0x%08llX prevents image from working in dyld shared cache", address);
1132 void SplitSegInfoAtom<ppc>::addSplitSegInfo(uint64_t address, ld::Fixup::Kind kind) const
1135 case ld::Fixup::kindStorePPCPicHigh16AddLow:
1136 _ppcHi16Locations.push_back(address);
1138 case ld::Fixup::kindStoreBigEndian32:
1139 _32bitPointerLocations.push_back(address);
1142 warning("codegen at address 0x%08llX prevents image from working in dyld shared cache", address);
1149 void SplitSegInfoAtom<ppc64>::addSplitSegInfo(uint64_t address, ld::Fixup::Kind kind) const
1152 case ld::Fixup::kindStorePPCPicHigh16AddLow:
1153 _ppcHi16Locations.push_back(address);
1156 warning("codegen at address 0x%08llX prevents image from working in dyld shared cache", address);
1162 template <typename A>
1163 void SplitSegInfoAtom<A>::uleb128EncodeAddresses(const std::vector<uint64_t>& locations) const
1165 pint_t addr = this->_options.baseAddress();
1166 for(typename std::vector<uint64_t>::const_iterator it = locations.begin(); it != locations.end(); ++it) {
1167 pint_t nextAddr = *it;
1168 //fprintf(stderr, "nextAddr=0x%0llX\n", (uint64_t)nextAddr);
1169 uint64_t delta = nextAddr - addr;
1170 //fprintf(stderr, "delta=0x%0llX\n", delta);
1172 throw "double split seg info for same address";
1176 byte = delta & 0x7F;
1180 this->_encodedData.append_byte(byte);
1183 while( byte >= 0x80 );
1189 template <typename A>
1190 void SplitSegInfoAtom<A>::encode() const
1192 // sort into group by pointer adjustment kind
1193 std::vector<OutputFile::SplitSegInfoEntry>& info = this->_writer._splitSegInfos;
1194 for (std::vector<OutputFile::SplitSegInfoEntry>::const_iterator it = info.begin(); it != info.end(); ++it) {
1195 this->addSplitSegInfo(it->address, it->kind);
1198 // delta compress runs of addresses
1199 this->_encodedData.reserve(8192);
1200 if ( _32bitPointerLocations.size() != 0 ) {
1201 this->_encodedData.append_byte(1);
1202 //fprintf(stderr, "type 1:\n");
1203 std::sort(_32bitPointerLocations.begin(), _32bitPointerLocations.end());
1204 this->uleb128EncodeAddresses(_32bitPointerLocations);
1205 this->_encodedData.append_byte(0); // terminator
1208 if ( _64bitPointerLocations.size() != 0 ) {
1209 this->_encodedData.append_byte(2);
1210 //fprintf(stderr, "type 2:\n");
1211 std::sort(_64bitPointerLocations.begin(), _64bitPointerLocations.end());
1212 this->uleb128EncodeAddresses(_64bitPointerLocations);
1213 this->_encodedData.append_byte(0); // terminator
1216 if ( _ppcHi16Locations.size() != 0 ) {
1217 this->_encodedData.append_byte(3);
1218 //fprintf(stderr, "type 3:\n");
1219 std::sort(_ppcHi16Locations.begin(), _ppcHi16Locations.end());
1220 this->uleb128EncodeAddresses(_ppcHi16Locations);
1221 this->_encodedData.append_byte(0); // terminator
1224 // always add zero byte to mark end
1225 this->_encodedData.append_byte(0);
1227 // align to pointer size
1228 this->_encodedData.pad_to_size(sizeof(pint_t));
1230 this->_encoded = true;
1232 // clean up temporaries
1233 _32bitPointerLocations.clear();
1234 _64bitPointerLocations.clear();
1235 _ppcHi16Locations.clear();
1239 template <typename A>
1240 class FunctionStartsAtom : public LinkEditAtom
1243 FunctionStartsAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
1244 : LinkEditAtom(opts, state, writer, _s_section, sizeof(pint_t)) { }
1246 // overrides of ld::Atom
1247 virtual const char* name() const { return "function starts"; }
1248 // overrides of LinkEditAtom
1249 virtual void encode() const;
1252 typedef typename A::P P;
1253 typedef typename A::P::E E;
1254 typedef typename A::P::uint_t pint_t;
1256 static ld::Section _s_section;
1259 template <typename A>
1260 ld::Section FunctionStartsAtom<A>::_s_section("__LINKEDIT", "__funcStarts", ld::Section::typeLinkEdit, true);
1263 template <typename A>
1264 void FunctionStartsAtom<A>::encode() const
1266 this->_encodedData.reserve(8192);
1267 const uint64_t badAddress = 1;
1268 uint64_t addr = badAddress;
1269 // delta compress all function addresses
1270 for (std::vector<ld::Internal::FinalSection*>::iterator it = this->_state.sections.begin(); it != this->_state.sections.end(); ++it) {
1271 ld::Internal::FinalSection* sect = *it;
1272 if ( sect->type() == ld::Section::typeMachHeader ) {
1273 // start with delta from start of __TEXT
1274 addr = sect->address;
1276 else if ( sect->type() == ld::Section::typeCode ) {
1277 assert(addr != badAddress);
1278 std::vector<const ld::Atom*>& atoms = sect->atoms;
1279 for (std::vector<const ld::Atom*>::iterator ait = atoms.begin(); ait != atoms.end(); ++ait) {
1280 const ld::Atom* atom = *ait;
1281 uint64_t nextAddr = atom->finalAddress();
1282 if ( atom->isThumb() )
1284 uint64_t delta = nextAddr - addr;
1286 this->_encodedData.append_uleb128(delta);
1293 this->_encodedData.append_byte(0);
1295 // align to pointer size
1296 this->_encodedData.pad_to_size(sizeof(pint_t));
1298 this->_encoded = true;
1306 #endif // __LINKEDIT_HPP__