]> git.saurik.com Git - apple/ld64.git/blob - src/ld/passes/order.cpp
ld64-128.2.tar.gz
[apple/ld64.git] / src / ld / passes / order.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2009-2011 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
32 #include <vector>
33 #include <map>
34
35 #include "ld.hpp"
36 #include "order.h"
37
38 namespace ld {
39 namespace passes {
40 namespace order {
41
42 //
43 // The purpose of this pass is to take the graph of all Atoms and produce an ordered
44 // sequence of atoms. The constraints are that: 1) all Atoms of the same Segment must
45 // be contiguous, 2) all Atoms of the same Section must be contigous, 3) Atoms specified
46 // in an order are sequenced as in the order file and before Atoms not specified,
47 // 4) Atoms in the same section from the same .o file should be contiguous and sequenced
48 // in the same order they were in the .o file, 5) Atoms in the same Section but which came
49 // from different .o files should be sequenced in the same order that the .o files
50 // were passed to the linker (i.e. command line order).
51 //
52 // The way this is implemented is that the linker passes a "base ordinal" to each File
53 // as it is constructed. Add each atom has an objectAddress() method. Then
54 // sorting is just sorting by section, then by file ordinal, then by object address.
55 //
56 // If an -order_file is specified, it gets more complicated. First, an override-ordinal map
57 // is created. It causes the sort routine to ignore the value returned by ordinal() and objectAddress()
58 // and use the override value instead. Next some Atoms must be laid out consecutively
59 // (e.g. hand written assembly that does not end with return, but rather falls into
60 // the next label). This is modeled in via a kindNoneFollowOn fixup. The use of
61 // kindNoneFollowOn fixups produces "clusters" of atoms that must stay together.
62 // If an order_file tries to move one atom, it may need to move a whole cluster. The
63 // algorithm to do this models clusters using two maps. The "starts" maps maps any
64 // atom in a cluster to the first Atom in the cluster. The "nexts" maps an Atom in a
65 // cluster to the next Atom in the cluster. With this in place, while processing an
66 // order_file, if any entry is in a cluster (in "starts" map), then the entire cluster is
67 // given ordinal overrides.
68 //
69
70 class Layout
71 {
72 public:
73 Layout(const Options& opts, ld::Internal& state);
74 void doPass();
75 private:
76
77 class Comparer {
78 public:
79 Comparer(const Layout& l) : _layout(l) {}
80 bool operator()(const ld::Atom* left, const ld::Atom* right);
81 private:
82 const Layout& _layout;
83 };
84
85 class CStringEquals {
86 public:
87 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
88 };
89 typedef __gnu_cxx::hash_map<const char*, const ld::Atom*, __gnu_cxx::hash<const char*>, CStringEquals> NameToAtom;
90
91 typedef std::map<const ld::Atom*, const ld::Atom*> AtomToAtom;
92
93 typedef std::map<const ld::Atom*, uint32_t> AtomToOrdinal;
94
95 const ld::Atom* findAtom(const Options::OrderedSymbol& orderedSymbol);
96 void buildNameTable();
97 void buildFollowOnTables();
98 void buildOrdinalOverrideMap();
99 const ld::Atom* follower(const ld::Atom* atom);
100 static bool matchesObjectFile(const ld::Atom* atom, const char* objectFileLeafName);
101 bool possibleToOrder(const ld::Internal::FinalSection*);
102
103 const Options& _options;
104 ld::Internal& _state;
105 AtomToAtom _followOnStarts;
106 AtomToAtom _followOnNexts;
107 NameToAtom _nameTable;
108 std::vector<const ld::Atom*> _nameCollisionAtoms;
109 AtomToOrdinal _ordinalOverrideMap;
110 Comparer _comparer;
111 bool _haveOrderFile;
112
113 static bool _s_log;
114 };
115
116 bool Layout::_s_log = false;
117
118 Layout::Layout(const Options& opts, ld::Internal& state)
119 : _options(opts), _state(state), _comparer(*this), _haveOrderFile(opts.orderedSymbolsCount() != 0)
120 {
121 }
122
123
124 bool Layout::Comparer::operator()(const ld::Atom* left, const ld::Atom* right)
125 {
126 if ( left == right )
127 return false;
128
129 // magic section$start symbol always sorts to the start of its section
130 if ( left->contentType() == ld::Atom::typeSectionStart )
131 return true;
132 if ( right->contentType() == ld::Atom::typeSectionStart )
133 return false;
134
135 // if an -order_file is specified, then sorting is altered to sort those symbols first
136 if ( _layout._haveOrderFile ) {
137 AtomToOrdinal::const_iterator leftPos = _layout._ordinalOverrideMap.find(left);
138 AtomToOrdinal::const_iterator rightPos = _layout._ordinalOverrideMap.find(right);
139 AtomToOrdinal::const_iterator end = _layout._ordinalOverrideMap.end();
140 if ( leftPos != end ) {
141 if ( rightPos != end ) {
142 // both left and right are overridden, so compare overridden ordinals
143 return leftPos->second < rightPos->second;
144 }
145 else {
146 // left is overridden and right is not, so left < right
147 return true;
148 }
149 }
150 else {
151 if ( rightPos != end ) {
152 // right is overridden and left is not, so right < left
153 return false;
154 }
155 else {
156 // neither are overridden,
157 // fall into default sorting below
158 }
159 }
160 }
161
162 // magic section$end symbol always sorts to the end of its section
163 if ( left->contentType() == ld::Atom::typeSectionEnd )
164 return false;
165 if ( right->contentType() == ld::Atom::typeSectionEnd )
166 return true;
167
168 // the __common section can have real or tentative definitions
169 // we want the real ones to sort before tentative ones
170 bool leftIsTent = (left->definition() == ld::Atom::definitionTentative);
171 bool rightIsTent = (right->definition() == ld::Atom::definitionTentative);
172 if ( leftIsTent != rightIsTent )
173 return rightIsTent;
174
175 #if 0
176 // initializers are auto sorted to start of section
177 if ( !fInitializerSet.empty() ) {
178 bool leftFirst = (fInitializerSet.count(left) != 0);
179 bool rightFirst = (fInitializerSet.count(right) != 0);
180 if ( leftFirst != rightFirst )
181 return leftFirst;
182 }
183
184 // terminators are auto sorted to end of section
185 if ( !fTerminatorSet.empty() ) {
186 bool leftLast = (fTerminatorSet.count(left) != 0);
187 bool rightLast = (fTerminatorSet.count(right) != 0);
188 if ( leftLast != rightLast )
189 return rightLast;
190 }
191 #endif
192
193 // sort by .o order
194 const ld::File* leftFile = left->file();
195 const ld::File* rightFile = right->file();
196 uint32_t leftFileOrdinal = (leftFile != NULL) ? leftFile->ordinal() : 0;
197 uint32_t rightFileOrdinal = (rightFile != NULL) ? rightFile->ordinal() : 0;
198 if ( leftFileOrdinal != rightFileOrdinal )
199 return leftFileOrdinal< rightFileOrdinal;
200
201 // tentative defintions have no address in .o file, they are traditionally laid out by name
202 if ( leftIsTent && rightIsTent )
203 return (strcmp(left->name(), right->name()) < 0);
204
205 // lastly sort by atom address
206 int64_t addrDiff = left->objectAddress() - right->objectAddress();
207 if ( addrDiff == 0 ) {
208 // have same address so one might be an alias, and aliases need to sort before target
209 bool leftIsAlias = left->isAlias();
210 bool rightIsAlias = right->isAlias();
211 if ( leftIsAlias != rightIsAlias )
212 return leftIsAlias;
213
214 // both at same address, sort by name
215 return (strcmp(left->name(), right->name()) < 0);
216 }
217 return (addrDiff < 0);
218 }
219
220 bool Layout::matchesObjectFile(const ld::Atom* atom, const char* objectFileLeafName)
221 {
222 if ( objectFileLeafName == NULL )
223 return true;
224 const char* atomFullPath = atom->file()->path();
225 const char* lastSlash = strrchr(atomFullPath, '/');
226 if ( lastSlash != NULL ) {
227 if ( strcmp(&lastSlash[1], objectFileLeafName) == 0 )
228 return true;
229 }
230 else {
231 if ( strcmp(atomFullPath, objectFileLeafName) == 0 )
232 return true;
233 }
234 return false;
235 }
236
237
238 bool Layout::possibleToOrder(const ld::Internal::FinalSection* sect)
239 {
240 // atoms in only some sections can have order_file applied
241 switch ( sect->type() ) {
242 case ld::Section::typeUnclassified:
243 case ld::Section::typeCode:
244 case ld::Section::typeZeroFill:
245 return true;
246 case ld::Section::typeImportProxies:
247 return false;
248 default:
249 // if section has command line aliases, then we must apply ordering so aliases layout before targets
250 if ( _options.haveCmdLineAliases() ) {
251 for (std::vector<const ld::Atom*>::const_iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
252 const ld::Atom* atom = *ait;
253 if ( atom->isAlias() )
254 return true;
255 }
256 }
257 break;
258 }
259 return false;
260 }
261
262 void Layout::buildNameTable()
263 {
264 for (std::vector<ld::Internal::FinalSection*>::iterator sit=_state.sections.begin(); sit != _state.sections.end(); ++sit) {
265 ld::Internal::FinalSection* sect = *sit;
266 // some sections are not worth scanning for names
267 if ( ! possibleToOrder(sect) )
268 continue;
269 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
270 const ld::Atom* atom = *ait;
271 if ( atom->symbolTableInclusion() == ld::Atom::symbolTableIn ) {
272 const char* name = atom->name();
273 if ( name != NULL) {
274 // static function or data
275 NameToAtom::iterator pos = _nameTable.find(name);
276 if ( pos == _nameTable.end() )
277 _nameTable[name] = atom;
278 else {
279 const ld::Atom* existing = _nameTable[name];
280 if ( existing != NULL ) {
281 _nameCollisionAtoms.push_back(existing);
282 _nameTable[name] = NULL; // collision, denote with NULL
283 }
284 _nameCollisionAtoms.push_back(atom);
285 }
286 }
287 }
288 }
289 }
290 if ( _s_log ) {
291 fprintf(stderr, "buildNameTable() _nameTable:\n");
292 for(NameToAtom::iterator it=_nameTable.begin(); it != _nameTable.end(); ++it)
293 fprintf(stderr, " %p <- %s\n", it->second, it->first);
294 fprintf(stderr, "buildNameTable() _nameCollisionAtoms:\n");
295 for(std::vector<const ld::Atom*>::iterator it=_nameCollisionAtoms.begin(); it != _nameCollisionAtoms.end(); ++it)
296 fprintf(stderr, " %p, %s\n", *it, (*it)->name());
297 }
298 }
299
300
301 const ld::Atom* Layout::findAtom(const Options::OrderedSymbol& orderedSymbol)
302 {
303 // look for name in _nameTable
304 NameToAtom::iterator pos = _nameTable.find(orderedSymbol.symbolName);
305 if ( pos != _nameTable.end() ) {
306 if ( (pos->second != NULL) && matchesObjectFile(pos->second, orderedSymbol.objectFileName) ) {
307 //fprintf(stderr, "found %s in hash table\n", orderedSymbol.symbolName);
308 return pos->second;
309 }
310 if ( pos->second == NULL ) {
311 // name is in hash table, but atom is NULL, so that means there are duplicates, so we use super slow way
312 if ( ( orderedSymbol.objectFileName == NULL) && _options.printOrderFileStatistics() ) {
313 warning("%s specified in order_file but it exists in multiple .o files. "
314 "Prefix symbol with .o filename in order_file to disambiguate", orderedSymbol.symbolName);
315 }
316 for (std::vector<const ld::Atom*>::iterator it=_nameCollisionAtoms.begin(); it != _nameCollisionAtoms.end(); it++) {
317 const ld::Atom* atom = *it;
318 if ( strcmp(atom->name(), orderedSymbol.symbolName) == 0 ) {
319 if ( matchesObjectFile(atom, orderedSymbol.objectFileName) ) {
320 return atom;
321 }
322 }
323 }
324 }
325 }
326
327 return NULL;
328 }
329
330 const ld::Atom* Layout::follower(const ld::Atom* atom)
331 {
332 for (const ld::Atom* a = _followOnStarts[atom]; a != NULL; a = _followOnNexts[a]) {
333 assert(a != NULL);
334 if ( _followOnNexts[a] == atom ) {
335 return a;
336 }
337 }
338 // no follower, first in chain
339 return NULL;
340 }
341
342 void Layout::buildFollowOnTables()
343 {
344 // if no -order_file, then skip building follow on table
345 if ( ! _haveOrderFile )
346 return;
347
348 // first make a pass to find all follow-on references and build start/next maps
349 // which are a way to represent clusters of atoms that must layout together
350 for (std::vector<ld::Internal::FinalSection*>::iterator sit=_state.sections.begin(); sit != _state.sections.end(); ++sit) {
351 ld::Internal::FinalSection* sect = *sit;
352 if ( !possibleToOrder(sect) )
353 continue;
354 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
355 const ld::Atom* atom = *ait;
356 for (ld::Fixup::iterator fit = atom->fixupsBegin(), end=atom->fixupsEnd(); fit != end; ++fit) {
357 if ( fit->kind == ld::Fixup::kindNoneFollowOn ) {
358 assert(fit->binding == ld::Fixup::bindingDirectlyBound);
359 const ld::Atom* followOnAtom = fit->u.target;
360 if ( _s_log ) fprintf(stderr, "ref %p %s -> %p %s\n", atom, atom->name(), followOnAtom, followOnAtom->name());
361 assert(_followOnNexts.count(atom) == 0);
362 _followOnNexts[atom] = followOnAtom;
363 if ( _followOnStarts.count(atom) == 0 ) {
364 // first time atom has been seen, make it start of chain
365 _followOnStarts[atom] = atom;
366 if ( _s_log ) fprintf(stderr, " start %s -> %s\n", atom->name(), atom->name());
367 }
368 if ( _followOnStarts.count(followOnAtom) == 0 ) {
369 // first time followOnAtom has been seen, make atom start of chain
370 _followOnStarts[followOnAtom] = _followOnStarts[atom];
371 if ( _s_log ) fprintf(stderr, " start %s -> %s\n", followOnAtom->name(), _followOnStarts[atom]->name());
372 }
373 else {
374 if ( _followOnStarts[followOnAtom] == followOnAtom ) {
375 // followOnAtom atom already start of another chain, hook together
376 // and change all to use atom as start
377 const ld::Atom* a = followOnAtom;
378 while ( true ) {
379 assert(_followOnStarts[a] == followOnAtom);
380 _followOnStarts[a] = _followOnStarts[atom];
381 if ( _s_log ) fprintf(stderr, " adjust start for %s -> %s\n", a->name(), _followOnStarts[atom]->name());
382 AtomToAtom::iterator pos = _followOnNexts.find(a);
383 if ( pos != _followOnNexts.end() )
384 a = pos->second;
385 else
386 break;
387 }
388 }
389 else {
390 // attempt to insert atom into existing followOn chain
391 const ld::Atom* curPrevToFollowOnAtom = this->follower(followOnAtom);
392 assert(curPrevToFollowOnAtom != NULL);
393 assert((atom->size() == 0) || (curPrevToFollowOnAtom->size() == 0));
394 if ( atom->size() == 0 ) {
395 // insert alias into existing chain right before followOnAtom
396 _followOnNexts[curPrevToFollowOnAtom] = atom;
397 _followOnNexts[atom] = followOnAtom;
398 _followOnStarts[atom] = _followOnStarts[followOnAtom];
399 }
400 else {
401 // insert real atom into existing chain right before alias of followOnAtom
402 const ld::Atom* curPrevPrevToFollowOn = this->follower(curPrevToFollowOnAtom);
403 if ( curPrevPrevToFollowOn == NULL ) {
404 // nothing previous, so make this a start of a new chain
405 _followOnNexts[atom] = curPrevToFollowOnAtom;
406 for (const ld::Atom* a = atom; a != NULL; a = _followOnNexts[a]) {
407 if ( _s_log ) fprintf(stderr, " adjust start for %s -> %s\n", a->name(), atom->name());
408 _followOnStarts[a] = atom;
409 }
410 }
411 else {
412 // is previous, insert into existing chain before previous
413 _followOnNexts[curPrevPrevToFollowOn] = atom;
414 _followOnNexts[atom] = curPrevToFollowOnAtom;
415 _followOnStarts[atom] = _followOnStarts[curPrevToFollowOnAtom];
416 }
417 }
418 }
419 }
420 }
421 }
422 }
423 }
424
425 if ( _s_log ) {
426 for(AtomToAtom::iterator it = _followOnStarts.begin(); it != _followOnStarts.end(); ++it)
427 fprintf(stderr, "start %s -> %s\n", it->first->name(), it->second->name());
428
429 for(AtomToAtom::iterator it = _followOnNexts.begin(); it != _followOnNexts.end(); ++it)
430 fprintf(stderr, "next %s -> %s\n", it->first->name(), (it->second != NULL) ? it->second->name() : "null");
431 }
432 }
433
434
435 class InSet
436 {
437 public:
438 InSet(const std::set<const ld::Atom*>& theSet) : _set(theSet) {}
439
440 bool operator()(const ld::Atom* atom) const {
441 return ( _set.count(atom) != 0 );
442 }
443 private:
444 const std::set<const ld::Atom*>& _set;
445 };
446
447
448 void Layout::buildOrdinalOverrideMap()
449 {
450 // if no -order_file, then skip building override map
451 if ( ! _haveOrderFile )
452 return;
453
454 // build fast name->atom table
455 this->buildNameTable();
456
457 // handle .o files that cannot have their atoms rearranged
458 // with the start/next maps of follow-on atoms we can process the order file and produce override ordinals
459 uint32_t index = 0;
460 uint32_t matchCount = 0;
461 std::set<const ld::Atom*> moveToData;
462 for(Options::OrderedSymbolsIterator it = _options.orderedSymbolsBegin(); it != _options.orderedSymbolsEnd(); ++it) {
463 const ld::Atom* atom = this->findAtom(*it);
464 if ( atom != NULL ) {
465 // <rdar://problem/8612550> When order file used on data, turn ordered zero fill symbols into zero data
466 switch ( atom->section().type() ) {
467 case ld::Section::typeZeroFill:
468 case ld::Section::typeTentativeDefs:
469 if ( atom->size() <= 512 )
470 moveToData.insert(atom);
471 break;
472 default:
473 break;
474 }
475
476 AtomToAtom::iterator start = _followOnStarts.find(atom);
477 if ( start != _followOnStarts.end() ) {
478 // this symbol for the order file corresponds to an atom that is in a cluster that must lay out together
479 for(const ld::Atom* nextAtom = start->second; nextAtom != NULL; nextAtom = _followOnNexts[nextAtom]) {
480 AtomToOrdinal::iterator pos = _ordinalOverrideMap.find(nextAtom);
481 if ( pos == _ordinalOverrideMap.end() ) {
482 _ordinalOverrideMap[nextAtom] = index++;
483 if (_s_log ) fprintf(stderr, "override ordinal %u assigned to %s in cluster from %s\n", index, nextAtom->name(), nextAtom->file()->path());
484 }
485 else {
486 if (_s_log ) fprintf(stderr, "could not order %s as %u because it was already laid out earlier by %s as %u\n",
487 atom->name(), index, _followOnStarts[atom]->name(), _ordinalOverrideMap[atom] );
488 }
489 }
490 }
491 else {
492 _ordinalOverrideMap[atom] = index;
493 if (_s_log ) fprintf(stderr, "override ordinal %u assigned to %s from %s\n", index, atom->name(), atom->file()->path());
494 }
495 ++matchCount;
496 }
497 else {
498 if ( _options.printOrderFileStatistics() ) {
499 if ( it->objectFileName == NULL )
500 warning("can't find match for order_file entry: %s", it->symbolName);
501 else
502 warning("can't find match for order_file entry: %s/%s", it->objectFileName, it->symbolName);
503 }
504 }
505 ++index;
506 }
507 if ( _options.printOrderFileStatistics() && (_options.orderedSymbolsCount() != matchCount) ) {
508 warning("only %u out of %lu order_file symbols were applicable", matchCount, _options.orderedSymbolsCount() );
509 }
510
511
512 // <rdar://problem/8612550> When order file used on data, turn ordered zero fill symbols into zeroed data
513 if ( ! moveToData.empty() ) {
514 for (std::vector<ld::Internal::FinalSection*>::iterator sit=_state.sections.begin(); sit != _state.sections.end(); ++sit) {
515 ld::Internal::FinalSection* sect = *sit;
516 switch ( sect->type() ) {
517 case ld::Section::typeZeroFill:
518 case ld::Section::typeTentativeDefs:
519 sect->atoms.erase(std::remove_if(sect->atoms.begin(), sect->atoms.end(), InSet(moveToData)), sect->atoms.end());
520 break;
521 case ld::Section::typeUnclassified:
522 if ( (strcmp(sect->sectionName(), "__data") == 0) && (strcmp(sect->segmentName(), "__DATA") == 0) )
523 sect->atoms.insert(sect->atoms.end(), moveToData.begin(), moveToData.end());
524 break;
525 default:
526 break;
527 }
528 }
529 }
530
531 }
532
533 void Layout::doPass()
534 {
535 // handle .o files that cannot have their atoms rearranged
536 this->buildFollowOnTables();
537
538 // assign new ordinal value to all ordered atoms
539 this->buildOrdinalOverrideMap();
540
541 // sort atoms in each section
542 for (std::vector<ld::Internal::FinalSection*>::iterator sit=_state.sections.begin(); sit != _state.sections.end(); ++sit) {
543 ld::Internal::FinalSection* sect = *sit;
544 std::sort(sect->atoms.begin(), sect->atoms.end(), _comparer);
545 }
546
547 //fprintf(stderr, "Sorted atoms:\n");
548 //for (std::vector<ld::Internal::FinalSection*>::iterator sit=_state.sections.begin(); sit != _state.sections.end(); ++sit) {
549 // ld::Internal::FinalSection* sect = *sit;
550 // for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
551 // const ld::Atom* atom = *ait;
552 // fprintf(stderr, "\t%s\t%s\n", sect->sectionName(), atom->name());
553 // }
554 //}
555
556 }
557
558
559 void doPass(const Options& opts, ld::Internal& state)
560 {
561 Layout layout(opts, state);
562 layout.doPass();
563 }
564
565
566 } // namespace order_file
567 } // namespace passes
568 } // namespace ld