]> git.saurik.com Git - apple/ld64.git/blame - src/ld/passes/order_file.cpp
ld64-127.2.tar.gz
[apple/ld64.git] / src / ld / passes / order_file.cpp
CommitLineData
a645023d
A
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
32#include <vector>
33#include <map>
34
35#include "ld.hpp"
36#include "order_file.h"
37
38namespace ld {
39namespace passes {
40namespace order_file {
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_file 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 layed 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
70class Layout
71{
72public:
73 Layout(const Options& opts, ld::Internal& state);
74 void doPass();
75private:
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 orderableSection(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
116bool Layout::_s_log = false;
117
118Layout::Layout(const Options& opts, ld::Internal& state)
119 : _options(opts), _state(state), _comparer(*this), _haveOrderFile(opts.orderedSymbolsCount() != 0)
120{
121}
122
123
124bool 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 a -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 uint32_t leftFileOrdinal = left->file()->ordinal();
195 uint32_t rightFileOrdinal = right->file()->ordinal();
196 if ( leftFileOrdinal != rightFileOrdinal )
197 return leftFileOrdinal< rightFileOrdinal;
198
199 // tentative defintions have no address in .o file, they are traditionally laid out by name
200 if ( leftIsTent && rightIsTent )
201 return (strcmp(left->name(), right->name()) < 0);
202
203 // lastly sort by atom address
204 int64_t addrDiff = left->objectAddress() - right->objectAddress();
205 if ( addrDiff == 0 ) {
206 // have same address so one might be an alias, and aliases need to sort before target
207 bool leftIsAlias = left->isAlias();
208 bool rightIsAlias = right->isAlias();
209 if ( leftIsAlias != rightIsAlias )
210 return leftIsAlias;
211
212 // both at same address, sort by name
213 return (strcmp(left->name(), right->name()) < 0);
214 }
215 return (addrDiff < 0);
216}
217
218bool Layout::matchesObjectFile(const ld::Atom* atom, const char* objectFileLeafName)
219{
220 if ( objectFileLeafName == NULL )
221 return true;
222 const char* atomFullPath = atom->file()->path();
223 const char* lastSlash = strrchr(atomFullPath, '/');
224 if ( lastSlash != NULL ) {
225 if ( strcmp(&lastSlash[1], objectFileLeafName) == 0 )
226 return true;
227 }
228 else {
229 if ( strcmp(atomFullPath, objectFileLeafName) == 0 )
230 return true;
231 }
232 return false;
233}
234
235
236bool Layout::orderableSection(const ld::Internal::FinalSection* sect)
237{
238 // atoms in only some sections are ordered
239 switch ( sect->type() ) {
240 case ld::Section::typeUnclassified:
241 case ld::Section::typeCode:
242 case ld::Section::typeZeroFill:
243 return true;
244 case ld::Section::typeImportProxies:
245 return false;
246 default:
247 // if section has command line aliases, then we must apply ordering so aliases layout before targets
248 if ( _options.haveCmdLineAliases() ) {
249 for (std::vector<const ld::Atom*>::const_iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
250 const ld::Atom* atom = *ait;
251 if ( atom->isAlias() )
252 return true;
253 }
254 }
255 break;
256 }
257 return false;
258}
259
260void Layout::buildNameTable()
261{
262 for (std::vector<ld::Internal::FinalSection*>::iterator sit=_state.sections.begin(); sit != _state.sections.end(); ++sit) {
263 ld::Internal::FinalSection* sect = *sit;
264 // atoms in some special sections are never ordered
265 if ( ! orderableSection(sect) )
266 continue;
267 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
268 const ld::Atom* atom = *ait;
269 if ( atom->symbolTableInclusion() == ld::Atom::symbolTableIn ) {
270 const char* name = atom->name();
271 if ( name != NULL) {
272 // static function or data
273 NameToAtom::iterator pos = _nameTable.find(name);
274 if ( pos == _nameTable.end() )
275 _nameTable[name] = atom;
276 else {
afe874b1
A
277 const ld::Atom* existing = _nameTable[name];
278 if ( existing != NULL ) {
279 _nameCollisionAtoms.push_back(existing);
280 _nameTable[name] = NULL; // collision, denote with NULL
281 }
a645023d
A
282 _nameCollisionAtoms.push_back(atom);
283 }
284 }
285 }
286 }
287 }
afe874b1
A
288 if ( _s_log ) {
289 fprintf(stderr, "buildNameTable() _nameTable:\n");
290 for(NameToAtom::iterator it=_nameTable.begin(); it != _nameTable.end(); ++it)
291 fprintf(stderr, " %p <- %s\n", it->second, it->first);
292 fprintf(stderr, "buildNameTable() _nameCollisionAtoms:\n");
293 for(std::vector<const ld::Atom*>::iterator it=_nameCollisionAtoms.begin(); it != _nameCollisionAtoms.end(); ++it)
294 fprintf(stderr, " %p, %s\n", *it, (*it)->name());
295 }
a645023d
A
296}
297
298
299const ld::Atom* Layout::findAtom(const Options::OrderedSymbol& orderedSymbol)
300{
301 // look for name in _nameTable
302 NameToAtom::iterator pos = _nameTable.find(orderedSymbol.symbolName);
303 if ( pos != _nameTable.end() ) {
304 if ( (pos->second != NULL) && matchesObjectFile(pos->second, orderedSymbol.objectFileName) ) {
305 //fprintf(stderr, "found %s in hash table\n", orderedSymbol.symbolName);
306 return pos->second;
307 }
308 if ( pos->second == NULL ) {
309 // name is in hash table, but atom is NULL, so that means there are duplicates, so we use super slow way
afe874b1
A
310 if ( ( orderedSymbol.objectFileName == NULL) && _options.printOrderFileStatistics() ) {
311 warning("%s specified in order_file but it exists in multiple .o files. "
312 "Prefix symbol with .o filename in order_file to disambiguate", orderedSymbol.symbolName);
313 }
a645023d
A
314 for (std::vector<const ld::Atom*>::iterator it=_nameCollisionAtoms.begin(); it != _nameCollisionAtoms.end(); it++) {
315 const ld::Atom* atom = *it;
316 if ( strcmp(atom->name(), orderedSymbol.symbolName) == 0 ) {
317 if ( matchesObjectFile(atom, orderedSymbol.objectFileName) ) {
a645023d
A
318 return atom;
319 }
320 }
321 }
322 }
323 }
324
325 return NULL;
326}
327
328const ld::Atom* Layout::follower(const ld::Atom* atom)
329{
330 for (const ld::Atom* a = _followOnStarts[atom]; a != NULL; a = _followOnNexts[a]) {
331 assert(a != NULL);
332 if ( _followOnNexts[a] == atom ) {
333 return a;
334 }
335 }
336 // no follower, first in chain
337 return NULL;
338}
339
340void Layout::buildFollowOnTables()
341{
342 // first make a pass to find all follow-on references and build start/next maps
343 // which are a way to represent clusters of atoms that must layout together
344 for (std::vector<ld::Internal::FinalSection*>::iterator sit=_state.sections.begin(); sit != _state.sections.end(); ++sit) {
345 ld::Internal::FinalSection* sect = *sit;
346 if ( !orderableSection(sect) )
347 continue;
348 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
349 const ld::Atom* atom = *ait;
350 for (ld::Fixup::iterator fit = atom->fixupsBegin(), end=atom->fixupsEnd(); fit != end; ++fit) {
351 if ( fit->kind == ld::Fixup::kindNoneFollowOn ) {
352 assert(fit->binding == ld::Fixup::bindingDirectlyBound);
353 const ld::Atom* followOnAtom = fit->u.target;
354 if ( _s_log ) fprintf(stderr, "ref %p %s -> %p %s\n", atom, atom->name(), followOnAtom, followOnAtom->name());
355 assert(_followOnNexts.count(atom) == 0);
356 _followOnNexts[atom] = followOnAtom;
357 if ( _followOnStarts.count(atom) == 0 ) {
358 // first time atom has been seen, make it start of chain
359 _followOnStarts[atom] = atom;
360 if ( _s_log ) fprintf(stderr, " start %s -> %s\n", atom->name(), atom->name());
361 }
362 if ( _followOnStarts.count(followOnAtom) == 0 ) {
363 // first time followOnAtom has been seen, make atom start of chain
364 _followOnStarts[followOnAtom] = _followOnStarts[atom];
365 if ( _s_log ) fprintf(stderr, " start %s -> %s\n", followOnAtom->name(), _followOnStarts[atom]->name());
366 }
367 else {
368 if ( _followOnStarts[followOnAtom] == followOnAtom ) {
369 // followOnAtom atom already start of another chain, hook together
370 // and change all to use atom as start
371 const ld::Atom* a = followOnAtom;
372 while ( true ) {
373 assert(_followOnStarts[a] == followOnAtom);
374 _followOnStarts[a] = _followOnStarts[atom];
375 if ( _s_log ) fprintf(stderr, " adjust start for %s -> %s\n", a->name(), _followOnStarts[atom]->name());
376 AtomToAtom::iterator pos = _followOnNexts.find(a);
377 if ( pos != _followOnNexts.end() )
378 a = pos->second;
379 else
380 break;
381 }
382 }
383 else {
384 // attempt to insert atom into existing followOn chain
385 const ld::Atom* curPrevToFollowOnAtom = this->follower(followOnAtom);
386 assert(curPrevToFollowOnAtom != NULL);
387 assert((atom->size() == 0) || (curPrevToFollowOnAtom->size() == 0));
388 if ( atom->size() == 0 ) {
389 // insert alias into existing chain right before followOnAtom
390 _followOnNexts[curPrevToFollowOnAtom] = atom;
391 _followOnNexts[atom] = followOnAtom;
392 _followOnStarts[atom] = _followOnStarts[followOnAtom];
393 }
394 else {
395 // insert real atom into existing chain right before alias of followOnAtom
396 const ld::Atom* curPrevPrevToFollowOn = this->follower(curPrevToFollowOnAtom);
397 if ( curPrevPrevToFollowOn == NULL ) {
398 // nothing previous, so make this a start of a new chain
399 _followOnNexts[atom] = curPrevToFollowOnAtom;
400 for (const ld::Atom* a = atom; a != NULL; a = _followOnNexts[a]) {
401 if ( _s_log ) fprintf(stderr, " adjust start for %s -> %s\n", a->name(), atom->name());
402 _followOnStarts[a] = atom;
403 }
404 }
405 else {
406 // is previous, insert into existing chain before previous
407 _followOnNexts[curPrevPrevToFollowOn] = atom;
408 _followOnNexts[atom] = curPrevToFollowOnAtom;
409 _followOnStarts[atom] = _followOnStarts[curPrevToFollowOnAtom];
410 }
411 }
412 }
413 }
414 }
415 }
416 }
417 }
418
419 if ( _s_log ) {
420 for(AtomToAtom::iterator it = _followOnStarts.begin(); it != _followOnStarts.end(); ++it)
421 fprintf(stderr, "start %s -> %s\n", it->first->name(), it->second->name());
422
423 for(AtomToAtom::iterator it = _followOnNexts.begin(); it != _followOnNexts.end(); ++it)
424 fprintf(stderr, "next %s -> %s\n", it->first->name(), (it->second != NULL) ? it->second->name() : "null");
425 }
426}
427
428void Layout::buildOrdinalOverrideMap()
429{
430 // if no -order_file, then skip building override map
431 if ( ! _haveOrderFile )
432 return;
433
434 // build fast name->atom table
435 this->buildNameTable();
436
437 // handle .o files that cannot have their atoms rearranged
438 // with the start/next maps of follow-on atoms we can process the order file and produce override ordinals
439 uint32_t index = 0;
440 uint32_t matchCount = 0;
441 for(Options::OrderedSymbolsIterator it = _options.orderedSymbolsBegin(); it != _options.orderedSymbolsEnd(); ++it) {
442 const ld::Atom* atom = this->findAtom(*it);
443 if ( atom != NULL ) {
444 AtomToAtom::iterator start = _followOnStarts.find(atom);
445 if ( start != _followOnStarts.end() ) {
446 // this symbol for the order file corresponds to an atom that is in a cluster that must lay out together
447 for(const ld::Atom* nextAtom = start->second; nextAtom != NULL; nextAtom = _followOnNexts[nextAtom]) {
448 AtomToOrdinal::iterator pos = _ordinalOverrideMap.find(nextAtom);
449 if ( pos == _ordinalOverrideMap.end() ) {
450 _ordinalOverrideMap[nextAtom] = index++;
451 if (_s_log ) fprintf(stderr, "override ordinal %u assigned to %s in cluster from %s\n", index, nextAtom->name(), nextAtom->file()->path());
452 }
453 else {
454 if (_s_log ) fprintf(stderr, "could not order %s as %u because it was already laid out earlier by %s as %u\n",
455 atom->name(), index, _followOnStarts[atom]->name(), _ordinalOverrideMap[atom] );
456 }
457 }
458 }
459 else {
460 _ordinalOverrideMap[atom] = index;
461 if (_s_log ) fprintf(stderr, "override ordinal %u assigned to %s from %s\n", index, atom->name(), atom->file()->path());
462 }
463 ++matchCount;
464 }
465 else {
466 if ( _options.printOrderFileStatistics() ) {
467 if ( it->objectFileName == NULL )
468 warning("can't find match for order_file entry: %s", it->symbolName);
469 else
470 warning("can't find match for order_file entry: %s/%s", it->objectFileName, it->symbolName);
471 }
472 }
473 ++index;
474 }
475 if ( _options.printOrderFileStatistics() && (_options.orderedSymbolsCount() != matchCount) ) {
476 warning("only %u out of %lu order_file symbols were applicable", matchCount, _options.orderedSymbolsCount() );
477 }
478
479
480}
481
482void Layout::doPass()
483{
484 // handle .o files that cannot have their atoms rearranged
485 this->buildFollowOnTables();
486
487 //
488 this->buildOrdinalOverrideMap();
489
490
491 // sort atoms in each section
492 for (std::vector<ld::Internal::FinalSection*>::iterator sit=_state.sections.begin(); sit != _state.sections.end(); ++sit) {
493 ld::Internal::FinalSection* sect = *sit;
494 if ( orderableSection(sect) ) {
495 std::sort(sect->atoms.begin(), sect->atoms.end(), _comparer);
496 }
497 else {
498 // bubble sort any typeSectionStart atom to the beginning
499 bool moving = false;
500 for (int i=sect->atoms.size()-1; i >= 0; --i) {
501 if ( moving ) {
502 const ld::Atom* temp = sect->atoms[i];
503 sect->atoms[i] = sect->atoms[i+1];
504 sect->atoms[i+1] = temp;
505 }
506 if ( sect->atoms[i]->contentType() == ld::Atom::typeSectionStart )
507 moving = true;
508 }
509 // bubble sort any typeSectionEnd atom to the end
510 moving = false;
511 for (unsigned int i=sect->atoms.size(); i < sect->atoms.size(); ++i) {
512 if ( moving ) {
513 const ld::Atom* temp = sect->atoms[i];
514 sect->atoms[i] = sect->atoms[i-1];
515 sect->atoms[i-1] = temp;
516 }
517 if ( sect->atoms[i]->contentType() == ld::Atom::typeSectionEnd )
518 moving = true;
519 }
520 }
521 }
522
523 //fprintf(stderr, "Sorted atoms:\n");
524 //for (std::vector<ld::Internal::FinalSection*>::iterator sit=_state.sections.begin(); sit != _state.sections.end(); ++sit) {
525 // ld::Internal::FinalSection* sect = *sit;
526 // for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
527 // const ld::Atom* atom = *ait;
528 // fprintf(stderr, "\t%s\t%s\n", sect->sectionName(), atom->name());
529 // }
530 //}
531
532}
533
534
535void doPass(const Options& opts, ld::Internal& state)
536{
537 Layout layout(opts, state);
538 layout.doPass();
539}
540
541
542} // namespace order_file
543} // namespace passes
544} // namespace ld