diff options
author | Chris Johns <chrisj@rtems.org> | 2012-05-07 08:47:11 +1000 |
---|---|---|
committer | Chris Johns <chrisj@rtems.org> | 2012-05-07 08:47:11 +1000 |
commit | 75bb1e696321cf1b9ac6b124312b57560a06ff39 (patch) | |
tree | 510f1acc95a08ef2761aab7efba82ef5af894c8a /rld-resolver.cpp |
Add to git.
Diffstat (limited to 'rld-resolver.cpp')
-rw-r--r-- | rld-resolver.cpp | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/rld-resolver.cpp b/rld-resolver.cpp new file mode 100644 index 0000000..9a5ad31 --- /dev/null +++ b/rld-resolver.cpp @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2011, Chris Johns <chrisj@rtems.org> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +/** + * @file + * + * @ingroup rtems_ld + * + * @brief RTEMS Linker. + * + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include <iomanip> +#include <iostream> + +#include <sys/stat.h> + +#include <rld.h> +#include <rld.h> + +namespace rld +{ + namespace resolver + { + static void + resolve (rld::files::object_list& dependents, + rld::files::cache& cache, + rld::symbols::table& base_symbols, + rld::symbols::table& symbols, + rld::files::object& object) + { + static int nesting = 0; + + ++nesting; + + /* + * Find each unresolved symbol in the symbol table pointing the + * unresolved symbol's object file to the file that resolves the + * symbol. Record each object file that is found and when all unresolved + * symbols in this object file have been found iterate over the found + * object files resolving them. The 'usr' is the unresolved symbol and + * 'es' is the exported symbol. + */ + + rld::symbols::table& unresolved = object.unresolved_symbols (); + + if (rld::verbose () >= RLD_VERBOSE_INFO) + std::cout << "resolver:resolving: " + << std::setw (nesting - 1) << ' ' + << object.name ().basename () + << ", unresolved: " + << unresolved.size () + << ((*unresolved.begin ()).second.object () ? " (resolved)" : "") + << std::endl; + + rld::files::object_list objects; + + for (rld::symbols::table::iterator ursi = unresolved.begin (); + (ursi != unresolved.end ()) && !(*ursi).second.object (); + ++ursi) + { + rld::symbols::symbol& urs = (*ursi).second; + rld::symbols::table::iterator esi = base_symbols.find (urs.name ()); + bool base = true; + + if (esi == base_symbols.end ()) + { + esi = symbols.find (urs.name ()); + if (esi == symbols.end ()) + throw rld::error ("symbol referenced in '" + object.name ().basename () + + "' not found: " + urs.name (), "resolving"); + base = false; + } + + rld::symbols::symbol& es = (*esi).second; + + if (rld::verbose () >= RLD_VERBOSE_INFO) + { + std::cout << "resolver:resolved : " + << std::setw (nesting + 1) << ' ' + << urs.name () + << " -> "; + if (es.object()) + std::cout << es.object()->name ().basename (); + else + std::cout << "null"; + std::cout << std::endl; + } + + if (!base) + { + urs.set_object (*es.object ()); + objects.push_back (es.object ()); + } + + es.referenced (); + } + + /* + * Recurse into any references object files. First remove any duplicate + * entries. + */ + objects.unique (); + + for (rld::files::object_list::iterator oli = objects.begin (); + oli != objects.end (); + ++oli) + resolve (dependents, cache, base_symbols, symbols, *(*oli)); + + --nesting; + + dependents.merge (objects); + dependents.unique (); + } + + void + resolve (rld::files::object_list& dependents, + rld::files::cache& cache, + rld::symbols::table& base_symbols, + rld::symbols::table& symbols, + rld::symbols::table& undefined) + { + rld::files::object_list objects; + cache.get_objects (objects); + + for (rld::files::object_list::iterator oi = objects.begin (); + oi != objects.end (); + ++oi) + { + rld::files::object& object = *(*oi); + if (rld::verbose ()) + std::cout << "resolver:resolving: top: " + << object.name ().basename () << std::endl; + resolve (dependents, cache, base_symbols, symbols, object); + } + } + } + +} |