]> git.saurik.com Git - apple/ld64.git/blob - src/ld/passes/compact_unwind.cpp
d2847bb98d16a84ab176e96110214f849fe1ae02
[apple/ld64.git] / src / ld / passes / compact_unwind.cpp
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
26 #include <stdint.h>
27 #include <math.h>
28 #include <unistd.h>
29 #include <dlfcn.h>
30 #include <mach/machine.h>
31 #include <mach-o/compact_unwind_encoding.h>
32
33 #include <vector>
34 #include <map>
35
36 #include "ld.hpp"
37 #include "compact_unwind.h"
38 #include "Architectures.hpp"
39 #include "MachOFileAbstraction.hpp"
40
41
42 namespace ld {
43 namespace passes {
44 namespace compact_unwind {
45
46
47 struct UnwindEntry {
48 UnwindEntry(const ld::Atom* f, uint64_t a, uint32_t o, const ld::Atom* d,
49 const ld::Atom* l, const ld::Atom* p, uint32_t en)
50 : func(f), fde(d), lsda(l), personalityPointer(p), funcTentAddress(a),
51 functionOffset(o), encoding(en) { }
52 const ld::Atom* func;
53 const ld::Atom* fde;
54 const ld::Atom* lsda;
55 const ld::Atom* personalityPointer;
56 uint64_t funcTentAddress;
57 uint32_t functionOffset;
58 compact_unwind_encoding_t encoding;
59 };
60
61 struct LSDAEntry {
62 const ld::Atom* func;
63 const ld::Atom* lsda;
64 };
65
66
67 template <typename A>
68 class UnwindInfoAtom : public ld::Atom {
69 public:
70 UnwindInfoAtom(const std::vector<UnwindEntry>& entries,uint64_t ehFrameSize);
71 ~UnwindInfoAtom();
72
73 virtual const ld::File* file() const { return NULL; }
74 virtual const char* name() const { return "compact unwind info"; }
75 virtual uint64_t size() const { return _headerSize+_pagesSize; }
76 virtual uint64_t objectAddress() const { return 0; }
77 virtual void copyRawContent(uint8_t buffer[]) const;
78 virtual void setScope(Scope) { }
79 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixups[0]; }
80 virtual ld::Fixup::iterator fixupsEnd() const { return (ld::Fixup*)&_fixups[_fixups.size()]; }
81
82 private:
83 typedef typename A::P P;
84 typedef typename A::P::E E;
85 typedef typename A::P::uint_t pint_t;
86
87 typedef macho_unwind_info_compressed_second_level_page_header<P> CSLP;
88
89 bool encodingMeansUseDwarf(compact_unwind_encoding_t enc);
90 void compressDuplicates(const std::vector<UnwindEntry>& entries,
91 std::vector<UnwindEntry>& uniqueEntries);
92 void makePersonalityIndexes(std::vector<UnwindEntry>& entries,
93 std::map<const ld::Atom*, uint32_t>& personalityIndexMap);
94 void findCommonEncoding(const std::vector<UnwindEntry>& entries,
95 std::map<compact_unwind_encoding_t, unsigned int>& commonEncodings);
96 void makeLsdaIndex(const std::vector<UnwindEntry>& entries, std::vector<LSDAEntry>& lsdaIndex,
97 std::map<const ld::Atom*, uint32_t>& lsdaIndexOffsetMap);
98 unsigned int makeCompressedSecondLevelPage(const std::vector<UnwindEntry>& uniqueInfos,
99 const std::map<compact_unwind_encoding_t,unsigned int> commonEncodings,
100 uint32_t pageSize, unsigned int endIndex, uint8_t*& pageEnd);
101 unsigned int makeRegularSecondLevelPage(const std::vector<UnwindEntry>& uniqueInfos, uint32_t pageSize,
102 unsigned int endIndex, uint8_t*& pageEnd);
103 void addCompressedAddressOffsetFixup(uint32_t offset, const ld::Atom* func, const ld::Atom* fromFunc);
104 void addCompressedEncodingFixup(uint32_t offset, const ld::Atom* fde);
105 void addRegularAddressFixup(uint32_t offset, const ld::Atom* func);
106 void addRegularFDEOffsetFixup(uint32_t offset, const ld::Atom* fde);
107 void addImageOffsetFixup(uint32_t offset, const ld::Atom* targ);
108 void addImageOffsetFixupPlusAddend(uint32_t offset, const ld::Atom* targ, uint32_t addend);
109
110 uint8_t* _pagesForDelete;
111 uint8_t* _pageAlignedPages;
112 uint8_t* _pages;
113 uint64_t _pagesSize;
114 uint8_t* _header;
115 uint64_t _headerSize;
116 std::vector<ld::Fixup> _fixups;
117
118 static bool _s_log;
119 static ld::Section _s_section;
120 };
121
122 template <typename A>
123 bool UnwindInfoAtom<A>::_s_log = false;
124
125 template <typename A>
126 ld::Section UnwindInfoAtom<A>::_s_section("__TEXT", "__unwind_info", ld::Section::typeUnwindInfo);
127
128
129 template <typename A>
130 UnwindInfoAtom<A>::UnwindInfoAtom(const std::vector<UnwindEntry>& entries, uint64_t ehFrameSize)
131 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
132 ld::Atom::scopeLinkageUnit, ld::Atom::typeUnclassified,
133 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
134 _pagesForDelete(NULL), _pageAlignedPages(NULL), _pages(NULL), _pagesSize(0), _header(NULL), _headerSize(0)
135 {
136 // build new compressed list by removing entries where next function has same encoding
137 std::vector<UnwindEntry> uniqueEntries;
138 compressDuplicates(entries, uniqueEntries);
139
140 // reserve room so _fixups vector is not reallocated a bunch of times
141 _fixups.reserve(uniqueEntries.size()*3);
142
143 // build personality index, update encodings with personality index
144 std::map<const ld::Atom*, uint32_t> personalityIndexMap;
145 makePersonalityIndexes(uniqueEntries, personalityIndexMap);
146 if ( personalityIndexMap.size() > 3 ) {
147 throw "too many personality routines for compact unwind to encode";
148 }
149
150 // put the most common encodings into the common table, but at most 127 of them
151 std::map<compact_unwind_encoding_t, unsigned int> commonEncodings;
152 findCommonEncoding(uniqueEntries, commonEncodings);
153
154 // build lsda index
155 std::map<const ld::Atom*, uint32_t> lsdaIndexOffsetMap;
156 std::vector<LSDAEntry> lsdaIndex;
157 makeLsdaIndex(uniqueEntries, lsdaIndex, lsdaIndexOffsetMap);
158
159 // calculate worst case size for all unwind info pages when allocating buffer
160 const unsigned int entriesPerRegularPage = (4096-sizeof(unwind_info_regular_second_level_page_header))/sizeof(unwind_info_regular_second_level_entry);
161 assert(uniqueEntries.size() > 0);
162 const unsigned int pageCount = ((uniqueEntries.size() - 1)/entriesPerRegularPage) + 2;
163 _pagesForDelete = (uint8_t*)calloc(pageCount+1,4096);
164 if ( _pagesForDelete == NULL ) {
165 warning("could not allocate space for compact unwind info");
166 return;
167 }
168 _pageAlignedPages = (uint8_t*)((((uintptr_t)_pagesForDelete) + 4095) & -4096);
169
170 // make last second level page smaller so that all other second level pages can be page aligned
171 uint32_t maxLastPageSize = 4096 - (ehFrameSize % 4096);
172 uint32_t tailPad = 0;
173 if ( maxLastPageSize < 128 ) {
174 tailPad = maxLastPageSize;
175 maxLastPageSize = 4096;
176 }
177
178 // fill in pages in reverse order
179 const ld::Atom* secondLevelFirstFuncs[pageCount*3];
180 uint8_t* secondLevelPagesStarts[pageCount*3];
181 unsigned int endIndex = uniqueEntries.size();
182 unsigned int secondLevelPageCount = 0;
183 uint8_t* pageEnd = &_pageAlignedPages[pageCount*4096];
184 uint32_t pageSize = maxLastPageSize;
185 while ( endIndex > 0 ) {
186 endIndex = makeCompressedSecondLevelPage(uniqueEntries, commonEncodings, pageSize, endIndex, pageEnd);
187 secondLevelPagesStarts[secondLevelPageCount] = pageEnd;
188 secondLevelFirstFuncs[secondLevelPageCount] = uniqueEntries[endIndex].func;
189 ++secondLevelPageCount;
190 // if this requires more than one page, align so that next starts on page boundary
191 if ( (pageSize != 4096) && (endIndex > 0) ) {
192 pageEnd = (uint8_t*)((uintptr_t)(pageEnd) & -4096);
193 pageSize = 4096; // last page can be odd size, make rest up to 4096 bytes in size
194 }
195 }
196 _pages = pageEnd;
197 _pagesSize = &_pageAlignedPages[pageCount*4096] - pageEnd;
198
199 // calculate section layout
200 const uint32_t commonEncodingsArraySectionOffset = sizeof(macho_unwind_info_section_header<P>);
201 const uint32_t commonEncodingsArrayCount = commonEncodings.size();
202 const uint32_t commonEncodingsArraySize = commonEncodingsArrayCount * sizeof(compact_unwind_encoding_t);
203 const uint32_t personalityArraySectionOffset = commonEncodingsArraySectionOffset + commonEncodingsArraySize;
204 const uint32_t personalityArrayCount = personalityIndexMap.size();
205 const uint32_t personalityArraySize = personalityArrayCount * sizeof(uint32_t);
206 const uint32_t indexSectionOffset = personalityArraySectionOffset + personalityArraySize;
207 const uint32_t indexCount = secondLevelPageCount+1;
208 const uint32_t indexSize = indexCount * sizeof(macho_unwind_info_section_header_index_entry<P>);
209 const uint32_t lsdaIndexArraySectionOffset = indexSectionOffset + indexSize;
210 const uint32_t lsdaIndexArrayCount = lsdaIndex.size();
211 const uint32_t lsdaIndexArraySize = lsdaIndexArrayCount * sizeof(macho_unwind_info_section_header_lsda_index_entry<P>);
212 const uint32_t headerEndSectionOffset = lsdaIndexArraySectionOffset + lsdaIndexArraySize;
213
214 // now that we know the size of the header, slide all existing fixups on the pages
215 const int32_t fixupSlide = headerEndSectionOffset + (_pageAlignedPages - _pages);
216 for(std::vector<ld::Fixup>::iterator it = _fixups.begin(); it != _fixups.end(); ++it) {
217 it->offsetInAtom += fixupSlide;
218 }
219
220 // allocate and fill in section header
221 _headerSize = headerEndSectionOffset;
222 _header = new uint8_t[_headerSize];
223 bzero(_header, _headerSize);
224 macho_unwind_info_section_header<P>* sectionHeader = (macho_unwind_info_section_header<P>*)_header;
225 sectionHeader->set_version(UNWIND_SECTION_VERSION);
226 sectionHeader->set_commonEncodingsArraySectionOffset(commonEncodingsArraySectionOffset);
227 sectionHeader->set_commonEncodingsArrayCount(commonEncodingsArrayCount);
228 sectionHeader->set_personalityArraySectionOffset(personalityArraySectionOffset);
229 sectionHeader->set_personalityArrayCount(personalityArrayCount);
230 sectionHeader->set_indexSectionOffset(indexSectionOffset);
231 sectionHeader->set_indexCount(indexCount);
232
233 // copy common encodings
234 uint32_t* commonEncodingsTable = (uint32_t*)&_header[commonEncodingsArraySectionOffset];
235 for (std::map<uint32_t, unsigned int>::iterator it=commonEncodings.begin(); it != commonEncodings.end(); ++it)
236 E::set32(commonEncodingsTable[it->second], it->first);
237
238 // make references for personality entries
239 uint32_t* personalityArray = (uint32_t*)&_header[sectionHeader->personalityArraySectionOffset()];
240 for (std::map<const ld::Atom*, unsigned int>::iterator it=personalityIndexMap.begin(); it != personalityIndexMap.end(); ++it) {
241 uint32_t offset = (uint8_t*)&personalityArray[it->second-1] - _header;
242 this->addImageOffsetFixup(offset, it->first);
243 }
244
245 // build first level index and references
246 macho_unwind_info_section_header_index_entry<P>* indexTable = (macho_unwind_info_section_header_index_entry<P>*)&_header[indexSectionOffset];
247 uint32_t refOffset;
248 for (unsigned int i=0; i < secondLevelPageCount; ++i) {
249 unsigned int reverseIndex = secondLevelPageCount - 1 - i;
250 indexTable[i].set_functionOffset(0);
251 indexTable[i].set_secondLevelPagesSectionOffset(secondLevelPagesStarts[reverseIndex]-_pages+headerEndSectionOffset);
252 indexTable[i].set_lsdaIndexArraySectionOffset(lsdaIndexOffsetMap[secondLevelFirstFuncs[reverseIndex]]+lsdaIndexArraySectionOffset);
253 refOffset = (uint8_t*)&indexTable[i] - _header;
254 this->addImageOffsetFixup(refOffset, secondLevelFirstFuncs[reverseIndex]);
255 }
256 indexTable[secondLevelPageCount].set_functionOffset(0);
257 indexTable[secondLevelPageCount].set_secondLevelPagesSectionOffset(0);
258 indexTable[secondLevelPageCount].set_lsdaIndexArraySectionOffset(lsdaIndexArraySectionOffset+lsdaIndexArraySize);
259 refOffset = (uint8_t*)&indexTable[secondLevelPageCount] - _header;
260 this->addImageOffsetFixupPlusAddend(refOffset, entries.back().func, entries.back().func->size()+1);
261
262 // build lsda references
263 uint32_t lsdaEntrySectionOffset = lsdaIndexArraySectionOffset;
264 for (std::vector<LSDAEntry>::iterator it = lsdaIndex.begin(); it != lsdaIndex.end(); ++it) {
265 this->addImageOffsetFixup(lsdaEntrySectionOffset, it->func);
266 this->addImageOffsetFixup(lsdaEntrySectionOffset+4, it->lsda);
267 lsdaEntrySectionOffset += sizeof(unwind_info_section_header_lsda_index_entry);
268 }
269
270 }
271
272 template <typename A>
273 UnwindInfoAtom<A>::~UnwindInfoAtom()
274 {
275 free(_pagesForDelete);
276 free(_header);
277 }
278
279 template <typename A>
280 void UnwindInfoAtom<A>::copyRawContent(uint8_t buffer[]) const
281 {
282 // content is in two parts
283 memcpy(buffer, _header, _headerSize);
284 memcpy(&buffer[_headerSize], _pages, _pagesSize);
285 }
286
287
288 template <>
289 bool UnwindInfoAtom<x86>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
290 {
291 return ((enc & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF);
292 }
293
294 template <>
295 bool UnwindInfoAtom<x86_64>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
296 {
297 return ((enc & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF);
298 }
299
300 template <>
301 bool UnwindInfoAtom<arm64>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
302 {
303 return ((enc & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF);
304 }
305
306 template <>
307 bool UnwindInfoAtom<arm>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
308 {
309 return ((enc & UNWIND_ARM_MODE_MASK) == UNWIND_ARM_MODE_DWARF);
310 }
311
312
313 template <typename A>
314 void UnwindInfoAtom<A>::compressDuplicates(const std::vector<UnwindEntry>& entries, std::vector<UnwindEntry>& uniqueEntries)
315 {
316 // build new list removing entries where next function has same encoding
317 uniqueEntries.reserve(entries.size());
318 UnwindEntry last(NULL, 0, 0, NULL, NULL, NULL, 0xFFFFFFFF);
319 for(std::vector<UnwindEntry>::const_iterator it=entries.begin(); it != entries.end(); ++it) {
320 const UnwindEntry& next = *it;
321 bool newNeedsDwarf = encodingMeansUseDwarf(next.encoding);
322 // remove entries which have same encoding and personalityPointer as last one
323 if ( newNeedsDwarf || (next.encoding != last.encoding) || (next.personalityPointer != last.personalityPointer)
324 || (next.lsda != NULL) || (last.lsda != NULL) ) {
325 uniqueEntries.push_back(next);
326 }
327 last = next;
328 }
329 if (_s_log) fprintf(stderr, "compressDuplicates() entries.size()=%lu, uniqueEntries.size()=%lu\n",
330 entries.size(), uniqueEntries.size());
331 }
332
333 template <typename A>
334 void UnwindInfoAtom<A>::makePersonalityIndexes(std::vector<UnwindEntry>& entries, std::map<const ld::Atom*, uint32_t>& personalityIndexMap)
335 {
336 for(std::vector<UnwindEntry>::iterator it=entries.begin(); it != entries.end(); ++it) {
337 if ( it->personalityPointer != NULL ) {
338 std::map<const ld::Atom*, uint32_t>::iterator pos = personalityIndexMap.find(it->personalityPointer);
339 if ( pos == personalityIndexMap.end() ) {
340 const uint32_t nextIndex = personalityIndexMap.size() + 1;
341 personalityIndexMap[it->personalityPointer] = nextIndex;
342 }
343 uint32_t personalityIndex = personalityIndexMap[it->personalityPointer];
344 it->encoding |= (personalityIndex << (__builtin_ctz(UNWIND_PERSONALITY_MASK)) );
345 }
346 }
347 if (_s_log) fprintf(stderr, "makePersonalityIndexes() %lu personality routines used\n", personalityIndexMap.size());
348 }
349
350
351 template <typename A>
352 void UnwindInfoAtom<A>::findCommonEncoding(const std::vector<UnwindEntry>& entries,
353 std::map<compact_unwind_encoding_t, unsigned int>& commonEncodings)
354 {
355 // scan infos to get frequency counts for each encoding
356 std::map<compact_unwind_encoding_t, unsigned int> encodingsUsed;
357 unsigned int mostCommonEncodingUsageCount = 0;
358 for(std::vector<UnwindEntry>::const_iterator it=entries.begin(); it != entries.end(); ++it) {
359 // never put dwarf into common table
360 if ( encodingMeansUseDwarf(it->encoding) )
361 continue;
362 std::map<compact_unwind_encoding_t, unsigned int>::iterator pos = encodingsUsed.find(it->encoding);
363 if ( pos == encodingsUsed.end() ) {
364 encodingsUsed[it->encoding] = 1;
365 }
366 else {
367 encodingsUsed[it->encoding] += 1;
368 if ( mostCommonEncodingUsageCount < encodingsUsed[it->encoding] )
369 mostCommonEncodingUsageCount = encodingsUsed[it->encoding];
370 }
371 }
372 // put the most common encodings into the common table, but at most 127 of them
373 for(unsigned int usages=mostCommonEncodingUsageCount; usages > 1; --usages) {
374 for (std::map<compact_unwind_encoding_t, unsigned int>::iterator euit=encodingsUsed.begin(); euit != encodingsUsed.end(); ++euit) {
375 if ( euit->second == usages ) {
376 unsigned int sz = commonEncodings.size();
377 if ( sz < 127 ) {
378 commonEncodings[euit->first] = sz;
379 }
380 }
381 }
382 }
383 if (_s_log) fprintf(stderr, "findCommonEncoding() %lu common encodings found\n", commonEncodings.size());
384 }
385
386
387 template <typename A>
388 void UnwindInfoAtom<A>::makeLsdaIndex(const std::vector<UnwindEntry>& entries, std::vector<LSDAEntry>& lsdaIndex, std::map<const ld::Atom*, uint32_t>& lsdaIndexOffsetMap)
389 {
390 for(std::vector<UnwindEntry>::const_iterator it=entries.begin(); it != entries.end(); ++it) {
391 lsdaIndexOffsetMap[it->func] = lsdaIndex.size() * sizeof(unwind_info_section_header_lsda_index_entry);
392 if ( it->lsda != NULL ) {
393 LSDAEntry entry;
394 entry.func = it->func;
395 entry.lsda = it->lsda;
396 lsdaIndex.push_back(entry);
397 }
398 }
399 if (_s_log) fprintf(stderr, "makeLsdaIndex() %lu LSDAs found\n", lsdaIndex.size());
400 }
401
402
403 template <>
404 void UnwindInfoAtom<x86>::addCompressedAddressOffsetFixup(uint32_t offset, const ld::Atom* func, const ld::Atom* fromFunc)
405 {
406 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of3, ld::Fixup::kindSetTargetAddress, func));
407 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of3, ld::Fixup::kindSubtractTargetAddress, fromFunc));
408 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k3of3, ld::Fixup::kindStoreLittleEndianLow24of32));
409 }
410
411 template <>
412 void UnwindInfoAtom<x86_64>::addCompressedAddressOffsetFixup(uint32_t offset, const ld::Atom* func, const ld::Atom* fromFunc)
413 {
414 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of3, ld::Fixup::kindSetTargetAddress, func));
415 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of3, ld::Fixup::kindSubtractTargetAddress, fromFunc));
416 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k3of3, ld::Fixup::kindStoreLittleEndianLow24of32));
417 }
418
419 template <>
420 void UnwindInfoAtom<arm64>::addCompressedAddressOffsetFixup(uint32_t offset, const ld::Atom* func, const ld::Atom* fromFunc)
421 {
422 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of3, ld::Fixup::kindSetTargetAddress, func));
423 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of3, ld::Fixup::kindSubtractTargetAddress, fromFunc));
424 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k3of3, ld::Fixup::kindStoreLittleEndianLow24of32));
425 }
426
427 template <>
428 void UnwindInfoAtom<arm>::addCompressedAddressOffsetFixup(uint32_t offset, const ld::Atom* func, const ld::Atom* fromFunc)
429 {
430 if ( fromFunc->isThumb() ) {
431 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, func));
432 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, fromFunc));
433 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, 1));
434 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndianLow24of32));
435 }
436 else {
437 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of3, ld::Fixup::kindSetTargetAddress, func));
438 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of3, ld::Fixup::kindSubtractTargetAddress, fromFunc));
439 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k3of3, ld::Fixup::kindStoreLittleEndianLow24of32));
440 }
441 }
442
443 template <>
444 void UnwindInfoAtom<x86>::addCompressedEncodingFixup(uint32_t offset, const ld::Atom* fde)
445 {
446 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of2, ld::Fixup::kindSetTargetSectionOffset, fde));
447 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndianLow24of32));
448 }
449
450 template <>
451 void UnwindInfoAtom<x86_64>::addCompressedEncodingFixup(uint32_t offset, const ld::Atom* fde)
452 {
453 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of2, ld::Fixup::kindSetTargetSectionOffset, fde));
454 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndianLow24of32));
455 }
456
457 template <>
458 void UnwindInfoAtom<arm64>::addCompressedEncodingFixup(uint32_t offset, const ld::Atom* fde)
459 {
460 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of2, ld::Fixup::kindSetTargetSectionOffset, fde));
461 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndianLow24of32));
462 }
463
464 template <>
465 void UnwindInfoAtom<arm>::addCompressedEncodingFixup(uint32_t offset, const ld::Atom* fde)
466 {
467 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of2, ld::Fixup::kindSetTargetSectionOffset, fde));
468 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndianLow24of32));
469 }
470
471 template <>
472 void UnwindInfoAtom<x86>::addRegularAddressFixup(uint32_t offset, const ld::Atom* func)
473 {
474 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of2, ld::Fixup::kindSetTargetImageOffset, func));
475 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndian32));
476 }
477
478 template <>
479 void UnwindInfoAtom<x86_64>::addRegularAddressFixup(uint32_t offset, const ld::Atom* func)
480 {
481 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of2, ld::Fixup::kindSetTargetImageOffset, func));
482 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndian32));
483 }
484
485 template <>
486 void UnwindInfoAtom<arm64>::addRegularAddressFixup(uint32_t offset, const ld::Atom* func)
487 {
488 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of2, ld::Fixup::kindSetTargetImageOffset, func));
489 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndian32));
490 }
491
492 template <>
493 void UnwindInfoAtom<arm>::addRegularAddressFixup(uint32_t offset, const ld::Atom* func)
494 {
495 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of2, ld::Fixup::kindSetTargetImageOffset, func));
496 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndian32));
497 }
498
499 template <>
500 void UnwindInfoAtom<x86>::addRegularFDEOffsetFixup(uint32_t offset, const ld::Atom* fde)
501 {
502 _fixups.push_back(ld::Fixup(offset+4, ld::Fixup::k1of2, ld::Fixup::kindSetTargetSectionOffset, fde));
503 _fixups.push_back(ld::Fixup(offset+4, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndianLow24of32));
504 }
505
506 template <>
507 void UnwindInfoAtom<x86_64>::addRegularFDEOffsetFixup(uint32_t offset, const ld::Atom* fde)
508 {
509 _fixups.push_back(ld::Fixup(offset+4, ld::Fixup::k1of2, ld::Fixup::kindSetTargetSectionOffset, fde));
510 _fixups.push_back(ld::Fixup(offset+4, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndianLow24of32));
511 }
512
513 template <>
514 void UnwindInfoAtom<arm64>::addRegularFDEOffsetFixup(uint32_t offset, const ld::Atom* fde)
515 {
516 _fixups.push_back(ld::Fixup(offset+4, ld::Fixup::k1of2, ld::Fixup::kindSetTargetSectionOffset, fde));
517 _fixups.push_back(ld::Fixup(offset+4, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndianLow24of32));
518 }
519
520 template <>
521 void UnwindInfoAtom<arm>::addRegularFDEOffsetFixup(uint32_t offset, const ld::Atom* fde)
522 {
523 _fixups.push_back(ld::Fixup(offset+4, ld::Fixup::k1of2, ld::Fixup::kindSetTargetSectionOffset, fde));
524 _fixups.push_back(ld::Fixup(offset+4, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndianLow24of32));
525 }
526
527 template <>
528 void UnwindInfoAtom<x86>::addImageOffsetFixup(uint32_t offset, const ld::Atom* targ)
529 {
530 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of2, ld::Fixup::kindSetTargetImageOffset, targ));
531 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndian32));
532 }
533
534 template <>
535 void UnwindInfoAtom<x86_64>::addImageOffsetFixup(uint32_t offset, const ld::Atom* targ)
536 {
537 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of2, ld::Fixup::kindSetTargetImageOffset, targ));
538 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndian32));
539 }
540
541 template <>
542 void UnwindInfoAtom<arm64>::addImageOffsetFixup(uint32_t offset, const ld::Atom* targ)
543 {
544 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of2, ld::Fixup::kindSetTargetImageOffset, targ));
545 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndian32));
546 }
547
548 template <>
549 void UnwindInfoAtom<arm>::addImageOffsetFixup(uint32_t offset, const ld::Atom* targ)
550 {
551 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of2, ld::Fixup::kindSetTargetImageOffset, targ));
552 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndian32));
553 }
554
555 template <>
556 void UnwindInfoAtom<x86>::addImageOffsetFixupPlusAddend(uint32_t offset, const ld::Atom* targ, uint32_t addend)
557 {
558 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of3, ld::Fixup::kindSetTargetImageOffset, targ));
559 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of3, ld::Fixup::kindAddAddend, addend));
560 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k3of3, ld::Fixup::kindStoreLittleEndian32));
561 }
562
563 template <>
564 void UnwindInfoAtom<x86_64>::addImageOffsetFixupPlusAddend(uint32_t offset, const ld::Atom* targ, uint32_t addend)
565 {
566 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of3, ld::Fixup::kindSetTargetImageOffset, targ));
567 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of3, ld::Fixup::kindAddAddend, addend));
568 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k3of3, ld::Fixup::kindStoreLittleEndian32));
569 }
570
571 template <>
572 void UnwindInfoAtom<arm64>::addImageOffsetFixupPlusAddend(uint32_t offset, const ld::Atom* targ, uint32_t addend)
573 {
574 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of3, ld::Fixup::kindSetTargetImageOffset, targ));
575 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of3, ld::Fixup::kindAddAddend, addend));
576 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k3of3, ld::Fixup::kindStoreLittleEndian32));
577 }
578
579 template <>
580 void UnwindInfoAtom<arm>::addImageOffsetFixupPlusAddend(uint32_t offset, const ld::Atom* targ, uint32_t addend)
581 {
582 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of3, ld::Fixup::kindSetTargetImageOffset, targ));
583 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k2of3, ld::Fixup::kindAddAddend, addend));
584 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k3of3, ld::Fixup::kindStoreLittleEndian32));
585 }
586
587
588
589
590 template <typename A>
591 unsigned int UnwindInfoAtom<A>::makeRegularSecondLevelPage(const std::vector<UnwindEntry>& uniqueInfos, uint32_t pageSize,
592 unsigned int endIndex, uint8_t*& pageEnd)
593 {
594 const unsigned int maxEntriesPerPage = (pageSize - sizeof(unwind_info_regular_second_level_page_header))/sizeof(unwind_info_regular_second_level_entry);
595 const unsigned int entriesToAdd = ((endIndex > maxEntriesPerPage) ? maxEntriesPerPage : endIndex);
596 uint8_t* pageStart = pageEnd
597 - entriesToAdd*sizeof(unwind_info_regular_second_level_entry)
598 - sizeof(unwind_info_regular_second_level_page_header);
599 macho_unwind_info_regular_second_level_page_header<P>* page = (macho_unwind_info_regular_second_level_page_header<P>*)pageStart;
600 page->set_kind(UNWIND_SECOND_LEVEL_REGULAR);
601 page->set_entryPageOffset(sizeof(macho_unwind_info_regular_second_level_page_header<P>));
602 page->set_entryCount(entriesToAdd);
603 macho_unwind_info_regular_second_level_entry<P>* entryTable = (macho_unwind_info_regular_second_level_entry<P>*)(pageStart + page->entryPageOffset());
604 for (unsigned int i=0; i < entriesToAdd; ++i) {
605 const UnwindEntry& info = uniqueInfos[endIndex-entriesToAdd+i];
606 entryTable[i].set_functionOffset(0);
607 entryTable[i].set_encoding(info.encoding);
608 // add fixup for address part of entry
609 uint32_t offset = (uint8_t*)(&entryTable[i]) - _pageAlignedPages;
610 this->addRegularAddressFixup(offset, info.func);
611 if ( encodingMeansUseDwarf(info.encoding) ) {
612 // add fixup for dwarf offset part of page specific encoding
613 uint32_t encOffset = (uint8_t*)(&entryTable[i]) - _pageAlignedPages;
614 this->addRegularFDEOffsetFixup(encOffset, info.fde);
615 }
616 }
617 if (_s_log) fprintf(stderr, "regular page with %u entries\n", entriesToAdd);
618 pageEnd = pageStart;
619 return endIndex - entriesToAdd;
620 }
621
622
623 template <typename A>
624 unsigned int UnwindInfoAtom<A>::makeCompressedSecondLevelPage(const std::vector<UnwindEntry>& uniqueInfos,
625 const std::map<compact_unwind_encoding_t,unsigned int> commonEncodings,
626 uint32_t pageSize, unsigned int endIndex, uint8_t*& pageEnd)
627 {
628 if (_s_log) fprintf(stderr, "makeCompressedSecondLevelPage(pageSize=%u, endIndex=%u)\n", pageSize, endIndex);
629 // first pass calculates how many compressed entries we could fit in this sized page
630 // keep adding entries to page until:
631 // 1) encoding table plus entry table plus header exceed page size
632 // 2) the file offset delta from the first to last function > 24 bits
633 // 3) custom encoding index reaches 255
634 // 4) run out of uniqueInfos to encode
635 std::map<compact_unwind_encoding_t, unsigned int> pageSpecificEncodings;
636 uint32_t space4 = (pageSize - sizeof(unwind_info_compressed_second_level_page_header))/sizeof(uint32_t);
637 int index = endIndex-1;
638 int entryCount = 0;
639 uint64_t lastEntryAddress = uniqueInfos[index].funcTentAddress;
640 bool canDo = true;
641 while ( canDo && (index >= 0) ) {
642 const UnwindEntry& info = uniqueInfos[index--];
643 // compute encoding index
644 unsigned int encodingIndex;
645 std::map<compact_unwind_encoding_t, unsigned int>::const_iterator pos = commonEncodings.find(info.encoding);
646 if ( pos != commonEncodings.end() ) {
647 encodingIndex = pos->second;
648 if (_s_log) fprintf(stderr, "makeCompressedSecondLevelPage(): funcIndex=%d, re-use commonEncodings[%d]=0x%08X\n", index, encodingIndex, info.encoding);
649 }
650 else {
651 // no commmon entry, so add one on this page
652 uint32_t encoding = info.encoding;
653 if ( encodingMeansUseDwarf(encoding) ) {
654 // make unique pseudo encoding so this dwarf will gets is own encoding entry slot
655 encoding += (index+1);
656 }
657 std::map<compact_unwind_encoding_t, unsigned int>::iterator ppos = pageSpecificEncodings.find(encoding);
658 if ( ppos != pageSpecificEncodings.end() ) {
659 encodingIndex = pos->second;
660 if (_s_log) fprintf(stderr, "makeCompressedSecondLevelPage(): funcIndex=%d, re-use pageSpecificEncodings[%d]=0x%08X\n", index, encodingIndex, encoding);
661 }
662 else {
663 encodingIndex = commonEncodings.size() + pageSpecificEncodings.size();
664 if ( encodingIndex <= 255 ) {
665 pageSpecificEncodings[encoding] = encodingIndex;
666 if (_s_log) fprintf(stderr, "makeCompressedSecondLevelPage(): funcIndex=%d, pageSpecificEncodings[%d]=0x%08X\n", index, encodingIndex, encoding);
667 }
668 else {
669 canDo = false; // case 3)
670 if (_s_log) fprintf(stderr, "end of compressed page with %u entries, %lu custom encodings because too many custom encodings\n",
671 entryCount, pageSpecificEncodings.size());
672 }
673 }
674 }
675 // compute function offset
676 uint32_t funcOffsetWithInPage = lastEntryAddress - info.funcTentAddress;
677 if ( funcOffsetWithInPage > 0x00FFFF00 ) {
678 // don't use 0x00FFFFFF because addresses may vary after atoms are laid out again
679 canDo = false; // case 2)
680 if (_s_log) fprintf(stderr, "can't use compressed page with %u entries because function offset too big\n", entryCount);
681 }
682 // check room for entry
683 if ( (pageSpecificEncodings.size()+entryCount) > space4 ) {
684 canDo = false; // case 1)
685 --entryCount;
686 if (_s_log) fprintf(stderr, "end of compressed page with %u entries because full\n", entryCount);
687 }
688 //if (_s_log) fprintf(stderr, "space4=%d, pageSpecificEncodings.size()=%ld, entryCount=%d\n", space4, pageSpecificEncodings.size(), entryCount);
689 if ( canDo ) {
690 ++entryCount;
691 }
692 }
693
694 // check for cases where it would be better to use a regular (non-compressed) page
695 const unsigned int compressPageUsed = sizeof(unwind_info_compressed_second_level_page_header)
696 + pageSpecificEncodings.size()*sizeof(uint32_t)
697 + entryCount*sizeof(uint32_t);
698 if ( (compressPageUsed < (pageSize-4) && (index >= 0) ) ) {
699 const int regularEntriesPerPage = (pageSize - sizeof(unwind_info_regular_second_level_page_header))/sizeof(unwind_info_regular_second_level_entry);
700 if ( entryCount < regularEntriesPerPage ) {
701 return makeRegularSecondLevelPage(uniqueInfos, pageSize, endIndex, pageEnd);
702 }
703 }
704
705 // check if we need any padding because adding another entry would take 8 bytes but only have room for 4
706 uint32_t pad = 0;
707 if ( compressPageUsed == (pageSize-4) )
708 pad = 4;
709
710 // second pass fills in page
711 uint8_t* pageStart = pageEnd - compressPageUsed - pad;
712 CSLP* page = (CSLP*)pageStart;
713 page->set_kind(UNWIND_SECOND_LEVEL_COMPRESSED);
714 page->set_entryPageOffset(sizeof(CSLP));
715 page->set_entryCount(entryCount);
716 page->set_encodingsPageOffset(page->entryPageOffset()+entryCount*sizeof(uint32_t));
717 page->set_encodingsCount(pageSpecificEncodings.size());
718 uint32_t* const encodingsArray = (uint32_t*)&pageStart[page->encodingsPageOffset()];
719 // fill in entry table
720 uint32_t* const entiresArray = (uint32_t*)&pageStart[page->entryPageOffset()];
721 const ld::Atom* firstFunc = uniqueInfos[endIndex-entryCount].func;
722 for(unsigned int i=endIndex-entryCount; i < endIndex; ++i) {
723 const UnwindEntry& info = uniqueInfos[i];
724 uint8_t encodingIndex;
725 if ( encodingMeansUseDwarf(info.encoding) ) {
726 // dwarf entries are always in page specific encodings
727 assert(pageSpecificEncodings.find(info.encoding+i) != pageSpecificEncodings.end());
728 encodingIndex = pageSpecificEncodings[info.encoding+i];
729 }
730 else {
731 std::map<uint32_t, unsigned int>::const_iterator pos = commonEncodings.find(info.encoding);
732 if ( pos != commonEncodings.end() )
733 encodingIndex = pos->second;
734 else
735 encodingIndex = pageSpecificEncodings[info.encoding];
736 }
737 uint32_t entryIndex = i - endIndex + entryCount;
738 E::set32(entiresArray[entryIndex], encodingIndex << 24);
739 // add fixup for address part of entry
740 uint32_t offset = (uint8_t*)(&entiresArray[entryIndex]) - _pageAlignedPages;
741 this->addCompressedAddressOffsetFixup(offset, info.func, firstFunc);
742 if ( encodingMeansUseDwarf(info.encoding) ) {
743 // add fixup for dwarf offset part of page specific encoding
744 uint32_t encOffset = (uint8_t*)(&encodingsArray[encodingIndex-commonEncodings.size()]) - _pageAlignedPages;
745 this->addCompressedEncodingFixup(encOffset, info.fde);
746 }
747 }
748 // fill in encodings table
749 for(std::map<uint32_t, unsigned int>::const_iterator it = pageSpecificEncodings.begin(); it != pageSpecificEncodings.end(); ++it) {
750 E::set32(encodingsArray[it->second-commonEncodings.size()], it->first);
751 }
752
753 if (_s_log) fprintf(stderr, "compressed page with %u entries, %lu custom encodings\n", entryCount, pageSpecificEncodings.size());
754
755 // update pageEnd;
756 pageEnd = pageStart;
757 return endIndex-entryCount; // endIndex for next page
758 }
759
760
761
762
763 static uint64_t calculateEHFrameSize(ld::Internal& state)
764 {
765 bool allCIEs = true;
766 uint64_t size = 0;
767 for (ld::Internal::FinalSection* sect : state.sections) {
768 if ( sect->type() == ld::Section::typeCFI ) {
769 for (const ld::Atom* atom : sect->atoms) {
770 size += atom->size();
771 if ( strcmp(atom->name(), "CIE") != 0 )
772 allCIEs = false;
773 }
774 if ( allCIEs ) {
775 // <rdar://problem/21427393> Linker generates eh_frame data even when there's only an unused CIEs in it
776 sect->atoms.clear();
777 state.sections.erase(std::remove(state.sections.begin(), state.sections.end(), sect), state.sections.end());
778 return 0;
779 }
780 }
781 }
782 return size;
783 }
784
785 static void getAllUnwindInfos(const ld::Internal& state, std::vector<UnwindEntry>& entries)
786 {
787 uint64_t address = 0;
788 for (std::vector<ld::Internal::FinalSection*>::const_iterator sit=state.sections.begin(); sit != state.sections.end(); ++sit) {
789 ld::Internal::FinalSection* sect = *sit;
790 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
791 const ld::Atom* atom = *ait;
792 // adjust address for atom alignment
793 uint64_t alignment = 1 << atom->alignment().powerOf2;
794 uint64_t currentModulus = (address % alignment);
795 uint64_t requiredModulus = atom->alignment().modulus;
796 if ( currentModulus != requiredModulus ) {
797 if ( requiredModulus > currentModulus )
798 address += requiredModulus-currentModulus;
799 else
800 address += requiredModulus+alignment-currentModulus;
801 }
802
803 if ( atom->beginUnwind() == atom->endUnwind() ) {
804 // be sure to mark that we have no unwind info for stuff in the TEXT segment without unwind info
805 if ( (atom->section().type() == ld::Section::typeCode) && (atom->size() !=0) ) {
806 entries.push_back(UnwindEntry(atom, address, 0, NULL, NULL, NULL, 0));
807 }
808 }
809 else {
810 // atom has unwind info(s), add entry for each
811 const ld::Atom* fde = NULL;
812 const ld::Atom* lsda = NULL;
813 const ld::Atom* personalityPointer = NULL;
814 for (ld::Fixup::iterator fit = atom->fixupsBegin(), end=atom->fixupsEnd(); fit != end; ++fit) {
815 switch ( fit->kind ) {
816 case ld::Fixup::kindNoneGroupSubordinateFDE:
817 assert(fit->binding == ld::Fixup::bindingDirectlyBound);
818 fde = fit->u.target;
819 break;
820 case ld::Fixup::kindNoneGroupSubordinateLSDA:
821 assert(fit->binding == ld::Fixup::bindingDirectlyBound);
822 lsda = fit->u.target;
823 break;
824 case ld::Fixup::kindNoneGroupSubordinatePersonality:
825 assert(fit->binding == ld::Fixup::bindingDirectlyBound);
826 personalityPointer = fit->u.target;
827 assert(personalityPointer->section().type() == ld::Section::typeNonLazyPointer);
828 break;
829 default:
830 break;
831 }
832 }
833 if ( fde != NULL ) {
834 // find CIE for this FDE
835 const ld::Atom* cie = NULL;
836 for (ld::Fixup::iterator fit = fde->fixupsBegin(), end=fde->fixupsEnd(); fit != end; ++fit) {
837 if ( fit->kind != ld::Fixup::kindSubtractTargetAddress )
838 continue;
839 if ( fit->binding != ld::Fixup::bindingDirectlyBound )
840 continue;
841 cie = fit->u.target;
842 // CIE is only direct subtracted target in FDE
843 assert(cie->section().type() == ld::Section::typeCFI);
844 break;
845 }
846 if ( cie != NULL ) {
847 // if CIE can have just one fixup - to the personality pointer
848 for (ld::Fixup::iterator fit = cie->fixupsBegin(), end=cie->fixupsEnd(); fit != end; ++fit) {
849 if ( fit->kind == ld::Fixup::kindSetTargetAddress ) {
850 switch ( fit->binding ) {
851 case ld::Fixup::bindingsIndirectlyBound:
852 personalityPointer = state.indirectBindingTable[fit->u.bindingIndex];
853 assert(personalityPointer->section().type() == ld::Section::typeNonLazyPointer);
854 break;
855 case ld::Fixup::bindingDirectlyBound:
856 personalityPointer = fit->u.target;
857 assert(personalityPointer->section().type() == ld::Section::typeNonLazyPointer);
858 break;
859 default:
860 break;
861 }
862 }
863 }
864 }
865 }
866 for ( ld::Atom::UnwindInfo::iterator uit = atom->beginUnwind(); uit != atom->endUnwind(); ++uit ) {
867 entries.push_back(UnwindEntry(atom, address, uit->startOffset, fde, lsda, personalityPointer, uit->unwindInfo));
868 }
869 }
870 address += atom->size();
871 }
872 }
873 }
874
875
876 static void makeFinalLinkedImageCompactUnwindSection(const Options& opts, ld::Internal& state)
877 {
878 // walk every atom and gets its unwind info
879 std::vector<UnwindEntry> entries;
880 entries.reserve(64);
881 getAllUnwindInfos(state, entries);
882
883 // don't generate an __unwind_info section if there is no code in this linkage unit
884 if ( entries.size() == 0 )
885 return;
886
887 // calculate size of __eh_frame section, so __unwind_info can go before it and page align
888 uint64_t ehFrameSize = calculateEHFrameSize(state);
889
890 // create atom that contains the whole compact unwind table
891 switch ( opts.architecture() ) {
892 #if SUPPORT_ARCH_x86_64
893 case CPU_TYPE_X86_64:
894 state.addAtom(*new UnwindInfoAtom<x86_64>(entries, ehFrameSize));
895 break;
896 #endif
897 #if SUPPORT_ARCH_i386
898 case CPU_TYPE_I386:
899 state.addAtom(*new UnwindInfoAtom<x86>(entries, ehFrameSize));
900 break;
901 #endif
902 #if SUPPORT_ARCH_arm64
903 case CPU_TYPE_ARM64:
904 state.addAtom(*new UnwindInfoAtom<arm64>(entries, ehFrameSize));
905 break;
906 #endif
907 #if SUPPORT_ARCH_arm_any
908 case CPU_TYPE_ARM:
909 if ( opts.armUsesZeroCostExceptions() )
910 state.addAtom(*new UnwindInfoAtom<arm>(entries, ehFrameSize));
911 break;
912 #endif
913 default:
914 assert(0 && "no compact unwind for arch");
915 }
916 }
917
918
919
920 template <typename A>
921 class CompactUnwindAtom : public ld::Atom {
922 public:
923 CompactUnwindAtom(ld::Internal& state,const ld::Atom* funcAtom,
924 uint32_t startOffset, uint32_t len, uint32_t cui);
925 ~CompactUnwindAtom() {}
926
927 virtual const ld::File* file() const { return NULL; }
928 virtual const char* name() const { return "compact unwind info"; }
929 virtual uint64_t size() const { return sizeof(macho_compact_unwind_entry<P>); }
930 virtual uint64_t objectAddress() const { return 0; }
931 virtual void copyRawContent(uint8_t buffer[]) const;
932 virtual void setScope(Scope) { }
933 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixups[0]; }
934 virtual ld::Fixup::iterator fixupsEnd() const { return (ld::Fixup*)&_fixups[_fixups.size()]; }
935
936 private:
937 typedef typename A::P P;
938 typedef typename A::P::E E;
939 typedef typename A::P::uint_t pint_t;
940
941
942 const ld::Atom* _atom;
943 const uint32_t _startOffset;
944 const uint32_t _len;
945 const uint32_t _compactUnwindInfo;
946 std::vector<ld::Fixup> _fixups;
947
948 static ld::Fixup::Kind _s_pointerKind;
949 static ld::Fixup::Kind _s_pointerStoreKind;
950 static ld::Section _s_section;
951 };
952
953
954 template <typename A>
955 ld::Section CompactUnwindAtom<A>::_s_section("__LD", "__compact_unwind", ld::Section::typeDebug);
956
957 template <> ld::Fixup::Kind CompactUnwindAtom<x86>::_s_pointerKind = ld::Fixup::kindStoreLittleEndian32;
958 template <> ld::Fixup::Kind CompactUnwindAtom<x86>::_s_pointerStoreKind = ld::Fixup::kindStoreTargetAddressLittleEndian32;
959 template <> ld::Fixup::Kind CompactUnwindAtom<x86_64>::_s_pointerKind = ld::Fixup::kindStoreLittleEndian64;
960 template <> ld::Fixup::Kind CompactUnwindAtom<x86_64>::_s_pointerStoreKind = ld::Fixup::kindStoreTargetAddressLittleEndian64;
961 #if SUPPORT_ARCH_arm64
962 template <> ld::Fixup::Kind CompactUnwindAtom<arm64>::_s_pointerKind = ld::Fixup::kindStoreLittleEndian64;
963 template <> ld::Fixup::Kind CompactUnwindAtom<arm64>::_s_pointerStoreKind = ld::Fixup::kindStoreTargetAddressLittleEndian64;
964 #endif
965 template <> ld::Fixup::Kind CompactUnwindAtom<arm>::_s_pointerKind = ld::Fixup::kindStoreLittleEndian32;
966 template <> ld::Fixup::Kind CompactUnwindAtom<arm>::_s_pointerStoreKind = ld::Fixup::kindStoreTargetAddressLittleEndian32;
967
968 template <typename A>
969 CompactUnwindAtom<A>::CompactUnwindAtom(ld::Internal& state,const ld::Atom* funcAtom, uint32_t startOffset,
970 uint32_t len, uint32_t cui)
971 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
972 ld::Atom::scopeTranslationUnit, ld::Atom::typeUnclassified,
973 symbolTableNotIn, false, false, false, ld::Atom::Alignment(log2(sizeof(pint_t)))),
974 _atom(funcAtom), _startOffset(startOffset), _len(len), _compactUnwindInfo(cui)
975 {
976 _fixups.push_back(ld::Fixup(macho_compact_unwind_entry<P>::codeStartFieldOffset(), ld::Fixup::k1of3, ld::Fixup::kindSetTargetAddress, funcAtom));
977 _fixups.push_back(ld::Fixup(macho_compact_unwind_entry<P>::codeStartFieldOffset(), ld::Fixup::k2of3, ld::Fixup::kindAddAddend, _startOffset));
978 _fixups.push_back(ld::Fixup(macho_compact_unwind_entry<P>::codeStartFieldOffset(), ld::Fixup::k3of3, _s_pointerKind));
979 // see if atom has subordinate personality function or lsda
980 for (ld::Fixup::iterator fit = funcAtom->fixupsBegin(), end=funcAtom->fixupsEnd(); fit != end; ++fit) {
981 switch ( fit->kind ) {
982 case ld::Fixup::kindNoneGroupSubordinatePersonality:
983 assert(fit->binding == ld::Fixup::bindingsIndirectlyBound);
984 _fixups.push_back(ld::Fixup(macho_compact_unwind_entry<P>::personalityFieldOffset(), ld::Fixup::k1of1, _s_pointerStoreKind, state.indirectBindingTable[fit->u.bindingIndex]));
985 break;
986 case ld::Fixup::kindNoneGroupSubordinateLSDA:
987 assert(fit->binding == ld::Fixup::bindingDirectlyBound);
988 _fixups.push_back(ld::Fixup(macho_compact_unwind_entry<P>::lsdaFieldOffset(), ld::Fixup::k1of1, _s_pointerStoreKind, fit->u.target));
989 break;
990 default:
991 break;
992 }
993 }
994
995 }
996
997 template <typename A>
998 void CompactUnwindAtom<A>::copyRawContent(uint8_t buffer[]) const
999 {
1000 macho_compact_unwind_entry<P>* buf = (macho_compact_unwind_entry<P>*)buffer;
1001 buf->set_codeStart(0);
1002 buf->set_codeLen(_len);
1003 buf->set_compactUnwindInfo(_compactUnwindInfo);
1004 buf->set_personality(0);
1005 buf->set_lsda(0);
1006 }
1007
1008
1009 static void makeCompactUnwindAtom(const Options& opts, ld::Internal& state, const ld::Atom* atom,
1010 uint32_t startOffset, uint32_t endOffset, uint32_t cui)
1011 {
1012 switch ( opts.architecture() ) {
1013 #if SUPPORT_ARCH_x86_64
1014 case CPU_TYPE_X86_64:
1015 state.addAtom(*new CompactUnwindAtom<x86_64>(state, atom, startOffset, endOffset-startOffset, cui));
1016 break;
1017 #endif
1018 #if SUPPORT_ARCH_i386
1019 case CPU_TYPE_I386:
1020 state.addAtom(*new CompactUnwindAtom<x86>(state, atom, startOffset, endOffset-startOffset, cui));
1021 break;
1022 #endif
1023 #if SUPPORT_ARCH_arm64
1024 case CPU_TYPE_ARM64:
1025 state.addAtom(*new CompactUnwindAtom<arm64>(state, atom, startOffset, endOffset-startOffset, cui));
1026 break;
1027 #endif
1028 case CPU_TYPE_ARM:
1029 state.addAtom(*new CompactUnwindAtom<arm>(state, atom, startOffset, endOffset-startOffset, cui));
1030 break;
1031 }
1032 }
1033
1034 static void makeRelocateableCompactUnwindSection(const Options& opts, ld::Internal& state)
1035 {
1036 // can't add CompactUnwindAtom atoms will iterating, so pre-scan
1037 std::vector<const ld::Atom*> atomsWithUnwind;
1038 for (std::vector<ld::Internal::FinalSection*>::const_iterator sit=state.sections.begin(); sit != state.sections.end(); ++sit) {
1039 ld::Internal::FinalSection* sect = *sit;
1040 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
1041 const ld::Atom* atom = *ait;
1042 if ( atom->beginUnwind() != atom->endUnwind() )
1043 atomsWithUnwind.push_back(atom);
1044 }
1045 }
1046 // make one CompactUnwindAtom for each compact unwind range in each atom
1047 for (std::vector<const ld::Atom*>::iterator it = atomsWithUnwind.begin(); it != atomsWithUnwind.end(); ++it) {
1048 const ld::Atom* atom = *it;
1049 uint32_t lastOffset = 0;
1050 uint32_t lastCUE = 0;
1051 bool first = true;
1052 for (ld::Atom::UnwindInfo::iterator uit=atom->beginUnwind(); uit != atom->endUnwind(); ++uit) {
1053 if ( !first ) {
1054 makeCompactUnwindAtom(opts, state, atom, lastOffset, uit->startOffset, lastCUE);
1055 }
1056 lastOffset = uit->startOffset;
1057 lastCUE = uit->unwindInfo;
1058 first = false;
1059 }
1060 makeCompactUnwindAtom(opts, state, atom, lastOffset, (uint32_t)atom->size(), lastCUE);
1061 }
1062 }
1063
1064
1065 void doPass(const Options& opts, ld::Internal& state)
1066 {
1067 if ( opts.outputKind() == Options::kObjectFile )
1068 makeRelocateableCompactUnwindSection(opts, state);
1069
1070 else if ( opts.needsUnwindInfoSection() )
1071 makeFinalLinkedImageCompactUnwindSection(opts, state);
1072 }
1073
1074
1075 } // namespace compact_unwind
1076 } // namespace passes
1077 } // namespace ld