1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2009-2011 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@
30 #include <mach/machine.h>
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).
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.
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.
73 Layout(const Options
& opts
, ld::Internal
& state
);
79 Comparer(const Layout
& l
) : _layout(l
) {}
80 bool operator()(const ld::Atom
* left
, const ld::Atom
* right
);
82 const Layout
& _layout
;
87 bool operator()(const char* left
, const char* right
) const { return (strcmp(left
, right
) == 0); }
89 typedef __gnu_cxx::hash_map
<const char*, const ld::Atom
*, __gnu_cxx::hash
<const char*>, CStringEquals
> NameToAtom
;
91 typedef std::map
<const ld::Atom
*, const ld::Atom
*> AtomToAtom
;
93 typedef std::map
<const ld::Atom
*, uint32_t> AtomToOrdinal
;
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
*);
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
;
116 bool Layout::_s_log
= false;
118 Layout::Layout(const Options
& opts
, ld::Internal
& state
)
119 : _options(opts
), _state(state
), _comparer(*this), _haveOrderFile(opts
.orderedSymbolsCount() != 0)
124 bool Layout::Comparer::operator()(const ld::Atom
* left
, const ld::Atom
* right
)
129 // magic section$start symbol always sorts to the start of its section
130 if ( left
->contentType() == ld::Atom::typeSectionStart
)
132 if ( right
->contentType() == ld::Atom::typeSectionStart
)
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
;
146 // left is overridden and right is not, so left < right
151 if ( rightPos
!= end
) {
152 // right is overridden and left is not, so right < left
156 // neither are overridden,
157 // fall into default sorting below
162 // magic section$end symbol always sorts to the end of its section
163 if ( left
->contentType() == ld::Atom::typeSectionEnd
)
165 if ( right
->contentType() == ld::Atom::typeSectionEnd
)
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
)
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
)
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
)
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
;
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);
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
)
214 // both at same address, sort by name
215 return (strcmp(left
->name(), right
->name()) < 0);
217 return (addrDiff
< 0);
220 bool Layout::matchesObjectFile(const ld::Atom
* atom
, const char* objectFileLeafName
)
222 if ( objectFileLeafName
== NULL
)
224 const char* atomFullPath
= atom
->file()->path();
225 const char* lastSlash
= strrchr(atomFullPath
, '/');
226 if ( lastSlash
!= NULL
) {
227 if ( strcmp(&lastSlash
[1], objectFileLeafName
) == 0 )
231 if ( strcmp(atomFullPath
, objectFileLeafName
) == 0 )
238 bool Layout::possibleToOrder(const ld::Internal::FinalSection
* sect
)
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
:
246 case ld::Section::typeImportProxies
:
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() )
262 void Layout::buildNameTable()
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
) )
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();
274 // static function or data
275 NameToAtom::iterator pos
= _nameTable
.find(name
);
276 if ( pos
== _nameTable
.end() )
277 _nameTable
[name
] = atom
;
279 const ld::Atom
* existing
= _nameTable
[name
];
280 if ( existing
!= NULL
) {
281 _nameCollisionAtoms
.push_back(existing
);
282 _nameTable
[name
] = NULL
; // collision, denote with NULL
284 _nameCollisionAtoms
.push_back(atom
);
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());
301 const ld::Atom
* Layout::findAtom(const Options::OrderedSymbol
& orderedSymbol
)
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);
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
);
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
) ) {
330 const ld::Atom
* Layout::follower(const ld::Atom
* atom
)
332 for (const ld::Atom
* a
= _followOnStarts
[atom
]; a
!= NULL
; a
= _followOnNexts
[a
]) {
334 if ( _followOnNexts
[a
] == atom
) {
338 // no follower, first in chain
342 void Layout::buildFollowOnTables()
344 // if no -order_file, then skip building follow on table
345 if ( ! _haveOrderFile
)
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
) )
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());
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());
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
;
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() )
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
];
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
;
412 // is previous, insert into existing chain before previous
413 _followOnNexts
[curPrevPrevToFollowOn
] = atom
;
414 _followOnNexts
[atom
] = curPrevToFollowOnAtom
;
415 _followOnStarts
[atom
] = _followOnStarts
[curPrevToFollowOnAtom
];
426 for(AtomToAtom::iterator it
= _followOnStarts
.begin(); it
!= _followOnStarts
.end(); ++it
)
427 fprintf(stderr
, "start %s -> %s\n", it
->first
->name(), it
->second
->name());
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");
438 InSet(const std::set
<const ld::Atom
*>& theSet
) : _set(theSet
) {}
440 bool operator()(const ld::Atom
* atom
) const {
441 return ( _set
.count(atom
) != 0 );
444 const std::set
<const ld::Atom
*>& _set
;
448 void Layout::buildOrdinalOverrideMap()
450 // if no -order_file, then skip building override map
451 if ( ! _haveOrderFile
)
454 // build fast name->atom table
455 this->buildNameTable();
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
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
);
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());
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
] );
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());
498 if ( _options
.printOrderFileStatistics() ) {
499 if ( it
->objectFileName
== NULL
)
500 warning("can't find match for order_file entry: %s", it
->symbolName
);
502 warning("can't find match for order_file entry: %s/%s", it
->objectFileName
, it
->symbolName
);
507 if ( _options
.printOrderFileStatistics() && (_options
.orderedSymbolsCount() != matchCount
) ) {
508 warning("only %u out of %lu order_file symbols were applicable", matchCount
, _options
.orderedSymbolsCount() );
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());
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());
533 void Layout::doPass()
535 // handle .o files that cannot have their atoms rearranged
536 this->buildFollowOnTables();
538 // assign new ordinal value to all ordered atoms
539 this->buildOrdinalOverrideMap();
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
);
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());
559 void doPass(const Options
& opts
, ld::Internal
& state
)
561 Layout
layout(opts
, state
);
566 } // namespace order_file
567 } // namespace passes