source: rtems-tools/linkers/rld-resolver.cpp @ 977c3de

4.104.11
Last change on this file since 977c3de was 977c3de, checked in by Chris Johns <chrisj@…>, on Nov 17, 2012 at 6:34:33 AM

Refactor the ELF support to allow ELF write suppport.

The refactoring allows better reuse of the ELF support and cleans up
some hacks from the generic file and archive handling improving the
separation of the file handling from the file format, ie ELF. The
handling of ELF object files and ELF object files inside archives
is cleaner.

The refactor cleaned up the symbol handling where the symbols now
reside in the ELF file object and references are take in symbol
pointer containers and symbol table containers.

The main purpose of the refactor is to allow support for creating
and writing ELF files.

Also added an rtems-syms command where special symbol support
can be added.

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*
2 * Copyright (c) 2011, Chris Johns <chrisj@rtems.org>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16/**
17 * @file
18 *
19 * @ingroup rtems_ld
20 *
21 * @brief RTEMS Linker.
22 *
23 */
24
25#if HAVE_CONFIG_H
26#include "config.h"
27#endif
28
29#include <iomanip>
30#include <iostream>
31
32#include <sys/stat.h>
33
34#include <rld.h>
35#include <rld.h>
36
37namespace rld
38{
39  namespace resolver
40  {
41    static void
42    resolve (rld::files::object_list& dependents,
43             rld::files::cache&       cache,
44             rld::symbols::table&     base_symbols,
45             rld::symbols::table&     symbols,
46             rld::files::object&      object)
47    {
48      static int nesting = 0;
49
50      ++nesting;
51
52      /*
53       * Find each unresolved symbol in the symbol table pointing the
54       * unresolved symbol's object file to the file that resolves the
55       * symbol. Record each object file that is found and when all unresolved
56       * symbols in this object file have been found iterate over the found
57       * object files resolving them. The 'urs' is the unresolved symbol and
58       * 'es' is the exported symbol.
59       */
60
61      rld::symbols::table& unresolved = object.unresolved_symbols ();
62
63      if (rld::verbose () >= RLD_VERBOSE_INFO)
64        std::cout << "resolver:resolving: "
65                  << std::setw (nesting - 1) << ' '
66                  << object.name ().basename ()
67                  << ", unresolved: "
68                  << unresolved.size ()
69                  << (((*unresolved.begin ()).second)->object () ? " (resolved)" : "")
70                  << std::endl;
71
72      rld::files::object_list objects;
73
74      for (rld::symbols::table::iterator ursi = unresolved.begin ();
75           (ursi != unresolved.end ()) && !((*ursi).second)->object ();
76           ++ursi)
77      {
78        rld::symbols::symbol&         urs = *((*ursi).second);
79        rld::symbols::table::iterator esi = base_symbols.find (urs.name ());
80        bool                          base = true;
81
82        if (rld::verbose () >= RLD_VERBOSE_INFO)
83        {
84          std::cout << "resolver:resolve  : "
85                    << std::setw (nesting + 1) << ' '
86                    << urs.name () << std::endl;
87        }
88
89        if (esi == base_symbols.end ())
90        {
91          esi = symbols.find (urs.name ());
92          if (esi == symbols.end ())
93            throw rld::error ("symbol referenced in '" + object.name ().basename () +
94                              "' not found: " + urs.name (), "resolving");
95          base = false;
96        }
97
98        rld::symbols::symbol& es = *((*esi).second);
99
100        if (rld::verbose () >= RLD_VERBOSE_INFO)
101        {
102          std::cout << "resolver:resolved : "
103                    << std::setw (nesting + 1) << ' '
104                    << urs.name ()
105                    << " -> ";
106          if (es.object())
107            std::cout << es.object()->name ().basename ();
108          else
109            std::cout << "null";
110          std::cout << std::endl;
111        }
112
113        if (!base)
114        {
115          urs.set_object (*es.object ());
116          objects.push_back (es.object ());
117        }
118
119        es.referenced ();
120      }
121
122      /*
123       * Recurse into any references object files. First remove any duplicate
124       * entries.
125       */
126      objects.unique ();
127
128      for (rld::files::object_list::iterator oli = objects.begin ();
129           oli != objects.end ();
130           ++oli)
131        resolve (dependents, cache, base_symbols, symbols, *(*oli));
132
133      --nesting;
134
135      dependents.merge (objects);
136      dependents.unique ();
137    }
138
139    void
140    resolve (rld::files::object_list& dependents,
141             rld::files::cache&       cache,
142             rld::symbols::table&     base_symbols,
143             rld::symbols::table&     symbols,
144             rld::symbols::table&     undefined)
145    {
146      rld::files::object_list objects;
147      cache.get_objects (objects);
148
149      for (rld::files::object_list::iterator oi = objects.begin ();
150           oi != objects.end ();
151           ++oi)
152      {
153        rld::files::object& object = *(*oi);
154        if (rld::verbose () >= RLD_VERBOSE_INFO)
155          std::cout << "resolver:resolving: top: "
156                    << object.name ().basename () << std::endl;
157        resolve (dependents, cache, base_symbols, symbols, object);
158      }
159    }
160  }
161
162}
Note: See TracBrowser for help on using the repository browser.